diff --git a/models/customFields.js b/models/customFields.js index 280f87749..3996a3843 100644 --- a/models/customFields.js +++ b/models/customFields.js @@ -370,6 +370,180 @@ if (Meteor.isServer) { }); }); + /** + * @operation edit_custom_field + * @summary Update a Custom Field + * + * @param {string} name the name of the custom field + * @param {string} type the type of the custom field + * @param {string} settings the settings object of the custom field + * @param {boolean} showOnCard should we show the custom field on cards? + * @param {boolean} automaticallyOnCard should the custom fields automatically be added on cards? + * @param {boolean} showLabelOnMiniCard should the label of the custom field be shown on minicards? + * @return_type {_id: string} + */ + JsonRoutes.add( + 'PUT', + '/api/boards/:boardId/custom-fields/:customFieldId', + (req, res) => { + Authentication.checkUserId(req.userId); + + const paramFieldId = req.params.customFieldId; + + if (req.body.hasOwnProperty('name')) { + CustomFields.direct.update( + { _id: paramFieldId }, + { $set: { name: req.body.name } }, + ); + } + if (req.body.hasOwnProperty('type')) { + CustomFields.direct.update( + { _id: paramFieldId }, + { $set: { type: req.body.type } }, + ); + } + if (req.body.hasOwnProperty('settings')) { + CustomFields.direct.update( + { _id: paramFieldId }, + { $set: { settings: req.body.settings } }, + ); + } + if (req.body.hasOwnProperty('showOnCard')) { + CustomFields.direct.update( + { _id: paramFieldId }, + { $set: { showOnCard: req.body.showOnCard } }, + ); + } + if (req.body.hasOwnProperty('automaticallyOnCard')) { + CustomFields.direct.update( + { _id: paramFieldId }, + { $set: { automaticallyOnCard: req.body.automaticallyOnCard } }, + ); + } + if (req.body.hasOwnProperty('alwaysOnCard')) { + CustomFields.direct.update( + { _id: paramFieldId }, + { $set: { alwaysOnCard: req.body.alwaysOnCard } }, + ); + } + if (req.body.hasOwnProperty('showLabelOnMiniCard')) { + CustomFields.direct.update( + { _id: paramFieldId }, + { $set: { showLabelOnMiniCard: req.body.showLabelOnMiniCard } }, + ); + } + + JsonRoutes.sendResult(res, { + code: 200, + data: { _id: paramFieldId }, + }); + }, + ); + + /** + * @operation add_custom_field_dropdown_items + * @summary Update a Custom Field's dropdown items + * + * @param {string[]} items names of the custom field + * @return_type {_id: string} + */ + JsonRoutes.add( + 'POST', + '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items', + (req, res) => { + Authentication.checkUserId(req.userId); + + if (req.body.hasOwnProperty('items') && Array.isArray(req.body.items)) { + CustomFields.direct.update( + { _id: req.params.customFieldId }, + { + $push: { + 'settings.dropdownItems': { + $each: req.body.items + .filter(name => typeof name === 'string') + .map(name => ({ + _id: Random.id(6), + name, + })), + }, + }, + }, + ); + } + + JsonRoutes.sendResult(res, { + code: 200, + data: { _id: req.params.customFieldId }, + }); + }, + ); + + /** + * @operation edit_custom_field_dropdown_item + * @summary Update a Custom Field's dropdown item + * + * @param {string} name names of the custom field + * @return_type {_id: string} + */ + JsonRoutes.add( + 'PUT', + '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId', + (req, res) => { + Authentication.checkUserId(req.userId); + + if (req.body.hasOwnProperty('name')) { + CustomFields.direct.update( + { + _id: req.params.customFieldId, + 'settings.dropdownItems._id': req.params.dropdownItemId, + }, + { + $set: { + 'settings.dropdownItems.$': { + _id: req.params.dropdownItemId, + name: req.body.name, + }, + }, + }, + ); + } + + JsonRoutes.sendResult(res, { + code: 200, + data: { _id: req.params.customFieldId }, + }); + }, + ); + + /** + * @operation delete_custom_field_dropdown_item + * @summary Update a Custom Field's dropdown items + * + * @param {string} itemId ID of the dropdown item + * @return_type {_id: string} + */ + JsonRoutes.add( + 'DELETE', + '/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId', + (req, res) => { + Authentication.checkUserId(req.userId); + + CustomFields.direct.update( + { _id: req.params.customFieldId }, + { + $pull: { + 'settings.dropdownItems': { _id: req.params.dropdownItemId }, + }, + }, + ); + + JsonRoutes.sendResult(res, { + code: 200, + data: { _id: req.params.customFieldId }, + }); + }, + ); + /** * @operation delete_custom_field * @summary Delete a Custom Fields attached to a board