|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import { db } from "@comp/db"; |
| 4 | +import { Departments, Frequency, PolicyStatus } from "@comp/db/types"; |
| 5 | +import { revalidatePath, revalidateTag } from "next/cache"; |
| 6 | +import { authActionClient } from "../safe-action"; |
| 7 | +import { z } from "zod"; |
| 8 | + |
| 9 | +const acceptRequestedPolicyChangesSchema = z.object({ |
| 10 | + id: z.string(), |
| 11 | + approverId: z.string(), |
| 12 | + comment: z.string().optional(), |
| 13 | + entityId: z.string(), |
| 14 | +}); |
| 15 | + |
| 16 | +export const acceptRequestedPolicyChangesAction = authActionClient |
| 17 | + .schema(acceptRequestedPolicyChangesSchema) |
| 18 | + .metadata({ |
| 19 | + name: "accept-requested-policy-changes", |
| 20 | + track: { |
| 21 | + event: "accept-requested-policy-changes", |
| 22 | + description: "Accept Policy Changes", |
| 23 | + channel: "server", |
| 24 | + }, |
| 25 | + }) |
| 26 | + .action(async ({ parsedInput, ctx }) => { |
| 27 | + const { id, approverId, comment } = parsedInput; |
| 28 | + const { user, session } = ctx; |
| 29 | + |
| 30 | + if (!user.id || !session.activeOrganizationId) { |
| 31 | + throw new Error("Unauthorized"); |
| 32 | + } |
| 33 | + |
| 34 | + if (!approverId) { |
| 35 | + throw new Error("Approver is required"); |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + const policy = await db.policy.findUnique({ |
| 40 | + where: { |
| 41 | + id, |
| 42 | + organizationId: session.activeOrganizationId, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + if (!policy) { |
| 47 | + throw new Error("Policy not found"); |
| 48 | + } |
| 49 | + |
| 50 | + if (policy.approverId !== approverId) { |
| 51 | + throw new Error("Approver is not the same"); |
| 52 | + } |
| 53 | + |
| 54 | + // Update policy status |
| 55 | + await db.policy.update({ |
| 56 | + where: { |
| 57 | + id, |
| 58 | + organizationId: session.activeOrganizationId, |
| 59 | + }, |
| 60 | + data: { |
| 61 | + status: PolicyStatus.published, |
| 62 | + approverId: null, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + // If a comment was provided, create a comment |
| 67 | + if (comment && comment.trim() !== "") { |
| 68 | + const member = await db.member.findFirst({ |
| 69 | + where: { |
| 70 | + userId: user.id, |
| 71 | + organizationId: session.activeOrganizationId, |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + if (member) { |
| 76 | + await db.comment.create({ |
| 77 | + data: { |
| 78 | + content: `Policy changes accepted: ${comment}`, |
| 79 | + entityId: id, |
| 80 | + entityType: "policy", |
| 81 | + organizationId: session.activeOrganizationId, |
| 82 | + authorId: member.id, |
| 83 | + }, |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + revalidatePath(`/${session.activeOrganizationId}/policies`); |
| 89 | + revalidatePath(`/${session.activeOrganizationId}/policies/${id}`); |
| 90 | + revalidateTag("policies"); |
| 91 | + |
| 92 | + return { |
| 93 | + success: true, |
| 94 | + }; |
| 95 | + } catch (error) { |
| 96 | + console.error("Error submitting policy for approval:", error); |
| 97 | + |
| 98 | + return { |
| 99 | + success: false, |
| 100 | + }; |
| 101 | + } |
| 102 | + }); |
0 commit comments