From 2ca793ca86b124c440c5a6a58a80c42279faea38 Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Wed, 5 Nov 2025 11:40:46 +1100 Subject: [PATCH] Allow lookup by oldId value as well as normal ID --- src/api/group/group.service.ts | 122 +++++++++++++++++++++------------ 1 file changed, 77 insertions(+), 45 deletions(-) diff --git a/src/api/group/group.service.ts b/src/api/group/group.service.ts index 6effb5d..351d14f 100644 --- a/src/api/group/group.service.ts +++ b/src/api/group/group.service.ts @@ -226,68 +226,100 @@ export class GroupService { parseCommaSeparatedString(criteria.fields, ALLOWED_FIELD_NAMES) || ALLOWED_FIELD_NAMES; - const prismaFilter: any = { - where: { - id: groupId, - }, - }; - - if (oldId) { - prismaFilter.where = { - oldId: oldId, + const buildPrismaFilter = (whereClause: Record) => { + const prismaFilter: any = { + where: { + ...whereClause, + }, }; - } - if ( - criteria.includeSubGroups || - criteria.includeParentGroup || - criteria.flattenGroupIdTree - ) { - prismaFilter.include = {}; - - if (criteria.includeSubGroups || criteria.flattenGroupIdTree) { - if (criteria.oneLevel) { - prismaFilter.include.subGroups = true; - } else { - // max 3 level subGroups - prismaFilter.include.subGroups = { - include: { - subGroups: { - include: { - subGroups: true, + if ( + criteria.includeSubGroups || + criteria.includeParentGroup || + criteria.flattenGroupIdTree + ) { + prismaFilter.include = {}; + + if (criteria.includeSubGroups || criteria.flattenGroupIdTree) { + if (criteria.oneLevel) { + prismaFilter.include.subGroups = true; + } else { + // max 3 level subGroups + prismaFilter.include.subGroups = { + include: { + subGroups: { + include: { + subGroups: true, + }, }, }, - }, - }; + }; + } } - } - if (criteria.includeParentGroup) { - if (criteria.oneLevel) { - prismaFilter.include.parentGroups = true; - } else { - // max 3 level parentGroups - prismaFilter.include.parentGroups = { - include: { - parentGroups: { - include: { - parentGroups: true, + if (criteria.includeParentGroup) { + if (criteria.oneLevel) { + prismaFilter.include.parentGroups = true; + } else { + // max 3 level parentGroups + prismaFilter.include.parentGroups = { + include: { + parentGroups: { + include: { + parentGroups: true, + }, }, }, - }, - }; + }; + } } } + + return prismaFilter; + }; + + const lookupOrder: Array<{ field: 'id' | 'oldId'; value: string }> = []; + + if (groupId) { + lookupOrder.push({ field: 'id', value: groupId }); + } + + if (oldId) { + lookupOrder.push({ field: 'oldId', value: oldId }); + } else if (groupId) { + lookupOrder.push({ field: 'oldId', value: groupId }); + } + + if (!lookupOrder.length) { + lookupOrder.push({ field: 'id', value: groupId }); + } + + let entity; + let resolvedGroupId: string | undefined; + for (const lookup of lookupOrder) { + const prismaFilter = buildPrismaFilter({ [lookup.field]: lookup.value }); + // eslint-disable-next-line no-await-in-loop + entity = await this.prisma.group.findFirst(prismaFilter); + if (entity) { + resolvedGroupId = entity.id; + break; + } } - let entity = await this.prisma.group.findFirst(prismaFilter); if (!entity) { - throw new NotFoundException(`Not found group of id ${groupId}`); + const identifier = oldId ?? groupId ?? ''; + throw new NotFoundException(`Not group found with id or oldId: ${identifier}`); } + const groupIdentifier = resolvedGroupId ?? entity.id; + // if the group is private, the user needs to be a member of the group, or an admin if (entity.privateGroup && !isAdmin) { - await ensureGroupMember(this.prisma, groupId, authUser.userId || ''); + await ensureGroupMember( + this.prisma, + groupIdentifier, + authUser.userId || '', + ); } if (criteria.flattenGroupIdTree) {