forked from Yara724/api
Initial commit after migration to gitea
This commit is contained in:
284
src/reports/reports.service.ts
Normal file
284
src/reports/reports.service.ts
Normal file
@@ -0,0 +1,284 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { Types } from "mongoose";
|
||||
import { ClaimRequestManagementDbService } from "src/claim-request-management/entites/db-service/claim-request-management.db.service";
|
||||
import { RequestManagementDbService } from "src/request-management/entities/db-service/request-management.db.service";
|
||||
import { ReqBlameStatus } from "src/Types&Enums/blame-request-management/status.enum";
|
||||
import { ReqClaimStatus } from "src/Types&Enums/claim-request-management/status.enum";
|
||||
import {
|
||||
CompanyAllRequestsCountReportDtoRs,
|
||||
DamageExpertAllRequestsCountReportDtoRs,
|
||||
ExpertAllRequestsCountReportDtoRs,
|
||||
} from "./dto/reports.dto";
|
||||
|
||||
@Injectable()
|
||||
export class ReportsService {
|
||||
constructor(
|
||||
private readonly requestManagementDbService: RequestManagementDbService,
|
||||
private readonly claimRequestManagementDbService: ClaimRequestManagementDbService,
|
||||
) {}
|
||||
|
||||
async getAllCheckedRequestsCountFn(role: string, client: string) {
|
||||
const statuses = ["UnChecked", "CheckedRequest"];
|
||||
const data: Record<string, number> = { all: 0 };
|
||||
const clientId = new Types.ObjectId(client);
|
||||
|
||||
for (const status of statuses) {
|
||||
let count = 0;
|
||||
if (role === "damage_expert") {
|
||||
count = await this.claimRequestManagementDbService.countByFilter({
|
||||
claimStatus: status,
|
||||
userClientKey: clientId,
|
||||
});
|
||||
} else {
|
||||
count = await this.requestManagementDbService.countByFilter({
|
||||
blameStatus: status,
|
||||
$or: [
|
||||
{ "firstPartyDetails.firstPartyClient.clientId": clientId },
|
||||
{ "secondPartyDetails.secondPartyClient.clientId": clientId },
|
||||
],
|
||||
});
|
||||
}
|
||||
data[status] = count;
|
||||
data.all += count;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
async getDateFilteredRequestByStatus(
|
||||
role: string,
|
||||
client: string,
|
||||
start: Date,
|
||||
end: Date,
|
||||
) {
|
||||
const statuses = ["UnChecked", "CheckedRequest", "CheckAgain"];
|
||||
const data: Record<string, number> = {};
|
||||
const clientId = new Types.ObjectId(client);
|
||||
|
||||
for (const status of statuses) {
|
||||
let blameCount = 0;
|
||||
let claimCount = 0;
|
||||
|
||||
if (role === "expert" || role === "company") {
|
||||
blameCount = await this.requestManagementDbService.countByFilter({
|
||||
blameStatus: status,
|
||||
createdAt: { $gte: start, $lte: end },
|
||||
$or: [
|
||||
{ "firstPartyDetails.firstPartyClient.clientId": clientId },
|
||||
{ "secondPartyDetails.secondPartyClient.clientId": clientId },
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
if (role === "damage_expert" || role === "company") {
|
||||
claimCount = await this.claimRequestManagementDbService.countByFilter({
|
||||
claimStatus: status,
|
||||
userClientKey: clientId,
|
||||
createdAt: { $gte: start, $lte: end },
|
||||
});
|
||||
}
|
||||
data[status] = blameCount + claimCount;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
async getAllRequestsCountByRole(role: string, client: string) {
|
||||
if (role === "expert") {
|
||||
const statuses = Object.values(ReqBlameStatus);
|
||||
const data: Record<string, number> = { all: 0 };
|
||||
|
||||
for (const status of statuses) {
|
||||
const filter = {
|
||||
blameStatus: status,
|
||||
$or: [
|
||||
{
|
||||
"firstPartyDetails.firstPartyClient.clientId": new Types.ObjectId(
|
||||
client,
|
||||
),
|
||||
},
|
||||
// {
|
||||
// "secondPartyDetails.secondPartyClient.clientId":
|
||||
// new Types.ObjectId(client),
|
||||
// },
|
||||
],
|
||||
};
|
||||
|
||||
const count =
|
||||
await this.requestManagementDbService.countByFilter(filter);
|
||||
data[status] = count;
|
||||
data.all += count;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
if (role === "damage_expert") {
|
||||
const statuses = Object.values(ReqClaimStatus);
|
||||
const data: Record<string, number> = { all: 0 };
|
||||
|
||||
for (const status of statuses) {
|
||||
const filter = {
|
||||
claimStatus: status,
|
||||
userClientKey: new Types.ObjectId(client),
|
||||
};
|
||||
|
||||
const count =
|
||||
await this.claimRequestManagementDbService.countByFilter(filter);
|
||||
data[status] = count;
|
||||
data.all += count;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// ✅ Company logic
|
||||
if (role === "company") {
|
||||
const blameStatuses = Object.values(ReqBlameStatus);
|
||||
const claimStatuses = Object.values(ReqClaimStatus);
|
||||
|
||||
const blameData: Record<string, number> = { all: 0 };
|
||||
const claimData: Record<string, number> = { all: 0 };
|
||||
|
||||
// Only count files where this company is FIRST party in Blame
|
||||
for (const status of blameStatuses) {
|
||||
const filter = {
|
||||
blameStatus: status,
|
||||
"firstPartyDetails.firstPartyClient.clientId": new Types.ObjectId(
|
||||
client,
|
||||
),
|
||||
};
|
||||
|
||||
const count =
|
||||
await this.requestManagementDbService.countByFilter(filter);
|
||||
blameData[status] = count;
|
||||
blameData.all += count;
|
||||
}
|
||||
|
||||
// Claims always use `userClientKey`
|
||||
for (const status of claimStatuses) {
|
||||
const filter = {
|
||||
claimStatus: status,
|
||||
userClientKey: new Types.ObjectId(client),
|
||||
};
|
||||
|
||||
const count =
|
||||
await this.claimRequestManagementDbService.countByFilter(filter);
|
||||
claimData[status] = count;
|
||||
claimData.all += count;
|
||||
}
|
||||
|
||||
return { blameData, claimData };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async getAllRequestsReportCount(actor, client) {
|
||||
if (actor.role === "damage_expert") {
|
||||
const data = await this.getAllRequestsCountByRole(actor.role, client);
|
||||
return new DamageExpertAllRequestsCountReportDtoRs(data);
|
||||
} else if (actor.role === "expert") {
|
||||
const data = await this.getAllRequestsCountByRole(actor.role, client);
|
||||
return new ExpertAllRequestsCountReportDtoRs(data);
|
||||
} else {
|
||||
// company
|
||||
const damageExpertData = await this.getAllRequestsCountByRole(
|
||||
"damage_expert",
|
||||
client,
|
||||
);
|
||||
const expertData = await this.getAllRequestsCountByRole("expert", client);
|
||||
return new CompanyAllRequestsCountReportDtoRs(
|
||||
expertData,
|
||||
damageExpertData,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getAllRequestsReportPerMonth(actor, client) {
|
||||
const result = [];
|
||||
const now = new Date();
|
||||
|
||||
if (actor.role === "company") {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const monthStart = new Date(now.getFullYear(), now.getMonth() - i, 1);
|
||||
const monthEnd = new Date(
|
||||
now.getFullYear(),
|
||||
now.getMonth() - i + 1,
|
||||
0,
|
||||
23,
|
||||
59,
|
||||
59,
|
||||
);
|
||||
const label = monthStart.toLocaleDateString("fa-IR", {
|
||||
month: "long",
|
||||
year: "numeric",
|
||||
});
|
||||
|
||||
const blameData = await this.getDateFilteredRequestByStatus(
|
||||
"expert",
|
||||
client,
|
||||
monthStart,
|
||||
monthEnd,
|
||||
);
|
||||
const claimData = await this.getDateFilteredRequestByStatus(
|
||||
"damage_expert",
|
||||
client,
|
||||
monthStart,
|
||||
monthEnd,
|
||||
);
|
||||
|
||||
result.unshift({
|
||||
stDate: monthStart,
|
||||
enDate: monthEnd,
|
||||
faLabel: label,
|
||||
data: {
|
||||
blame: blameData,
|
||||
claim: claimData,
|
||||
},
|
||||
});
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const monthStart = new Date(now.getFullYear(), now.getMonth() - i, 1);
|
||||
const monthEnd = new Date(
|
||||
now.getFullYear(),
|
||||
now.getMonth() - i + 1,
|
||||
0,
|
||||
23,
|
||||
59,
|
||||
59,
|
||||
);
|
||||
const label = monthStart.toLocaleDateString("fa-IR", {
|
||||
month: "long",
|
||||
year: "numeric",
|
||||
});
|
||||
const data = await this.getDateFilteredRequestByStatus(
|
||||
actor.role,
|
||||
client,
|
||||
monthStart,
|
||||
monthEnd,
|
||||
);
|
||||
result.unshift({
|
||||
stDate: monthStart,
|
||||
enDate: monthEnd,
|
||||
faLabel: label,
|
||||
data,
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
async getAllCheckedRequestsCount(actor, client) {
|
||||
if (actor.role === "damage_expert" || actor.role === "expert") {
|
||||
return this.getAllCheckedRequestsCountFn(actor.role, client);
|
||||
} else {
|
||||
const blame = await this.getAllCheckedRequestsCountFn("expert", client);
|
||||
const claim = await this.getAllCheckedRequestsCountFn(
|
||||
"damage_expert",
|
||||
client,
|
||||
);
|
||||
return { blame, claim };
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user