feat: Implement notification preferences management for both users and agents.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Put,
|
||||
Param,
|
||||
Patch,
|
||||
Body,
|
||||
@@ -79,6 +80,55 @@ export class UsersController {
|
||||
return this.usersService.updateMyUserProfile(user.id, data);
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// NOTIFICATION PREFERENCES (for regular users)
|
||||
// =============================================
|
||||
|
||||
@Get('preferences/notifications')
|
||||
@Roles(UserRole.USER)
|
||||
@ApiOperation({ summary: 'Get notification preferences (Regular users 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() user: { id: string }) {
|
||||
return this.usersService.getNotificationPreferences(user.id);
|
||||
}
|
||||
|
||||
@Put('preferences/notifications')
|
||||
@Roles(UserRole.USER)
|
||||
@ApiOperation({ summary: 'Update notification preferences (Regular users only)' })
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'Notification preferences updated',
|
||||
})
|
||||
async updateNotificationPreferences(
|
||||
@CurrentUser() user: { id: string },
|
||||
@Body()
|
||||
data: {
|
||||
email?: Record<string, boolean>;
|
||||
push?: Record<string, boolean>;
|
||||
},
|
||||
) {
|
||||
return this.usersService.updateNotificationPreferences(user.id, data);
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// ADMIN ENDPOINTS
|
||||
// =============================================
|
||||
|
||||
@@ -400,4 +400,45 @@ export class UsersService {
|
||||
message: `Verification ${data.status.toLowerCase()} successfully`,
|
||||
};
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// NOTIFICATION PREFERENCES
|
||||
// =============================================
|
||||
|
||||
async getNotificationPreferences(userId: string) {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { notificationPreferences: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
// 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 },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
const updatedUser = await this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
notificationPreferences: preferences,
|
||||
},
|
||||
select: { notificationPreferences: true },
|
||||
});
|
||||
|
||||
return updatedUser.notificationPreferences;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user