feat: Implement privacy preferences for users and agents with new database field, service methods, and API endpoints.

This commit is contained in:
pradeepkumar
2026-02-09 00:55:55 +05:30
parent 6ade2c74c5
commit 789551e443
5 changed files with 191 additions and 0 deletions

View File

@@ -104,6 +104,7 @@ model User {
// User Preferences // User Preferences
notificationPreferences Json? // { email: {...}, push: {...} } notificationPreferences Json? // { email: {...}, push: {...} }
privacyPreferences Json? // { privacySettings: {...}, dataSettings: {...} }
// Timestamps // Timestamps
createdAt DateTime @default(now()) createdAt DateTime @default(now())

View File

@@ -181,6 +181,52 @@ export class AgentsController {
return this.agentsService.updateNotificationPreferences(userId, data); 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<string, string>;
dataSettings?: Record<string, boolean>;
},
) {
return this.agentsService.updatePrivacyPreferences(userId, data);
}
// Public parameterized routes - MUST come after specific routes like profile/* // Public parameterized routes - MUST come after specific routes like profile/*
@Get(':id') @Get(':id')
@Public() @Public()

View File

@@ -840,4 +840,57 @@ export class AgentsService {
return updatedUser.notificationPreferences; 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<string, string>;
dataSettings?: Record<string, boolean>;
},
) {
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;
}
} }

View File

@@ -129,6 +129,53 @@ export class UsersController {
return this.usersService.updateNotificationPreferences(user.id, data); 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<string, string>;
dataSettings?: Record<string, boolean>;
},
) {
return this.usersService.updatePrivacyPreferences(user.id, data);
}
// ============================================= // =============================================
// ADMIN ENDPOINTS // ADMIN ENDPOINTS
// ============================================= // =============================================

View File

@@ -441,4 +441,48 @@ export class UsersService {
return updatedUser.notificationPreferences; 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<string, string>;
dataSettings?: Record<string, boolean>;
},
) {
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;
}
} }