Skip to content

Commit

Permalink
Enforce one workspace group per workspace (#6505)
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph authored Jul 25, 2024
1 parent 4ab90aa commit 6f4319a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
19 changes: 10 additions & 9 deletions front/lib/resources/storage/models/groups.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GroupType } from "@dust-tt/types";
import { isSystemGroupType } from "@dust-tt/types";
import { isSystemGroupType, isWorkspaceGroupType } from "@dust-tt/types";
import type {
CreationOptional,
ForeignKey,
Expand Down Expand Up @@ -59,21 +59,22 @@ GroupModel.init(
);

GroupModel.addHook(
"beforeSave",
"enforce_one_system_group_per_workspace",
"beforeCreate",
"enforce_one_system_and_workspace_group_per_workspace",
async (group: GroupModel, options: { transaction: Transaction }) => {
if (isSystemGroupType(group.type)) {
const existingSystemGroupType = await GroupModel.findOne({
const groupType = group.type;
if (isSystemGroupType(groupType) || isWorkspaceGroupType(groupType)) {
const existingSystemOrWorkspaceGroupType = await GroupModel.findOne({
where: {
workspaceId: group.workspaceId,
type: group.type,
type: groupType,
},
transaction: options.transaction,
});

if (existingSystemGroupType) {
throw new Error("A system group exists for this workspace.", {
cause: "enforce_one_system_group_per_workspace",
if (existingSystemOrWorkspaceGroupType) {
throw new Error(`A ${groupType} group exists for this workspace.`, {
cause: `enforce_one_${groupType}_group_per_workspace`,
});
}
}
Expand Down
23 changes: 15 additions & 8 deletions front/migrations/20240724_workspaces_groups_backfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,21 @@ async function backfillWorkspacesGroup(execute: boolean) {
});
console.log(`System group created for workspace ${w.id}`);
} catch (error) {
if (
error instanceof Error &&
error.cause &&
error.cause === "enforce_one_system_group_per_workspace"
) {
console.log(
`System group already exists for workspace ${w.id}`
);
if (error instanceof Error && error.cause) {
switch (error.cause) {
case "enforce_one_system_group_per_workspace":
console.log(
`System group already exists for workspace ${w.id}`
);
break;
case "enforce_one_workspace_group_per_workspace":
console.log(
`Workspace group already exists for workspace ${w.id}`
);
break;
default:
console.error(error);
}
} else {
console.error(error);
}
Expand Down
3 changes: 3 additions & 0 deletions types/src/front/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ export function isValidGroupType(value: unknown): value is GroupType {
export function isSystemGroupType(value: GroupType): boolean {
return value === "system";
}
export function isWorkspaceGroupType(value: GroupType): boolean {
return value === "workspace";
}

0 comments on commit 6f4319a

Please sign in to comment.