From 4a64a767e102e9ac33f2acdaf38bcbf74bcc8766 Mon Sep 17 00:00:00 2001 From: Hemachandar <132386067+hmacr@users.noreply.github.com> Date: Mon, 6 Jan 2025 06:03:51 +0530 Subject: [PATCH] Convert `GroupUser` mutations (#8187) * Convert 'GroupUser' mutations * cleanup commands --- server/commands/groupUserCreator.ts | 55 ------------------- server/commands/groupUserDestroyer.ts | 45 --------------- server/commands/userSuspender.ts | 1 + server/models/GroupUser.ts | 25 +++++++++ server/models/base/Model.ts | 2 +- .../queues/processors/UserDeletedProcessor.ts | 1 + server/routes/api/groups/groups.ts | 31 ++++++----- server/types.ts | 3 - 8 files changed, 46 insertions(+), 117 deletions(-) delete mode 100644 server/commands/groupUserCreator.ts delete mode 100644 server/commands/groupUserDestroyer.ts diff --git a/server/commands/groupUserCreator.ts b/server/commands/groupUserCreator.ts deleted file mode 100644 index 074d1799b5..0000000000 --- a/server/commands/groupUserCreator.ts +++ /dev/null @@ -1,55 +0,0 @@ -import type { Transaction } from "sequelize"; -import { Event, GroupUser, type Group, type User } from "@server/models"; - -type Props = { - group: Group; - user: User; - actor: User; - ip: string; - transaction?: Transaction; -}; - -export default async function groupUserCreator({ - group, - user, - actor, - ip, - transaction, -}: Props): Promise { - let groupUser = await GroupUser.findOne({ - where: { - groupId: group.id, - userId: user.id, - }, - transaction, - }); - - if (!groupUser) { - groupUser = await GroupUser.create( - { - groupId: group.id, - userId: user.id, - createdById: actor.id, - }, - { transaction } - ); - groupUser.user = user; - - await Event.create( - { - name: "groups.add_user", - userId: user.id, - modelId: group.id, - teamId: user.teamId, - actorId: actor.id, - data: { - name: user.name, - }, - ip, - }, - { transaction } - ); - } - - return groupUser; -} diff --git a/server/commands/groupUserDestroyer.ts b/server/commands/groupUserDestroyer.ts deleted file mode 100644 index de3bd4a5ad..0000000000 --- a/server/commands/groupUserDestroyer.ts +++ /dev/null @@ -1,45 +0,0 @@ -import type { Transaction } from "sequelize"; -import { Event, GroupUser, type Group, type User } from "@server/models"; - -type Props = { - group: Group; - user: User; - actor: User; - ip: string; - transaction?: Transaction; -}; - -export default async function groupUserDestroyer({ - group, - user, - actor, - ip, - transaction, -}: Props): Promise { - const groupUser = await GroupUser.unscoped().findOne({ - where: { - groupId: group.id, - userId: user.id, - }, - transaction, - lock: transaction?.LOCK.UPDATE, - }); - - if (groupUser) { - await groupUser.destroy({ transaction }); - await Event.create( - { - name: "groups.remove_user", - userId: user.id, - modelId: group.id, - teamId: user.teamId, - actorId: actor.id, - data: { - name: user.name, - }, - ip, - }, - { transaction } - ); - } -} diff --git a/server/commands/userSuspender.ts b/server/commands/userSuspender.ts index 023a7f3f9d..4ecfa54408 100644 --- a/server/commands/userSuspender.ts +++ b/server/commands/userSuspender.ts @@ -38,6 +38,7 @@ export default async function userSuspender({ userId: user.id, }, transaction, + hooks: false, }); await Event.create( { diff --git a/server/models/GroupUser.ts b/server/models/GroupUser.ts index f891342813..028b263bd2 100644 --- a/server/models/GroupUser.ts +++ b/server/models/GroupUser.ts @@ -7,7 +7,10 @@ import { Table, DataType, Scopes, + AfterCreate, + AfterDestroy, } from "sequelize-typescript"; +import { APIContext } from "@server/types"; import Group from "./Group"; import User from "./User"; import Model from "./base/Model"; @@ -62,6 +65,28 @@ class GroupUser extends Model< @ForeignKey(() => User) @Column(DataType.UUID) createdById: string; + + get modelId() { + return this.groupId; + } + + // hooks + + @AfterCreate + public static async publishAddUserEvent( + model: GroupUser, + context: APIContext["context"] + ) { + await Group.insertEvent("add_user", model, context); + } + + @AfterDestroy + public static async publishRemoveUserEvent( + model: GroupUser, + context: APIContext["context"] + ) { + await Group.insertEvent("remove_user", model, context); + } } export default GroupUser; diff --git a/server/models/base/Model.ts b/server/models/base/Model.ts index ec6017cc3e..b1097c2d8c 100644 --- a/server/models/base/Model.ts +++ b/server/models/base/Model.ts @@ -183,7 +183,7 @@ class Model< return models.event.create( { name: `${namespace}.${model.eventOverride?.name ?? name}`, - modelId: model.id, + modelId: "modelId" in model ? model.modelId : model.id, collectionId: "collectionId" in model ? model.collectionId diff --git a/server/queues/processors/UserDeletedProcessor.ts b/server/queues/processors/UserDeletedProcessor.ts index d6dcef76fd..7936263c08 100644 --- a/server/queues/processors/UserDeletedProcessor.ts +++ b/server/queues/processors/UserDeletedProcessor.ts @@ -20,6 +20,7 @@ export default class UserDeletedProcessor extends BaseProcessor { userId: event.userId, }, transaction, + hooks: false, }); await UserAuthentication.destroy({ where: { diff --git a/server/routes/api/groups/groups.ts b/server/routes/api/groups/groups.ts index 21438ceb3d..7bc49b5e85 100644 --- a/server/routes/api/groups/groups.ts +++ b/server/routes/api/groups/groups.ts @@ -1,8 +1,6 @@ import Router from "koa-router"; import { Op, WhereOptions } from "sequelize"; import { MAX_AVATAR_DISPLAY } from "@shared/constants"; -import groupUserCreator from "@server/commands/groupUserCreator"; -import groupUserDestroyer from "@server/commands/groupUserDestroyer"; import auth from "@server/middlewares/authentication"; import { rateLimiter } from "@server/middlewares/rateLimiter"; import { transaction } from "@server/middlewares/transaction"; @@ -263,14 +261,18 @@ router.post( const group = await Group.findByPk(id, { transaction }); authorize(actor, "update", group); - const groupUser = await groupUserCreator({ - group, - user, - actor, - ip: ctx.request.ip, - transaction, + const [groupUser] = await GroupUser.findOrCreateWithCtx(ctx, { + where: { + groupId: group.id, + userId: user.id, + }, + defaults: { + createdById: actor.id, + }, }); + groupUser.user = user; + ctx.body = { data: { users: [presentUser(user)], @@ -297,14 +299,17 @@ router.post( const user = await User.findByPk(userId, { transaction }); authorize(actor, "read", user); - await groupUserDestroyer({ - group, - user, - actor, - ip: ctx.request.ip, + const groupUser = await GroupUser.unscoped().findOne({ + where: { + groupId: group.id, + userId: user.id, + }, transaction, + lock: transaction.LOCK.UPDATE, }); + await groupUser?.destroyWithCtx(ctx); + ctx.body = { data: { groups: [await presentGroup(group)], diff --git a/server/types.ts b/server/types.ts index dfaa80370c..26a72d115a 100644 --- a/server/types.ts +++ b/server/types.ts @@ -343,9 +343,6 @@ export type GroupUserEvent = BaseEvent & { name: "groups.add_user" | "groups.remove_user"; userId: string; modelId: string; - data: { - name: string; - }; }; export type GroupEvent = BaseEvent &