diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts index c04af5fbf..5a5bb10c5 100644 --- a/apps/api/src/app/user/user.service.ts +++ b/apps/api/src/app/user/user.service.ts @@ -70,7 +70,7 @@ export class UserService { }, where: { userId: id } }), - this.tagService.getInUseByUser(id) + this.tagService.getTagsForUser(id) ]); const access = accessesResult[0]; const firstActivity = accessesResult[1]; diff --git a/apps/api/src/services/tag/tag.service.ts b/apps/api/src/services/tag/tag.service.ts index 5426a0582..5bc6f97fb 100644 --- a/apps/api/src/services/tag/tag.service.ts +++ b/apps/api/src/services/tag/tag.service.ts @@ -17,18 +17,41 @@ export class TagService { }); } - public async getInUseByUser(userId: string) { - return this.prismaService.tag.findMany({ + public async getTagsForUser(userId: string) { + const tags = await this.prismaService.tag.findMany({ orderBy: { name: 'asc' }, where: { + OR: [ + { + orders: { + some: { + userId + } + } + }, + { + userId: null + } + ] + }, + include: { orders: { - some: { + where: { userId + }, + select: { + id: true } } } }); + + return tags.map((tag) => ({ + ...tag, + isUsed: tag.orders.length > 0, + orders: undefined + })); } }