notification

This commit is contained in:
2026-02-23 10:47:23 +03:30
parent 1e7ad2f72e
commit 8303a4c108
14 changed files with 245 additions and 466 deletions
+13 -39
View File
@@ -7,7 +7,6 @@ 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';
import { PermissionEnum } from 'src/common/enums/permission.enum';
@Injectable()
@@ -19,7 +18,7 @@ export class AdminService {
) { }
async create(dto: CreateAdminDto) {
const { phone, firstName, lastName, roleId, notificationPrefrences } = dto;
const { phone, firstName, lastName, roleId } = dto;
const normalizedPhone = normalizePhone(phone);
@@ -31,39 +30,25 @@ export class AdminService {
throw new BadRequestException('This phone number is already used');
}
// retrieve deleted admin
if (currentAdmin && currentAdmin.deletedAt) {
currentAdmin.deletedAt = undefined
await this.em.flush()
return currentAdmin
currentAdmin.deletedAt = undefined;
await this.em.flush();
return currentAdmin;
}
const role = await this.roleRepository.findOne({ id: roleId })
const role = await this.roleRepository.findOne({ id: roleId });
if (!role) {
throw new BadRequestException('Role not found');
}
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
})
const admin = this.em.create(Admin, {
phone: normalizedPhone,
firstName,
lastName,
role,
});
await this.em.persistAndFlush(admin);
return admin;
}
async findByPhone(phone: string): Promise<Admin | null> {
@@ -103,17 +88,6 @@ export class AdminService {
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 });