Skip to content
Merged
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
114 changes: 57 additions & 57 deletions apps/app/src/actions/policies/archive-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,70 @@ import { authActionClient } from "../safe-action";
import { z } from "zod";

const archivePolicySchema = z.object({
id: z.string(),
action: z.enum(["archive", "restore"]).optional(),
id: z.string(),
action: z.enum(["archive", "restore"]).optional(),
});

export const archivePolicyAction = authActionClient
.schema(archivePolicySchema)
.metadata({
name: "archive-policy",
track: {
event: "archive-policy",
channel: "server",
},
})
.action(async ({ parsedInput, ctx }) => {
const { id, action } = parsedInput;
const { activeOrganizationId } = ctx.session;
.schema(archivePolicySchema)
.metadata({
name: "archive-policy",
track: {
event: "archive-policy",
channel: "server",
},
})
.action(async ({ parsedInput, ctx }) => {
const { id, action } = parsedInput;
const { activeOrganizationId } = ctx.session;

if (!activeOrganizationId) {
return {
success: false,
error: "Not authorized",
};
}
if (!activeOrganizationId) {
return {
success: false,
error: "Not authorized",
};
}

try {
const policy = await db.policy.findUnique({
where: {
id,
organizationId: activeOrganizationId,
},
});
try {
const policy = await db.policy.findUnique({
where: {
id,
organizationId: activeOrganizationId,
},
});

if (!policy) {
return {
success: false,
error: "Policy not found",
};
}
if (!policy) {
return {
success: false,
error: "Policy not found",
};
}

// Determine if we should archive or restore based on action or current state
const shouldArchive =
action === "archive" || (action === undefined && !policy.isArchived);
// Determine if we should archive or restore based on action or current state
const shouldArchive =
action === "archive" || (action === undefined && !policy.isArchived);

await db.policy.update({
where: { id },
data: {
isArchived: shouldArchive,
},
});
await db.policy.update({
where: { id },
data: {
isArchived: shouldArchive,
},
});

revalidatePath(`/${activeOrganizationId}/policies/all/${id}`);
revalidatePath(`/${activeOrganizationId}/policies/all`);
revalidatePath(`/${activeOrganizationId}/policies`);
revalidateTag("policies");
revalidatePath(`/${activeOrganizationId}/policies/${id}`);
revalidatePath(`/${activeOrganizationId}/policies/all`);
revalidatePath(`/${activeOrganizationId}/policies`);
revalidateTag("policies");

return {
success: true,
isArchived: shouldArchive,
};
} catch (error) {
console.error(error);
return {
success: false,
error: "Failed to update policy archive status",
};
}
});
return {
success: true,
isArchived: shouldArchive,
};
} catch (error) {
console.error(error);
return {
success: false,
error: "Failed to update policy archive status",
};
}
});
180 changes: 90 additions & 90 deletions apps/app/src/actions/policies/update-policy-form-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,107 +10,107 @@ import { PolicyStatus } from "@comp/db/types";

// Helper function to calculate next review date based on frequency
function calculateNextReviewDate(
frequency: string,
baseDate: Date = new Date()
frequency: string,
baseDate: Date = new Date(),
): Date {
const nextDate = new Date(baseDate);
const nextDate = new Date(baseDate);

switch (frequency) {
case "monthly":
nextDate.setMonth(nextDate.getMonth() + 1);
break;
case "quarterly":
nextDate.setMonth(nextDate.getMonth() + 3);
break;
case "yearly":
nextDate.setFullYear(nextDate.getFullYear() + 1);
break;
default:
// If frequency is not recognized, default to yearly
nextDate.setFullYear(nextDate.getFullYear() + 1);
}
switch (frequency) {
case "monthly":
nextDate.setMonth(nextDate.getMonth() + 1);
break;
case "quarterly":
nextDate.setMonth(nextDate.getMonth() + 3);
break;
case "yearly":
nextDate.setFullYear(nextDate.getFullYear() + 1);
break;
default:
// If frequency is not recognized, default to yearly
nextDate.setFullYear(nextDate.getFullYear() + 1);
}

return nextDate;
return nextDate;
}

export const updatePolicyFormAction = authActionClient
.schema(updatePolicyFormSchema)
.metadata({
name: "update-policy-form",
track: {
event: "update-policy-form",
channel: "server",
},
})
.action(async ({ parsedInput, ctx }) => {
const {
id,
status,
assigneeId,
department,
review_frequency,
review_date,
isRequiredToSign,
} = parsedInput;
const { user, session } = ctx;
.schema(updatePolicyFormSchema)
.metadata({
name: "update-policy-form",
track: {
event: "update-policy-form",
channel: "server",
},
})
.action(async ({ parsedInput, ctx }) => {
const {
id,
status,
assigneeId,
department,
review_frequency,
review_date,
isRequiredToSign,
} = parsedInput;
const { user, session } = ctx;

if (!user.id || !session.activeOrganizationId) {
throw new Error("Unauthorized");
}
if (!user.id || !session.activeOrganizationId) {
throw new Error("Unauthorized");
}

try {
// Get the current policy to check if status is changing to published
const currentPolicy = await db.policy.findUnique({
where: {
id,
organizationId: session.activeOrganizationId,
},
select: {
status: true,
},
});
try {
// Get the current policy to check if status is changing to published
const currentPolicy = await db.policy.findUnique({
where: {
id,
organizationId: session.activeOrganizationId,
},
select: {
status: true,
},
});

// Determine if we need to update the review date
let reviewDate = review_date;
let lastPublishedAt = undefined;
// Determine if we need to update the review date
let reviewDate = review_date;
let lastPublishedAt = undefined;

// If status is changing to 'published', calculate next review date based on frequency
if (
status === PolicyStatus.published &&
currentPolicy?.status !== PolicyStatus.published
) {
reviewDate = calculateNextReviewDate(review_frequency);
lastPublishedAt = new Date(); // Set lastPublishedAt to now when publishing
}
// If status is changing to 'published', calculate next review date based on frequency
if (
status === PolicyStatus.published &&
currentPolicy?.status !== PolicyStatus.published
) {
reviewDate = calculateNextReviewDate(review_frequency);
lastPublishedAt = new Date(); // Set lastPublishedAt to now when publishing
}

await db.policy.update({
where: {
id,
organizationId: session.activeOrganizationId,
},
data: {
status,
assigneeId,
department,
frequency: review_frequency,
reviewDate,
isRequiredToSign: isRequiredToSign === "required",
...(lastPublishedAt && { lastPublishedAt }),
},
});
await db.policy.update({
where: {
id,
organizationId: session.activeOrganizationId,
},
data: {
status,
assigneeId,
department,
frequency: review_frequency,
reviewDate,
isRequiredToSign: isRequiredToSign === "required",
...(lastPublishedAt && { lastPublishedAt }),
},
});

revalidatePath(`/${session.activeOrganizationId}/policies`);
revalidatePath(`/${session.activeOrganizationId}/policies/all/${id}`);
revalidateTag("policies");
revalidatePath(`/${session.activeOrganizationId}/policies`);
revalidatePath(`/${session.activeOrganizationId}/policies/${id}`);
revalidateTag("policies");

return {
success: true,
};
} catch (error) {
console.error("Error updating policy:", error);
return {
success: true,
};
} catch (error) {
console.error("Error updating policy:", error);

return {
success: false,
};
}
});
return {
success: false,
};
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const updatePolicyOverviewAction = authActionClient
},
});

revalidatePath(`/${session.activeOrganizationId}/policies/all/${id}`);
revalidatePath(`/${session.activeOrganizationId}/policies/${id}`);
revalidatePath(`/${session.activeOrganizationId}/policies/all`);
revalidatePath(`/${session.activeOrganizationId}/policies`);

Expand Down
Loading