Tidied up the project

This commit is contained in:
2026-05-30 08:59:57 +03:30
parent 5c372947dd
commit 10df869efb
19 changed files with 7064 additions and 4620 deletions

View File

@@ -1,95 +1,94 @@
import { Injectable, Logger } from "@nestjs/common";
import { Cron, CronExpression } from "@nestjs/schedule";
import { Injectable } from "@nestjs/common";
import { RequestManagementDbService } from "src/request-management/entities/db-service/request-management.db.service";
import { SubmitReply } from "src/request-management/entities/schema/request-management.schema";
import { ReqBlameStatus } from "src/Types&Enums/blame-request-management/status.enum";
@Injectable()
export class AutoCloseRequestService {
private readonly logger = new Logger(AutoCloseRequestService.name);
// private readonly logger = new Logger(AutoCloseRequestService.name);
constructor(private readonly requestDb: RequestManagementDbService) {}
// constructor(private readonly requestDb: RequestManagementDbService) {}
async scheduleAutoClose(params: { requestId: string; runAt: Date }) {
await this.requestDb.findAndUpdate(
{ _id: params.requestId },
{ $set: { autoCloseTriggerTime: params.runAt } },
);
this.logger.log(
`Scheduled auto-close for ${params.requestId} at ${params.runAt.toISOString()}`,
);
}
// async scheduleAutoClose(params: { requestId: string; runAt: Date }) {
// await this.requestDb.findAndUpdate(
// { _id: params.requestId },
// { $set: { autoCloseTriggerTime: params.runAt } },
// );
// this.logger.log(
// `Scheduled auto-close for ${params.requestId} at ${params.runAt.toISOString()}`,
// );
// }
// Runs every hour
@Cron(CronExpression.EVERY_HOUR)
async handleAutoClose() {
const now = new Date();
const threshold = new Date(now.getTime() - 24 * 60 * 60 * 1000);
// // Runs every hour
// @Cron(CronExpression.EVERY_HOUR)
// async handleAutoClose() {
// const now = new Date();
// const threshold = new Date(now.getTime() - 24 * 60 * 60 * 1000);
const requestsToClose = await this.requestDb.findAll({
blameStatus: { $ne: ReqBlameStatus.CloseRequest },
$or: [
{ "expertSubmitReply.firstPartyComment.isAccept": true },
{ "expertSubmitReply.secondPartyComment.isAccept": true },
{ "expertSubmitReplyFinal.firstPartyComment.isAccept": true },
{ "expertSubmitReplyFinal.secondPartyComment.isAccept": true },
],
autoCloseTriggerTime: { $lte: threshold },
});
// const requestsToClose = await this.requestDb.findAll({
// blameStatus: { $ne: ReqBlameStatus.CloseRequest },
// $or: [
// { "expertSubmitReply.firstPartyComment.isAccept": true },
// { "expertSubmitReply.secondPartyComment.isAccept": true },
// { "expertSubmitReplyFinal.firstPartyComment.isAccept": true },
// { "expertSubmitReplyFinal.secondPartyComment.isAccept": true },
// ],
// autoCloseTriggerTime: { $lte: threshold },
// });
for (const request of requestsToClose) {
const reply = request.expertSubmitReplyFinal || request.expertSubmitReply;
// for (const request of requestsToClose) {
// const reply = request.expertSubmitReplyFinal || request.expertSubmitReply;
const isSubmitReply = (reply: any): reply is SubmitReply =>
reply &&
typeof reply === "object" &&
("firstPartyComment" in reply || "secondPartyComment" in reply);
// const isSubmitReply = (reply: any): reply is SubmitReply =>
// reply &&
// typeof reply === "object" &&
// ("firstPartyComment" in reply || "secondPartyComment" in reply);
if (isSubmitReply(reply)) {
const firstAccepted = reply.firstPartyComment?.isAccept;
const secondAccepted = reply.secondPartyComment?.isAccept;
// if (isSubmitReply(reply)) {
// const firstAccepted = reply.firstPartyComment?.isAccept;
// const secondAccepted = reply.secondPartyComment?.isAccept;
if (!firstAccepted || !secondAccepted) {
await this.requestDb.findByIdAndUpdate(request._id.toString(), {
blameStatus: ReqBlameStatus.CloseRequest,
});
this.logger.log(`Auto-closed request ${request._id}`);
}
}
}
}
// if (!firstAccepted || !secondAccepted) {
// await this.requestDb.findByIdAndUpdate(request._id.toString(), {
// blameStatus: ReqBlameStatus.CloseRequest,
// });
// this.logger.log(`Auto-closed request ${request._id}`);
// }
// }
// }
// }
@Cron(CronExpression.EVERY_DAY_AT_2AM)
async handleBlameFileCleanup() {
this.logger.log("Running scheduled blame file cleanup job...");
// @Cron(CronExpression.EVERY_DAY_AT_2AM)
// async handleBlameFileCleanup() {
// this.logger.log("Running scheduled blame file cleanup job...");
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
// const sevenDaysAgo = new Date();
// sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
const staleStatuses = [
ReqBlameStatus.PendingForFirstParty,
ReqBlameStatus.PendingForSecondParty,
];
// const staleStatuses = [
// ReqBlameStatus.PendingForFirstParty,
// ReqBlameStatus.PendingForSecondParty,
// ];
const query = {
createdAt: { $lt: sevenDaysAgo },
blameStatus: { $in: staleStatuses },
};
// const query = {
// createdAt: { $lt: sevenDaysAgo },
// blameStatus: { $in: staleStatuses },
// };
try {
const result = await this.requestDb.updateMany(query, {
$set: { blameStatus: ReqBlameStatus.CloseRequest },
});
// try {
// const result = await this.requestDb.updateMany(query, {
// $set: { blameStatus: ReqBlameStatus.CloseRequest },
// });
if (result.modifiedCount > 0) {
this.logger.log(
`Successfully closed ${result.modifiedCount} stale blame files.`,
);
} else {
this.logger.log("No stale blame files found to close.");
}
} catch (error) {
this.logger.error("Error during scheduled blame file cleanup:", error);
}
}
// if (result.modifiedCount > 0) {
// this.logger.log(
// `Successfully closed ${result.modifiedCount} stale blame files.`,
// );
// } else {
// this.logger.log("No stale blame files found to close.");
// }
// } catch (error) {
// this.logger.error("Error during scheduled blame file cleanup:", error);
// }
// }
}