fix: join address to the customer get by admin
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { Column, Entity, ManyToOne } from "typeorm";
|
||||
|
||||
import { User } from "./user.entity";
|
||||
import { BaseEntity } from "../../../common/entities/base.entity";
|
||||
|
||||
@Entity()
|
||||
export class RefreshToken extends BaseEntity {
|
||||
@Column({ type: "text" })
|
||||
token: string;
|
||||
|
||||
@ManyToOne(() => User, (user) => user.refreshTokens, { onDelete: "CASCADE" })
|
||||
user: User;
|
||||
|
||||
@Column({ type: "timestamptz", nullable: true })
|
||||
expiresAt: Date;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { Column, Entity, JoinTable, ManyToMany, OneToMany, OneToOne } from "type
|
||||
|
||||
import { LegalUser } from "./legal-user.entity";
|
||||
import { RealUser } from "./real-user.entity";
|
||||
import { RefreshToken } from "./refresh-token.entity";
|
||||
import { Role } from "./role.entity";
|
||||
import { UserGroup } from "./user-group.entity";
|
||||
import { BaseEntity } from "../../../common/entities/base.entity";
|
||||
@@ -120,6 +121,9 @@ export class User extends BaseEntity {
|
||||
|
||||
@OneToMany(() => UserQuickAccess, (quickAccess) => quickAccess.user)
|
||||
quickAccesses: UserQuickAccess[];
|
||||
|
||||
@OneToMany(() => RefreshToken, (refreshToken) => refreshToken.user, { cascade: true })
|
||||
refreshTokens: RefreshToken[];
|
||||
}
|
||||
|
||||
// @ManyToMany(() => DanakService, (danakService) => danakService.users)
|
||||
|
||||
@@ -2,6 +2,7 @@ import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
||||
import { DataSource, Not } from "typeorm";
|
||||
|
||||
import { CommonMessage, UserMessage } from "../../../common/enums/message.enum";
|
||||
import { Address } from "../../address/entities/address.entity";
|
||||
import { AddressService } from "../../address/providers/address.service";
|
||||
import { UserSettingsService } from "../../settings/providers/user-settings.service";
|
||||
import { PasswordService } from "../../utils/providers/password.service";
|
||||
@@ -83,67 +84,144 @@ export class CustomersService {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
const {
|
||||
registrationName,
|
||||
economicCode,
|
||||
nationalIdentity,
|
||||
registrationCode,
|
||||
postalCode,
|
||||
nationalCode,
|
||||
address,
|
||||
cityId,
|
||||
birthDate,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
password,
|
||||
phone,
|
||||
} = updateDto;
|
||||
|
||||
const legalUserUpdateData = {
|
||||
registrationName,
|
||||
economicCode,
|
||||
nationalIdentity,
|
||||
registrationCode,
|
||||
phone,
|
||||
};
|
||||
|
||||
const userUpdateData = {
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
password,
|
||||
phone,
|
||||
birthDate,
|
||||
};
|
||||
|
||||
// const addressUpdateData = {
|
||||
// postalCode,
|
||||
// address,
|
||||
// cityId,
|
||||
// };
|
||||
|
||||
const user = await queryRunner.manager.findOne(this.userRepository.target, { where: { id: customerId } });
|
||||
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||
|
||||
if (updateDto.email) {
|
||||
if (email) {
|
||||
const existingEmail = await queryRunner.manager.findOne(this.userRepository.target, {
|
||||
where: { email: updateDto.email, id: Not(user.id) },
|
||||
where: { email, id: Not(user.id) },
|
||||
});
|
||||
if (existingEmail) throw new BadRequestException(UserMessage.EMAIL_EXIST);
|
||||
}
|
||||
|
||||
if (updateDto.nationalCode) {
|
||||
if (nationalCode) {
|
||||
const existingNationalCode = await queryRunner.manager.findOne(this.userRepository.target, {
|
||||
where: { nationalCode: updateDto.nationalCode, id: Not(user.id) },
|
||||
where: { nationalCode: nationalCode, id: Not(user.id) },
|
||||
});
|
||||
if (existingNationalCode) throw new BadRequestException(UserMessage.NATIONAL_CODE_EXIST);
|
||||
}
|
||||
|
||||
if (updateDto.phone) {
|
||||
if (phone) {
|
||||
const existingPhone = await queryRunner.manager.findOne(this.userRepository.target, {
|
||||
where: { phone: updateDto.phone, id: Not(user.id) },
|
||||
where: { phone: phone, id: Not(user.id) },
|
||||
});
|
||||
if (existingPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
|
||||
}
|
||||
|
||||
await queryRunner.manager.update(this.userRepository.target, { id: customerId }, updateDto);
|
||||
if (nationalCode) {
|
||||
const existingNationalCode = await queryRunner.manager.findOne(this.realUserRepository.target, {
|
||||
where: { nationalCode: nationalCode, id: Not(user.id) },
|
||||
});
|
||||
if (existingNationalCode) throw new BadRequestException(UserMessage.NATIONAL_CODE_EXIST);
|
||||
}
|
||||
|
||||
const legalUser = await queryRunner.manager.findOne(this.legalUserRepository.target, { where: { user: { id: customerId } } });
|
||||
await queryRunner.manager.update(this.userRepository.target, { id: customerId }, userUpdateData);
|
||||
if (password) {
|
||||
const hashPassword = await this.passwordService.hashPassword(password);
|
||||
await queryRunner.manager.update(this.userRepository.target, { id: customerId }, { password: hashPassword });
|
||||
}
|
||||
|
||||
const legalUser = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { user: { id: customerId } },
|
||||
relations: { address: true },
|
||||
});
|
||||
|
||||
if (!legalUser) throw new BadRequestException(UserMessage.LEGAL_DATA_NOT_FOUND);
|
||||
|
||||
if (updateDto.economicCode) {
|
||||
if (economicCode) {
|
||||
const existingEconomicCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { economicCode: updateDto.economicCode, id: Not(legalUser.id) },
|
||||
where: { economicCode, id: Not(legalUser.id) },
|
||||
});
|
||||
if (existingEconomicCode) throw new BadRequestException(UserMessage.ECONOMIC_CODE_EXIST);
|
||||
}
|
||||
|
||||
if (updateDto.registrationCode) {
|
||||
if (registrationCode) {
|
||||
const existingRegistrationCode = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { registrationCode: updateDto.registrationCode, id: Not(legalUser.id) },
|
||||
where: { registrationCode, id: Not(legalUser.id) },
|
||||
});
|
||||
if (existingRegistrationCode) throw new BadRequestException(UserMessage.REGISTRATION_CODE_EXIST);
|
||||
}
|
||||
|
||||
if (updateDto.nationalIdentity) {
|
||||
if (nationalIdentity) {
|
||||
const existingNationalIdentity = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { nationalIdentity: updateDto.nationalIdentity, id: Not(legalUser.id) },
|
||||
where: { nationalIdentity, id: Not(legalUser.id) },
|
||||
});
|
||||
if (existingNationalIdentity) throw new BadRequestException(UserMessage.NATIONAL_IDENTITY_EXIST);
|
||||
}
|
||||
|
||||
if (updateDto.cityId) {
|
||||
const { city } = await this.addressService.getCity(+updateDto.cityId);
|
||||
await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, { address: { city } });
|
||||
if (registrationName) {
|
||||
const existingRegistrationName = await queryRunner.manager.findOne(this.legalUserRepository.target, {
|
||||
where: { registrationName, id: Not(legalUser.id) },
|
||||
});
|
||||
if (existingRegistrationName) throw new BadRequestException(UserMessage.REGISTRATION_NAME_EXIST);
|
||||
}
|
||||
|
||||
const { address, ...legalUserUpdateData } = updateDto;
|
||||
await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, legalUserUpdateData);
|
||||
|
||||
if (address) {
|
||||
await queryRunner.manager.update(this.legalUserRepository.target, { id: legalUser.id }, { address: { fullAddress: address } });
|
||||
// Update address if needed
|
||||
if (cityId || address || postalCode) {
|
||||
// Get the address entity
|
||||
const addressEntity = legalUser.address;
|
||||
|
||||
if (!addressEntity) throw new BadRequestException(UserMessage.ADDRESS_NOT_FOUND);
|
||||
|
||||
// Prepare address updates
|
||||
const addressUpdates: any = {};
|
||||
|
||||
if (cityId) {
|
||||
const { city } = await this.addressService.getCity(+cityId);
|
||||
addressUpdates.city = city;
|
||||
}
|
||||
|
||||
if (address) {
|
||||
addressUpdates.fullAddress = address;
|
||||
}
|
||||
|
||||
if (postalCode) {
|
||||
addressUpdates.postalCode = postalCode;
|
||||
}
|
||||
|
||||
await queryRunner.manager.update(Address, { id: addressEntity.id }, addressUpdates);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
@@ -306,7 +384,7 @@ export class CustomersService {
|
||||
async getCustomerById(userId: string) {
|
||||
const customer = await this.userRepository.findOne({
|
||||
where: { id: userId, roles: { name: RoleEnum.USER } },
|
||||
relations: { legalUser: true, realUser: true },
|
||||
relations: { legalUser: { address: { city: { province: true } } }, realUser: { address: { city: { province: true } } } },
|
||||
});
|
||||
|
||||
return { customer };
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
|
||||
import { RefreshToken } from "../entities/refresh-token.entity";
|
||||
|
||||
@Injectable()
|
||||
export class RefreshTokensRepository extends Repository<RefreshToken> {
|
||||
constructor(@InjectRepository(RefreshToken) refreshTokenRepository: Repository<RefreshToken>) {
|
||||
super(refreshTokenRepository.target, refreshTokenRepository.manager, refreshTokenRepository.queryRunner);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import { CreateRoleDto } from "./DTO/create-role.dto";
|
||||
import { SearchAdminQueryDto } from "./DTO/search-admins-query.dto";
|
||||
import { SearchCustomersDto } from "./DTO/search-customers.dto";
|
||||
import { SearchRolesQueryDto } from "./DTO/search-roles.dto";
|
||||
import { UpdateCustomerDto } from "./DTO/update-customer.dto";
|
||||
import { UpdateProfileDto } from "./DTO/update-profile.dto";
|
||||
import { CreateUserGroupDto } from "./DTO/user-group.dto";
|
||||
import { EmailVerifyQueryDto } from "./DTO/verify-email-query.dto";
|
||||
@@ -144,11 +145,18 @@ export class UsersController {
|
||||
@ApiOperation({ summary: "Create customer" })
|
||||
@AuthGuards()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@Post("customer")
|
||||
@Post("customers")
|
||||
createCustomer(@Body() createDto: CreateCustomerDto) {
|
||||
return this.customersService.createCustomer(createDto);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "Update customer" })
|
||||
@AuthGuards()
|
||||
@Patch("customers/:id")
|
||||
updateCustomer(@Param() paramDto: ParamDto, @Body() updateDto: UpdateCustomerDto) {
|
||||
return this.customersService.updateCustomer(paramDto.id, updateDto);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "get customers ==> admin route" })
|
||||
@AuthGuards()
|
||||
@PermissionsDec(PermissionEnum.ADMINS, PermissionEnum.CUSTOMERS)
|
||||
|
||||
@@ -3,6 +3,7 @@ import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
|
||||
import { LegalUser } from "./entities/legal-user.entity";
|
||||
import { Permission } from "./entities/permission.entity";
|
||||
import { RefreshToken } from "./entities/refresh-token.entity";
|
||||
import { Role } from "./entities/role.entity";
|
||||
import { UserGroup } from "./entities/user-group.entity";
|
||||
import { User } from "./entities/user.entity";
|
||||
@@ -24,10 +25,11 @@ import { UsersController } from "./users.controller";
|
||||
import { UtilsModule } from "../utils/utils.module";
|
||||
import { AdminsService } from "./providers/admins.service";
|
||||
import { CustomersService } from "./providers/customers.service";
|
||||
import { RefreshTokensRepository } from "./repositories/refresh-token.repository";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([User, Role, UserGroup, UserSetting, RealUser, LegalUser, Permission]),
|
||||
TypeOrmModule.forFeature([User, Role, UserGroup, UserSetting, RealUser, LegalUser, Permission, RefreshToken]),
|
||||
WalletsModule,
|
||||
UtilsModule,
|
||||
AddressModule,
|
||||
@@ -37,6 +39,7 @@ import { CustomersService } from "./providers/customers.service";
|
||||
AdminsService,
|
||||
CustomersService,
|
||||
UserRepository,
|
||||
RefreshTokensRepository,
|
||||
RoleRepository,
|
||||
UserGroupRepository,
|
||||
UserSettingsRepository,
|
||||
@@ -47,6 +50,6 @@ import { CustomersService } from "./providers/customers.service";
|
||||
AddressService,
|
||||
],
|
||||
controllers: [UsersController],
|
||||
exports: [UsersService, UserRepository, CustomersService, AdminsService],
|
||||
exports: [UsersService, UserRepository, CustomersService, AdminsService, RefreshTokensRepository],
|
||||
})
|
||||
export class UsersModule {}
|
||||
|
||||
Reference in New Issue
Block a user