Skip to content

Commit

Permalink
cleanup query triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
dblythy committed Dec 24, 2024
1 parent f8c5e28 commit c43a128
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 102 deletions.
149 changes: 50 additions & 99 deletions src/Triggers/QueryTrigger.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,72 @@
import { getTrigger, Types } from "./TriggerStore";
import { getTrigger } from "./TriggerStore";
import { getRequestObject } from './Trigger';
import { resolveError, toJSONwithObjects } from "./Utils";
import { resolveError, toJSONwithObjects, logTriggerErrorHook } from "./Utils";
import { maybeRunValidator } from "./Validator";
import { logTriggerAfterHook, logTriggerSuccessBeforeHook } from "./Logger";

function getResponseObject(request, resolve, reject) {
return {
success: function (response) {
if (request.triggerName === Types.afterFind) {
if (!response) {
response = request.objects;
}
response = response.map(object => {
return toJSONwithObjects(object);
});
return resolve(response);
}
// Use the JSON response
if (
response &&
typeof response === 'object' &&
!request.object.equals(response) &&
request.triggerName === Types.beforeSave
) {
return resolve(response);
}
if (response && typeof response === 'object' && request.triggerName === Types.afterSave) {
return resolve(response);
}
if (request.triggerName === Types.afterSave) {
return resolve();
}
response = {};
if (request.triggerName === Types.beforeSave) {
response['object'] = request.object._getSaveJSON();
response['object']['objectId'] = request.object.id;
}
return resolve(response);
},
error: function (error) {
const e = resolveError(error, {
code: Parse.Error.SCRIPT_FAILED,
message: 'Script failed. Unknown error.',
});
reject(e);
},
};
}

export function maybeRunAfterFindTrigger(
export const maybeRunAfterFindTrigger = async (
triggerType,
auth,
className,
objects,
config,
query,
context
) {
return new Promise((resolve, reject) => {
const trigger = getTrigger(className, triggerType, config.applicationId);
if (!trigger) {
return resolve();
}
const request = getRequestObject(triggerType, auth, null, null, config, context);
if (query) {
request.query = query;
}
const { success, error } = getResponseObject(
request,
object => {
resolve(object);
},
error => {
reject(error);
}
);
logTriggerSuccessBeforeHook(
) => {
const trigger = getTrigger(className, triggerType, config.applicationId);
if (!trigger) {
return;
}

const request = getRequestObject(triggerType, auth, null, null, config, context);
if (query) {
request.query = query;
}

request.objects = objects.map((object) => {
object.className = className;
return Parse.Object.fromJSON(object);
});

logTriggerSuccessBeforeHook(
triggerType,
className,
'AfterFind',
JSON.stringify(objects),
auth,
config.logLevels.triggerBeforeSuccess
);

try {
await maybeRunValidator(request, `${triggerType}.${className}`, auth);

if (request.skipWithMasterKey) {
return request.objects;
}

const response = await trigger(request);
const results = await Promise.resolve(response);

logTriggerAfterHook(
triggerType,
className,
'AfterFind',
JSON.stringify(objects),
JSON.stringify(results),
auth,
config.logLevels.triggerBeforeSuccess
config.logLevels.triggerAfter
);
request.objects = objects.map(object => {
//setting the class name to transform into parse object
object.className = className;
return Parse.Object.fromJSON(object);
});
return Promise.resolve()
.then(() => {
return maybeRunValidator(request, `${triggerType}.${className}`, auth);
})
.then(() => {
if (request.skipWithMasterKey) {
return request.objects;
}
const response = trigger(request);
if (response && typeof response.then === 'function') {
return response.then(results => {
return results;
});
}
return response;
})
.then(success, error);
}).then(results => {
logTriggerAfterHook(

return results.map(toJSONwithObjects)
} catch (error) {
logTriggerErrorHook(
triggerType,
className,
JSON.stringify(results),
error,
auth,
config.logLevels.triggerAfter
config.logLevels.triggerError
);
return results;
});
}
throw error;
}
};

export async function maybeRunQueryTrigger(
triggerType,
Expand Down
2 changes: 1 addition & 1 deletion src/Triggers/TriggerStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function getJob(jobName, applicationId) {
}

export function getJobs(applicationId) {
var manager = _triggerStore[applicationId];
const manager = _triggerStore[applicationId];
if (manager && manager.Jobs) {
return manager.Jobs;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Triggers/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export function getClassName(parseClass) {
}

export function inflate(data, restObject) {
var copy = typeof data == 'object' ? data : { className: data };
for (var key in restObject) {
const copy = typeof data == 'object' ? data : { className: data };
for (const key in restObject) {
copy[key] = restObject[key];
}
return Parse.Object.fromJSON(copy);
Expand Down

0 comments on commit c43a128

Please sign in to comment.