From 789551e4433dff77b39cea5483679396e59246f6 Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Mon, 9 Feb 2026 00:55:55 +0530 Subject: [PATCH] feat: Implement privacy preferences for users and agents with new database field, service methods, and API endpoints. --- prisma/schema.prisma | 1 + src/agents/agents.controller.ts | 46 ++++++++++++++++++++++++++++ src/agents/agents.service.ts | 53 +++++++++++++++++++++++++++++++++ src/users/users.controller.ts | 47 +++++++++++++++++++++++++++++ src/users/users.service.ts | 44 +++++++++++++++++++++++++++ 5 files changed, 191 insertions(+) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f11b6f2..f38a38a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -104,6 +104,7 @@ model User { // User Preferences notificationPreferences Json? // { email: {...}, push: {...} } + privacyPreferences Json? // { privacySettings: {...}, dataSettings: {...} } // Timestamps createdAt DateTime @default(now()) diff --git a/src/agents/agents.controller.ts b/src/agents/agents.controller.ts index 7d70934..d837e6e 100644 --- a/src/agents/agents.controller.ts +++ b/src/agents/agents.controller.ts @@ -181,6 +181,52 @@ export class AgentsController { return this.agentsService.updateNotificationPreferences(userId, data); } + // Privacy preferences endpoints + + @Get('preferences/privacy') + @UseGuards(JwtAuthGuard, RolesGuard) + @Roles(UserRole.AGENT) + @ApiBearerAuth() + @ApiOperation({ summary: 'Get privacy preferences (Agents only)' }) + @ApiResponse({ + status: 200, + description: 'Privacy preferences retrieved', + schema: { + example: { + privacySettings: { + profile_visibility: 'public', + contact_info: 'connections', + activity_status: 'public', + }, + dataSettings: { + shareAnalytics: false, + personalizedAds: false, + thirdPartySharing: false, + }, + }, + }, + }) + async getPrivacyPreferences(@CurrentUser('id') userId: string) { + return this.agentsService.getPrivacyPreferences(userId); + } + + @Put('preferences/privacy') + @UseGuards(JwtAuthGuard, RolesGuard) + @Roles(UserRole.AGENT) + @ApiBearerAuth() + @ApiOperation({ summary: 'Update privacy preferences (Agents only)' }) + @ApiResponse({ status: 200, description: 'Privacy preferences updated' }) + async updatePrivacyPreferences( + @CurrentUser('id') userId: string, + @Body() + data: { + privacySettings?: Record; + dataSettings?: Record; + }, + ) { + return this.agentsService.updatePrivacyPreferences(userId, data); + } + // Public parameterized routes - MUST come after specific routes like profile/* @Get(':id') @Public() diff --git a/src/agents/agents.service.ts b/src/agents/agents.service.ts index 35b082e..bb30fdd 100644 --- a/src/agents/agents.service.ts +++ b/src/agents/agents.service.ts @@ -840,4 +840,57 @@ export class AgentsService { return updatedUser.notificationPreferences; } + + // ============================================= + // PRIVACY PREFERENCES + // ============================================= + + async getPrivacyPreferences(userId: string) { + const user = await this.prisma.user.findUnique({ + where: { id: userId }, + select: { privacyPreferences: true, role: true }, + }); + + if (!user) { + throw new NotFoundException('User not found'); + } + + if (user.role !== 'AGENT') { + throw new BadRequestException('This endpoint is for agents only'); + } + + // Return preferences or default empty object + return user.privacyPreferences || { privacySettings: {}, dataSettings: {} }; + } + + async updatePrivacyPreferences( + userId: string, + preferences: { + privacySettings?: Record; + dataSettings?: Record; + }, + ) { + const user = await this.prisma.user.findUnique({ + where: { id: userId }, + select: { role: true }, + }); + + if (!user) { + throw new NotFoundException('User not found'); + } + + if (user.role !== 'AGENT') { + throw new BadRequestException('This endpoint is for agents only'); + } + + const updatedUser = await this.prisma.user.update({ + where: { id: userId }, + data: { + privacyPreferences: preferences, + }, + select: { privacyPreferences: true }, + }); + + return updatedUser.privacyPreferences; + } } diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index f708e97..d3f46e8 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -129,6 +129,53 @@ export class UsersController { return this.usersService.updateNotificationPreferences(user.id, data); } + // ============================================= + // PRIVACY PREFERENCES (for regular users) + // ============================================= + + @Get('preferences/privacy') + @Roles(UserRole.USER) + @ApiOperation({ summary: 'Get privacy preferences (Regular users only)' }) + @ApiResponse({ + status: 200, + description: 'Privacy preferences retrieved', + schema: { + example: { + privacySettings: { + profile_visibility: 'public', + contact_info: 'connections', + activity_status: 'public', + }, + dataSettings: { + shareAnalytics: false, + personalizedAds: false, + thirdPartySharing: false, + }, + }, + }, + }) + async getPrivacyPreferences(@CurrentUser() user: { id: string }) { + return this.usersService.getPrivacyPreferences(user.id); + } + + @Put('preferences/privacy') + @Roles(UserRole.USER) + @ApiOperation({ summary: 'Update privacy preferences (Regular users only)' }) + @ApiResponse({ + status: 200, + description: 'Privacy preferences updated', + }) + async updatePrivacyPreferences( + @CurrentUser() user: { id: string }, + @Body() + data: { + privacySettings?: Record; + dataSettings?: Record; + }, + ) { + return this.usersService.updatePrivacyPreferences(user.id, data); + } + // ============================================= // ADMIN ENDPOINTS // ============================================= diff --git a/src/users/users.service.ts b/src/users/users.service.ts index fc528b1..f7843be 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -441,4 +441,48 @@ export class UsersService { return updatedUser.notificationPreferences; } + + // ============================================= + // PRIVACY PREFERENCES + // ============================================= + + async getPrivacyPreferences(userId: string) { + const user = await this.prisma.user.findUnique({ + where: { id: userId }, + select: { privacyPreferences: true }, + }); + + if (!user) { + throw new NotFoundException('User not found'); + } + + // Return preferences or default empty object + return user.privacyPreferences || { privacySettings: {}, dataSettings: {} }; + } + + async updatePrivacyPreferences( + userId: string, + preferences: { + privacySettings?: Record; + dataSettings?: Record; + }, + ) { + const user = await this.prisma.user.findUnique({ + where: { id: userId }, + }); + + if (!user) { + throw new NotFoundException('User not found'); + } + + const updatedUser = await this.prisma.user.update({ + where: { id: userId }, + data: { + privacyPreferences: preferences, + }, + select: { privacyPreferences: true }, + }); + + return updatedUser.privacyPreferences; + } }