2024-02-16 01:49:19 +00:00
|
|
|
'use server';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
2024-02-16 01:49:19 +00:00
|
|
|
import { auth } from '@clerk/nextjs';
|
|
|
|
import { revalidatePath } from 'next/cache';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
2024-02-16 01:49:19 +00:00
|
|
|
import { db } from '@/lib/db';
|
|
|
|
import { createSafeAction } from '@/lib/create-safe-action';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
2024-02-16 01:49:19 +00:00
|
|
|
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';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
|
|
|
const handler = async (data: InputType): Promise<ReturnType> => {
|
|
|
|
const { userId, orgId } = auth();
|
|
|
|
|
|
|
|
if (!userId || !orgId) {
|
|
|
|
return {
|
2024-02-16 01:49:19 +00:00
|
|
|
error: 'Unauthorized',
|
2024-02-15 02:30:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const canCreate = await hasAvailableCount();
|
|
|
|
const isPro = await checkSubscription();
|
|
|
|
|
|
|
|
if (!canCreate && !isPro) {
|
|
|
|
return {
|
|
|
|
error:
|
2024-02-16 01:49:19 +00:00
|
|
|
'You have reached your limit of free boards. Please upgrade to create more.',
|
2024-02-15 02:30:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const { title, image } = data;
|
|
|
|
|
2024-02-18 00:21:19 +00:00
|
|
|
const [
|
|
|
|
imageId,
|
|
|
|
imageThumbUrl,
|
|
|
|
imageFullUrl,
|
|
|
|
imageLinkHTML,
|
|
|
|
imageUserName,
|
|
|
|
imageDownloadUrl,
|
|
|
|
] = image.split('|');
|
2024-02-15 02:30:10 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
!imageId ||
|
|
|
|
!imageThumbUrl ||
|
|
|
|
!imageFullUrl ||
|
|
|
|
!imageUserName ||
|
2024-02-18 00:21:19 +00:00
|
|
|
!imageLinkHTML ||
|
|
|
|
!imageDownloadUrl
|
2024-02-15 02:30:10 +00:00
|
|
|
) {
|
|
|
|
return {
|
2024-02-16 01:49:19 +00:00
|
|
|
error: 'Missing fields. Failed to create board.',
|
2024-02-15 02:30:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let board;
|
|
|
|
|
|
|
|
try {
|
2024-02-18 00:21:19 +00:00
|
|
|
await fetch(imageDownloadUrl, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Authorization: `Client-ID ${process.env.UNSPLASH_ACCESS_KEY}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-02-15 02:30:10 +00:00
|
|
|
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 {
|
2024-02-16 01:49:19 +00:00
|
|
|
error: 'Failed to create board',
|
2024-02-15 02:30:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
revalidatePath(`/board/${board.id}`);
|
|
|
|
return { data: board };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const createBoard = createSafeAction(CreateBoard, handler);
|