Merge branch 'pull/637' into integration-637
# Conflicts: # client/src/lists/forms/CUD.js
This commit is contained in:
commit
6eeef7a991
7 changed files with 126 additions and 54 deletions
|
@ -175,6 +175,31 @@ async function updateWithConsistencyCheck(context, entity) {
|
|||
});
|
||||
}
|
||||
|
||||
async function copy(context, entity, formId) {
|
||||
return await knex.transaction(async tx => {
|
||||
await shares.enforceEntityPermissionTx(tx, context, 'namespace', entity.namespace, 'createCustomForm');
|
||||
const existing = await _getById(tx, formId);
|
||||
await namespaceHelpers.validateEntity(tx, entity);
|
||||
|
||||
const form = filterObject(existing, allowedFormKeys);
|
||||
enforce(!Object.keys(checkForMjmlErrors(form)).length, 'Error(s) in form templates');
|
||||
|
||||
const ids = await tx('custom_forms').insert(filterObject(entity, formAllowedKeys));
|
||||
const id = ids[0];
|
||||
|
||||
for (const formKey in form) {
|
||||
await tx('custom_forms_data').insert({
|
||||
form: id,
|
||||
data_key: formKey,
|
||||
data_value: form[formKey]
|
||||
})
|
||||
}
|
||||
|
||||
await shares.rebuildPermissionsTx(tx, { entityTypeId: 'customForm', entityId: id });
|
||||
return id;
|
||||
});
|
||||
}
|
||||
|
||||
async function remove(context, id) {
|
||||
await knex.transaction(async tx => {
|
||||
await shares.enforceEntityPermissionTx(tx, context, 'customForm', id, 'delete');
|
||||
|
@ -285,6 +310,7 @@ module.exports.hash = hash;
|
|||
module.exports.getById = getById;
|
||||
module.exports.create = create;
|
||||
module.exports.updateWithConsistencyCheck = updateWithConsistencyCheck;
|
||||
module.exports.copy = copy;
|
||||
module.exports.remove = remove;
|
||||
module.exports.getDefaultCustomFormValues = getDefaultCustomFormValues;
|
||||
module.exports.serverValidate = serverValidate;
|
||||
|
|
|
@ -26,6 +26,12 @@ router.postAsync('/forms', passport.loggedIn, passport.csrfProtection, async (re
|
|||
return res.json(await forms.create(req.context, req.body));
|
||||
});
|
||||
|
||||
router.postAsync('/forms/:formId', passport.loggedIn, passport.csrfProtection, async (req, res) => {
|
||||
const entity = req.body;
|
||||
const formId= castToInteger(req.params.formId);
|
||||
return res.json(await forms.copy(req.context, entity, formId));
|
||||
});
|
||||
|
||||
router.putAsync('/forms/:formId', passport.loggedIn, passport.csrfProtection, async (req, res) => {
|
||||
const entity = req.body;
|
||||
entity.id = castToInteger(req.params.formId);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue