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
855 changes: 434 additions & 421 deletions apps/app/src/actions/organization/lib/utils.ts

Large diffs are not rendered by default.

115 changes: 57 additions & 58 deletions apps/app/src/actions/policies/archive-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +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.status !== "archived");
// 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: {
status: shouldArchive ? "archived" : "published",
},
});
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/all/${id}`);
revalidatePath(`/${activeOrganizationId}/policies/all`);
revalidatePath(`/${activeOrganizationId}/policies`);
revalidateTag("policies");

return {
success: true,
status: shouldArchive ? "archived" : "published",
};
} 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",
};
}
});
175 changes: 95 additions & 80 deletions apps/app/src/actions/policies/create-new-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,92 +7,107 @@ import { authActionClient } from "../safe-action";
import { createPolicySchema } from "../schema";

export const createPolicyAction = authActionClient
.schema(createPolicySchema)
.metadata({
name: "create-policy",
track: {
event: "create-policy",
channel: "server",
},
})
.action(async ({ parsedInput, ctx }) => {
const { title, description, frameworkIds, controlIds } = parsedInput;
const { activeOrganizationId } = ctx.session;
const { user } = ctx;
.schema(createPolicySchema)
.metadata({
name: "create-policy",
track: {
event: "create-policy",
channel: "server",
},
})
.action(async ({ parsedInput, ctx }) => {
const { title, description, frameworkIds, controlIds } = parsedInput;
const { activeOrganizationId } = ctx.session;
const { user } = ctx;

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

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

try {
// Create the policy
const policy = await db.policy.create({
data: {
name: title,
description,
organizationId: activeOrganizationId,
ownerId: user.id,
department: Departments.none,
frequency: Frequency.monthly,
content: [
{ type: "paragraph", content: [{ type: "text", text: "" }] },
],
},
});
// Find member id in the organization
const member = await db.member.findFirst({
where: {
userId: user.id,
organizationId: activeOrganizationId,
},
});

// Create artifacts for each control
if (controlIds && controlIds.length > 0) {
// Create artifacts that link the policy to controls
await Promise.all(
controlIds.map(async (controlId) => {
// Create the artifact
const artifact = await db.artifact.create({
data: {
type: "policy",
policyId: policy.id,
organizationId: activeOrganizationId,
},
});
if (!member) {
return {
success: false,
error: "Not authorized",
};
}

// Connect the artifact to the control
await db.control.update({
where: { id: controlId },
data: {
artifacts: {
connect: { id: artifact.id },
},
},
});
try {
// Create the policy
const policy = await db.policy.create({
data: {
name: title,
description,
organizationId: activeOrganizationId,
assigneeId: member.id,
department: Departments.none,
frequency: Frequency.monthly,
content: [
{ type: "paragraph", content: [{ type: "text", text: "" }] },
],
},
});

return artifact;
}),
);
}
// Create artifacts for each control
if (controlIds && controlIds.length > 0) {
// Create artifacts that link the policy to controls
await Promise.all(
controlIds.map(async (controlId) => {
// Create the artifact
const artifact = await db.artifact.create({
data: {
type: "policy",
policyId: policy.id,
organizationId: activeOrganizationId,
},
});

revalidatePath(`/${activeOrganizationId}/policies/all`);
revalidatePath(`/${activeOrganizationId}/policies`);
revalidateTag("policies");
// Connect the artifact to the control
await db.control.update({
where: { id: controlId },
data: {
artifacts: {
connect: { id: artifact.id },
},
},
});

return {
success: true,
policyId: policy.id,
};
} catch (error) {
console.error(error);
return artifact;
})
);
}

return {
success: false,
error: "Failed to create policy",
};
}
});
revalidatePath(`/${activeOrganizationId}/policies/all`);
revalidatePath(`/${activeOrganizationId}/policies`);
revalidateTag("policies");

return {
success: true,
policyId: policy.id,
};
} catch (error) {
console.error(error);

return {
success: false,
error: "Failed to create policy",
};
}
});
Loading