forked from Yara724/api
blame and claim refactored
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectModel } from "@nestjs/mongoose";
|
||||
import { FilterQuery, Model, Types } from "mongoose";
|
||||
import {
|
||||
BlameRequest,
|
||||
BlameRequestDocument,
|
||||
} from "src/request-management/entities/schema/blame-cases.schema";
|
||||
|
||||
@Injectable()
|
||||
export class BlameRequestDbService {
|
||||
constructor(
|
||||
@InjectModel(BlameRequest.name)
|
||||
private readonly blameRequestModel: Model<BlameRequestDocument>,
|
||||
) {}
|
||||
|
||||
async create(payload: Partial<BlameRequest>): Promise<BlameRequestDocument> {
|
||||
return this.blameRequestModel.create(payload);
|
||||
}
|
||||
|
||||
async findOne(
|
||||
filter: FilterQuery<BlameRequest>,
|
||||
): Promise<BlameRequestDocument | null> {
|
||||
return this.blameRequestModel.findOne(filter);
|
||||
}
|
||||
|
||||
async findById(id: string | Types.ObjectId): Promise<BlameRequestDocument | null> {
|
||||
return this.blameRequestModel.findById(id);
|
||||
}
|
||||
|
||||
/** Find by id excluding history (e.g. for expert detail view). Returns lean plain object. */
|
||||
async findByIdWithoutHistory(
|
||||
id: string | Types.ObjectId,
|
||||
): Promise<Record<string, unknown> | null> {
|
||||
const doc = await this.blameRequestModel
|
||||
.findById(id)
|
||||
.select("-history")
|
||||
.lean()
|
||||
.exec();
|
||||
return doc as Record<string, unknown> | null;
|
||||
}
|
||||
|
||||
async findByIdAndUpdate(
|
||||
id: string | Types.ObjectId,
|
||||
update: any,
|
||||
): Promise<BlameRequestDocument | null> {
|
||||
return this.blameRequestModel.findByIdAndUpdate(id, update, { new: true });
|
||||
}
|
||||
|
||||
/** Atomic find and update with filter conditions (e.g. for locking). */
|
||||
async findOneAndUpdate(
|
||||
filter: FilterQuery<BlameRequest>,
|
||||
update: any,
|
||||
options?: { new?: boolean },
|
||||
): Promise<BlameRequestDocument | null> {
|
||||
return this.blameRequestModel.findOneAndUpdate(filter, update, {
|
||||
new: options?.new ?? true,
|
||||
});
|
||||
}
|
||||
|
||||
async find(
|
||||
filter: FilterQuery<BlameRequest>,
|
||||
options?: { lean?: boolean; select?: string },
|
||||
): Promise<BlameRequestDocument[] | Record<string, unknown>[]> {
|
||||
if (options?.lean) {
|
||||
const query = options?.select
|
||||
? this.blameRequestModel.find(filter).select(options.select).lean()
|
||||
: this.blameRequestModel.find(filter).lean();
|
||||
return query.exec() as Promise<Record<string, unknown>[]>;
|
||||
}
|
||||
const query = options?.select
|
||||
? this.blameRequestModel.find(filter).select(options.select)
|
||||
: this.blameRequestModel.find(filter);
|
||||
return query.exec() as Promise<BlameRequestDocument[]>;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user