fix: update company
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseInterceptors } from "@nestjs/common";
|
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseInterceptors } from "@nestjs/common";
|
||||||
import { ApiOperation } from "@nestjs/swagger";
|
import { ApiHeader, ApiOperation } from "@nestjs/swagger";
|
||||||
|
|
||||||
import { CompanyListPublicQueryDto, CompanyListQueryDto } from "./DTO/company-list-query.dto";
|
import { CompanyListPublicQueryDto, CompanyListQueryDto } from "./DTO/company-list-query.dto";
|
||||||
import { ChangeCompanyRequestStatusDto, CreateCompanyDto, CreateCompanyRequestDto } from "./DTO/create-company.dto";
|
import { ChangeCompanyRequestStatusDto, CreateCompanyDto, CreateCompanyRequestDto } from "./DTO/create-company.dto";
|
||||||
@@ -27,6 +27,7 @@ export class CompaniesController {
|
|||||||
|
|
||||||
@Patch(":id")
|
@Patch(":id")
|
||||||
@AuthGuards()
|
@AuthGuards()
|
||||||
|
@ApiHeader({ name: "x-business-id" })
|
||||||
@ApiOperation({ summary: "update company (admin)" })
|
@ApiOperation({ summary: "update company (admin)" })
|
||||||
update(@Param() paramDto: ParamDto, @Body() updateCompanyDto: UpdateCompanyDto, @BusinessDec("id") businessId: string) {
|
update(@Param() paramDto: ParamDto, @Body() updateCompanyDto: UpdateCompanyDto, @BusinessDec("id") businessId: string) {
|
||||||
return this.companiesService.updateCompanyById(paramDto.id, updateCompanyDto, businessId);
|
return this.companiesService.updateCompanyById(paramDto.id, updateCompanyDto, businessId);
|
||||||
|
|||||||
@@ -95,21 +95,38 @@ export class UsersService {
|
|||||||
/*******************************/
|
/*******************************/
|
||||||
|
|
||||||
async updateUser(userId: string, updateData: Partial<User>, em: EntityManager) {
|
async updateUser(userId: string, updateData: Partial<User>, em: EntityManager) {
|
||||||
const user = await em.findOne(User, { id: userId, deletedAt: null });
|
const user = await em.findOne(User, { id: userId, deletedAt: null }, { populate: ["business"] });
|
||||||
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||||
|
|
||||||
|
const businessId = user.business.id;
|
||||||
|
|
||||||
if (updateData.phone) {
|
if (updateData.phone) {
|
||||||
const existingUser = await em.findOne(User, { phone: updateData.phone, deletedAt: null, id: { $ne: userId } });
|
const existingUser = await em.findOne(User, {
|
||||||
|
phone: updateData.phone,
|
||||||
|
deletedAt: null,
|
||||||
|
id: { $ne: userId },
|
||||||
|
business: { id: businessId },
|
||||||
|
});
|
||||||
if (existingUser) throw new BadRequestException(UserMessage.PHONE_EXIST);
|
if (existingUser) throw new BadRequestException(UserMessage.PHONE_EXIST);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updateData.email) {
|
if (updateData.email) {
|
||||||
const existingUser = await em.findOne(User, { email: updateData.email, deletedAt: null, id: { $ne: userId } });
|
const existingUser = await em.findOne(User, {
|
||||||
|
email: updateData.email,
|
||||||
|
deletedAt: null,
|
||||||
|
id: { $ne: userId },
|
||||||
|
business: { id: businessId },
|
||||||
|
});
|
||||||
if (existingUser) throw new BadRequestException(UserMessage.EMAIL_EXIST);
|
if (existingUser) throw new BadRequestException(UserMessage.EMAIL_EXIST);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updateData.nationalCode) {
|
if (updateData.nationalCode) {
|
||||||
const existingUser = await em.findOne(User, { nationalCode: updateData.nationalCode, deletedAt: null, id: { $ne: userId } });
|
const existingUser = await em.findOne(User, {
|
||||||
|
nationalCode: updateData.nationalCode,
|
||||||
|
deletedAt: null,
|
||||||
|
id: { $ne: userId },
|
||||||
|
business: { id: businessId },
|
||||||
|
});
|
||||||
if (existingUser) throw new BadRequestException(UserMessage.NATIONAL_CODE_EXIST);
|
if (existingUser) throw new BadRequestException(UserMessage.NATIONAL_CODE_EXIST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user