forked from Yara724/api
YARA-814
This commit is contained in:
98
src/helpers/mongoose-fa-timestamps.plugin.ts
Normal file
98
src/helpers/mongoose-fa-timestamps.plugin.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user