Skip to content

Commit 6d66761

Browse files
authored
Merge pull request #839 from trycompai/mariano/comp-190-unable-to-edit-policy-name-or-description-after-creating
[dev] [Marfuen] mariano/comp-190-unable-to-edit-policy-name-or-description-after-creating
2 parents fe24ba1 + 77835c5 commit 6d66761

35 files changed

Lines changed: 1727 additions & 1866 deletions

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

Lines changed: 95 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -10,108 +10,112 @@ import { updatePolicyFormSchema } from "../schema";
1010

1111
// Helper function to calculate next review date based on frequency
1212
function calculateNextReviewDate(
13-
frequency: string,
14-
baseDate: Date = new Date(),
13+
frequency: string,
14+
baseDate: Date = new Date()
1515
): Date {
16-
const nextDate = new Date(baseDate);
16+
const nextDate = new Date(baseDate);
1717

18-
switch (frequency) {
19-
case "monthly":
20-
nextDate.setMonth(nextDate.getMonth() + 1);
21-
break;
22-
case "quarterly":
23-
nextDate.setMonth(nextDate.getMonth() + 3);
24-
break;
25-
case "yearly":
26-
nextDate.setFullYear(nextDate.getFullYear() + 1);
27-
break;
28-
default:
29-
// If frequency is not recognized, default to yearly
30-
nextDate.setFullYear(nextDate.getFullYear() + 1);
31-
}
18+
switch (frequency) {
19+
case "monthly":
20+
nextDate.setMonth(nextDate.getMonth() + 1);
21+
break;
22+
case "quarterly":
23+
nextDate.setMonth(nextDate.getMonth() + 3);
24+
break;
25+
case "yearly":
26+
nextDate.setFullYear(nextDate.getFullYear() + 1);
27+
break;
28+
default:
29+
// If frequency is not recognized, default to yearly
30+
nextDate.setFullYear(nextDate.getFullYear() + 1);
31+
}
3232

33-
return nextDate;
33+
return nextDate;
3434
}
3535

3636
export const updatePolicyFormAction = authActionClient
37-
.schema(updatePolicyFormSchema)
38-
.metadata({
39-
name: "update-policy-form",
40-
track: {
41-
event: "update-policy-form",
42-
description: "Update Policy",
43-
channel: "server",
44-
},
45-
})
46-
.action(async ({ parsedInput, ctx }) => {
47-
const {
48-
id,
49-
status,
50-
assigneeId,
51-
department,
52-
review_frequency,
53-
review_date,
54-
isRequiredToSign,
55-
} = parsedInput;
56-
const { user, session } = ctx;
37+
.schema(updatePolicyFormSchema)
38+
.metadata({
39+
name: "update-policy-form",
40+
track: {
41+
event: "update-policy-form",
42+
description: "Update Policy",
43+
channel: "server",
44+
},
45+
})
46+
.action(async ({ parsedInput, ctx }) => {
47+
const {
48+
id,
49+
name,
50+
description,
51+
status,
52+
assigneeId,
53+
department,
54+
review_frequency,
55+
review_date,
56+
isRequiredToSign,
57+
} = parsedInput;
58+
const { user, session } = ctx;
5759

58-
if (!user.id || !session.activeOrganizationId) {
59-
throw new Error("Unauthorized");
60-
}
60+
if (!user.id || !session.activeOrganizationId) {
61+
throw new Error("Unauthorized");
62+
}
6163

62-
try {
63-
// Get the current policy to check if status is changing to published
64-
const currentPolicy = await db.policy.findUnique({
65-
where: {
66-
id,
67-
organizationId: session.activeOrganizationId,
68-
},
69-
select: {
70-
status: true,
71-
},
72-
});
64+
try {
65+
// Get the current policy to check if status is changing to published
66+
const currentPolicy = await db.policy.findUnique({
67+
where: {
68+
id,
69+
organizationId: session.activeOrganizationId,
70+
},
71+
select: {
72+
status: true,
73+
},
74+
});
7375

74-
// Determine if we need to update the review date
75-
let reviewDate = review_date;
76-
let lastPublishedAt = undefined;
76+
// Determine if we need to update the review date
77+
let reviewDate = review_date;
78+
let lastPublishedAt = undefined;
7779

78-
// If status is changing to 'published', calculate next review date based on frequency
79-
if (
80-
status === PolicyStatus.published &&
81-
currentPolicy?.status !== PolicyStatus.published
82-
) {
83-
reviewDate = calculateNextReviewDate(review_frequency);
84-
lastPublishedAt = new Date(); // Set lastPublishedAt to now when publishing
85-
}
80+
// If status is changing to 'published', calculate next review date based on frequency
81+
if (
82+
status === PolicyStatus.published &&
83+
currentPolicy?.status !== PolicyStatus.published
84+
) {
85+
reviewDate = calculateNextReviewDate(review_frequency);
86+
lastPublishedAt = new Date(); // Set lastPublishedAt to now when publishing
87+
}
8688

87-
await db.policy.update({
88-
where: {
89-
id,
90-
organizationId: session.activeOrganizationId,
91-
},
92-
data: {
93-
status,
94-
assigneeId,
95-
department,
96-
frequency: review_frequency,
97-
reviewDate,
98-
isRequiredToSign: isRequiredToSign === "required",
99-
...(lastPublishedAt && { lastPublishedAt }),
100-
},
101-
});
89+
await db.policy.update({
90+
where: {
91+
id,
92+
organizationId: session.activeOrganizationId,
93+
},
94+
data: {
95+
name,
96+
description,
97+
status,
98+
assigneeId,
99+
department,
100+
frequency: review_frequency,
101+
reviewDate,
102+
isRequiredToSign: isRequiredToSign === "required",
103+
...(lastPublishedAt && { lastPublishedAt }),
104+
},
105+
});
102106

103-
revalidatePath(`/${session.activeOrganizationId}/policies`);
104-
revalidatePath(`/${session.activeOrganizationId}/policies/${id}`);
105-
revalidateTag("policies");
107+
revalidatePath(`/${session.activeOrganizationId}/policies`);
108+
revalidatePath(`/${session.activeOrganizationId}/policies/${id}`);
109+
revalidateTag("policies");
106110

107-
return {
108-
success: true,
109-
};
110-
} catch (error) {
111-
console.error("Error updating policy:", error);
111+
return {
112+
success: true,
113+
};
114+
} catch (error) {
115+
console.error("Error updating policy:", error);
112116

113-
return {
114-
success: false,
115-
};
116-
}
117-
});
117+
return {
118+
success: false,
119+
};
120+
}
121+
});

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

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,69 @@ import { authActionClient } from "../safe-action";
88
import { updatePolicyOverviewSchema } from "../schema";
99

1010
export const updatePolicyOverviewAction = authActionClient
11-
.schema(updatePolicyOverviewSchema)
12-
.metadata({
13-
name: "update-policy-overview",
14-
track: {
15-
event: "update-policy-overview",
16-
description: "Update Policy",
17-
channel: "server",
18-
},
19-
})
20-
.action(async ({ parsedInput, ctx }) => {
21-
const { id, title, description, isRequiredToSign } = parsedInput;
22-
const { user, session } = ctx;
11+
.schema(updatePolicyOverviewSchema)
12+
.metadata({
13+
name: "update-policy-overview",
14+
track: {
15+
event: "update-policy-overview",
16+
description: "Update Policy",
17+
channel: "server",
18+
},
19+
})
20+
.action(async ({ parsedInput, ctx }) => {
21+
const { id, title, description, isRequiredToSign } = parsedInput;
22+
const { user, session } = ctx;
2323

24-
if (!user) {
25-
return {
26-
success: false,
27-
error: "Not authorized",
28-
};
29-
}
24+
if (!user) {
25+
return {
26+
success: false,
27+
error: "Not authorized",
28+
};
29+
}
3030

31-
if (!session.activeOrganizationId) {
32-
return {
33-
success: false,
34-
error: "Not authorized",
35-
};
36-
}
31+
if (!session.activeOrganizationId) {
32+
return {
33+
success: false,
34+
error: "Not authorized",
35+
};
36+
}
3737

38-
try {
39-
const policy = await db.policy.findUnique({
40-
where: { id, organizationId: session.activeOrganizationId },
41-
});
38+
try {
39+
const policy = await db.policy.findUnique({
40+
where: { id, organizationId: session.activeOrganizationId },
41+
});
4242

43-
if (!policy) {
44-
return {
45-
success: false,
46-
error: "Policy not found",
47-
};
48-
}
43+
if (!policy) {
44+
return {
45+
success: false,
46+
error: "Policy not found",
47+
};
48+
}
4949

50-
await db.policy.update({
51-
where: { id },
52-
data: {
53-
name: title,
54-
description,
55-
// Use type assertion to handle the new field
56-
// that might not be in the generated types yet
57-
...(isRequiredToSign !== undefined
58-
? ({
59-
isRequiredToSign:
60-
isRequiredToSign === "required",
61-
} as any)
62-
: {}),
63-
},
64-
});
50+
await db.policy.update({
51+
where: { id },
52+
data: {
53+
name: title,
54+
description,
55+
...(isRequiredToSign !== undefined
56+
? {
57+
isRequiredToSign: isRequiredToSign === "required",
58+
}
59+
: {}),
60+
},
61+
});
6562

66-
revalidatePath(`/${session.activeOrganizationId}/policies/${id}`);
67-
revalidatePath(`/${session.activeOrganizationId}/policies/all`);
68-
revalidatePath(`/${session.activeOrganizationId}/policies`);
63+
revalidatePath(`/${session.activeOrganizationId}/policies/${id}`);
64+
revalidatePath(`/${session.activeOrganizationId}/policies/all`);
65+
revalidatePath(`/${session.activeOrganizationId}/policies`);
6966

70-
return {
71-
success: true,
72-
};
73-
} catch (error) {
74-
return {
75-
success: false,
76-
error: "Failed to update policy overview",
77-
};
78-
}
79-
});
67+
return {
68+
success: true,
69+
};
70+
} catch (error) {
71+
return {
72+
success: false,
73+
error: "Failed to update policy overview",
74+
};
75+
}
76+
});

0 commit comments

Comments
 (0)