forked from Yara724/api
Added in-person expert
This commit is contained in:
49
src/users/entities/db-service/field-expert.db.service.ts
Normal file
49
src/users/entities/db-service/field-expert.db.service.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectModel } from "@nestjs/mongoose";
|
||||
import { FilterQuery, Model, Types } from "mongoose";
|
||||
import { FieldExpertModel } from "../schema/field-expert.schema";
|
||||
|
||||
@Injectable()
|
||||
export class FieldExpertDbService {
|
||||
constructor(
|
||||
@InjectModel(FieldExpertModel.name)
|
||||
private readonly fieldExpertModel: Model<FieldExpertModel>,
|
||||
) {}
|
||||
|
||||
async create(
|
||||
user: Partial<FieldExpertModel> & { password: string },
|
||||
): Promise<FieldExpertModel> {
|
||||
return await this.fieldExpertModel.create(user);
|
||||
}
|
||||
|
||||
async findOne(
|
||||
filter: FilterQuery<FieldExpertModel>,
|
||||
): Promise<FieldExpertModel | null> {
|
||||
return await this.fieldExpertModel.findOne(filter);
|
||||
}
|
||||
|
||||
async findById(userId: string): Promise<FieldExpertModel | null> {
|
||||
return await this.fieldExpertModel.findOne({
|
||||
_id: new Types.ObjectId(userId),
|
||||
});
|
||||
}
|
||||
|
||||
async findOneAndUpdate(
|
||||
filter: FilterQuery<FieldExpertModel>,
|
||||
update: FilterQuery<FieldExpertModel>,
|
||||
) {
|
||||
return await this.fieldExpertModel.findOneAndUpdate(filter, update, {
|
||||
new: true,
|
||||
});
|
||||
}
|
||||
|
||||
async findAll(
|
||||
filter: FilterQuery<FieldExpertModel>,
|
||||
): Promise<(FieldExpertModel & { _id: Types.ObjectId })[]> {
|
||||
return await this.fieldExpertModel.find(filter).lean();
|
||||
}
|
||||
|
||||
async updateOne(filter: FilterQuery<FieldExpertModel>, update: any) {
|
||||
return this.fieldExpertModel.updateOne(filter, update);
|
||||
}
|
||||
}
|
||||
78
src/users/entities/schema/field-expert.schema.ts
Normal file
78
src/users/entities/schema/field-expert.schema.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
|
||||
import { Types } from "mongoose";
|
||||
import { RoleEnum } from "src/Types&Enums/role.enum";
|
||||
|
||||
@Schema({ _id: false })
|
||||
export class RequestStats {
|
||||
@Prop({ default: 0 })
|
||||
totalHandled: number;
|
||||
|
||||
@Prop({ default: 0 })
|
||||
totalChecked: number;
|
||||
}
|
||||
|
||||
const RequestStatsSchema = SchemaFactory.createForClass(RequestStats);
|
||||
|
||||
@Schema({
|
||||
collection: "field-expert",
|
||||
versionKey: false,
|
||||
timestamps: true,
|
||||
})
|
||||
export class FieldExpertModel {
|
||||
_id?: Types.ObjectId;
|
||||
|
||||
@Prop({ required: true })
|
||||
firstName: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
lastName: string;
|
||||
|
||||
@Prop({ type: "string", unique: true })
|
||||
email: string;
|
||||
|
||||
@Prop({ type: "string" })
|
||||
username: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
password: string;
|
||||
|
||||
@Prop({ required: true })
|
||||
mobile: string;
|
||||
|
||||
@Prop({ required: false })
|
||||
phone?: string;
|
||||
|
||||
@Prop({ default: RoleEnum.FIELD_EXPERT })
|
||||
role: RoleEnum;
|
||||
|
||||
@Prop({ type: "string", default: "" })
|
||||
otp: string;
|
||||
|
||||
@Prop({ type: RequestStatsSchema, default: () => ({}) })
|
||||
requestStats: RequestStats;
|
||||
|
||||
@Prop({
|
||||
type: [{ type: String }],
|
||||
default: [],
|
||||
validate: {
|
||||
validator: (arr: any[]) => arr.every((val) => typeof val === "string"),
|
||||
message: "All countedRequests must be strings",
|
||||
},
|
||||
})
|
||||
countedRequests?: string[];
|
||||
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
export const FieldExpertDbSchema =
|
||||
SchemaFactory.createForClass(FieldExpertModel);
|
||||
|
||||
FieldExpertDbSchema.pre("save", function (next) {
|
||||
if (!this.username) {
|
||||
this.username = this.email;
|
||||
}
|
||||
if (!this.requestStats) {
|
||||
this.requestStats = { totalChecked: 0, totalHandled: 0 };
|
||||
}
|
||||
next();
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import { ExpertModel } from "src/users/entities/schema/expert.schema";
|
||||
import { HashModule } from "src/utils/hash/hash.module";
|
||||
import { OtpModule } from "src/utils/otp/otp.module";
|
||||
import { ExpertDbService } from "./entities/db-service/expert.db.service";
|
||||
import { FieldExpertDbService } from "./entities/db-service/field-expert.db.service";
|
||||
import { InsurerExpertDbService } from "./entities/db-service/insurer-expert.db.service";
|
||||
import { UserDbService } from "./entities/db-service/user.db.service";
|
||||
import {
|
||||
@@ -12,6 +13,10 @@ import {
|
||||
DamageExpertModel,
|
||||
} from "./entities/schema/damage-expert.schema";
|
||||
import { ExpertDbSchema } from "./entities/schema/expert.schema";
|
||||
import {
|
||||
FieldExpertDbSchema,
|
||||
FieldExpertModel,
|
||||
} from "./entities/schema/field-expert.schema";
|
||||
import {
|
||||
InsurerExpertDbSchema,
|
||||
InsurerExpertModel,
|
||||
@@ -24,6 +29,7 @@ import { UserModel, UserDbSchema } from "./entities/schema/user.schema";
|
||||
{ name: UserModel.name, schema: UserDbSchema },
|
||||
{ name: DamageExpertModel.name, schema: DamageExpertDbSchema },
|
||||
{ name: ExpertModel.name, schema: ExpertDbSchema },
|
||||
{ name: FieldExpertModel.name, schema: FieldExpertDbSchema },
|
||||
{ name: InsurerExpertModel.name, schema: InsurerExpertDbSchema },
|
||||
]),
|
||||
OtpModule,
|
||||
@@ -34,12 +40,14 @@ import { UserModel, UserDbSchema } from "./entities/schema/user.schema";
|
||||
UserDbService,
|
||||
ExpertDbService,
|
||||
DamageExpertDbService,
|
||||
FieldExpertDbService,
|
||||
InsurerExpertDbService,
|
||||
],
|
||||
exports: [
|
||||
UserDbService,
|
||||
ExpertDbService,
|
||||
DamageExpertDbService,
|
||||
FieldExpertDbService,
|
||||
InsurerExpertDbService,
|
||||
],
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user