forked from Yara724/api
Initial commit after migration to gitea
This commit is contained in:
24
src/plates/dto/plate.dto.ts
Normal file
24
src/plates/dto/plate.dto.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
|
||||
export class PlatesDto {
|
||||
@ApiProperty({ type: "number" })
|
||||
leftDigits: number;
|
||||
|
||||
@ApiProperty({ type: "string" })
|
||||
centerAlphabet: string;
|
||||
|
||||
@ApiProperty({ type: "number" })
|
||||
centerDigits: number;
|
||||
|
||||
@ApiProperty({ type: "number" })
|
||||
ir: number;
|
||||
}
|
||||
export class PlatesDtoRs extends PlatesDto {
|
||||
constructor(newPlate: PlatesDto) {
|
||||
super();
|
||||
this.leftDigits = newPlate.leftDigits;
|
||||
this.centerAlphabet = newPlate.centerAlphabet;
|
||||
this.centerDigits = newPlate.centerDigits;
|
||||
this.ir = newPlate.ir;
|
||||
}
|
||||
}
|
||||
26
src/plates/entites/db-service/plate.db.service.ts
Normal file
26
src/plates/entites/db-service/plate.db.service.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { InjectModel } from "@nestjs/mongoose";
|
||||
import { Model, Types } from "mongoose";
|
||||
import { PlatesModel } from "../schema/plate.schema";
|
||||
|
||||
export class PlateDbService {
|
||||
constructor(
|
||||
@InjectModel(PlatesModel.name)
|
||||
private readonly plateModel: Model<PlatesModel>,
|
||||
) {}
|
||||
|
||||
async create(plate: PlatesModel): Promise<PlatesModel> {
|
||||
return await this.plateModel.create(plate);
|
||||
}
|
||||
|
||||
async findOne(plate: PlatesModel): Promise<PlatesModel> {
|
||||
return await this.plateModel.findOne(plate);
|
||||
}
|
||||
|
||||
async findAndRemove(id: Types.ObjectId): Promise<PlatesModel> {
|
||||
return await this.plateModel.findOneAndUpdate(
|
||||
{ _id: id },
|
||||
{ isDelete: true },
|
||||
{ upsert: true },
|
||||
);
|
||||
}
|
||||
}
|
||||
24
src/plates/entites/schema/plate.schema.ts
Normal file
24
src/plates/entites/schema/plate.schema.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
|
||||
|
||||
@Schema({ collection: "plates", versionKey: false })
|
||||
export class PlatesModel {
|
||||
@Prop({ required: true, unique: false })
|
||||
userId: string;
|
||||
|
||||
@Prop({ type: "number", maxlength: 2 })
|
||||
leftDigits: number;
|
||||
|
||||
@Prop({ type: "string", maxlength: 1 })
|
||||
centerAlphabet: string;
|
||||
|
||||
@Prop({ type: "number", maxlength: 3 })
|
||||
centerDigits: number;
|
||||
|
||||
@Prop({ type: "number", maxlength: 2 })
|
||||
ir: number;
|
||||
|
||||
@Prop()
|
||||
nationalCodeOfInsurer: string;
|
||||
}
|
||||
|
||||
export const PlatesSchema = SchemaFactory.createForClass(PlatesModel);
|
||||
8
src/plates/interface/plate.interface.ts
Normal file
8
src/plates/interface/plate.interface.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export interface PlateType {
|
||||
nationalCodeOfInsurer: string;
|
||||
userId: string;
|
||||
leftDigits: number;
|
||||
centerAlphabet: string;
|
||||
centerDigits: number;
|
||||
ir: number;
|
||||
}
|
||||
22
src/plates/plates.module.ts
Normal file
22
src/plates/plates.module.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { MongooseModule } from "@nestjs/mongoose";
|
||||
import { ClientModule } from "src/client/client.module";
|
||||
import { PlateDbService } from "src/plates/entites/db-service/plate.db.service";
|
||||
import { SandHubModule } from "src/sand-hub/sand-hub.module";
|
||||
import { UsersModule } from "src/users/users.module";
|
||||
import { PlatesModel, PlatesSchema } from "./entites/schema/plate.schema";
|
||||
import { PlatesService } from "./plates.service";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
UsersModule,
|
||||
SandHubModule,
|
||||
ClientModule,
|
||||
MongooseModule.forFeature([
|
||||
{ name: PlatesModel.name, schema: PlatesSchema },
|
||||
]),
|
||||
],
|
||||
providers: [PlatesService, PlateDbService],
|
||||
exports: [PlatesService, PlateDbService],
|
||||
})
|
||||
export class PlatesModule {}
|
||||
71
src/plates/plates.service.ts
Normal file
71
src/plates/plates.service.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { Types } from "mongoose";
|
||||
import { PlateDbService } from "src/plates/entites/db-service/plate.db.service";
|
||||
import { AddPlateDto } from "src/profile/dto/user/AddPlateDto";
|
||||
|
||||
@Injectable()
|
||||
export class PlatesService {
|
||||
constructor(
|
||||
private readonly plateDbService: PlateDbService,
|
||||
) {}
|
||||
|
||||
async addPlate(currentUser, body: AddPlateDto) {
|
||||
const { plate, nationalCodeOfInsurer } = body;
|
||||
|
||||
const duplicate = await this.plateDbService.findOne({
|
||||
userId: currentUser,
|
||||
leftDigits: plate.leftDigits,
|
||||
centerAlphabet: plate.centerAlphabet,
|
||||
centerDigits: plate.centerDigits,
|
||||
ir: plate.ir,
|
||||
nationalCodeOfInsurer,
|
||||
});
|
||||
|
||||
if (duplicate)
|
||||
throw new HttpException("duplicate plate", HttpStatus.CONFLICT);
|
||||
|
||||
const newPlateData = await this.plateDbService.create({
|
||||
...plate,
|
||||
userId: currentUser,
|
||||
nationalCodeOfInsurer: body.nationalCodeOfInsurer,
|
||||
});
|
||||
|
||||
return {
|
||||
message: "Plate successfully added",
|
||||
plate: newPlateData,
|
||||
};
|
||||
}
|
||||
|
||||
private async checkUniquePlate(plate): Promise<boolean> {
|
||||
const {
|
||||
userId,
|
||||
leftDigits,
|
||||
centerAlphabet,
|
||||
centerDigits,
|
||||
ir,
|
||||
nationalCodeOfInsurer,
|
||||
} = plate;
|
||||
|
||||
const found = await this.plateDbService.findOne({
|
||||
leftDigits,
|
||||
centerAlphabet,
|
||||
centerDigits,
|
||||
userId,
|
||||
nationalCodeOfInsurer,
|
||||
ir,
|
||||
});
|
||||
|
||||
return !found;
|
||||
}
|
||||
|
||||
async deletePlate(id: Types.ObjectId) {
|
||||
const deleted = await this.plateDbService.findAndRemove(id);
|
||||
if (!deleted) throw new NotFoundException("Plate not found");
|
||||
return { message: "Plate deleted successfully" };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user