update
This commit is contained in:
@@ -1,80 +1,16 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { wrap } from '@mikro-orm/core';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
||||
import { AdminRepository } from '../repositories/admin.repository';
|
||||
import { CreateAdminDto } from '../dto/create-admin.dto';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class AdminService {
|
||||
constructor(
|
||||
private readonly adminRepository: AdminRepository,
|
||||
private readonly em: EntityManager,
|
||||
private readonly em: EntityManager,
|
||||
) { }
|
||||
|
||||
async findByPhone(phone: string) {
|
||||
return this.adminRepository.findOne({ phone });
|
||||
}
|
||||
|
||||
// async create(dto:CreateAdminDto){
|
||||
// this.em.create(dto)
|
||||
// }
|
||||
|
||||
async finAdminById(
|
||||
adminId: string,
|
||||
restId: string,
|
||||
) {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId },
|
||||
{},
|
||||
);
|
||||
if (!admin) {
|
||||
throw new NotFoundException('Admin not found');
|
||||
}
|
||||
const adminPlain = wrap(admin).toObject();
|
||||
|
||||
return { ...adminPlain };
|
||||
}
|
||||
|
||||
async findAllByRestaurantId(restId: string) {
|
||||
return this.adminRepository.find({}, { populate: [] });
|
||||
}
|
||||
|
||||
|
||||
|
||||
async update(adminId: string, restId: string, dto: UpdateAdminDto) {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId },
|
||||
{ populate: [] },
|
||||
);
|
||||
|
||||
if (!admin) {
|
||||
throw new NotFoundException('Admin not found');
|
||||
}
|
||||
|
||||
const { ...rest } = dto;
|
||||
|
||||
// Update scalar fields (firstName, lastName, phone)
|
||||
if (rest.firstName !== undefined) {
|
||||
admin.firstName = rest.firstName;
|
||||
}
|
||||
if (rest.lastName !== undefined) {
|
||||
admin.lastName = rest.lastName;
|
||||
}
|
||||
if (rest.phone !== undefined && rest.phone !== admin.phone) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user