From 4e2a9765bc96acdafd0f592549958fe14acae4cf Mon Sep 17 00:00:00 2001 From: rameen Date: Sun, 21 Jun 2026 13:24:15 +0330 Subject: [PATCH] backend api service for Count incoming pending connection requests for sidebar badge. --- .../organization/organization.controller.ts | 8 +++++++ .../organization/organization.service.ts | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/backend/src/modules/organization/organization.controller.ts b/backend/src/modules/organization/organization.controller.ts index 7e56c60..7f0fc68 100644 --- a/backend/src/modules/organization/organization.controller.ts +++ b/backend/src/modules/organization/organization.controller.ts @@ -64,6 +64,14 @@ export class OrganizationController { return this.organizationService.searchCounterpartOrganizations(req.user.id, organizationId, q); } + @Get('connections/pending-count') + @UseGuards(JwtAuthGuard) + @ApiOperation({ summary: 'Count incoming pending connection requests for sidebar badge' }) + countPendingConnections(@Req() req: { user: { id: string; organizationId?: string } }) { + const organizationId = this.organizationService.getOrganizationIdFromUser(req.user); + return this.organizationService.countIncomingPendingConnections(req.user.id, organizationId); + } + @Get('connections') @UseGuards(JwtAuthGuard) @ApiOperation({ summary: 'List counterpart connections for current organization' }) diff --git a/backend/src/modules/organization/organization.service.ts b/backend/src/modules/organization/organization.service.ts index 5198d77..6945d09 100644 --- a/backend/src/modules/organization/organization.service.ts +++ b/backend/src/modules/organization/organization.service.ts @@ -160,6 +160,29 @@ export class OrganizationService { }; } + /** Lightweight count for sidebar badge — incoming PENDING requests only. */ + async countIncomingPendingConnections(userId: string, organizationId: string) { + const actor = await this.getActorMembership(userId, organizationId); + if (!actor || !this.canEditOrganizations(actor)) { + throw new ForbiddenException('You do not have permission to manage organizations'); + } + + const links = await this.prisma.organizationLink.findMany({ + where: { + status: LinkStatus.PENDING, + OR: [{ organizationAId: organizationId }, { organizationBId: organizationId }], + }, + select: { sharedDataTypes: true }, + }); + + const count = links.filter((link) => { + const requesterOrgId = this.getRequesterOrganizationId(link.sharedDataTypes); + return requesterOrgId !== null && requesterOrgId !== organizationId; + }).length; + + return { success: true, data: { count } }; + } + async listInvitationHistory(userId: string, organizationId: string) { const actor = await this.getActorMembership(userId, organizationId); if (!actor || !this.canEditOrganizations(actor)) {