From f8170f1a509f1e238954a407826c31deacec963d Mon Sep 17 00:00:00 2001 From: Leonardo Viva Date: Fri, 14 Jun 2024 16:48:10 -0300 Subject: [PATCH] feat: script after save async --- src/imports/auth/login/index.ts | 3 +++ src/imports/data/scripts.js | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/imports/auth/login/index.ts b/src/imports/auth/login/index.ts index 9218fef..6d82bf8 100644 --- a/src/imports/auth/login/index.ts +++ b/src/imports/auth/login/index.ts @@ -132,6 +132,7 @@ export async function login({ ip, user, password, password_SHA256, geolocation, $set: { lastLogin: new Date(), }, + // @ts-ignore-next-line $push: { 'services.resume.loginTokens': hashStampedToken, }, @@ -172,6 +173,7 @@ export const cleanupSessions = async (userId: ObjectId, all = false) => { await MetaObject.Collections.User.updateOne( { _id: userId }, { + // @ts-ignore-next-line $pull: { 'services.resume.loginTokens': { $or: [{ when: { $exists: true } }], @@ -184,6 +186,7 @@ export const cleanupSessions = async (userId: ObjectId, all = false) => { await MetaObject.Collections.User.updateOne( { _id: userId, $or: [{ 'services.resume.loginTokens.when': { $lt: oldestValidDate } }, { 'services.resume.loginTokens.when': { $lt: +oldestValidDate } }] }, { + // @ts-ignore-next-line $pull: { 'services.resume.loginTokens': { $or: [{ when: { $lt: oldestValidDate } }, { when: { $lt: +oldestValidDate } }], diff --git a/src/imports/data/scripts.js b/src/imports/data/scripts.js index d1155d5..9b7a105 100644 --- a/src/imports/data/scripts.js +++ b/src/imports/data/scripts.js @@ -138,10 +138,14 @@ export async function runScriptAfterSave({ script, data, user, extraData = {} }) }; const sandbox = createContext(contextData); - const scriptToRun = `result = (function(data, user, console, Models, extraData) { ${script} })(data, user, console, Models, extraData);`; + const scriptToRun = `result = (async function(data, user, console, Models, extraData) { ${script} })(data, user, console, Models, extraData);`; await runInContext(scriptToRun, sandbox); if (sandbox.result != null && isObject(sandbox.result)) { + if (sandbox.result.then != null) { + const result = await sandbox.result; + return result || {}; + } return sandbox.result; } else { return {};