feat: Add Cloud Code triggers Parse.Cloud.beforeSave and Parse.Cloud.afterSave for Parse Config (#9232)

This commit is contained in:
Diamond Lewis
2024-07-20 13:35:41 -05:00
committed by GitHub
parent 4d86ace2cc
commit 90a1e4a200
5 changed files with 308 additions and 5 deletions

View File

@@ -2,6 +2,15 @@
import Parse from 'parse/node';
import PromiseRouter from '../PromiseRouter';
import * as middleware from '../middlewares';
import * as triggers from '../triggers';
const getConfigFromParams = params => {
const config = new Parse.Config();
for (const attr in params) {
config.attributes[attr] = Parse._decode(undefined, params[attr]);
}
return config;
};
export class GlobalConfigRouter extends PromiseRouter {
getGlobalConfig(req) {
@@ -30,7 +39,7 @@ export class GlobalConfigRouter extends PromiseRouter {
});
}
updateGlobalConfig(req) {
async updateGlobalConfig(req) {
if (req.auth.isReadOnly) {
throw new Parse.Error(
Parse.Error.OPERATION_FORBIDDEN,
@@ -45,9 +54,37 @@ export class GlobalConfigRouter extends PromiseRouter {
acc[`masterKeyOnly.${key}`] = masterKeyOnly[key] || false;
return acc;
}, {});
return req.config.database
.update('_GlobalConfig', { objectId: '1' }, update, { upsert: true }, true)
.then(() => ({ response: { result: true } }));
const className = triggers.getClassName(Parse.Config);
const hasBeforeSaveHook = triggers.triggerExists(className, triggers.Types.beforeSave, req.config.applicationId);
const hasAfterSaveHook = triggers.triggerExists(className, triggers.Types.afterSave, req.config.applicationId);
let originalConfigObject;
let updatedConfigObject;
const configObject = new Parse.Config();
configObject.attributes = params;
const results = await req.config.database.find('_GlobalConfig', { objectId: '1' }, { limit: 1 });
const isNew = results.length !== 1;
if (!isNew && (hasBeforeSaveHook || hasAfterSaveHook)) {
originalConfigObject = getConfigFromParams(results[0].params);
}
try {
await triggers.maybeRunGlobalConfigTrigger(triggers.Types.beforeSave, req.auth, configObject, originalConfigObject, req.config, req.context);
if (isNew) {
await req.config.database.update('_GlobalConfig', { objectId: '1' }, update, { upsert: true }, true)
updatedConfigObject = configObject;
} else {
const result = await req.config.database.update('_GlobalConfig', { objectId: '1' }, update, {}, true);
updatedConfigObject = getConfigFromParams(result.params);
}
await triggers.maybeRunGlobalConfigTrigger(triggers.Types.afterSave, req.auth, updatedConfigObject, originalConfigObject, req.config, req.context);
return { response: { result: true } }
} catch (err) {
const error = triggers.resolveError(err, {
code: Parse.Error.SCRIPT_FAILED,
message: 'Script failed. Unknown error.',
});
throw error;
}
}
mountRoutes() {

View File

@@ -79,10 +79,14 @@ const getRoute = parseClass => {
_User: 'users',
_Session: 'sessions',
'@File': 'files',
'@Config' : 'config',
}[parseClass] || 'classes';
if (parseClass === '@File') {
return `/${route}/:id?(.*)`;
}
if (parseClass === '@Config') {
return `/${route}`;
}
return `/${route}/${parseClass}/:id?(.*)`;
};
/** @namespace

View File

@@ -1027,3 +1027,38 @@ export async function maybeRunFileTrigger(triggerType, fileObject, config, auth)
}
return fileObject;
}
export async function maybeRunGlobalConfigTrigger(triggerType, auth, configObject, originalConfigObject, config, context) {
const GlobalConfigClassName = getClassName(Parse.Config);
const configTrigger = getTrigger(GlobalConfigClassName, triggerType, config.applicationId);
if (typeof configTrigger === 'function') {
try {
const request = getRequestObject(triggerType, auth, configObject, originalConfigObject, config, context);
await maybeRunValidator(request, `${triggerType}.${GlobalConfigClassName}`, auth);
if (request.skipWithMasterKey) {
return configObject;
}
const result = await configTrigger(request);
logTriggerSuccessBeforeHook(
triggerType,
'Parse.Config',
configObject,
result,
auth,
config.logLevels.triggerBeforeSuccess
);
return result || configObject;
} catch (error) {
logTriggerErrorBeforeHook(
triggerType,
'Parse.Config',
configObject,
auth,
error,
config.logLevels.triggerBeforeError
);
throw error;
}
}
return configObject;
}