chore: change the real user abd legal user data

This commit is contained in:
mahyargdz
2025-03-02 17:59:02 +03:30
parent c559fdb9e8
commit 0da12b89eb
13 changed files with 203 additions and 187 deletions
+22
View File
@@ -85,6 +85,14 @@ export const enum UserMessage {
CITY_REQUIRED = "شهر نمی‌تواند خالی باشد",
CITY_NOT_FOUND = "شهر یافت نشد",
CITY_SHOULD_BE_UUID = "شناسه شهر باید یک UUID باشد",
REAL_DATA_EXIST = "اطلاعات حقیقی قبلا ثبت شده است",
LEGAL_DATA_EXIST = "اطلاعات حقوقی قبلا ثبت شده است",
ECONOMIC_CODE_EXIST = "کد اقتصادی قبلا ثبت شده است",
REGISTRATION_CODE_EXIST = "کد ثبت قبلا ثبت شده است",
NATIONAL_IDENTITY_EXIST = "شناسه ملی قبلا ثبت شده است",
PHONE_SHOULD_BE_11_DIGIT = "شماره تلفن باید ۱۱ رقم باشد",
FINANCIAL_TYPE_IS_LEGAL = "نوع کاربر حقوقی می‌باشد",
FINANCIAL_TYPE_IS_REAL = "نوع کاربر حقیقی می‌باشد",
}
export const enum CommonMessage {
@@ -485,6 +493,20 @@ export const enum FinancialMessage {
BIRTH_DATE_REQUIRED = "تاریخ تولد مورد نیاز است",
ADDRESS_STRING = "آدرس باید یک رشته باشد",
POSTAL_CODE_NUMBER_STRING = "کد پستی باید یک رشته عددی باشد",
ECONOMIC_CODE_REQUIRED = "کد اقتصادی مورد نیاز است",
ECONOMIC_CODE_INVALID = "کد اقتصادی نامعتبر است",
REGISTRATION_CODE_REQUIRED = "کد ثبت مورد نیاز است",
REGISTRATION_ID_INVALID = "شناسه ثبت نامعتبر است",
REGISTRATION_NAME_REQUIRED = "نام ثبت شده مورد نیاز است",
REGISTRATION_NAME_INVALID = "نام ثبت شده نامعتبر است",
NATIONAL_ID_REQUIRED = "شناسه ملی مورد نیاز است",
NATIONAL_ID_INVALID = "شناسه ملی نامعتبر است",
ECONOMIC_CODE_STRING = "کد اقتصادی باید یک رشته باشد",
REGISTRATION_ID_STRING = "شناسه ثبت باید یک رشته باشد",
REGISTRATION_NAME_STRING = "نام ثبت شده باید یک رشته باشد",
NATIONAL_ID_STRING = "شناسه ملی باید یک رشته باشد",
ECONOMIC_CODE_NUMBER_STRING = "کد اقتصادی باید یک رشته عددی باشد",
REGISTRATION_ID_NUMBER_STRING = "شناسه ثبت باید یک رشته عددی باشد",
}
export const enum AdminMessage {
@@ -12,5 +12,5 @@ export class Address extends BaseEntity {
postalCode: string;
@Column({ type: "text" })
address: string;
fullAddress: string;
}
+1 -1
View File
@@ -10,6 +10,6 @@ export class City {
@Column({ type: "varchar", length: 255 })
name: string;
@ManyToOne(() => Province, (province) => province.cities, { nullable: false })
@ManyToOne(() => Province, (province) => province.cities, { eager: true, nullable: false })
province: Province;
}
@@ -12,6 +12,7 @@ export class AddressService {
private readonly cityRepository: CityRepository,
private readonly provinceRepository: ProvinceRepository,
) {}
//*********************************** */
async getAllCities(queryDto: SearchCitiesDto) {
const { limit, skip } = PaginationUtils(queryDto);
@@ -32,6 +33,7 @@ export class AddressService {
paginate: true,
};
}
//*********************************** */
async getAllProvinces(queryDto: SearchCitiesDto) {
const { limit, skip } = PaginationUtils(queryDto);
@@ -52,6 +54,7 @@ export class AddressService {
paginate: true,
};
}
//*********************************** */
async getProvince(id: number) {
const province = await this.provinceRepository.findOneBy({
@@ -61,25 +64,25 @@ export class AddressService {
return { province };
}
//*********************************** */
async getCity(id: number) {
const city = await this.cityRepository.findOneBy({
id,
});
const city = await this.cityRepository.findOneBy({ id });
if (!city) throw new BadRequestException(CityMessage.NOT_FOUND);
return { city };
}
//*********************************** */
async getCitiesByProvince(queryDto: SearchCitiesDto, id: number) {
const { limit, skip } = PaginationUtils(queryDto);
const province = await this.provinceRepository.findOneBy({
id,
});
const province = await this.provinceRepository.findOneBy({ id });
if (!province) throw new BadRequestException(CityMessage.NOT_FOUND);
const queryBuilder = this.cityRepository.createQueryBuilder("city").where("city.provinceId = :provinceId", { provinceId: province.id });
//
if (queryDto.q) {
queryBuilder.andWhere("city.name ILIKE :q", { q: `%${queryDto.q}%` });
}
@@ -94,15 +97,9 @@ export class AddressService {
pagination: true,
};
}
//*********************************** */
async getProvinceByCity(id: number) {
const province = await this.provinceRepository.findOne({
where: {
cities: {
id,
},
},
});
const province = await this.provinceRepository.findOne({ where: { cities: { id } } });
if (!province) throw new BadRequestException(ProvinceMessage.NOT_FOUND);
return { province };
@@ -477,6 +477,7 @@ export class InvoicesService {
const invoiceCount = await this.invoiceRepository.count({
where: {
user: { id: userId },
status: InvoiceStatus.WAIT_PAYMENT,
},
});
return invoiceCount;
+23 -41
View File
@@ -1,46 +1,28 @@
// import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
// import { IsNotEmpty, IsOptional, IsString } from "class-validator";
import { ApiProperty, PickType } from "@nestjs/swagger";
import { IsNotEmpty, IsNumberString, IsString } from "class-validator";
// import { FinancialMessage } from "../../../common/enums/message.enum";
import { CreateRealUserDto } from "./create-real-user.dto";
import { FinancialMessage } from "../../../common/enums/message.enum";
// export class CreateLegalUserDto {
// @ApiProperty({ description: "Economic code" })
// @IsOptional()
// @IsString({ message: FinancialMessage.ECONOMIC_CODE_INVALID })
// economicCode?: string;
//TODO:fix the validation
export class CreateLegalUserDto extends PickType(CreateRealUserDto, ["phone", "address", "postalCode", "cityId"] as const) {
@IsNotEmpty({ message: FinancialMessage.ECONOMIC_CODE_REQUIRED })
@IsNumberString({ no_symbols: true }, { message: FinancialMessage.ECONOMIC_CODE_NUMBER_STRING })
@ApiProperty({ description: "Economic code" })
economicCode: string;
// @ApiProperty({ description: "Company registered name" })
// @IsOptional()
// @IsString({ message: FinancialMessage.ECONOMIC_CODE_INVALID })
// companyRegisteredName?: string;
@IsNotEmpty({ message: FinancialMessage.REGISTRATION_CODE_REQUIRED })
@IsNumberString({ no_symbols: true }, { message: FinancialMessage.REGISTRATION_ID_NUMBER_STRING })
@ApiProperty({ description: "Registration code" })
registrationCode: string;
// @ApiProperty({ description: "Registration ID" })
// @IsOptional()
// @IsString({ message: FinancialMessage.REGISTRATION_ID_INVALID })
// registrationId?: string;
@IsNotEmpty({ message: FinancialMessage.REGISTRATION_NAME_REQUIRED })
@IsString({ message: FinancialMessage.REGISTRATION_NAME_STRING })
@ApiProperty({ description: "Registration name" })
registrationName: string;
// @ApiProperty({ description: "national Id" })
// @IsOptional()
// @IsString({ message: FinancialMessage.NATIONAL_ID_INVALID })
// nationalId?: string;
// @ApiPropertyOptional({ description: "number" })
// @IsOptional()
// @IsString({ message: FinancialMessage.FIXED_PHONE_INVALID })
// number?: string;
// @ApiPropertyOptional({ description: "postalCode" })
// @IsOptional()
// @IsString({ message: FinancialMessage.NATIONAL_ID_INVALID })
// postalCode?: string;
// @ApiPropertyOptional({ description: "address" })
// @IsOptional()
// @IsString({ message: FinancialMessage.NATIONAL_ID_INVALID })
// address?: string;
// @ApiPropertyOptional({ description: "City id", example: "1" })
// @IsOptional()
// @IsNotEmpty({ message: FinancialMessage.CITY_ID_REQUIRED })
// cityId: string;
// }
@IsNotEmpty({ message: FinancialMessage.NATIONAL_ID_REQUIRED })
@IsString({ message: FinancialMessage.NATIONAL_ID_STRING })
@ApiProperty({ description: "National identity" })
nationalIdentity: string;
}
@@ -1,11 +1,12 @@
import { ApiProperty } from "@nestjs/swagger";
import { ApiProperty, PickType } from "@nestjs/swagger";
import { IsEnum, IsInt, IsNotEmpty, IsNumberString, IsString } from "class-validator";
import { FinancialMessage } from "../../../common/enums/message.enum";
import { CompleteRegistrationDto } from "../../auth/DTO/complete-register.dto";
import { GenderEnum } from "../enums/gender-type.enum";
import { NationalityEnum } from "../enums/nationality.enum";
export class CreateRealUserDto {
export class CreateRealUserDto extends PickType(CompleteRegistrationDto, ["firstName", "lastName", "birthDate", "nationalCode", "phone"]) {
@IsNotEmpty({ message: FinancialMessage.GENDER_TYPE_REQUIRED })
@IsEnum(GenderEnum)
@ApiProperty({ enum: GenderEnum, description: "Gender type", example: GenderEnum.MALE })
@@ -19,8 +20,9 @@ export class CreateRealUserDto {
@IsNotEmpty({ message: FinancialMessage.NATIONALITY_REQUIRED })
@IsEnum(NationalityEnum)
@ApiProperty({ description: "Nationality" })
nationality: string;
nationality: NationalityEnum;
//for address entity
@IsNotEmpty({ message: FinancialMessage.ADDRESS_REQUIRED })
@IsString({ message: FinancialMessage.ADDRESS_STRING })
@ApiProperty({ description: "Address" })
+10 -11
View File
@@ -1,26 +1,25 @@
import { Column, Entity, JoinColumn, OneToOne, Unique } from "typeorm";
import { Column, Entity, JoinColumn, OneToOne } from "typeorm";
import { User } from "./user.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { Address } from "../../address/entities/address.entity";
@Unique(["user"])
@Entity()
export class LegalUser extends BaseEntity {
@Column({ type: "varchar", length: 50, nullable: true })
@Column({ type: "varchar", length: 50, unique: true })
economicCode: string;
@Column({ type: "varchar", length: 50, nullable: true })
registrationId: string;
@Column({ type: "varchar", length: 50, unique: true })
registrationCode: string;
@Column({ type: "varchar", length: 100, nullable: true })
companyRegisteredName: string;
@Column({ type: "varchar", length: 100, unique: true })
registrationName: string;
@Column({ type: "varchar", length: 20, nullable: true })
nationalId: string;
@Column({ type: "varchar", length: 20, unique: true })
nationalIdentity: string;
@Column({ type: "varchar", length: 20, nullable: true })
number: string;
@Column({ type: "varchar", length: 20, unique: true })
phone: string;
@OneToOne(() => Address, { nullable: false, cascade: true })
@JoinColumn()
@@ -1,4 +1,4 @@
import { Column, Entity, JoinColumn, OneToOne, Unique } from "typeorm";
import { Column, Entity, JoinColumn, OneToOne } from "typeorm";
import { User } from "./user.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
@@ -6,7 +6,6 @@ import { Address } from "../../address/entities/address.entity";
import { GenderEnum } from "../enums/gender-type.enum";
import { NationalityEnum } from "../enums/nationality.enum";
@Unique(["user"])
@Entity()
export class RealUser extends BaseEntity {
@Column({ type: "varchar", length: 150 })
@@ -1,27 +0,0 @@
import { Column, Entity, ManyToOne, Unique } from "typeorm";
import { User } from "./user.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { FinancialType } from "../enums/financial-type.enum";
@Unique(["user", "type"])
@Entity()
export class UserFinancial extends BaseEntity {
@Column({ type: "enum", enum: FinancialType, default: FinancialType.REAL })
type: FinancialType;
@Column({ nullable: true })
economicCode: string;
@Column({ nullable: true })
registrationId: string;
@Column({ nullable: true })
nationalId: string;
@Column({ nullable: true })
number: string;
@ManyToOne(() => User)
user: User;
}
@@ -21,6 +21,7 @@ import { UserSubscription } from "../../subscriptions/entities/user-subscription
import { TicketMessage } from "../../tickets/entities/ticket-message.entity";
import { Ticket } from "../../tickets/entities/ticket.entity";
import { Wallet } from "../../wallets/entities/wallet.entity";
import { FinancialType } from "../enums/financial-type.enum";
@Entity()
export class User extends BaseEntity {
@@ -55,6 +56,9 @@ export class User extends BaseEntity {
@Column({ type: "boolean", default: false })
emailVerified: boolean;
@Column({ type: "enum", enum: FinancialType, nullable: true })
financialType: FinancialType | null;
//-----------------------------------------
@ManyToMany(() => Role, (role) => role.users)
+98 -67
View File
@@ -22,6 +22,8 @@ import { WalletsService } from "../../wallets/providers/wallets.service";
import { ChangeEmailDto } from "../DTO/change-email.dto";
import { CheckValidityDTO } from "../DTO/check-validity.dto";
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 { SearchAdminQueryDto } from "../DTO/search-admins-query.dto";
import { SearchRolesQueryDto } from "../DTO/search-roles.dto";
@@ -30,9 +32,12 @@ import { CreateUserGroupDto } from "../DTO/user-group.dto";
import { EmailVerifyQueryDto } from "../DTO/verify-email-query.dto";
import { Role } from "../entities/role.entity";
import { User } from "../entities/user.entity";
import { FinancialType } from "../enums/financial-type.enum";
import { RoleEnum } from "../enums/role.enum";
import { ValidityType } from "../enums/validity-type.enum";
import { LegalUserRepository } from "../repositories/legal-user.repository";
import { PermissionsRepository } from "../repositories/permissions.repository";
import { RealUserRepository } from "../repositories/real-user.repository";
import { RoleRepository } from "../repositories/roles.repository";
import { UserGroupRepository } from "../repositories/user-group.repository";
import { UserRepository } from "../repositories/users.repository";
@@ -49,6 +54,8 @@ export class UsersService {
private readonly permissionsRepository: PermissionsRepository,
private readonly passwordService: PasswordService,
private readonly addressService: AddressService,
private readonly realUserRepository: RealUserRepository,
private readonly legalUserRepository: LegalUserRepository,
private readonly emailService: EmailService,
private readonly configService: ConfigService,
private readonly otpService: OTPService,
@@ -81,20 +88,22 @@ export class UsersService {
/************************************************************ */
async getMyFinancialInfo(userId: string) {
const user = await this.userRepository
.createQueryBuilder("user")
.leftJoinAndSelect("user.address", "address")
.leftJoinAndSelect("address.city", "city")
.leftJoinAndSelect("city.province", "province")
.leftJoinAndSelect("user.realUser", "realUser")
.leftJoinAndSelect("user.legalUser", "legalUser")
.where("user.id = :id", { id: userId })
.getOne();
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return { user };
async getRealUserData(userId: string) {
const realUser = await this.realUserRepository.findOne({
where: { user: { id: userId } },
relations: { address: { city: { province: true } } },
});
return { realUser };
}
/************************************************************ */
async getLegalUserData(userId: string) {
const legalUser = await this.legalUserRepository.findOne({
where: { user: { id: userId } },
relations: { address: { city: { province: true } } },
});
return { legalUser };
}
/************************************************************ */
async updateProfile(userId: string, updateProfileDto: UpdateProfileDto) {
@@ -487,7 +496,10 @@ export class UsersService {
name: RoleEnum.USER,
},
},
relations: ["legalUser", "realUser"],
relations: {
legalUser: true,
realUser: true,
},
});
return { customer };
@@ -564,75 +576,94 @@ export class UsersService {
};
}
/************************************************************ */
//TODO:query manager
async createRealUserData(createDto: CreateRealUserDto, userId: string) {
const user = await this.userRepository.findOneBy({ id: userId });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
// async createRealUserData(createDto: CreateRealUserDto, userId: string) {
// const userExist = await this.userRepository.findOneBy({
// id: userId,
// });
// if (!userExist) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
// console.log(userExist);
// const existingRealUser = await this.realUserRepository.findOne({
// where: { user: { id: userExist.id } },
// });
// console.log(existingRealUser);
if (user.financialType && user.financialType === FinancialType.LEGAL)
throw new BadRequestException(UserMessage.FINANCIAL_TYPE_IS_LEGAL);
//
const existRealUser = await this.realUserRepository.findOne({ where: { user: { id: user.id } } });
// if (existingRealUser) throw new BadRequestException(RealUserMessage.REAL_DATA_EXIST);
if (existRealUser) throw new BadRequestException(UserMessage.REAL_DATA_EXIST);
// const { city } = await this.addressService.getCity(+createDto.cityId);
const { city } = await this.addressService.getCity(+createDto.cityId);
// const realUser = this.realUserRepository.create({
// ...createDto,
// user: userExist,
// address: {
// address: createDto.address,
// city,
// postalCode: createDto.postalCode,
// user: userExist,
// },
// });
const existNationalCode = await this.realUserRepository.findOne({ where: { nationalCode: createDto.nationalCode } });
if (existNationalCode) throw new BadRequestException(UserMessage.NATIONAL_CODE_EXIST);
// await this.realUserRepository.save(realUser);
const existPhone = await this.realUserRepository.findOne({ where: { phone: createDto.phone } });
if (existPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
// return {
// message: CommonMessage.CREATED,
// realUser,
// };
// }
const realUser = this.realUserRepository.create({
...createDto,
user,
address: {
fullAddress: createDto.address,
city,
postalCode: createDto.postalCode,
},
});
await this.realUserRepository.save(realUser);
user.financialType = FinancialType.REAL;
await this.userRepository.save(user);
return {
message: CommonMessage.CREATED,
realUser,
};
}
// /************************************************************ */
// async createLegalUserData(createDto: CreateLegalUserDto, userId: string) {
// const userExist = await this.userRepository.findOneBy({
// id: userId,
// });
// if (!userExist) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
//TODO:query manager
async createLegalUserData(createDto: CreateLegalUserDto, userId: string) {
const user = await this.userRepository.findOneBy({ id: userId });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
// const existingLegalUser = await this.legalUserRepository.findOne({
// where: { user: { id: userExist.id } },
// });
if (user.financialType && user.financialType === FinancialType.REAL) throw new BadRequestException(UserMessage.FINANCIAL_TYPE_IS_REAL);
// if (existingLegalUser) throw new BadRequestException(LegalUserMessage.LEGAL_DATA_EXIST);
const existingLegalUser = await this.legalUserRepository.findOne({ where: { user: { id: user.id } } });
// const { city } = await this.addressService.getCity(+createDto.cityId);
if (existingLegalUser) throw new BadRequestException(UserMessage.LEGAL_DATA_EXIST);
// const legalUser = this.legalUserRepository.create({
// ...createDto,
// user: userExist,
// address: {
// address: createDto.address,
// city,
// postalCode: createDto.postalCode,
// user: userExist,
// },
// });
const existEconomicCode = await this.legalUserRepository.findOne({ where: { economicCode: createDto.economicCode } });
if (existEconomicCode) throw new BadRequestException(UserMessage.ECONOMIC_CODE_EXIST);
// await this.legalUserRepository.save(legalUser);
const existPhone = await this.legalUserRepository.findOne({ where: { phone: createDto.phone } });
if (existPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
// return {
// message: CommonMessage.CREATED,
// legalUser,
// };
// }
const existRegistrationCode = await this.legalUserRepository.findOne({ where: { registrationCode: createDto.registrationCode } });
if (existRegistrationCode) throw new BadRequestException(UserMessage.REGISTRATION_CODE_EXIST);
const existNationalIdentity = await this.legalUserRepository.findOne({ where: { nationalIdentity: createDto.nationalIdentity } });
if (existNationalIdentity) throw new BadRequestException(UserMessage.NATIONAL_IDENTITY_EXIST);
const { city } = await this.addressService.getCity(+createDto.cityId);
const legalUser = this.legalUserRepository.create({
...createDto,
user,
address: {
fullAddress: createDto.address,
city,
postalCode: createDto.postalCode,
},
});
await this.legalUserRepository.save(legalUser);
user.financialType = FinancialType.LEGAL;
await this.userRepository.save(user);
return {
message: CommonMessage.CREATED,
legalUser,
};
}
/************************************************************ */
+26 -20
View File
@@ -5,6 +5,8 @@ import { FastifyReply } from "fastify";
import { ChangeEmailDto } from "./DTO/change-email.dto";
import { CheckValidityDTO } from "./DTO/check-validity.dto";
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 { SearchAdminQueryDto } from "./DTO/search-admins-query.dto";
import { SearchRolesQueryDto } from "./DTO/search-roles.dto";
@@ -33,14 +35,21 @@ export class UsersController {
return this.usersService.getMe(userId);
}
//
@ApiOperation({ summary: "Get user financial info" })
@ApiOperation({ summary: "Get legal user data" })
@AuthGuards()
@Get("me/financial-info")
getFinancialInfo(@UserDec("id") userId: string) {
return this.usersService.getMyFinancialInfo(userId);
@Get("legal")
getLegalUserData(@UserDec("id") userId: string) {
return this.usersService.getLegalUserData(userId);
}
@ApiOperation({ summary: "Get real user data" })
@AuthGuards()
@Get("real")
getRealUserData(@UserDec("id") userId: string) {
return this.usersService.getRealUserData(userId);
}
//
@ApiOperation({ summary: "Update user profile" })
@AuthGuards()
@Patch("update-profile")
@@ -164,22 +173,19 @@ export class UsersController {
return this.usersService.createRole(createDto);
}
//
// @ApiOperation({ summary: "Create real user data" })
// @AuthGuards()
// @HttpCode(HttpStatus.CREATED)
// @Post("real-user")
// createRealUser(@Body() createDto: CreateRealUserDto, @UserDec("id") userId: string) {
// return this.usersService.createRealUserData(createDto, userId);
// }
@ApiOperation({ summary: "Create real user data ==> user route" })
@AuthGuards()
@Post("real")
createRealUser(@Body() createDto: CreateRealUserDto, @UserDec("id") userId: string) {
return this.usersService.createRealUserData(createDto, userId);
}
// @ApiOperation({ summary: "Create legal user data" })
// @AuthGuards()
// @HttpCode(HttpStatus.CREATED)
// @Post("legal-user")
// createLegalUser(@Body() createDto: CreateLegalUserDto, @UserDec("id") userId: string) {
// return this.usersService.createLegalUserData(createDto, userId);
// }
@ApiOperation({ summary: "Create legal user data ==> user route" })
@AuthGuards()
@Post("legal")
createLegalUser(@Body() createDto: CreateLegalUserDto, @UserDec("id") userId: string) {
return this.usersService.createLegalUserData(createDto, userId);
}
// @ApiOperation({ summary: "Create customer" })
// @AuthGuards()