mirror of
https://github.com/ahmadk953/tasko.git
synced 2025-05-04 12:43:24 +00:00
Initial Commit
This commit is contained in:
commit
f3e2f01bd7
150 changed files with 13612 additions and 0 deletions
87
actions/create-board/index.ts
Normal file
87
actions/create-board/index.ts
Normal file
|
@ -0,0 +1,87 @@
|
|||
"use server";
|
||||
|
||||
import { auth } from "@clerk/nextjs";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
import { db } from "@/lib/db";
|
||||
import { createSafeAction } from "@/lib/create-safe-action";
|
||||
|
||||
import { InputType, ReturnType } from "./types";
|
||||
import { CreateBoard } from "./schema";
|
||||
import { createAuditLog } from "@/lib/create-audit-log";
|
||||
import { ACTION, ENTITY_TYPE } from "@prisma/client";
|
||||
import { incrementAvailableCount, hasAvailableCount } from "@/lib/org-limit";
|
||||
import { checkSubscription } from "@/lib/subscription";
|
||||
|
||||
const handler = async (data: InputType): Promise<ReturnType> => {
|
||||
const { userId, orgId } = auth();
|
||||
|
||||
if (!userId || !orgId) {
|
||||
return {
|
||||
error: "Unauthorized",
|
||||
};
|
||||
}
|
||||
|
||||
const canCreate = await hasAvailableCount();
|
||||
const isPro = await checkSubscription();
|
||||
|
||||
if (!canCreate && !isPro) {
|
||||
return {
|
||||
error:
|
||||
"You have reached your limit of free boards. Please upgrade to create more.",
|
||||
};
|
||||
}
|
||||
|
||||
const { title, image } = data;
|
||||
|
||||
const [imageId, imageThumbUrl, imageFullUrl, imageLinkHTML, imageUserName] =
|
||||
image.split("|");
|
||||
|
||||
if (
|
||||
!imageId ||
|
||||
!imageThumbUrl ||
|
||||
!imageFullUrl ||
|
||||
!imageUserName ||
|
||||
!imageLinkHTML
|
||||
) {
|
||||
return {
|
||||
error: "Missing fields. Failed to create board.",
|
||||
};
|
||||
}
|
||||
|
||||
let board;
|
||||
|
||||
try {
|
||||
board = await db.board.create({
|
||||
data: {
|
||||
title,
|
||||
orgId,
|
||||
imageId,
|
||||
imageThumbUrl,
|
||||
imageFullUrl,
|
||||
imageUserName,
|
||||
imageLinkHTML,
|
||||
},
|
||||
});
|
||||
|
||||
if (!isPro) {
|
||||
await incrementAvailableCount();
|
||||
}
|
||||
|
||||
await createAuditLog({
|
||||
entityTitle: board.title,
|
||||
entityId: board.id,
|
||||
entityType: ENTITY_TYPE.BOARD,
|
||||
action: ACTION.CREATE,
|
||||
});
|
||||
} catch (error) {
|
||||
return {
|
||||
error: "Failed to create board",
|
||||
};
|
||||
}
|
||||
|
||||
revalidatePath(`/board/${board.id}`);
|
||||
return { data: board };
|
||||
};
|
||||
|
||||
export const createBoard = createSafeAction(CreateBoard, handler);
|
Loading…
Add table
Add a link
Reference in a new issue