Files
zod-backend/src/customer/services/customer.service.ts
2025-01-13 16:42:27 +03:00

88 lines
3.5 KiB
TypeScript

import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import { DocumentService, OciService } from '~/document/services';
import { CreateJuniorRequestDto } from '~/junior/dtos/request';
import { DeviceService } from '~/user/services';
import { UpdateCustomerRequestDto, UpdateNotificationsSettingsRequestDto } from '../dtos/request';
import { Customer } from '../entities';
import { CustomerRepository } from '../repositories/customer.repository';
@Injectable()
export class CustomerService {
private readonly logger = new Logger(CustomerService.name);
constructor(
private readonly customerRepository: CustomerRepository,
private readonly ociService: OciService,
private readonly deviceService: DeviceService,
private readonly documentService: DocumentService,
) {}
async updateNotificationSettings(userId: string, data: UpdateNotificationsSettingsRequestDto, deviceId: string) {
this.logger.log(`Updating notification settings for user ${userId}`);
const customer = await this.findCustomerById(userId);
const notificationSettings = (await this.customerRepository.updateNotificationSettings(customer, data))
.notificationSettings;
if (data.isPushEnabled && deviceId) {
this.logger.log(`Updating device ${deviceId} with fcmToken`);
await this.deviceService.updateDevice(deviceId, {
fcmToken: data.fcmToken,
userId: userId,
});
}
this.logger.log(`Notification settings updated for user ${userId}`);
return notificationSettings;
}
async updateCustomer(userId: string, data: UpdateCustomerRequestDto): Promise<Customer> {
this.logger.log(`Updating customer ${userId}`);
await this.validateProfilePictureForCustomer(userId, data.profilePictureId);
await this.customerRepository.updateCustomer(userId, data);
this.logger.log(`Customer ${userId} updated successfully`);
return this.findCustomerById(userId);
}
createJuniorCustomer(guardianId: string, juniorId: string, body: CreateJuniorRequestDto) {
this.logger.log(`Creating customer for user ${juniorId}`);
return this.customerRepository.createJuniorCustomer(guardianId, juniorId, body);
}
createGuardianCustomer(userId: string) {
this.logger.log(`Creating guardian customer for user ${userId}`);
return this.customerRepository.createGuardianCustomer(userId);
}
async findCustomerById(id: string) {
this.logger.log(`Finding customer ${id}`);
const customer = await this.customerRepository.findOne({ id });
if (!customer) {
this.logger.error(`Customer ${id} not found`);
throw new BadRequestException('CUSTOMER.NOT_FOUND');
}
if (customer.profilePicture) {
this.logger.log(`Generating pre-signed url for profile picture of customer ${id}`);
customer.profilePicture.url = await this.ociService.generatePreSignedUrl(customer.profilePicture);
}
this.logger.log(`Customer ${id} found successfully`);
return customer;
}
private async validateProfilePictureForCustomer(userId: string, profilePictureId?: string) {
if (!profilePictureId) return;
this.logger.log(`Validating profile picture ${profilePictureId}`);
const profilePicture = await this.documentService.findDocumentById(profilePictureId);
if (!profilePicture) {
this.logger.error(`Profile picture ${profilePictureId} not found`);
throw new BadRequestException('DOCUMENT.NOT_FOUND');
}
if (profilePicture.createdById && profilePicture.createdById !== userId) {
}
}
}