Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@trigger.dev/sdk": "4",
"@trycompai/db": "^1.3.4",
"@types/canvas-confetti": "^1.9.0",
"@types/three": "^0.177.0",
"@types/three": "^0.180.0",
"@uploadthing/react": "^7.3.0",
"@upstash/ratelimit": "^2.0.5",
"@vercel/sdk": "^1.7.1",
Expand Down
79 changes: 79 additions & 0 deletions apps/app/src/actions/tasks/create-task-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use server';

import { authActionClient } from '@/actions/safe-action';
import { db, Departments, TaskFrequency } from '@db';
import { revalidatePath } from 'next/cache';
import { headers } from 'next/headers';
import { z } from 'zod';

const createTaskSchema = z.object({
title: z.string().min(1, {
message: 'Title is required',
}),
description: z.string().min(1, {
message: 'Description is required',
}),
assigneeId: z.string().nullable().optional(),
frequency: z.nativeEnum(TaskFrequency).nullable().optional(),
department: z.nativeEnum(Departments).nullable().optional(),
controlIds: z.array(z.string()).optional(),
});

export const createTaskAction = authActionClient
.inputSchema(createTaskSchema)
.metadata({
name: 'create-task',
track: {
event: 'create-task',
channel: 'server',
},
})
.action(async ({ parsedInput, ctx }) => {
const { title, description, assigneeId, frequency, department, controlIds } = parsedInput;
const {
session: { activeOrganizationId },
user,
} = ctx;

if (!user.id || !activeOrganizationId) {
throw new Error('Invalid user input');
}

try {
const task = await db.task.create({
data: {
title,
description,
assigneeId: assigneeId || null,
organizationId: activeOrganizationId,
status: 'todo',
order: 0,
frequency: frequency || null,
department: department || null,
...(controlIds &&
controlIds.length > 0 && {
controls: {
connect: controlIds.map((id) => ({ id })),
},
}),
},
});

// Revalidate the path based on the header
const headersList = await headers();
let path = headersList.get('x-pathname') || headersList.get('referer') || '';
path = path.replace(/\/[a-z]{2}\//, '/');
revalidatePath(path);

return {
success: true,
task,
};
} catch (error) {
console.error('Failed to create task:', error);
return {
success: false,
error: 'Failed to create task',
};
}
});
Loading
Loading