276 lines
7.5 KiB
TypeScript
276 lines
7.5 KiB
TypeScript
"use server";
|
|
|
|
import { prisma } from "../lib/prisma";
|
|
import { revalidatePath } from "next/cache";
|
|
import { auth } from "@/auth";
|
|
import { NewsletterStatus } from "@/lib/generated/prisma/enums";
|
|
|
|
async function getAuthorId() {
|
|
const session = await auth(); // returns valid session object
|
|
|
|
if (!session?.user?.id) { // if no session, user, or id
|
|
throw new Error("You must be logged in");
|
|
}
|
|
|
|
return session.user.id;
|
|
}
|
|
|
|
export async function getPubNewsletters() {
|
|
return await prisma.newsletters.findMany({
|
|
where: {
|
|
status: "Sent",
|
|
},
|
|
orderBy: {
|
|
createdAt: "desc",
|
|
},
|
|
include: { // includes author
|
|
author: {
|
|
select: {
|
|
name: true, // only takes authors name not private info
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getPubNewsletter(id: string) { // single pub newsletter
|
|
return await prisma.newsletters.findUnique({
|
|
where: {
|
|
id,
|
|
status: "Sent",
|
|
},
|
|
include: {
|
|
author: {
|
|
select: {
|
|
name: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getUserNewsletters() {
|
|
|
|
const authorID = await getAuthorId();
|
|
|
|
return await prisma.newsletters.findMany({
|
|
where: {
|
|
authorID,
|
|
},
|
|
orderBy: {
|
|
createdAt: "desc", // newest first
|
|
},
|
|
include: { // includes author
|
|
author: {
|
|
select: {
|
|
name: true, // only takes authors name not private info
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getUserNewsletter(id: string) {
|
|
const authorID = await getAuthorId();
|
|
|
|
return await prisma.newsletters.findUnique({
|
|
where: {
|
|
id,
|
|
authorID,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function publishNewsletter(id: string) {
|
|
try {
|
|
const authorID = await getAuthorId();
|
|
|
|
const newsletter = await prisma.newsletters.update({
|
|
where: {
|
|
id,
|
|
authorID, // if it was called with a post id that doesnt belong to that author it wont be found
|
|
},
|
|
data: {
|
|
status: "Sent",
|
|
},
|
|
});
|
|
|
|
revalidatePath("/dashboard");
|
|
revalidatePath("/newsletters");
|
|
revalidatePath(`/newsletters/${id}`);
|
|
revalidatePath(`/dashboard/${id}`);
|
|
|
|
return { success: true, newsletter };
|
|
} catch (error) {
|
|
console.error("Error publishing newsletter:", error);
|
|
return { success: false, error: "Failed to publish newsletter" };
|
|
}
|
|
}
|
|
|
|
export async function unPublishNewsletter(id: string) {
|
|
try {
|
|
const authorID = await getAuthorId();
|
|
|
|
const newsletter = await prisma.newsletters.update({
|
|
where: {
|
|
id,
|
|
authorID, // if it was called with a post id that doesnt belong to that author it wont be found
|
|
},
|
|
data: {
|
|
status: "Draft",
|
|
},
|
|
});
|
|
|
|
revalidatePath("/dashboard");
|
|
revalidatePath("/newsletters");
|
|
revalidatePath(`/newsletters/${id}`);
|
|
revalidatePath(`/dashboard/${id}`);
|
|
|
|
return { success: true, newsletter };
|
|
} catch (error) {
|
|
console.error("Error unpublishing newsletter:", error);
|
|
return { success: false, error: "Failed to unpublish newsletter" };
|
|
}
|
|
}
|
|
|
|
export async function scheduleNewsletter(id: string) {
|
|
try {
|
|
const authorID = await getAuthorId();
|
|
|
|
const newsletter = await prisma.newsletters.update({
|
|
where: {
|
|
id,
|
|
authorID, // if it was called with a post id that doesnt belong to that author it wont be found
|
|
},
|
|
data: {
|
|
status: "Schedule",
|
|
},
|
|
});
|
|
|
|
revalidatePath("/dashboard");
|
|
revalidatePath("/newsletters");
|
|
revalidatePath(`/newsletters/${id}`);
|
|
revalidatePath(`/dashboard/${id}`);
|
|
|
|
return { success: true, newsletter };
|
|
} catch (error) {
|
|
console.error("Error publishing newsletter:", error);
|
|
return { success: false, error: "Failed to publish newsletter" };
|
|
}
|
|
}
|
|
|
|
// export async function likeNewsletter(id: string) {
|
|
// try {
|
|
// const authorID = await getAuthorId();
|
|
|
|
// const newsletter = await prisma.newsletters.update({
|
|
// where: {
|
|
// id,
|
|
// authorID, // if it was called with a post id that doesnt belong to that author it wont be found
|
|
// },
|
|
// data: {
|
|
// status: "Sent",
|
|
// },
|
|
// });
|
|
|
|
// revalidatePath("/dashboard");
|
|
// revalidatePath("/newsletters");
|
|
// revalidatePath(`/newsletters/${id}`);
|
|
// revalidatePath(`/dashboard/${id}`);
|
|
|
|
// return { success: true, newsletter };
|
|
// } catch (error) {
|
|
// console.error("Error publishing newsletter:", error);
|
|
// return { success: false, error: "Failed to publish newsletter" };
|
|
// }
|
|
// }
|
|
|
|
export async function addNewsletter(data: {
|
|
title: string;
|
|
imageUrl: string;
|
|
content: string;
|
|
}
|
|
) {
|
|
try {
|
|
const authorID = await getAuthorId();
|
|
|
|
const newNewsletter = await prisma.newsletters.create({
|
|
data: {
|
|
title: data.title.trim(),
|
|
imageUrl: data.imageUrl.trim(),
|
|
content: data.content, // keep spaces
|
|
authorID,
|
|
}
|
|
})
|
|
|
|
revalidatePath("/newsletters");
|
|
revalidatePath("/dashboard");
|
|
|
|
return { success: true, newsletter: newNewsletter };
|
|
} catch (error) {
|
|
console.error("Error adding newsletter:", error);
|
|
return { success: false, error: "Failed to add newsletter" };
|
|
}
|
|
|
|
}
|
|
|
|
export async function editNewsletter(
|
|
id: string,
|
|
data: {
|
|
title: string;
|
|
imageUrl: string;
|
|
content: string;
|
|
},
|
|
) {
|
|
try {
|
|
const authorID = await getAuthorId();
|
|
|
|
const updatedNewsletter = await prisma.newsletters.update({
|
|
where: {
|
|
id, // find target newsletter
|
|
authorID, // thats also owned by current session owner
|
|
status: {
|
|
not: "Sent"
|
|
}
|
|
},
|
|
data: {
|
|
title: data.title.trim(),
|
|
imageUrl: data.imageUrl.trim(),
|
|
content: data.content,
|
|
},
|
|
});
|
|
|
|
revalidatePath("/newsletters");
|
|
revalidatePath("/dashboard");
|
|
revalidatePath(`/newsletters/${id}`);
|
|
revalidatePath(`/dashboard/${id}`);
|
|
|
|
return { success: true, newsletter: updatedNewsletter };
|
|
} catch (error) {
|
|
console.error("Error editing newsletter:", error);
|
|
return { success: false, error: "Failed to edit newsletter" };
|
|
}
|
|
}
|
|
|
|
export async function removeNewsletter(id: string) {
|
|
try {
|
|
const authorID = await getAuthorId();
|
|
|
|
const deletedNewsletter = await prisma.newsletters.delete({
|
|
where: {
|
|
id,
|
|
authorID,
|
|
},
|
|
});
|
|
|
|
revalidatePath("/newsletters");
|
|
revalidatePath("/dashboard");
|
|
revalidatePath(`/newsletters/${id}`);
|
|
revalidatePath(`/dashboard/${id}`);
|
|
|
|
return { success: true, newsletter: deletedNewsletter };
|
|
} catch (error) {
|
|
console.error("Error deleting newsletter:", error);
|
|
return { success: false, error: "Failed to delete newsletter" };
|
|
}
|
|
} |