/**
* ### Actions controller
*
* This controller is in charge of handling all publicly exposed actions logic.
*
* @module API/Controllers/Actions
*/
"use strict";
const path = require("path");
const {
get
} = require("lodash/fp");
const BPromise = require("bluebird");
const utils = require(path.resolve(__dirname, "../../lib/utils"));
const logAndReject = utils.logAndReject;
const sendBack = utils.sendBack;
const controllerFactory = require("./factory");
const modelName = "action";
const actions = controllerFactory.createController(modelName);
// Warning: models are loaded during startup, so they are not available right
// away. So we're just keeping a reference here and access models a bit later in
// the controller methods
const models = require(utils.getModulePath("models"));
module.exports = Object.assign(actions, {
/**
* Perform an action on an entity
* @param {Object} req - Request reference
* @param {Object} res - Response reference
* @return {Promise<Mixed>} Action result
*/
performAction: (req, res) => BPromise
.try(() => models[modelName].performAction(
get("swagger.params.id.value", req),
get("body", req)
))
.then(sendBack(res, 200))
.catch(logAndReject(
"error",
`An error happend while performing action: ${get("swagger.params.id.value ", req)}`
))
});