feat: Implement notification preferences management for both users and agents.

This commit is contained in:
pradeepkumar
2026-02-09 00:33:54 +05:30
parent 3b1a7b999e
commit 6ade2c74c5
5 changed files with 193 additions and 0 deletions

View File

@@ -133,6 +133,54 @@ export class AgentsController {
return this.agentsService.saveFieldValues(userId, dto);
}
// Notification preferences endpoints
@Get('preferences/notifications')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.AGENT)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get notification preferences (Agents only)' })
@ApiResponse({
status: 200,
description: 'Notification preferences retrieved',
schema: {
example: {
email: {
new_leads: true,
messages: true,
connection_requests: true,
property_updates: false,
marketing: false,
},
push: {
push_messages: true,
push_leads: true,
push_reminders: false,
},
},
},
})
async getNotificationPreferences(@CurrentUser('id') userId: string) {
return this.agentsService.getNotificationPreferences(userId);
}
@Put('preferences/notifications')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.AGENT)
@ApiBearerAuth()
@ApiOperation({ summary: 'Update notification preferences (Agents only)' })
@ApiResponse({ status: 200, description: 'Notification preferences updated' })
async updateNotificationPreferences(
@CurrentUser('id') userId: string,
@Body()
data: {
email?: Record<string, boolean>;
push?: Record<string, boolean>;
},
) {
return this.agentsService.updateNotificationPreferences(userId, data);
}
// Public parameterized routes - MUST come after specific routes like profile/*
@Get(':id')
@Public()

View File

@@ -2,6 +2,7 @@ import {
Injectable,
NotFoundException,
ConflictException,
BadRequestException,
} from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import {
@@ -789,4 +790,54 @@ export class AgentsService {
);
}
}
// =============================================
// NOTIFICATION PREFERENCES
// =============================================
async getNotificationPreferences(userId: string) {
const user = await this.prisma.user.findUnique({
where: { id: userId },
select: { notificationPreferences: 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.notificationPreferences || { email: {}, push: {} };
}
async updateNotificationPreferences(
userId: string,
preferences: { email?: Record<string, boolean>; push?: 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: {
notificationPreferences: preferences,
},
select: { notificationPreferences: true },
});
return updatedUser.notificationPreferences;
}
}