Skip to content

Commit 0ad2d21

Browse files
authored
Merge pull request #692 from trycompai/main
[comp] Production Deploy
2 parents 126ab02 + 52bcff3 commit 0ad2d21

File tree

35 files changed

+1850
-486
lines changed

35 files changed

+1850
-486
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
});

apps/app/src/actions/policies/archive-policy.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { authActionClient } from "../safe-action";
88
const archivePolicySchema = z.object({
99
id: z.string(),
1010
action: z.enum(["archive", "restore"]).optional(),
11+
entityId: z.string(),
1112
});
1213

1314
export const archivePolicyAction = authActionClient
@@ -16,6 +17,7 @@ export const archivePolicyAction = authActionClient
1617
name: "archive-policy",
1718
track: {
1819
event: "archive-policy",
20+
description: "Archive Policy",
1921
channel: "server",
2022
},
2123
})

apps/app/src/actions/policies/create-new-policy.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const createPolicyAction = authActionClient
1212
name: "create-policy",
1313
track: {
1414
event: "create-policy",
15+
description: "Create New Policy",
1516
channel: "server",
1617
},
1718
})
@@ -65,11 +66,12 @@ export const createPolicyAction = authActionClient
6566
content: [{ type: "text", text: "" }],
6667
},
6768
],
68-
...(controlIds && controlIds.length > 0 && {
69-
controls: {
70-
connect: controlIds.map((id) => ({ id })),
71-
},
72-
}),
69+
...(controlIds &&
70+
controlIds.length > 0 && {
71+
controls: {
72+
connect: controlIds.map((id) => ({ id })),
73+
},
74+
}),
7375
},
7476
});
7577

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"use server";
2+
3+
import { db } from "@comp/db";
4+
import { 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 denyRequestedPolicyChangesSchema = z.object({
10+
id: z.string(),
11+
approverId: z.string(),
12+
comment: z.string().optional(),
13+
entityId: z.string(),
14+
});
15+
16+
export const denyRequestedPolicyChangesAction = authActionClient
17+
.schema(denyRequestedPolicyChangesSchema)
18+
.metadata({
19+
name: "deny-requested-policy-changes",
20+
track: {
21+
event: "deny-requested-policy-changes",
22+
description: "Deny 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.draft,
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 denied: ${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+
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use server";
2+
3+
import { db } from "@comp/db";
4+
import { PolicyStatus } from "@comp/db/types";
5+
import { revalidatePath, revalidateTag } from "next/cache";
6+
import { authActionClient } from "../safe-action";
7+
import { updatePolicyFormSchema } from "../schema";
8+
9+
export const submitPolicyForApprovalAction = authActionClient
10+
.schema(updatePolicyFormSchema)
11+
.metadata({
12+
name: "submit-policy-for-approval",
13+
track: {
14+
event: "submit-policy-for-approval",
15+
description: "Submit Policy for Approval",
16+
channel: "server",
17+
},
18+
})
19+
.action(async ({ parsedInput, ctx }) => {
20+
const {
21+
id,
22+
assigneeId,
23+
department,
24+
review_frequency,
25+
review_date,
26+
isRequiredToSign,
27+
approverId,
28+
} = parsedInput;
29+
const { user, session } = ctx;
30+
31+
if (!user.id || !session.activeOrganizationId) {
32+
throw new Error("Unauthorized");
33+
}
34+
35+
if (!approverId) {
36+
throw new Error("Approver is required");
37+
}
38+
39+
try {
40+
const newReviewDate = review_date;
41+
42+
await db.policy.update({
43+
where: {
44+
id,
45+
organizationId: session.activeOrganizationId,
46+
},
47+
data: {
48+
status: PolicyStatus.needs_review,
49+
assigneeId,
50+
department,
51+
frequency: review_frequency,
52+
reviewDate: newReviewDate,
53+
isRequiredToSign: isRequiredToSign === "required",
54+
approverId,
55+
},
56+
});
57+
58+
revalidatePath(`/${session.activeOrganizationId}/policies/${id}`);
59+
60+
return {
61+
success: true,
62+
};
63+
} catch (error) {
64+
console.error("Error submitting policy for approval:", error);
65+
66+
return {
67+
success: false,
68+
};
69+
}
70+
});

apps/app/src/actions/policies/update-policy-action.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const updatePolicyAction = authActionClient
5959
name: "update-policy",
6060
track: {
6161
event: "update-policy",
62+
description: "Update Policy",
6263
channel: "server",
6364
},
6465
})

apps/app/src/actions/policies/update-policy-form-action.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const updatePolicyFormAction = authActionClient
3939
name: "update-policy-form",
4040
track: {
4141
event: "update-policy-form",
42+
description: "Update Policy",
4243
channel: "server",
4344
},
4445
})

apps/app/src/actions/policies/update-policy-overview-action.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const updatePolicyOverviewAction = authActionClient
1313
name: "update-policy-overview",
1414
track: {
1515
event: "update-policy-overview",
16+
description: "Update Policy",
1617
channel: "server",
1718
},
1819
})

0 commit comments

Comments
 (0)