chore: create customer

This commit is contained in:
Matin
2025-02-18 12:20:08 +03:30
parent cad8e15941
commit 38dc9d8e0f
4 changed files with 146 additions and 5 deletions
@@ -26,6 +26,7 @@ import { CreateAdminDto } from "../DTO/create-admin.dto";
import { CreateLegalUserDto } from "../DTO/create-legal-user.dto";
import { CreateRealUserDto } from "../DTO/create-real-user.dto";
import { CreateRoleDto } from "../DTO/create-role.dto";
import { CreateCustomerDto } from "../DTO/create-user.dto";
import { SearchAdminQueryDto } from "../DTO/search-admins-query.dto";
import { SearchCustomersDto } from "../DTO/search-customers.dto";
import { SearchRolesQueryDto } from "../DTO/search-roles.dto";
@@ -350,6 +351,69 @@ export class UsersService {
/************************************************************ */
async createCustomer(createDto: CreateCustomerDto) {
const { cityId, address, postalCode, economicCode, companyRegisteredName, registrationId, nationalId, number, ...userData } = createDto;
const queryRunner = this.userRepository.manager.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const existUser = await queryRunner.manager.findOneBy(User, { email: createDto.email });
if (existUser) throw new BadRequestException(UserMessage.EMAIL_EXIST);
const role = await queryRunner.manager.findOneBy(Role, { name: RoleEnum.USER });
if (!role) throw new BadRequestException(UserMessage.ROLE_NOT_FOUND);
const hashedPassword = await this.passwordService.hashPassword(createDto.password);
const legalUserData = economicCode
? {
economicCode,
companyRegisteredName,
registrationId,
nationalId,
number,
address: {
address,
city: cityId
? {
id: +cityId,
}
: undefined,
postalCode,
},
}
: undefined;
const user = queryRunner.manager.create(User, {
...userData,
password: hashedPassword,
roles: [role],
legalUser: legalUserData,
});
await queryRunner.manager.save(user);
await this.userSettingsService.createUserSettings(user.id, queryRunner);
await this.walletsService.createUserWallet(user.id, queryRunner);
await queryRunner.commitTransaction();
return {
message: CommonMessage.CREATED,
user,
};
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
}
}
/************************************************************ */
async findOneCustomer(userId: string) {
const customer = await this.userRepository.findOne({
where: {