From 5b0ccf64da3f46c89b9d261f5330ba1c5cee929f Mon Sep 17 00:00:00 2001 From: Jamie Howard Date: Mon, 13 Feb 2023 11:06:57 +0000 Subject: [PATCH] refactor(content-manager): add mapEntity to the entity manager --- .../server/services/entity-manager.js | 48 ++++++++++++++----- .../content-manager/entity-manager.js | 45 +++++------------ 2 files changed, 46 insertions(+), 47 deletions(-) diff --git a/packages/core/content-manager/server/services/entity-manager.js b/packages/core/content-manager/server/services/entity-manager.js index 55c1835eeb..f9168af576 100644 --- a/packages/core/content-manager/server/services/entity-manager.js +++ b/packages/core/content-manager/server/services/entity-manager.js @@ -2,6 +2,7 @@ const { assoc, has, prop, omit } = require('lodash/fp'); const strapiUtils = require('@strapi/utils'); +const { mapAsync } = require('@strapi/utils'); const { ApplicationError } = require('@strapi/utils').errors; const { getDeepPopulate, getDeepPopulateDraftCount } = require('./utils/populate'); const { getDeepRelationsCount } = require('./utils/count'); @@ -72,36 +73,57 @@ module.exports = ({ strapi }) => ({ return assoc(`${CREATED_BY_ATTRIBUTE}.roles`, roles, entity); }, - find(opts, uid) { + /** + * Extend this function from other plugins to add custom mapping of entity + * responses + * @param {Object} entity + * @returns + */ + mapEntity(entity) { + return entity; + }, + + async find(opts, uid) { const params = { ...opts, populate: getDeepPopulate(uid) }; - return strapi.entityService.findMany(uid, params); + const entities = await strapi.entityService.findMany(uid, params); + await mapAsync(entities.results, async (entity) => this.mapEntity(entity, uid)); + return entities; }, - findPage(opts, uid) { + async findPage(opts, uid) { const params = { ...opts, populate: getDeepPopulate(uid, { maxLevel: 1 }) }; - return strapi.entityService.findPage(uid, params); + const entities = await strapi.entityService.findPage(uid, params); + await mapAsync(entities.results, async (entity) => this.mapEntity(entity, uid)); + return entities; }, - findWithRelationCountsPage(opts, uid) { + async findWithRelationCountsPage(opts, uid) { const counterPopulate = getDeepPopulate(uid, { countMany: true, maxLevel: 1 }); const params = { ...opts, populate: addCreatedByRolesPopulate(counterPopulate) }; - return strapi.entityService.findWithRelationCountsPage(uid, params); + const entities = await strapi.entityService.findWithRelationCountsPage(uid, params); + await mapAsync(entities.results, async (entity) => this.mapEntity(entity, uid)); + + return entities; }, - findOneWithCreatorRolesAndCount(id, uid) { + async findOneWithCreatorRolesAndCount(id, uid) { const counterPopulate = getDeepPopulate(uid, { countMany: true, countOne: true }); const params = { populate: addCreatedByRolesPopulate(counterPopulate) }; - return strapi.entityService.findOne(uid, id, params); + const entities = await strapi.entityService.findOne(uid, id, params); + await mapAsync(entities.results, async (entity) => this.mapEntity(entity, uid)); + + return entities; }, async findOne(id, uid) { const params = { populate: getDeepPopulate(uid) }; - return strapi.entityService.findOne(uid, id, params); + const entity = await strapi.entityService.findOne(uid, id, params); + return this.mapEntity(entity, uid); }, async findOneWithCreatorRoles(id, uid) { @@ -137,7 +159,7 @@ module.exports = ({ strapi }) => ({ return getDeepRelationsCount(entity, uid); } - return entity; + return this.mapEntity(entity, uid); }, async update(entity, body, uid) { @@ -158,7 +180,7 @@ module.exports = ({ strapi }) => ({ return getDeepRelationsCount(updatedEntity, uid); } - return updatedEntity; + return this.mapEntity(updatedEntity, uid); }, async delete(entity, uid) { @@ -219,7 +241,7 @@ module.exports = ({ strapi }) => ({ return getDeepRelationsCount(updatedEntity, uid); } - return updatedEntity; + return this.mapEntity(updatedEntity, uid); }, async unpublish(entity, body = {}, uid) { @@ -246,7 +268,7 @@ module.exports = ({ strapi }) => ({ return getDeepRelationsCount(updatedEntity, uid); } - return updatedEntity; + return this.mapEntity(updatedEntity, uid); }, async getNumberOfDraftRelations(id, uid) { diff --git a/packages/core/upload/server/services/extensions/content-manager/entity-manager.js b/packages/core/upload/server/services/extensions/content-manager/entity-manager.js index b3fe85eced..5d7d6236c7 100644 --- a/packages/core/upload/server/services/extensions/content-manager/entity-manager.js +++ b/packages/core/upload/server/services/extensions/content-manager/entity-manager.js @@ -53,19 +53,6 @@ const signEntityMedia = async (entity, uid) => { return entity; }; -/** - * Sign media urls from entity manager results - * @param {Array|Object} result from the entity manager - * @param {string} uid of the model - */ -const fileSigningExtension = async (result, uid) => { - if (Array.isArray(result?.results)) { - await mapAsync(result.results, async (entity) => signEntityMedia(entity, uid)); - } else { - await signEntityMedia(result, uid); - } -}; - const addSignedFileUrlsToAdmin = () => { const { provider } = strapi.plugins.upload; @@ -82,7 +69,6 @@ const addSignedFileUrlsToAdmin = () => { // Test for every case in the Content manager so we don't miss any // Make entity file signing non mutating // Move this extend into a folder called /extensions - // Can we simplify the way to extend the content manager? // TOPICS: // What about the webhooks emitted by the entity manager? @@ -91,27 +77,18 @@ const addSignedFileUrlsToAdmin = () => { strapi.container .get('services') .extend('plugin::content-manager.entity-manager', (entityManager) => { - const functionsToExtend = [ - 'update', - 'publish', - 'unpublish', - 'findOneWithCreatorRolesAndCount', - 'findWithRelationCountsPage', - ]; + /** + * Map entity manager responses to sign private media URLs + * @param {Object} entity + * @param {string} uid + * @returns + */ + const mapEntity = async (entity, uid) => { + await signEntityMedia(entity, uid); + return entity; + }; - const extendedFunctions = {}; - functionsToExtend.reduce((acc, functionName) => { - acc[functionName] = async (...args) => { - const result = await entityManager[functionName](...args); - await fileSigningExtension(result, args.at(-1)); - - return result; - }; - - return acc; - }, extendedFunctions); - - return { ...entityManager, ...extendedFunctions }; + return { ...entityManager, mapEntity }; }); };