forked from Yara724/api
Initial commit after migration to gitea
This commit is contained in:
37
src/client/client.controller.ts
Normal file
37
src/client/client.controller.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
|
||||
import { GlobalGuard } from "src/auth/guards/global.guard";
|
||||
import { CurrentUser } from "src/decorators/user.decorator";
|
||||
import { ClientService } from "./client.service";
|
||||
import { ClientDto } from "./dto/create-client.dto";
|
||||
|
||||
@Controller("client")
|
||||
@ApiTags("client-management")
|
||||
export class ClientController {
|
||||
constructor(private readonly clientService: ClientService) {}
|
||||
|
||||
@Post()
|
||||
@UseGuards(GlobalGuard)
|
||||
@ApiBearerAuth()
|
||||
async addClient(@Body() client: ClientDto) {
|
||||
return await this.clientService.addClient(client);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(GlobalGuard)
|
||||
async getClient(@CurrentUser() user) {
|
||||
return await this.clientService.getClients();
|
||||
}
|
||||
|
||||
@Get("list")
|
||||
async getClientList(@CurrentUser() user) {
|
||||
return await this.clientService.getClientList();
|
||||
}
|
||||
}
|
||||
24
src/client/client.module.ts
Normal file
24
src/client/client.module.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { MongooseModule } from "@nestjs/mongoose";
|
||||
import { ClientController } from "./client.controller";
|
||||
import { ClientService } from "./client.service";
|
||||
import { BranchDbService } from "./entities/db-service/branch.db.service";
|
||||
import { ClientDbService } from "./entities/db-service/client.db.service";
|
||||
import { BranchModel, BranchSchema } from "./entities/schema/branch.schema";
|
||||
import { ClientDbSchema, ClientModel } from "./entities/schema/client.schema";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([
|
||||
{ name: ClientModel.name, schema: ClientDbSchema },
|
||||
{
|
||||
name: BranchModel.name,
|
||||
schema: BranchSchema,
|
||||
},
|
||||
]),
|
||||
],
|
||||
controllers: [ClientController],
|
||||
providers: [ClientService, ClientDbService, BranchDbService],
|
||||
exports: [ClientService, ClientDbService, BranchDbService],
|
||||
})
|
||||
export class ClientModule {}
|
||||
56
src/client/client.service.ts
Normal file
56
src/client/client.service.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { BadGatewayException, GoneException, Injectable } from "@nestjs/common";
|
||||
import { Types } from "mongoose";
|
||||
import {
|
||||
ClientDto,
|
||||
ClientDtoRs,
|
||||
ClientLists,
|
||||
} from "src/client/dto/create-client.dto";
|
||||
import { ClientDbService } from "./entities/db-service/client.db.service";
|
||||
|
||||
@Injectable()
|
||||
export class ClientService {
|
||||
constructor(private readonly clientDbService: ClientDbService) {}
|
||||
async addClient(client: ClientDto): Promise<ClientDtoRs> {
|
||||
try {
|
||||
const newClient = await this.clientDbService.create({
|
||||
clientCode: client.clientCode,
|
||||
clientName: {
|
||||
persian: client.clientName.persian,
|
||||
english: client.clientName.english || null,
|
||||
},
|
||||
property: {
|
||||
smsApiKey: client.property.smsApiKey || null,
|
||||
},
|
||||
useExpertMode: client.useExpertMode || null,
|
||||
});
|
||||
if (newClient) return new ClientDtoRs(newClient);
|
||||
else throw new GoneException("database not connected");
|
||||
} catch (er) {
|
||||
throw new BadGatewayException(er.errors);
|
||||
}
|
||||
}
|
||||
|
||||
findOne(filter) {
|
||||
return this.clientDbService.findOne(filter);
|
||||
}
|
||||
|
||||
async findClientWithPersianName(filter: string) {
|
||||
return await this.clientDbService.find({ "clientName.persian": filter });
|
||||
}
|
||||
|
||||
async findClientWithCompanyCode(companyCode: number) {
|
||||
return await this.clientDbService.find({ clientCode: companyCode });
|
||||
}
|
||||
|
||||
async getClients(): Promise<ClientDtoRs[]> {
|
||||
const clients = await this.clientDbService.findAll();
|
||||
const show = clients.map((c) => new ClientDtoRs(c));
|
||||
return show;
|
||||
}
|
||||
|
||||
async getClientList(): Promise<ClientLists[]> {
|
||||
const client = await this.clientDbService.findAll();
|
||||
const list = client.map((element) => new ClientLists(element));
|
||||
return list;
|
||||
}
|
||||
}
|
||||
34
src/client/dto/create-branch.dto.ts
Normal file
34
src/client/dto/create-branch.dto.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsString, IsNotEmpty, IsOptional } from "class-validator";
|
||||
|
||||
export class CreateBranchDto {
|
||||
@ApiProperty({ example: "شهرک غرب" })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiProperty({ example: "1234" })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
code: string;
|
||||
|
||||
@ApiProperty({ example: "استان" })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
city: string;
|
||||
|
||||
@ApiProperty({ example: "شهر" })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
state: string;
|
||||
|
||||
@ApiProperty({ example: "فلان آدرس" })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
address: string;
|
||||
|
||||
@ApiProperty({ required: false, example: "0912345678" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
phoneNumber?: string;
|
||||
}
|
||||
48
src/client/dto/create-client.dto.ts
Normal file
48
src/client/dto/create-client.dto.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
class ClientName {
|
||||
@ApiProperty({})
|
||||
persian: string;
|
||||
|
||||
@ApiProperty({})
|
||||
english: string;
|
||||
}
|
||||
class Property {
|
||||
@ApiProperty({})
|
||||
smsApiKey: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ClientDto {
|
||||
@ApiProperty({ required: true })
|
||||
clientName: ClientName;
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
clientCode: number;
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
property: Property;
|
||||
|
||||
@ApiProperty({ examples: ["legal", "genuine"] })
|
||||
useExpertMode: "legal" | "genuine";
|
||||
}
|
||||
export class ClientDtoRs {
|
||||
persian: string;
|
||||
english: string;
|
||||
clientId: Types.ObjectId;
|
||||
useExpertsMode: string;
|
||||
constructor(readonly client) {
|
||||
this.persian = client.clientName.persian;
|
||||
}
|
||||
}
|
||||
|
||||
export class ClientLists {
|
||||
name: string;
|
||||
id: Types.ObjectId;
|
||||
constructor(client: ClientDto) {
|
||||
this.name = client.clientName.persian;
|
||||
this.id = client["_id"];
|
||||
}
|
||||
}
|
||||
34
src/client/entities/db-service/branch.db.service.ts
Normal file
34
src/client/entities/db-service/branch.db.service.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectModel } from "@nestjs/mongoose";
|
||||
import { FilterQuery, Model, Types } from "mongoose";
|
||||
import { BranchModel, BranchDocument } from "../schema/branch.schema";
|
||||
|
||||
@Injectable()
|
||||
export class BranchDbService {
|
||||
constructor(
|
||||
@InjectModel(BranchModel.name)
|
||||
private readonly branchModel: Model<BranchModel>,
|
||||
) {}
|
||||
|
||||
async create(branch: BranchModel): Promise<BranchModel> {
|
||||
return await this.branchModel.create(branch);
|
||||
}
|
||||
|
||||
async find(branch: FilterQuery<BranchModel>): Promise<BranchDocument> {
|
||||
return await this.branchModel.findOne(branch);
|
||||
}
|
||||
|
||||
async findOne(branch: FilterQuery<BranchModel>) {
|
||||
return this.branchModel.findOne(branch);
|
||||
}
|
||||
|
||||
async findAll(insuranceId: string): Promise<BranchModel[]> {
|
||||
return await this.branchModel.find({
|
||||
clientKey: new Types.ObjectId(insuranceId),
|
||||
});
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<BranchModel | null> {
|
||||
return this.branchModel.findById(id).lean();
|
||||
}
|
||||
}
|
||||
28
src/client/entities/db-service/client.db.service.ts
Normal file
28
src/client/entities/db-service/client.db.service.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectModel } from "@nestjs/mongoose";
|
||||
import { FilterQuery, Model } from "mongoose";
|
||||
import { ClientModel, ClientDocument } from "../schema/client.schema";
|
||||
|
||||
@Injectable()
|
||||
export class ClientDbService {
|
||||
constructor(
|
||||
@InjectModel(ClientModel.name)
|
||||
private readonly clientModel: Model<ClientModel>,
|
||||
) {}
|
||||
|
||||
async create(client: ClientModel): Promise<ClientModel> {
|
||||
return await this.clientModel.create(client);
|
||||
}
|
||||
|
||||
async find(client: FilterQuery<ClientModel>): Promise<ClientDocument> {
|
||||
return await this.clientModel.findOne(client);
|
||||
}
|
||||
|
||||
async findOne(client: FilterQuery<ClientModel>) {
|
||||
return this.clientModel.findOne(client);
|
||||
}
|
||||
|
||||
async findAll(): Promise<ClientModel[]> {
|
||||
return await this.clientModel.find();
|
||||
}
|
||||
}
|
||||
32
src/client/entities/schema/branch.schema.ts
Normal file
32
src/client/entities/schema/branch.schema.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
export type BranchDocument = BranchModel & Document;
|
||||
|
||||
@Schema({ collection: "branches", versionKey: false, timestamps: true })
|
||||
export class BranchModel {
|
||||
@Prop({ required: true, type: Types.ObjectId, ref: "ClientModel" })
|
||||
clientKey: Types.ObjectId;
|
||||
|
||||
@Prop({ required: true })
|
||||
name: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
code: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
city: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
state: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
address: string;
|
||||
|
||||
@Prop()
|
||||
phoneNumber?: string;
|
||||
}
|
||||
|
||||
export const BranchSchema = SchemaFactory.createForClass(BranchModel);
|
||||
|
||||
BranchSchema.index({ clientKey: 1, code: 1 }, { unique: true });
|
||||
25
src/client/entities/schema/client.schema.ts
Normal file
25
src/client/entities/schema/client.schema.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
|
||||
|
||||
export type ClientDocument = ClientModel & Document;
|
||||
|
||||
@Schema({ collection: "clients", versionKey: false })
|
||||
export class ClientModel {
|
||||
@Prop({ required: true, unique: true, type: Object })
|
||||
clientName: {
|
||||
persian: string;
|
||||
english: string;
|
||||
};
|
||||
|
||||
@Prop({ required: false, unique: true, type: Object })
|
||||
property: {
|
||||
smsApiKey: string;
|
||||
};
|
||||
|
||||
@Prop({ required: true, unique: false })
|
||||
useExpertMode: "legal" | "genuine";
|
||||
|
||||
@Prop({ required: true, unique: false })
|
||||
clientCode: number;
|
||||
}
|
||||
|
||||
export const ClientDbSchema = SchemaFactory.createForClass(ClientModel);
|
||||
Reference in New Issue
Block a user