Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions BackendAcademy/src/notifications/dto/update-preferences.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface NotificationPreferences {
userId: string;
email_alerts: boolean;
push_notifications: boolean;
marketing_updates: boolean;
}
16 changes: 15 additions & 1 deletion BackendAcademy/src/notifications/notifications.controller.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}
25 changes: 25 additions & 0 deletions BackendAcademy/src/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
@@ -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<string, NotificationPreferences> = new Map();

create(createNotificationDto: CreateNotificationDto): Notification {
const newNotification: Notification = {
Expand All @@ -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,
};
}
}
Loading