@@ -10,108 +10,112 @@ import { updatePolicyFormSchema } from "../schema";
1010
1111// Helper function to calculate next review date based on frequency
1212function 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
3636export 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+ } ) ;
0 commit comments