Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 77 additions & 45 deletions src/api/group/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>) => {
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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[⚠️ performance]
Using await inside a loop can lead to performance issues because it waits for each promise to resolve before continuing to the next iteration. Consider using Promise.all to execute these operations concurrently if they are independent.

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}`);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[💡 correctness]
The error message in NotFoundException has a typo: 'Not group found' should be 'No group found'.

}

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) {
Expand Down