{"version":3,"file":"assignees.mjs","sources":["../../../server/src/controllers/assignees.ts"],"sourcesContent":["import type { Context } from 'koa';\n\nimport { getService } from '../utils';\nimport { validateUpdateAssigneeOnEntity, validateLocale } from '../validation/review-workflows';\nimport { ENTITY_STAGE_ATTRIBUTE, STAGE_TRANSITION_UID } from '../constants/workflows';\n\nexport default {\n  /**\n   * Updates an entity's assignee.\n   * @async\n   * @param {Object} ctx - The Koa context object.\n   * @param {Object} ctx.params - An object containing the parameters from the request URL.\n   * @param {string} ctx.params.model_uid - The model UID of the entity.\n   * @param {string} ctx.params.id - The ID of the entity to update.\n   * @param {Object} ctx.request.body.data - Optional data object containing the new assignee ID for the entity.\n   * @param {string} ctx.request.body.data.id - The ID of the new assignee for the entity.\n   * @throws {ApplicationError} If review workflows is not activated on the specified model UID.\n   * @throws {ValidationError} If the `data` object in the request body fails to pass validation.\n   * @returns {Promise<void>} A promise that resolves when the entity's assignee has been updated.\n   */\n  async updateEntity(ctx: Context) {\n    const assigneeService = getService('assignees');\n    const workflowService = getService('workflows');\n    const stagePermissions = getService('stage-permissions');\n\n    const { model_uid: model, id: documentId } = ctx.params;\n    const locale = (await validateLocale(ctx.request.query?.locale)) ?? undefined;\n\n    const { sanitizeOutput } = strapi\n      .plugin('content-manager')\n      .service('permission-checker')\n      .create({ userAbility: ctx.state.userAbility, model });\n\n    // Retrieve the entity so we can get its current stage\n    const entity = await strapi.documents(model).findOne({\n      documentId,\n      locale,\n      populate: [ENTITY_STAGE_ATTRIBUTE],\n    });\n\n    if (!entity) {\n      ctx.throw(404, 'Entity not found');\n    }\n\n    // Only allow users who can update the current stage to change the assignee\n    const canTransitionStage = stagePermissions.can(\n      STAGE_TRANSITION_UID,\n      entity[ENTITY_STAGE_ATTRIBUTE]?.id\n    );\n\n    if (!canTransitionStage) {\n      ctx.throw(403, 'Stage transition permission is required');\n    }\n\n    // TODO: check if user has update permission on the entity\n    const { id: assigneeId } = await validateUpdateAssigneeOnEntity(\n      ctx.request?.body?.data,\n      'You should pass a valid id to the body of the put request.'\n    );\n\n    await workflowService.assertContentTypeBelongsToWorkflow(model);\n\n    const updatedEntity = await assigneeService.updateEntityAssignee(entity, model, assigneeId);\n\n    ctx.body = { data: await sanitizeOutput(updatedEntity) };\n  },\n};\n"],"names":["updateEntity","ctx","assigneeService","getService","workflowService","stagePermissions","model_uid","model","id","documentId","params","locale","validateLocale","request","query","undefined","sanitizeOutput","strapi","plugin","service","create","userAbility","state","entity","documents","findOne","populate","ENTITY_STAGE_ATTRIBUTE","throw","canTransitionStage","can","STAGE_TRANSITION_UID","assigneeId","validateUpdateAssigneeOnEntity","body","data","assertContentTypeBelongsToWorkflow","updatedEntity","updateEntityAssignee"],"mappings":";;;;AAMA,gBAAe;AACb;;;;;;;;;;;;MAaA,MAAMA,cAAaC,GAAY,EAAA;AAC7B,QAAA,MAAMC,kBAAkBC,UAAAA,CAAW,WAAA,CAAA;AACnC,QAAA,MAAMC,kBAAkBD,UAAAA,CAAW,WAAA,CAAA;AACnC,QAAA,MAAME,mBAAmBF,UAAAA,CAAW,mBAAA,CAAA;QAEpC,MAAM,EAAEG,WAAWC,KAAK,EAAEC,IAAIC,UAAU,EAAE,GAAGR,GAAAA,CAAIS,MAAM;QACvD,MAAMC,MAAAA,GAAS,MAAOC,cAAAA,CAAeX,IAAIY,OAAO,CAACC,KAAK,EAAEH,MAAAA,CAAAA,IAAYI,SAAAA;AAEpE,QAAA,MAAM,EAAEC,cAAc,EAAE,GAAGC,MAAAA,CACxBC,MAAM,CAAC,iBAAA,CAAA,CACPC,OAAO,CAAC,oBAAA,CAAA,CACRC,MAAM,CAAC;YAAEC,WAAAA,EAAapB,GAAAA,CAAIqB,KAAK,CAACD,WAAW;AAAEd,YAAAA;AAAM,SAAA,CAAA;;AAGtD,QAAA,MAAMgB,SAAS,MAAMN,MAAAA,CAAOO,SAAS,CAACjB,KAAAA,CAAAA,CAAOkB,OAAO,CAAC;AACnDhB,YAAAA,UAAAA;AACAE,YAAAA,MAAA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