mirror of
https://github.com/strapi/strapi.git
synced 2025-10-29 17:04:13 +00:00
refactor(content-manager): add mapEntity to the entity manager
This commit is contained in:
parent
cd4ef1a92b
commit
5b0ccf64da
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const { assoc, has, prop, omit } = require('lodash/fp');
|
const { assoc, has, prop, omit } = require('lodash/fp');
|
||||||
const strapiUtils = require('@strapi/utils');
|
const strapiUtils = require('@strapi/utils');
|
||||||
|
const { mapAsync } = require('@strapi/utils');
|
||||||
const { ApplicationError } = require('@strapi/utils').errors;
|
const { ApplicationError } = require('@strapi/utils').errors;
|
||||||
const { getDeepPopulate, getDeepPopulateDraftCount } = require('./utils/populate');
|
const { getDeepPopulate, getDeepPopulateDraftCount } = require('./utils/populate');
|
||||||
const { getDeepRelationsCount } = require('./utils/count');
|
const { getDeepRelationsCount } = require('./utils/count');
|
||||||
@ -72,36 +73,57 @@ module.exports = ({ strapi }) => ({
|
|||||||
return assoc(`${CREATED_BY_ATTRIBUTE}.roles`, roles, entity);
|
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) };
|
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 }) };
|
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 counterPopulate = getDeepPopulate(uid, { countMany: true, maxLevel: 1 });
|
||||||
const params = { ...opts, populate: addCreatedByRolesPopulate(counterPopulate) };
|
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 counterPopulate = getDeepPopulate(uid, { countMany: true, countOne: true });
|
||||||
const params = { populate: addCreatedByRolesPopulate(counterPopulate) };
|
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) {
|
async findOne(id, uid) {
|
||||||
const params = { populate: getDeepPopulate(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) {
|
async findOneWithCreatorRoles(id, uid) {
|
||||||
@ -137,7 +159,7 @@ module.exports = ({ strapi }) => ({
|
|||||||
return getDeepRelationsCount(entity, uid);
|
return getDeepRelationsCount(entity, uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
return entity;
|
return this.mapEntity(entity, uid);
|
||||||
},
|
},
|
||||||
|
|
||||||
async update(entity, body, uid) {
|
async update(entity, body, uid) {
|
||||||
@ -158,7 +180,7 @@ module.exports = ({ strapi }) => ({
|
|||||||
return getDeepRelationsCount(updatedEntity, uid);
|
return getDeepRelationsCount(updatedEntity, uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
return updatedEntity;
|
return this.mapEntity(updatedEntity, uid);
|
||||||
},
|
},
|
||||||
|
|
||||||
async delete(entity, uid) {
|
async delete(entity, uid) {
|
||||||
@ -219,7 +241,7 @@ module.exports = ({ strapi }) => ({
|
|||||||
return getDeepRelationsCount(updatedEntity, uid);
|
return getDeepRelationsCount(updatedEntity, uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
return updatedEntity;
|
return this.mapEntity(updatedEntity, uid);
|
||||||
},
|
},
|
||||||
|
|
||||||
async unpublish(entity, body = {}, uid) {
|
async unpublish(entity, body = {}, uid) {
|
||||||
@ -246,7 +268,7 @@ module.exports = ({ strapi }) => ({
|
|||||||
return getDeepRelationsCount(updatedEntity, uid);
|
return getDeepRelationsCount(updatedEntity, uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
return updatedEntity;
|
return this.mapEntity(updatedEntity, uid);
|
||||||
},
|
},
|
||||||
|
|
||||||
async getNumberOfDraftRelations(id, uid) {
|
async getNumberOfDraftRelations(id, uid) {
|
||||||
|
|||||||
@ -53,19 +53,6 @@ const signEntityMedia = async (entity, uid) => {
|
|||||||
return entity;
|
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 addSignedFileUrlsToAdmin = () => {
|
||||||
const { provider } = strapi.plugins.upload;
|
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
|
// Test for every case in the Content manager so we don't miss any
|
||||||
// Make entity file signing non mutating
|
// Make entity file signing non mutating
|
||||||
// Move this extend into a folder called /extensions
|
// Move this extend into a folder called /extensions
|
||||||
// Can we simplify the way to extend the content manager?
|
|
||||||
|
|
||||||
// TOPICS:
|
// TOPICS:
|
||||||
// What about the webhooks emitted by the entity manager?
|
// What about the webhooks emitted by the entity manager?
|
||||||
@ -91,27 +77,18 @@ const addSignedFileUrlsToAdmin = () => {
|
|||||||
strapi.container
|
strapi.container
|
||||||
.get('services')
|
.get('services')
|
||||||
.extend('plugin::content-manager.entity-manager', (entityManager) => {
|
.extend('plugin::content-manager.entity-manager', (entityManager) => {
|
||||||
const functionsToExtend = [
|
/**
|
||||||
'update',
|
* Map entity manager responses to sign private media URLs
|
||||||
'publish',
|
* @param {Object} entity
|
||||||
'unpublish',
|
* @param {string} uid
|
||||||
'findOneWithCreatorRolesAndCount',
|
* @returns
|
||||||
'findWithRelationCountsPage',
|
*/
|
||||||
];
|
const mapEntity = async (entity, uid) => {
|
||||||
|
await signEntityMedia(entity, uid);
|
||||||
|
return entity;
|
||||||
|
};
|
||||||
|
|
||||||
const extendedFunctions = {};
|
return { ...entityManager, mapEntity };
|
||||||
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 };
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user