-
Notifications
You must be signed in to change notification settings - Fork 426
Improvements, new tables #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
608c461
add migrations for new schemas
Marfuen cf6c4a3
improvements, fixes, new tables
Marfuen 330bb0d
chore: (i18n) update translations using Languine.ai
languinebot fa37c3b
Merge branch 'main' of github.com:trycompai/comp into mariano/employe…
Marfuen f7df90e
Merge branch 'mariano/employees_table' of github.com:trycompai/comp i…
Marfuen 391cf48
fix build issues
Marfuen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| "use server"; | ||
|
|
||
| import { db } from "@bubba/db"; | ||
| import { authActionClient } from "../safe-action"; | ||
| import type { | ||
| Framework, | ||
| OrganizationControl, | ||
| OrganizationFramework, | ||
| } from "@bubba/db"; | ||
| import { z } from "zod"; | ||
| import type { ActionData } from "../types"; | ||
|
|
||
| type FrameworkWithControls = OrganizationFramework & { | ||
| organizationControl: OrganizationControl[]; | ||
| framework: Framework; | ||
| }; | ||
|
|
||
| export interface FrameworksResponse { | ||
| frameworks: FrameworkWithControls[]; | ||
| availableFrameworks: Framework[]; | ||
| } | ||
|
|
||
| export const getFrameworksAction = authActionClient | ||
| .schema(z.void()) | ||
| .metadata({ | ||
| name: "getFrameworks", | ||
| track: { | ||
| event: "get-frameworks", | ||
| channel: "server", | ||
| }, | ||
| }) | ||
| .action(async ({ ctx }): Promise<ActionData<FrameworksResponse>> => { | ||
| const { user } = ctx; | ||
|
|
||
| if (!user.organizationId) { | ||
| return { | ||
| error: "Not authorized - no organization found", | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| const [frameworks, availableFrameworks] = await Promise.all([ | ||
| db.organizationFramework.findMany({ | ||
| where: { organizationId: user.organizationId }, | ||
| include: { | ||
| organizationControl: true, | ||
| framework: true, | ||
| }, | ||
| }), | ||
| db.framework.findMany(), | ||
| ]); | ||
|
|
||
| return { | ||
| data: { frameworks, availableFrameworks }, | ||
| }; | ||
| } catch (error) { | ||
| console.error("Error fetching frameworks:", error); | ||
| return { | ||
| error: "Failed to fetch frameworks", | ||
| }; | ||
| } | ||
| }); |
143 changes: 143 additions & 0 deletions
143
apps/app/src/actions/framework/select-frameworks-action.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| "use server"; | ||
|
|
||
| import { db, type Policy, type User } from "@bubba/db"; | ||
| import { authActionClient } from "../safe-action"; | ||
| import { z } from "zod"; | ||
| import type { ActionData } from "../types"; | ||
|
|
||
| const selectFrameworksSchema = z.object({ | ||
| frameworkIds: z.array(z.string()), | ||
| }); | ||
|
|
||
| export const selectFrameworksAction = authActionClient | ||
| .schema(selectFrameworksSchema) | ||
| .metadata({ | ||
| name: "select-frameworks", | ||
| track: { | ||
| event: "select-frameworks", | ||
| channel: "server", | ||
| }, | ||
| }) | ||
| .action(async ({ parsedInput, ctx }): Promise<ActionData<true>> => { | ||
| const { frameworkIds } = parsedInput; | ||
| const { user } = ctx; | ||
|
|
||
| if (!user.organizationId) { | ||
| return { | ||
| error: "Not authorized - no organization found", | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| await Promise.all([ | ||
| frameworkIds.map((frameworkId) => | ||
| createOrganizationFramework(user as User, frameworkId) | ||
| ), | ||
| createOrganizationPolicy(user as User, frameworkIds), | ||
| ]); | ||
|
|
||
| return { | ||
| data: true, | ||
| }; | ||
| } catch (error) { | ||
| console.error("Error selecting frameworks:", error); | ||
| return { | ||
| error: "Failed to select frameworks", | ||
| }; | ||
| } | ||
| }); | ||
|
|
||
| const createOrganizationFramework = async (user: User, frameworkId: string) => { | ||
| if (!user.organizationId) { | ||
| throw new Error("Not authorized - no organization found"); | ||
| } | ||
|
|
||
| // Connect the framework to the organization. | ||
| const organizationFramework = await db.organizationFramework.create({ | ||
| data: { | ||
| organizationId: user.organizationId, | ||
| frameworkId, | ||
| status: "not_started", | ||
| }, | ||
| select: { | ||
| id: true, | ||
| }, | ||
| }); | ||
|
|
||
| // For each framework we need to get the categories and controls. | ||
| const framework = await db.framework.findUnique({ | ||
| where: { id: frameworkId }, | ||
| }); | ||
|
|
||
| if (!framework) { | ||
| throw new Error("Framework not found"); | ||
| } | ||
|
|
||
| const frameworkCategories = await db.frameworkCategory.findMany({ | ||
| where: { frameworkId }, | ||
| select: { | ||
| id: true, | ||
| }, | ||
| }); | ||
|
|
||
| // For each category we need to get the controls. | ||
| const frameworkControls = await db.control.findMany({ | ||
| where: { | ||
| frameworkCategoryId: { | ||
| in: frameworkCategories.map((category) => category.id), | ||
| }, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (!user.organizationId) { | ||
| throw new Error("Organization ID is required"); | ||
| } | ||
|
|
||
| await db.organizationControl.createMany({ | ||
| data: frameworkControls.map((control) => ({ | ||
| organizationFrameworkId: organizationFramework.id, | ||
| controlId: control.id, | ||
| organizationId: user.organizationId!, | ||
| status: "not_started", | ||
| })), | ||
| }); | ||
| }; | ||
|
|
||
| const createOrganizationPolicy = async (user: User, frameworkIds: string[]) => { | ||
| if (!user.organizationId) { | ||
| throw new Error("Not authorized - no organization found"); | ||
| } | ||
|
|
||
| const policies = await db.policy.findMany(); | ||
| const policiesForFrameworks: Policy[] = []; | ||
|
|
||
| for (const policy of policies) { | ||
| const usedBy = policy.usedBy; | ||
| if (!usedBy) { | ||
| continue; | ||
| } | ||
|
|
||
| const usedByFrameworkIds = Object.keys(usedBy); | ||
|
|
||
| if ( | ||
| usedByFrameworkIds.some((frameworkId) => | ||
| frameworkIds.includes(frameworkId) | ||
| ) | ||
| ) { | ||
| policiesForFrameworks.push(policy); | ||
| } | ||
| } | ||
|
|
||
| const organizationPolicies = await db.organizationPolicy.createMany({ | ||
| data: policiesForFrameworks.map((policy) => ({ | ||
| organizationId: user.organizationId!, | ||
| policyId: policy.id, | ||
| status: "draft", | ||
| })), | ||
| }); | ||
|
|
||
| return organizationPolicies; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix concurrency bug in
Promise.allusage.You're passing an array of promises as the first element rather than spreading them. Currently:
This means the first element is just an array, not a promise for each framework. Consider spreading or concatenating the mapped promises. For example: