From def365fdcaac082203d283171a5ab70637c09e2c Mon Sep 17 00:00:00 2001 From: Strategy Dan Date: Wed, 1 Jul 2026 07:55:35 +0100 Subject: [PATCH] feat(notifications): add user preferences API endpoint --- .../dto/update-preferences.dto.ts | 15 +++++++++++ .../interfaces/preferences.interface.ts | 6 +++++ .../notifications/notifications.controller.ts | 16 +++++++++++- .../notifications/notifications.service.ts | 25 +++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 BackendAcademy/src/notifications/dto/update-preferences.dto.ts create mode 100644 BackendAcademy/src/notifications/interfaces/preferences.interface.ts diff --git a/BackendAcademy/src/notifications/dto/update-preferences.dto.ts b/BackendAcademy/src/notifications/dto/update-preferences.dto.ts new file mode 100644 index 000000000..59dff33df --- /dev/null +++ b/BackendAcademy/src/notifications/dto/update-preferences.dto.ts @@ -0,0 +1,15 @@ +import { IsOptional, IsBoolean } from 'class-validator'; + +export class UpdateNotificationPreferencesDto { + @IsOptional() + @IsBoolean() + email_alerts?: boolean; + + @IsOptional() + @IsBoolean() + push_notifications?: boolean; + + @IsOptional() + @IsBoolean() + marketing_updates?: boolean; +} diff --git a/BackendAcademy/src/notifications/interfaces/preferences.interface.ts b/BackendAcademy/src/notifications/interfaces/preferences.interface.ts new file mode 100644 index 000000000..5986ba942 --- /dev/null +++ b/BackendAcademy/src/notifications/interfaces/preferences.interface.ts @@ -0,0 +1,6 @@ +export interface NotificationPreferences { + userId: string; + email_alerts: boolean; + push_notifications: boolean; + marketing_updates: boolean; +} diff --git a/BackendAcademy/src/notifications/notifications.controller.ts b/BackendAcademy/src/notifications/notifications.controller.ts index a0b673ed9..d9bab67de 100644 --- a/BackendAcademy/src/notifications/notifications.controller.ts +++ b/BackendAcademy/src/notifications/notifications.controller.ts @@ -1,6 +1,7 @@ -import { Controller, Get, Post, Body, Query } from '@nestjs/common'; +import { Controller, Get, Post, Body, Query, Patch } from '@nestjs/common'; import { NotificationsService } from './notifications.service'; import { CreateNotificationDto } from './dto/create-notification.dto'; +import { UpdateNotificationPreferencesDto } from './dto/update-preferences.dto'; @Controller('notifications') export class NotificationsController { @@ -20,4 +21,17 @@ export class NotificationsController { findByUserId(@Query('userId') userId: string) { return this.notificationsService.findByUserId(userId); } + + @Get('preferences') + getPreferences(@Query('userId') userId: string) { + return this.notificationsService.getPreferences(userId); + } + + @Patch('preferences') + upsertPreferences( + @Query('userId') userId: string, + @Body() updateDto: UpdateNotificationPreferencesDto, + ) { + return this.notificationsService.upsertPreferences(userId, updateDto); + } } diff --git a/BackendAcademy/src/notifications/notifications.service.ts b/BackendAcademy/src/notifications/notifications.service.ts index 40efe5d81..5fda67f5a 100644 --- a/BackendAcademy/src/notifications/notifications.service.ts +++ b/BackendAcademy/src/notifications/notifications.service.ts @@ -1,10 +1,13 @@ import { Injectable } from '@nestjs/common'; import { Notification } from './interfaces/notifications.interface'; import { CreateNotificationDto } from './dto/create-notification.dto'; +import { NotificationPreferences } from './interfaces/preferences.interface'; +import { UpdateNotificationPreferencesDto } from './dto/update-preferences.dto'; @Injectable() export class NotificationsService { private notifications: Notification[] = []; + private preferences: Map = new Map(); create(createNotificationDto: CreateNotificationDto): Notification { const newNotification: Notification = { @@ -24,4 +27,26 @@ export class NotificationsService { findByUserId(userId: string): Notification[] { return this.notifications.filter(n => n.userId === userId); } + + upsertPreferences(userId: string, updateDto: UpdateNotificationPreferencesDto): NotificationPreferences { + const existing = this.preferences.get(userId) || { + userId, + email_alerts: false, + push_notifications: false, + marketing_updates: false, + }; + + const updated = { ...existing, ...updateDto }; + this.preferences.set(userId, updated); + return updated; + } + + getPreferences(userId: string): NotificationPreferences { + return this.preferences.get(userId) || { + userId, + email_alerts: false, + push_notifications: false, + marketing_updates: false, + }; + } }