diff --git a/src/auth/services/device.service.ts b/src/auth/services/device.service.ts index d6f3576..c2f1f9f 100644 --- a/src/auth/services/device.service.ts +++ b/src/auth/services/device.service.ts @@ -1,7 +1,6 @@ import { Injectable } from '@nestjs/common'; import { Device } from '../entities'; import { DeviceRepository } from '../repositories'; - @Injectable() export class DeviceService { constructor(private readonly deviceRepository: DeviceRepository) {} diff --git a/src/customer/controllers/customer.controller.ts b/src/customer/controllers/customer.controller.ts index 83aad3c..abddf4c 100644 --- a/src/customer/controllers/customer.controller.ts +++ b/src/customer/controllers/customer.controller.ts @@ -1,6 +1,7 @@ -import { Body, Controller, Get, Patch, UseGuards } from '@nestjs/common'; +import { Body, Controller, Get, Headers, Patch, UseGuards } from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { IJwtPayload } from '~/auth/interfaces'; +import { DEVICE_ID_HEADER } from '~/common/constants'; import { AuthenticatedUser } from '~/common/decorators'; import { AccessTokenGuard } from '~/common/guards'; import { ApiDataResponse } from '~/core/decorators'; @@ -39,8 +40,9 @@ export class CustomerController { async updateNotificationSettings( @AuthenticatedUser() { sub }: IJwtPayload, @Body() body: UpdateNotificationsSettingsRequestDto, + @Headers(DEVICE_ID_HEADER) deviceId: string, ) { - const notificationSettings = await this.customerService.updateNotificationSettings(sub, body); + const notificationSettings = await this.customerService.updateNotificationSettings(sub, body, deviceId); return ResponseFactory.data(new NotificationSettingsResponseDto(notificationSettings)); } diff --git a/src/customer/dtos/request/update-notifications-settings.request.dto.ts b/src/customer/dtos/request/update-notifications-settings.request.dto.ts index c67eb75..e54ffb6 100644 --- a/src/customer/dtos/request/update-notifications-settings.request.dto.ts +++ b/src/customer/dtos/request/update-notifications-settings.request.dto.ts @@ -1,19 +1,24 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { IsBoolean, IsOptional } from 'class-validator'; - +import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; +import { IsBoolean, IsOptional, IsString, ValidateIf } from 'class-validator'; +import { i18nValidationMessage as i18n } from 'nestjs-i18n'; export class UpdateNotificationsSettingsRequestDto { @ApiProperty() - @IsBoolean() + @IsBoolean({ message: i18n('validation.isBoolean', { path: 'general', property: 'customer.isEmailEnabled' }) }) @IsOptional() isEmailEnabled!: boolean; @ApiProperty() - @IsBoolean() + @IsBoolean({ message: i18n('validation.isBoolean', { path: 'general', property: 'customer.isPushEnabled' }) }) @IsOptional() isPushEnabled!: boolean; @ApiProperty() - @IsBoolean() + @IsBoolean({ message: i18n('validation.isBoolean', { path: 'general', property: 'customer.isSmsEnabled' }) }) @IsOptional() isSmsEnabled!: boolean; + + @ApiPropertyOptional() + @IsString({ message: i18n('validation.isString', { path: 'general', property: 'customer.fcmToken' }) }) + @ValidateIf((o) => o.isPushEnabled) + fcmToken?: string; } diff --git a/src/customer/services/customer.service.ts b/src/customer/services/customer.service.ts index 7be024b..11407cb 100644 --- a/src/customer/services/customer.service.ts +++ b/src/customer/services/customer.service.ts @@ -1,5 +1,6 @@ -import { BadRequestException, Injectable } from '@nestjs/common'; +import { BadRequestException, forwardRef, Inject, Injectable } from '@nestjs/common'; import { User } from '~/auth/entities'; +import { DeviceService } from '~/auth/services'; import { OciService } from '~/document/services'; import { UpdateCustomerRequestDto, UpdateNotificationsSettingsRequestDto } from '../dtos/request'; import { Customer } from '../entities'; @@ -7,13 +8,24 @@ import { CustomerRepository } from '../repositories/customer.repository'; @Injectable() export class CustomerService { - constructor(private readonly customerRepository: CustomerRepository, private readonly ociService: OciService) {} - async updateNotificationSettings(userId: string, data: UpdateNotificationsSettingsRequestDto) { + constructor( + private readonly customerRepository: CustomerRepository, + private readonly ociService: OciService, + @Inject(forwardRef(() => DeviceService)) private readonly deviceService: DeviceService, + ) {} + async updateNotificationSettings(userId: string, data: UpdateNotificationsSettingsRequestDto, deviceId: string) { const customer = await this.findCustomerById(userId); const notificationSettings = (await this.customerRepository.updateNotificationSettings(customer, data)) .notificationSettings; + if (data.isPushEnabled && deviceId) { + await this.deviceService.updateDevice(deviceId, { + fcmToken: data.fcmToken, + userId: userId, + }); + } + return notificationSettings; } diff --git a/src/junior/services/junior.service.ts b/src/junior/services/junior.service.ts index 913b2ff..3252d0c 100644 --- a/src/junior/services/junior.service.ts +++ b/src/junior/services/junior.service.ts @@ -14,9 +14,9 @@ import { JuniorTokenService } from './junior-token.service'; export class JuniorService { constructor( private readonly juniorRepository: JuniorRepository, - private readonly customerService: CustomerService, private readonly juniorTokenService: JuniorTokenService, @Inject(forwardRef(() => UserService)) private readonly userService: UserService, + @Inject(forwardRef(() => CustomerService)) private readonly customerService: CustomerService, ) {} @Transactional()