import { Injectable, NotFoundException } from '@nestjs/common'; import { EntityManager } from '@mikro-orm/postgresql'; import { NotificationPreference } from '../entities/notification-preference.entity'; import { Restaurant } from '../../restaurants/entities/restaurant.entity'; import { CreatePreferenceDto } from '../dto/create-preference.dto'; import { UpdatePreferenceDto } from '../dto/update-preference.dto'; import { NotifTitleEnum } from '../interfaces/notification.interface'; @Injectable() export class NotificationPreferenceService { constructor(private readonly em: EntityManager) { } async create(restaurantId: string, dto: CreatePreferenceDto): Promise { const restaurant = await this.em.findOne(Restaurant, { id: restaurantId }); if (!restaurant) { throw new NotFoundException('Restaurant not found'); } const preference = this.em.create(NotificationPreference, { restaurant, channels: dto.channels, title: dto.title, }); await this.em.persistAndFlush(preference); return preference; } async findByRestaurant(restaurantId: string): Promise { return this.em.find(NotificationPreference, { restaurant: { id: restaurantId }, }); } async findByRestaurantAndType(restaurantId: string, title: NotifTitleEnum): Promise { return this.em.findOne(NotificationPreference, { restaurant: { id: restaurantId }, title, }); } // async updateEnabled( // restaurantId: string, // notificationType: string, // enabled: boolean, // ): Promise { // const preference = await this.findByRestaurantAndType(restaurantId, notificationType); // if (!preference) { // throw new NotFoundException('Notification preference not found'); // } // preference.enabled = enabled; // await this.em.persistAndFlush(preference); // return preference; // } async updatePreference( preferenceId: string, restaurantId: string, dto: UpdatePreferenceDto, ): Promise { const preference = await this.em.findOne(NotificationPreference, { id: preferenceId, restaurant: { id: restaurantId }, }); if (!preference) { throw new NotFoundException('Notification preference not found'); } this.em.assign(preference, dto); await this.em.persistAndFlush(preference); return preference; } async delete(restaurantId: string, preferenceId: string): Promise { const preference = await this.em.findOne(NotificationPreference, { restaurant: { id: restaurantId }, id: preferenceId, }); if (!preference) { throw new NotFoundException('Notification preference not found'); } await this.em.removeAndFlush(preference); } }