chore: user financial

This commit is contained in:
Matin
2025-02-15 10:59:53 +03:30
parent caa968ab4a
commit a13f374031
14 changed files with 246 additions and 19 deletions
@@ -0,0 +1,37 @@
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import { IsEnum, IsNotEmpty, IsOptional, IsString, IsUUID } from "class-validator";
import { FinancialMessage } from "../../../common/enums/message.enum";
import { FinancialType } from "../enums/financial-type.enum";
export class CreateFinancialDto {
@ApiProperty({ enum: FinancialType, description: "Financial information type: REAL or LEGAL" })
@IsEnum(FinancialType, { message: FinancialMessage.FINANCIAL_TYPE_INVALID })
type: FinancialType;
@ApiPropertyOptional({ description: "Economic code" })
@IsOptional()
@IsString({ message: FinancialMessage.ECONOMIC_CODE_INVALID })
economicCode?: string;
@ApiPropertyOptional({ description: "Registration ID" })
@IsOptional()
@IsString({ message: FinancialMessage.REGISTRATION_ID_INVALID })
registrationId?: string;
@ApiPropertyOptional({ description: "number" })
@IsOptional()
@IsString({ message: FinancialMessage.FIXED_PHONE_INVALID })
number?: string;
@ApiPropertyOptional({ description: "number" })
@IsOptional()
@IsString({ message: FinancialMessage.NATIONAL_ID_INVALID })
nationalId?: string;
@ApiPropertyOptional({ description: "User id", example: "123e4567-e89b-12d3-a456-426614174000" })
@IsOptional()
@IsNotEmpty({ message: FinancialMessage.USER_ID_REQUIRED })
@IsUUID("4", { message: FinancialMessage.USER_ID_SHOULD_BE_A_UUID })
userId: string;
}
@@ -0,0 +1,31 @@
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.userFinancials)
user: User;
}
@@ -2,6 +2,7 @@ import { Exclude } from "class-transformer";
import { Column, Entity, ManyToMany, ManyToOne, OneToMany, OneToOne } from "typeorm";
import { Role } from "./role.entity";
import { UserFinancial } from "./user-financial.entity";
import { UserGroup } from "./user-group.entity";
import { BaseEntity } from "../../../common/entities/base.entity";
import { UserAnnouncement } from "../../announcements/entities/user-announcement.entity";
@@ -82,6 +83,9 @@ export class User extends BaseEntity {
@OneToMany(() => LearningProgress, (learningProgress) => learningProgress.learning)
learningProgress: LearningProgress[];
@OneToMany(() => UserFinancial, (userFinancial) => userFinancial.user)
userFinancials: UserFinancial[];
}
// @ManyToMany(() => DanakService, (danakService) => danakService.users)
@@ -0,0 +1,4 @@
export enum FinancialType {
REAL = "REAL",
LEGAL = "LEGAL",
}
+74 -1
View File
@@ -2,17 +2,19 @@ import { BadRequestException, Injectable } from "@nestjs/common";
import slugify from "slugify";
import { In, Not, QueryRunner } from "typeorm";
import { CommonMessage, UserMessage } from "../../../common/enums/message.enum";
import { CommonMessage, FinancialMessage, UserMessage } from "../../../common/enums/message.enum";
import { CompleteRegistrationDto } from "../../auth/DTO/complete-register.dto";
import { UserSettingsService } from "../../settings/providers/user-settings.service";
import { WalletsService } from "../../wallets/providers/wallets.service";
import { CheckValidityDTO } from "../DTO/check-validity.dto";
import { CreateFinancialDto } from "../DTO/create-user-financial.dto";
import { UpdateProfileDto } from "../DTO/update-profile.dto";
import { CreateUserGroupDto } from "../DTO/user-group.dto";
import { Role } from "../entities/role.entity";
import { User } from "../entities/user.entity";
import { RoleEnum } from "../enums/role.enum";
import { ValidityType } from "../enums/validity-type.enum";
import { UserFinancialRepository } from "../repositories/user-financial.repository";
import { UserGroupRepository } from "../repositories/user-group.repository";
import { UserRepository } from "../repositories/users.repository";
@@ -36,6 +38,7 @@ export class UsersService {
private readonly userRepository: UserRepository,
private readonly userGroupRepository: UserGroupRepository,
private readonly userSettingsService: UserSettingsService,
private readonly userFinancialRepository: UserFinancialRepository,
private readonly walletsService: WalletsService,
) {}
@@ -121,6 +124,7 @@ export class UsersService {
// await this.userSettingsService.createUserSettings(user.id);
// return await this.userRepository.save(user);
}
/************************************************************ */
async checkValidity(checkDto: CheckValidityDTO, userId: string) {
@@ -175,6 +179,7 @@ export class UsersService {
}
///****************************************************** */
async updateUserPassword(userId: string, newPassword: string) {
const updatedUser = await this.userRepository.update(
{ id: userId },
@@ -186,4 +191,72 @@ export class UsersService {
updatedUser,
};
}
/************************************************************ */
async findAllUsers() {
const users = await this.userRepository.find({
relations: ["groups"],
});
return { users };
}
/************************************************************ */
async findAllCustomers() {
const customers = await this.userRepository.find({
where: {
role: {
name: RoleEnum.USER,
},
},
relations: ["tickets", "subscriptions", "invoices"],
});
return { customers };
}
/************************************************************ */
async createUserFinancial(createDto: CreateFinancialDto, user: User) {
const userExist = await this.userRepository.findOneBy({
id: user.role.name === RoleEnum.USER ? user.id : createDto.userId,
});
if (!userExist) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
const existingFinancial = await this.userFinancialRepository.findOne({
where: { user: { id: userExist.id }, type: createDto.type },
});
console.log(existingFinancial);
if (existingFinancial) throw new BadRequestException(FinancialMessage.FINANCIAL_INFO_ALREADY_EXISTS);
const userFinancial = this.userFinancialRepository.create({
...createDto,
user: userExist,
});
await this.userFinancialRepository.save(userFinancial);
return {
message: CommonMessage.CREATED,
userFinancial,
};
}
/************************************************************ */
async getUserFinancial(userId: string) {
const user = await this.userRepository.findOne({
where: {
id: userId,
},
relations: ["userFinancials"],
});
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return { user };
}
}
@@ -0,0 +1,12 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { UserFinancial } from "../entities/user-financial.entity";
@Injectable()
export class UserFinancialRepository extends Repository<UserFinancial> {
constructor(@InjectRepository(UserFinancial) userFinancialRepository: Repository<UserFinancial>) {
super(userFinancialRepository.target, userFinancialRepository.manager, userFinancialRepository.queryRunner);
}
}
+50
View File
@@ -2,6 +2,7 @@ import { Body, Controller, Get, HttpCode, HttpStatus, Patch, Post } from "@nestj
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { CheckValidityDTO } from "./DTO/check-validity.dto";
import { CreateFinancialDto } from "./DTO/create-user-financial.dto";
import { UpdateProfileDto } from "./DTO/update-profile.dto";
import { CreateUserGroupDto } from "./DTO/user-group.dto";
import { User } from "./entities/user.entity";
@@ -16,6 +17,8 @@ import { UserDec } from "../../common/decorators/user.decorator";
export class UsersController {
constructor(private usersService: UsersService) {}
/************************************************************ */
@AuthGuards()
@ApiOperation({ summary: "Get user profile" })
@Get("me")
@@ -23,6 +26,8 @@ export class UsersController {
return this.usersService.getMe(user.id);
}
/************************************************************ */
@AuthGuards()
@ApiOperation({ summary: "Update user profile" })
@Patch("update-profile")
@@ -30,6 +35,8 @@ export class UsersController {
return this.usersService.updateProfile(user.id, updateProfileDto);
}
/************************************************************ */
@AuthGuards()
@ApiOperation({ summary: "Check validity of user field" })
@HttpCode(HttpStatus.OK)
@@ -38,6 +45,8 @@ export class UsersController {
return this.usersService.checkValidity(checkValidityDto, user.id);
}
/************************************************************ */
@AuthGuards()
@Roles(RoleEnum.ADMIN)
@ApiOperation({ summary: "Create user group" })
@@ -47,6 +56,8 @@ export class UsersController {
return this.usersService.createUserGroup(createDto);
}
/************************************************************ */
@ApiOperation({ summary: "get all user group" })
@AuthGuards()
@Roles(RoleEnum.ADMIN)
@@ -54,4 +65,43 @@ export class UsersController {
UserGroups() {
return this.usersService.getUserGroups();
}
/************************************************************ */
@ApiOperation({ summary: "get all users" })
@AuthGuards()
@Roles(RoleEnum.ADMIN)
@Get("")
Users() {
return this.usersService.findAllUsers();
}
/************************************************************ */
@ApiOperation({ summary: "get all customers" })
@AuthGuards()
@Roles(RoleEnum.ADMIN)
@Get("customers")
customers() {
return this.usersService.findAllCustomers();
}
/************************************************************ */
@AuthGuards()
@ApiOperation({ summary: "Create user financial" })
@HttpCode(HttpStatus.CREATED)
@Post("user-financial")
createUserFinancial(@Body() createDto: CreateFinancialDto, @UserDec() user: User) {
return this.usersService.createUserFinancial(createDto, user);
}
/************************************************************ */
@ApiOperation({ summary: "get all user financials" })
@AuthGuards()
@Get("user-financials")
userFinancials(@UserDec("id") userId: string) {
return this.usersService.getUserFinancial(userId);
}
}
+12 -2
View File
@@ -13,10 +13,20 @@ import { UserSetting } from "../settings/entities/user-setting.entity";
import { UserSettingsService } from "../settings/providers/user-settings.service";
import { UserSettingsRepository } from "../settings/repositories/user-settings.repository";
import { WalletsModule } from "../wallets/wallets.module";
import { UserFinancial } from "./entities/user-financial.entity";
import { UserFinancialRepository } from "./repositories/user-financial.repository";
@Module({
imports: [TypeOrmModule.forFeature([User, Role, UserGroup, UserSetting]), WalletsModule],
providers: [UsersService, UserRepository, RoleRepository, UserGroupRepository, UserSettingsRepository, UserSettingsService],
imports: [TypeOrmModule.forFeature([User, Role, UserGroup, UserSetting, UserFinancial]), WalletsModule],
providers: [
UsersService,
UserRepository,
RoleRepository,
UserGroupRepository,
UserSettingsRepository,
UserFinancialRepository,
UserSettingsService,
],
controllers: [UsersController],
exports: [UsersService],
})