Skip to content

Commit

Permalink
feat: script after save async
Browse files Browse the repository at this point in the history
  • Loading branch information
7sete7 committed Jun 14, 2024
1 parent f105a36 commit f8170f1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/imports/auth/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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 } }],
Expand All @@ -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 } }],
Expand Down
6 changes: 5 additions & 1 deletion src/imports/data/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down

0 comments on commit f8170f1

Please sign in to comment.