fix: register fcm token on enabling push notification

This commit is contained in:
Abdalhamid Alhamad
2024-12-24 13:00:54 +03:00
parent 3719498c2f
commit bb8cc33d53
5 changed files with 31 additions and 13 deletions

View File

@ -1,7 +1,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Device } from '../entities'; import { Device } from '../entities';
import { DeviceRepository } from '../repositories'; import { DeviceRepository } from '../repositories';
@Injectable() @Injectable()
export class DeviceService { export class DeviceService {
constructor(private readonly deviceRepository: DeviceRepository) {} constructor(private readonly deviceRepository: DeviceRepository) {}

View File

@ -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 { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { IJwtPayload } from '~/auth/interfaces'; import { IJwtPayload } from '~/auth/interfaces';
import { DEVICE_ID_HEADER } from '~/common/constants';
import { AuthenticatedUser } from '~/common/decorators'; import { AuthenticatedUser } from '~/common/decorators';
import { AccessTokenGuard } from '~/common/guards'; import { AccessTokenGuard } from '~/common/guards';
import { ApiDataResponse } from '~/core/decorators'; import { ApiDataResponse } from '~/core/decorators';
@ -39,8 +40,9 @@ export class CustomerController {
async updateNotificationSettings( async updateNotificationSettings(
@AuthenticatedUser() { sub }: IJwtPayload, @AuthenticatedUser() { sub }: IJwtPayload,
@Body() body: UpdateNotificationsSettingsRequestDto, @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)); return ResponseFactory.data(new NotificationSettingsResponseDto(notificationSettings));
} }

View File

@ -1,19 +1,24 @@
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsBoolean, IsOptional } from 'class-validator'; import { IsBoolean, IsOptional, IsString, ValidateIf } from 'class-validator';
import { i18nValidationMessage as i18n } from 'nestjs-i18n';
export class UpdateNotificationsSettingsRequestDto { export class UpdateNotificationsSettingsRequestDto {
@ApiProperty() @ApiProperty()
@IsBoolean() @IsBoolean({ message: i18n('validation.isBoolean', { path: 'general', property: 'customer.isEmailEnabled' }) })
@IsOptional() @IsOptional()
isEmailEnabled!: boolean; isEmailEnabled!: boolean;
@ApiProperty() @ApiProperty()
@IsBoolean() @IsBoolean({ message: i18n('validation.isBoolean', { path: 'general', property: 'customer.isPushEnabled' }) })
@IsOptional() @IsOptional()
isPushEnabled!: boolean; isPushEnabled!: boolean;
@ApiProperty() @ApiProperty()
@IsBoolean() @IsBoolean({ message: i18n('validation.isBoolean', { path: 'general', property: 'customer.isSmsEnabled' }) })
@IsOptional() @IsOptional()
isSmsEnabled!: boolean; isSmsEnabled!: boolean;
@ApiPropertyOptional()
@IsString({ message: i18n('validation.isString', { path: 'general', property: 'customer.fcmToken' }) })
@ValidateIf((o) => o.isPushEnabled)
fcmToken?: string;
} }

View File

@ -1,5 +1,6 @@
import { BadRequestException, Injectable } from '@nestjs/common'; import { BadRequestException, forwardRef, Inject, Injectable } from '@nestjs/common';
import { User } from '~/auth/entities'; import { User } from '~/auth/entities';
import { DeviceService } from '~/auth/services';
import { OciService } from '~/document/services'; import { OciService } from '~/document/services';
import { UpdateCustomerRequestDto, UpdateNotificationsSettingsRequestDto } from '../dtos/request'; import { UpdateCustomerRequestDto, UpdateNotificationsSettingsRequestDto } from '../dtos/request';
import { Customer } from '../entities'; import { Customer } from '../entities';
@ -7,13 +8,24 @@ import { CustomerRepository } from '../repositories/customer.repository';
@Injectable() @Injectable()
export class CustomerService { export class CustomerService {
constructor(private readonly customerRepository: CustomerRepository, private readonly ociService: OciService) {} constructor(
async updateNotificationSettings(userId: string, data: UpdateNotificationsSettingsRequestDto) { 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 customer = await this.findCustomerById(userId);
const notificationSettings = (await this.customerRepository.updateNotificationSettings(customer, data)) const notificationSettings = (await this.customerRepository.updateNotificationSettings(customer, data))
.notificationSettings; .notificationSettings;
if (data.isPushEnabled && deviceId) {
await this.deviceService.updateDevice(deviceId, {
fcmToken: data.fcmToken,
userId: userId,
});
}
return notificationSettings; return notificationSettings;
} }

View File

@ -14,9 +14,9 @@ import { JuniorTokenService } from './junior-token.service';
export class JuniorService { export class JuniorService {
constructor( constructor(
private readonly juniorRepository: JuniorRepository, private readonly juniorRepository: JuniorRepository,
private readonly customerService: CustomerService,
private readonly juniorTokenService: JuniorTokenService, private readonly juniorTokenService: JuniorTokenService,
@Inject(forwardRef(() => UserService)) private readonly userService: UserService, @Inject(forwardRef(() => UserService)) private readonly userService: UserService,
@Inject(forwardRef(() => CustomerService)) private readonly customerService: CustomerService,
) {} ) {}
@Transactional() @Transactional()