From ac500a2b628b12e4a540ad1c071edbda652da66e Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Wed, 27 Sep 2023 00:25:10 +0300 Subject: [PATCH] Keep manager when morphing groups --- .../sendouq/queries/morphGroups.server.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/features/sendouq/queries/morphGroups.server.ts b/app/features/sendouq/queries/morphGroups.server.ts index 92d6beeb9..b8babdae3 100644 --- a/app/features/sendouq/queries/morphGroups.server.ts +++ b/app/features/sendouq/queries/morphGroups.server.ts @@ -1,6 +1,14 @@ import { nanoid } from "nanoid"; import { sql } from "~/db/sql"; import { deleteLikesByGroupId } from "./deleteLikesByGroupId.server"; +import type { GroupMember, User } from "~/db/types"; + +const findToBeDeletedGroupNonRegularsStm = sql.prepare(/* sql */ ` + select "userId" + from "GroupMember" + where "groupId" = @groupId + and "role" != 'REGULAR' +`); const deleteGroupStm = sql.prepare(/* sql */ ` delete from "Group" @@ -35,6 +43,10 @@ export const morphGroups = sql.transaction( newMembers: number[]; addChatCode: boolean; }) => { + const toBeDeletedGroupNonRegulars = findToBeDeletedGroupNonRegularsStm + .all({ groupId: otherGroupId }) + .map((row: any) => row.userId) as Array; + deleteGroupStm.run({ groupId: otherGroupId }); deleteGroupMapsStm.run({ groupId: otherGroupId }); @@ -49,10 +61,15 @@ export const morphGroups = sql.transaction( } for (const userId of newMembers) { + const role: GroupMember["role"] = toBeDeletedGroupNonRegulars.includes( + userId, + ) + ? "MANAGER" + : "REGULAR"; addGroupMemberStm.run({ groupId: survivingGroupId, userId, - role: "REGULAR", + role, }); } },