Skip to content

Commit f16f93f

Browse files
authored
Merge pull request #4 from topcoder-platform/develop
Allow lookup by oldId value as well as normal ID (PS-435)
2 parents 31a2a66 + 2ca793c commit f16f93f

File tree

1 file changed

+77
-45
lines changed

1 file changed

+77
-45
lines changed

src/api/group/group.service.ts

Lines changed: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -226,68 +226,100 @@ export class GroupService {
226226
parseCommaSeparatedString(criteria.fields, ALLOWED_FIELD_NAMES) ||
227227
ALLOWED_FIELD_NAMES;
228228

229-
const prismaFilter: any = {
230-
where: {
231-
id: groupId,
232-
},
233-
};
234-
235-
if (oldId) {
236-
prismaFilter.where = {
237-
oldId: oldId,
229+
const buildPrismaFilter = (whereClause: Record<string, string>) => {
230+
const prismaFilter: any = {
231+
where: {
232+
...whereClause,
233+
},
238234
};
239-
}
240235

241-
if (
242-
criteria.includeSubGroups ||
243-
criteria.includeParentGroup ||
244-
criteria.flattenGroupIdTree
245-
) {
246-
prismaFilter.include = {};
247-
248-
if (criteria.includeSubGroups || criteria.flattenGroupIdTree) {
249-
if (criteria.oneLevel) {
250-
prismaFilter.include.subGroups = true;
251-
} else {
252-
// max 3 level subGroups
253-
prismaFilter.include.subGroups = {
254-
include: {
255-
subGroups: {
256-
include: {
257-
subGroups: true,
236+
if (
237+
criteria.includeSubGroups ||
238+
criteria.includeParentGroup ||
239+
criteria.flattenGroupIdTree
240+
) {
241+
prismaFilter.include = {};
242+
243+
if (criteria.includeSubGroups || criteria.flattenGroupIdTree) {
244+
if (criteria.oneLevel) {
245+
prismaFilter.include.subGroups = true;
246+
} else {
247+
// max 3 level subGroups
248+
prismaFilter.include.subGroups = {
249+
include: {
250+
subGroups: {
251+
include: {
252+
subGroups: true,
253+
},
258254
},
259255
},
260-
},
261-
};
256+
};
257+
}
262258
}
263-
}
264259

265-
if (criteria.includeParentGroup) {
266-
if (criteria.oneLevel) {
267-
prismaFilter.include.parentGroups = true;
268-
} else {
269-
// max 3 level parentGroups
270-
prismaFilter.include.parentGroups = {
271-
include: {
272-
parentGroups: {
273-
include: {
274-
parentGroups: true,
260+
if (criteria.includeParentGroup) {
261+
if (criteria.oneLevel) {
262+
prismaFilter.include.parentGroups = true;
263+
} else {
264+
// max 3 level parentGroups
265+
prismaFilter.include.parentGroups = {
266+
include: {
267+
parentGroups: {
268+
include: {
269+
parentGroups: true,
270+
},
275271
},
276272
},
277-
},
278-
};
273+
};
274+
}
279275
}
280276
}
277+
278+
return prismaFilter;
279+
};
280+
281+
const lookupOrder: Array<{ field: 'id' | 'oldId'; value: string }> = [];
282+
283+
if (groupId) {
284+
lookupOrder.push({ field: 'id', value: groupId });
285+
}
286+
287+
if (oldId) {
288+
lookupOrder.push({ field: 'oldId', value: oldId });
289+
} else if (groupId) {
290+
lookupOrder.push({ field: 'oldId', value: groupId });
291+
}
292+
293+
if (!lookupOrder.length) {
294+
lookupOrder.push({ field: 'id', value: groupId });
295+
}
296+
297+
let entity;
298+
let resolvedGroupId: string | undefined;
299+
for (const lookup of lookupOrder) {
300+
const prismaFilter = buildPrismaFilter({ [lookup.field]: lookup.value });
301+
// eslint-disable-next-line no-await-in-loop
302+
entity = await this.prisma.group.findFirst(prismaFilter);
303+
if (entity) {
304+
resolvedGroupId = entity.id;
305+
break;
306+
}
281307
}
282308

283-
let entity = await this.prisma.group.findFirst(prismaFilter);
284309
if (!entity) {
285-
throw new NotFoundException(`Not found group of id ${groupId}`);
310+
const identifier = oldId ?? groupId ?? '';
311+
throw new NotFoundException(`Not group found with id or oldId: ${identifier}`);
286312
}
287313

314+
const groupIdentifier = resolvedGroupId ?? entity.id;
315+
288316
// if the group is private, the user needs to be a member of the group, or an admin
289317
if (entity.privateGroup && !isAdmin) {
290-
await ensureGroupMember(this.prisma, groupId, authUser.userId || '');
318+
await ensureGroupMember(
319+
this.prisma,
320+
groupIdentifier,
321+
authUser.userId || '',
322+
);
291323
}
292324

293325
if (criteria.flattenGroupIdTree) {

0 commit comments

Comments
 (0)