1
0
forked from Yara724/api
This commit is contained in:
SepehrYahyaee
2026-04-20 10:49:06 +03:30
parent b4501c503e
commit b826a133a4
2 changed files with 103 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ import { SandHubModule } from "./sand-hub/sand-hub.module";
import { ReportsModule } from "./reports/reports.module";
import { RequestManagementModule } from "./request-management/request-management.module";
import { UsersModule } from "./users/users.module";
import { applyIranFaTimestampPlugin } from "./helpers/mongoose-fa-timestamps.plugin";
import { CronModule } from "./utils/cron/cron.module";
import { WorkflowStepManagementModule } from "./workflow-step-management/workflow-step-management.module";
@@ -44,6 +45,10 @@ dotenv.config({ path: `.${process.env.NODE_ENV}.env` });
authMechanism: "SCRAM-SHA-256",
tls: true,
tlsAllowInvalidCertificates: true,
connectionFactory: (connection) => {
applyIranFaTimestampPlugin(connection);
return connection;
},
},
),
UsersModule,

View File

@@ -0,0 +1,98 @@
import { Connection, Query, Schema } from "mongoose";
const PERSIAN_TO_ENGLISH: Record<string, string> = {
"۰": "0",
"۱": "1",
"۲": "2",
"۳": "3",
"۴": "4",
"۵": "5",
"۶": "6",
"۷": "7",
"۸": "8",
"۹": "9",
};
function normalizeDigits(value: string): string {
return value.replace(/[۰-۹]/g, (ch) => PERSIAN_TO_ENGLISH[ch] ?? ch);
}
function toIranFaDateTimeTuple(input: Date): [string, string] {
const dateFa = normalizeDigits(
input.toLocaleDateString("fa-IR", {
timeZone: "Asia/Tehran",
year: "numeric",
month: "2-digit",
day: "2-digit",
}),
);
const timeFa = normalizeDigits(
input.toLocaleTimeString("fa-IR", {
timeZone: "Asia/Tehran",
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}),
);
return [dateFa, timeFa];
}
function writeFaTimestampsOnUpdate(query: Query<any, any>): void {
const nowFa = toIranFaDateTimeTuple(new Date());
const update = (query.getUpdate() ?? {}) as Record<string, any>;
if (!update.$set || typeof update.$set !== "object") {
update.$set = {};
}
if (update.$set.createdAtFa == null && update.createdAtFa == null) {
update.$set.createdAtFa = nowFa;
}
update.$set.updatedAtFa = nowFa;
query.setUpdate(update);
}
/**
* Adds `createdAtFa` / `updatedAtFa` (Iran timezone, UTC+03:30 offset string)
* to all schemas that use mongoose timestamps.
*/
export function applyIranFaTimestampPlugin(connection: Connection): void {
connection.plugin((schema: Schema) => {
const timestamps = schema.get("timestamps");
if (!timestamps) return;
schema.add({
createdAtFa: { type: [String], index: true },
updatedAtFa: { type: [String], index: true },
});
schema.pre("save", function (next) {
const nowFa = toIranFaDateTimeTuple(new Date());
if (!(this as any).createdAtFa) {
(this as any).createdAtFa = nowFa;
}
(this as any).updatedAtFa = nowFa;
next();
});
schema.pre("insertMany", function (next, docs: any[]) {
const nowFa = toIranFaDateTimeTuple(new Date());
for (const doc of docs) {
if (!doc.createdAtFa) doc.createdAtFa = nowFa;
doc.updatedAtFa = nowFa;
}
next();
});
schema.pre("findOneAndUpdate", function () {
writeFaTimestampsOnUpdate(this);
});
schema.pre("updateOne", function () {
writeFaTimestampsOnUpdate(this);
});
schema.pre("updateMany", function () {
writeFaTimestampsOnUpdate(this);
});
});
}