notifs
This commit is contained in:
@@ -1,26 +1,18 @@
|
||||
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Admin } from '../entities/admin.entity';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { wrap } from '@mikro-orm/core';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { CreateMyRestaurantAdminDto } from '../dto/create-my-restaurant-admin.dto';
|
||||
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
||||
import { AdminRole } from '../entities/adminRole.entity';
|
||||
import { normalizePhone } from '../../utils/phone.util';
|
||||
import { AdminRepository } from '../repositories/admin.repository';
|
||||
import { AdminRoleRepository } from '../repositories/admin-role.repository';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class AdminService {
|
||||
constructor(
|
||||
private readonly adminRepository: AdminRepository,
|
||||
private readonly adminRoleRepository: AdminRoleRepository,
|
||||
private readonly em: EntityManager,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async findByPhone(phone: string) {
|
||||
const normalizedPhone = normalizePhone(phone);
|
||||
return this.adminRepository.findOne({ phone: normalizedPhone });
|
||||
return this.adminRepository.findOne({ phone });
|
||||
}
|
||||
|
||||
async finAdminById(
|
||||
@@ -28,106 +20,27 @@ export class AdminService {
|
||||
restId: string,
|
||||
) {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId, roles: { restaurant: { id: restId } } },
|
||||
{ populate: ['roles'], exclude: ['roles'] },
|
||||
{ id: adminId },
|
||||
{},
|
||||
);
|
||||
if (!admin) {
|
||||
throw new NotFoundException('Admin not found');
|
||||
}
|
||||
const adminPlain = wrap(admin).toObject();
|
||||
const adminRole = await this.adminRoleRepository.findOne({ admin: admin, restaurant: { id: restId } });
|
||||
|
||||
|
||||
return { ...adminPlain };
|
||||
}
|
||||
|
||||
async findAllByRestaurantId(restId: string) {
|
||||
return this.adminRepository.find({ roles: { restaurant: { id: restId } } }, { populate: [] });
|
||||
return this.adminRepository.find({}, { populate: [] });
|
||||
}
|
||||
|
||||
async createAdminForMyRestaurant(restId: string, dto: CreateMyRestaurantAdminDto) {
|
||||
const { phone, firstName, lastName, roleId } = dto;
|
||||
const normalizedPhone = normalizePhone(phone);
|
||||
let admin: Admin | null = null;
|
||||
admin = await this.adminRepository.findOne({
|
||||
phone: normalizedPhone,
|
||||
});
|
||||
if (!admin) {
|
||||
admin = this.adminRepository.create({
|
||||
phone: normalizedPhone,
|
||||
firstName,
|
||||
lastName,
|
||||
});
|
||||
await this.em.persistAndFlush(admin);
|
||||
}
|
||||
|
||||
const restaurant = await this.em.findOne(Restaurant, { id: restId });
|
||||
if (!restaurant) throw new NotFoundException('Restaurant not found');
|
||||
|
||||
let adminRole = await this.adminRoleRepository.findOne({
|
||||
admin: admin,
|
||||
restaurant: restaurant,
|
||||
});
|
||||
|
||||
if (!adminRole) {
|
||||
adminRole = this.adminRoleRepository.create({
|
||||
admin: admin,
|
||||
restaurant,
|
||||
});
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
await this.em.persistAndFlush(adminRole);
|
||||
return admin;
|
||||
}
|
||||
|
||||
async createAdminForRestaurantBySuperAdmin(restId: string, dto: CreateMyRestaurantAdminDto) {
|
||||
const { phone, firstName, lastName, roleId } = dto;
|
||||
const normalizedPhone = normalizePhone(phone);
|
||||
let admin: Admin | null = null;
|
||||
admin = await this.adminRepository.findOne({
|
||||
phone: normalizedPhone,
|
||||
});
|
||||
if (!admin) {
|
||||
admin = this.adminRepository.create({
|
||||
phone: normalizedPhone,
|
||||
firstName,
|
||||
lastName,
|
||||
});
|
||||
await this.em.persistAndFlush(admin);
|
||||
}
|
||||
|
||||
const restaurant = await this.em.findOne(Restaurant, { id: restId });
|
||||
if (!restaurant) throw new NotFoundException('Restaurant not found');
|
||||
|
||||
let adminRole = await this.adminRoleRepository.findOne({
|
||||
admin: admin,
|
||||
restaurant: restaurant,
|
||||
});
|
||||
|
||||
if (!adminRole) {
|
||||
adminRole = this.adminRoleRepository.create({
|
||||
admin: admin,
|
||||
restaurant,
|
||||
});
|
||||
} else {
|
||||
}
|
||||
|
||||
await this.em.persistAndFlush(adminRole);
|
||||
return admin;
|
||||
}
|
||||
|
||||
async getAdminsForRestaurantBySuperAdmin(restId: string) {
|
||||
const restaurant = await this.em.findOne(Restaurant, { id: restId });
|
||||
if (!restaurant) throw new NotFoundException('Restaurant not found');
|
||||
|
||||
return this.adminRepository.find({ roles: { restaurant: restaurant } }, { populate: [ ] });
|
||||
}
|
||||
|
||||
async update(adminId: string, restId: string, dto: UpdateAdminDto) {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId, roles: { restaurant: { id: restId } } },
|
||||
{ populate: [ ] },
|
||||
{ id: adminId },
|
||||
{ populate: [] },
|
||||
);
|
||||
|
||||
if (!admin) {
|
||||
@@ -144,26 +57,19 @@ export class AdminService {
|
||||
admin.lastName = rest.lastName;
|
||||
}
|
||||
if (rest.phone !== undefined && rest.phone !== admin.phone) {
|
||||
const normalizedPhone = normalizePhone(rest.phone);
|
||||
const exists = await this.adminRepository.findOne({ phone: normalizedPhone });
|
||||
if (exists) {
|
||||
throw new ConflictException('This Phone Number is already taken');
|
||||
}
|
||||
admin.phone = normalizedPhone;
|
||||
// const exists = await this.adminRepository.findOne({ phone: normalizedPhone });
|
||||
// if (exists) {
|
||||
// throw new ConflictException('This Phone Number is already taken');
|
||||
// }
|
||||
// admin.phone = normalizedPhone;
|
||||
}
|
||||
|
||||
// Update role if roleId is provided
|
||||
|
||||
|
||||
|
||||
await this.em.persistAndFlush(admin);
|
||||
return admin;
|
||||
}
|
||||
|
||||
async remove(adminId: string, restId: string): Promise<void> {
|
||||
const adminRole = await this.adminRoleRepository.findOne({ admin: { id: adminId }, restaurant: { id: restId } });
|
||||
if (!adminRole) {
|
||||
throw new NotFoundException('Admin role not found');
|
||||
}
|
||||
return this.em.removeAndFlush(adminRole);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user