forked from Yara724/api
95 lines
3.4 KiB
TypeScript
95 lines
3.4 KiB
TypeScript
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);
|
|
|
|
// 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()}`,
|
|
// );
|
|
// }
|
|
|
|
// // 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 },
|
|
// });
|
|
|
|
// 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);
|
|
|
|
// 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}`);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// @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 staleStatuses = [
|
|
// ReqBlameStatus.PendingForFirstParty,
|
|
// ReqBlameStatus.PendingForSecondParty,
|
|
// ];
|
|
|
|
// const query = {
|
|
// createdAt: { $lt: sevenDaysAgo },
|
|
// blameStatus: { $in: staleStatuses },
|
|
// };
|
|
|
|
// 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);
|
|
// }
|
|
// }
|
|
}
|