forked from Yara724/api
Merge pull request 'Fixing createdAtFa' (#34) from s.yahyaee/yara724-api:main into main
Reviewed-on: Yara724/api#34
This commit is contained in:
@@ -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<any, any>): void {
|
||||
const nowFa = toIranFaDateTimeTuple(new Date());
|
||||
const update = (query.getUpdate() ?? {}) as Record<string, any>;
|
||||
@@ -54,7 +62,7 @@ function writeFaTimestampsOnUpdate(query: Query<any, any>): 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();
|
||||
|
||||
Reference in New Issue
Block a user