admin
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import { BadRequestException, ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Admin } from '../entities/admin.entity';
|
||||
import { Role } from '../../roles/entities/role.entity';
|
||||
import { EntityManager, RequiredEntityData } from '@mikro-orm/postgresql';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
||||
import { normalizePhone } from '../../util/phone.util';
|
||||
import { CreateAdminDto } from '../dto/create-admin.dto';
|
||||
import { AdminRepository } from '../repositories/admin.repository';
|
||||
import { RoleRepository } from 'src/modules/roles/respository/role.repository';
|
||||
import { NotificationPreference } from 'src/modules/notification/entities/notification-preference.entity';
|
||||
|
||||
@Injectable()
|
||||
export class AdminService {
|
||||
@@ -17,7 +18,7 @@ export class AdminService {
|
||||
) { }
|
||||
|
||||
async create(dto: CreateAdminDto) {
|
||||
const { phone, firstName, lastName, roleId } = dto;
|
||||
const { phone, firstName, lastName, roleId, notificationPrefrences } = dto;
|
||||
|
||||
const normalizedPhone = normalizePhone(phone);
|
||||
|
||||
@@ -25,25 +26,34 @@ export class AdminService {
|
||||
phone: normalizedPhone,
|
||||
});
|
||||
if (exist) {
|
||||
throw new NotFoundException('This phone number is already used');
|
||||
throw new BadRequestException('This phone number is already used');
|
||||
}
|
||||
|
||||
const role = await this.roleRepository.findOne({ id: roleId })
|
||||
if (!role) {
|
||||
throw new NotFoundException('Role not found');
|
||||
throw new BadRequestException('Role not found');
|
||||
}
|
||||
|
||||
const createData: RequiredEntityData<Admin> = {
|
||||
phone: normalizedPhone,
|
||||
firstName,
|
||||
lastName,
|
||||
role
|
||||
}
|
||||
const admin = this.adminRepository.create(createData)
|
||||
|
||||
await this.em.persistAndFlush(admin);
|
||||
return this.em.transactional(async em => {
|
||||
|
||||
const admin = em.create(Admin, {
|
||||
phone: normalizedPhone,
|
||||
firstName,
|
||||
lastName,
|
||||
role
|
||||
})
|
||||
|
||||
const notificationPreference = em.create(NotificationPreference, {
|
||||
admin,
|
||||
events: notificationPrefrences,
|
||||
})
|
||||
|
||||
await this.em.persistAndFlush([admin, notificationPreference]);
|
||||
|
||||
return admin
|
||||
})
|
||||
|
||||
return admin;
|
||||
}
|
||||
|
||||
async findByPhone(phone: string): Promise<Admin | null> {
|
||||
@@ -82,6 +92,18 @@ export class AdminService {
|
||||
if (rest.lastName !== undefined) {
|
||||
admin.lastName = rest.lastName;
|
||||
}
|
||||
|
||||
if (rest.notificationPrefrences) {
|
||||
//
|
||||
const nf = await this.em.findOne(NotificationPreference, {
|
||||
admin
|
||||
})
|
||||
if (!nf) {
|
||||
throw new BadRequestException('Notification prefrences not found!')
|
||||
}
|
||||
nf.events = rest.notificationPrefrences
|
||||
await this.em.flush()
|
||||
}
|
||||
if (rest.phone !== undefined && rest.phone !== admin.phone) {
|
||||
const normalizedPhone = normalizePhone(rest.phone);
|
||||
const exists = await this.adminRepository.findOne({ phone: normalizedPhone });
|
||||
|
||||
Reference in New Issue
Block a user