From 1e1edf91365148333e67c17d1ed8e0fff1e32ae1 Mon Sep 17 00:00:00 2001 From: SepehrYahyaee <7heycallmegray@gmail.com> Date: Wed, 22 Apr 2026 10:35:00 +0330 Subject: [PATCH] Fixing createdAtFa --- src/helpers/mongoose-fa-timestamps.plugin.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/helpers/mongoose-fa-timestamps.plugin.ts b/src/helpers/mongoose-fa-timestamps.plugin.ts index 4241cfc..3950f5e 100644 --- a/src/helpers/mongoose-fa-timestamps.plugin.ts +++ b/src/helpers/mongoose-fa-timestamps.plugin.ts @@ -38,6 +38,14 @@ function toIranFaDateTimeTuple(input: Date): [string, string] { return [dateFa, timeFa]; } +function isMissingFaTuple(value: unknown): boolean { + if (value == null) return true; + if (!Array.isArray(value)) return false; + if (value.length < 2) return true; + const [d, t] = value; + return !d || !t; +} + function writeFaTimestampsOnUpdate(query: Query): void { const nowFa = toIranFaDateTimeTuple(new Date()); const update = (query.getUpdate() ?? {}) as Record; @@ -54,7 +62,7 @@ function writeFaTimestampsOnUpdate(query: Query): void { if (!update.$setOnInsert || typeof update.$setOnInsert !== "object") { update.$setOnInsert = {}; } - if (update.$setOnInsert.createdAtFa == null) { + if (isMissingFaTuple(update.$setOnInsert.createdAtFa)) { update.$setOnInsert.createdAtFa = nowFa; } } @@ -78,7 +86,7 @@ export function applyIranFaTimestampPlugin(connection: Connection): void { schema.pre("save", function (next) { const nowFa = toIranFaDateTimeTuple(new Date()); - if (!(this as any).createdAtFa) { + if (isMissingFaTuple((this as any).createdAtFa)) { (this as any).createdAtFa = nowFa; } (this as any).updatedAtFa = nowFa; @@ -88,7 +96,7 @@ export function applyIranFaTimestampPlugin(connection: Connection): void { schema.pre("insertMany", function (next, docs: any[]) { const nowFa = toIranFaDateTimeTuple(new Date()); for (const doc of docs) { - if (!doc.createdAtFa) doc.createdAtFa = nowFa; + if (isMissingFaTuple(doc.createdAtFa)) doc.createdAtFa = nowFa; doc.updatedAtFa = nowFa; } next();