From c20a1a7a00bac33dde6ccf50c73dcf1f8b4e16d7 Mon Sep 17 00:00:00 2001 From: kassad Date: Mon, 19 Aug 2024 07:45:42 -0700 Subject: [PATCH] Fixed PR comments --- .../src/community/CreateGroup.command.ts | 25 +++++++++---------- .../server_groups_methods/create_group.ts | 24 ++++++++---------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/libs/model/src/community/CreateGroup.command.ts b/libs/model/src/community/CreateGroup.command.ts index 11ac7109aca..e33a508810f 100644 --- a/libs/model/src/community/CreateGroup.command.ts +++ b/libs/model/src/community/CreateGroup.command.ts @@ -56,20 +56,19 @@ export function CreateGroup(): Command { { transaction }, ); if (topicsToAssociate.length > 0) { - const createPermissionsPromises = topicsToAssociate.map((topic) => { - return models.GroupPermission.create( - { - group_id: group.id, - topic_id: topic.id, - allowed_actions: Object.values(ForumActionsEnum), - }, - { - transaction, - }, - ); - }); + const permissions = topicsToAssociate.map((topic) => ({ + group_id: group.id, + topic_id: topic.id, + allowed_actions: this.models.sequelize.literal( + `ARRAY[${Object.values(ForumActionsEnum) + .map((value) => `'${value}'`) + .join(', ')}]::"enum_GroupPermissions_allowed_actions"[]`, + ), + })); - await Promise.all(createPermissionsPromises); + await models.GroupPermission.bulkCreate(permissions, { + transaction, + }); } return group.toJSON(); }, diff --git a/packages/commonwealth/server/controllers/server_groups_methods/create_group.ts b/packages/commonwealth/server/controllers/server_groups_methods/create_group.ts index 13cd7f71025..7094a76d2c5 100644 --- a/packages/commonwealth/server/controllers/server_groups_methods/create_group.ts +++ b/packages/commonwealth/server/controllers/server_groups_methods/create_group.ts @@ -2,6 +2,7 @@ import { AppError } from '@hicommonwealth/core'; import { CommunityAttributes, GroupAttributes, + models, UserInstance, } from '@hicommonwealth/model'; import { ForumActionsEnum, GroupMetadata } from '@hicommonwealth/schemas'; @@ -116,20 +117,17 @@ export async function __createGroup( { transaction: t }, ); if (topicsToAssociate.length > 0) { - const createPermissionsPromises = topicsToAssociate.map((topic) => { - return this.models.GroupPermission.create( - { - group_id: group.id, - topic_id: topic.id, - allowed_actions: Object.values(ForumActionsEnum), - }, - { - transaction: t, - }, - ); - }); + const permissions = topicsToAssociate.map((topic) => ({ + group_id: group.id, + topic_id: topic.id, + allowed_actions: this.models.sequelize.literal( + `ARRAY[${Object.values(ForumActionsEnum) + .map((value) => `'${value}'`) + .join(', ')}]::"enum_GroupPermissions_allowed_actions"[]`, + ), + })); - await Promise.all(createPermissionsPromises); + await models.GroupPermission.bulkCreate(permissions, { transaction: t }); } return group.toJSON(); };