fix mapping entity when relations are populated

This commit is contained in:
Marc-Roig 2023-02-14 09:11:10 +01:00
parent bbafe2b01d
commit 37ef6e7cd6

View File

@ -115,19 +115,21 @@ module.exports = ({ strapi }) => ({
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) };
const entity = await strapi.entityService.findOne(uid, id, params); return strapi.entityService
return this.mapEntity(entity, uid); .findOne(uid, id, params)
.then((entity) => this.mapEntity(entity, uid));
}, },
async findOne(id, uid) { async findOne(id, uid) {
const params = { populate: getDeepPopulate(uid) }; const params = { populate: getDeepPopulate(uid) };
const entity = await strapi.entityService.findOne(uid, id, params); return strapi.entityService
return this.mapEntity(entity, uid); .findOne(uid, id, params)
.then((entity) => this.mapEntity(entity, uid));
}, },
async findOneWithCreatorRoles(id, uid) { async findOneWithCreatorRoles(id, uid) {
const entity = await this.findOne(id, uid); const entity = await this.findOne(id, uid).then((entity) => this.mapEntity(entity, uid));
if (!entity) { if (!entity) {
return entity; return entity;
@ -152,7 +154,9 @@ module.exports = ({ strapi }) => ({
: getDeepPopulate(uid, { countMany: true, countOne: true }), : getDeepPopulate(uid, { countMany: true, countOne: true }),
}; };
const entity = await strapi.entityService.create(uid, params); const entity = await strapi.entityService
.create(uid, params)
.then((entity) => this.mapEntity(entity, uid));
// If relations were populated, relations count will be returned instead of the array of relations. // If relations were populated, relations count will be returned instead of the array of relations.
if (populateRelations) { if (populateRelations) {
@ -173,14 +177,16 @@ module.exports = ({ strapi }) => ({
: getDeepPopulate(uid, { countMany: true, countOne: true }), : getDeepPopulate(uid, { countMany: true, countOne: true }),
}; };
const updatedEntity = await strapi.entityService.update(uid, entity.id, params); const updatedEntity = await strapi.entityService
.update(uid, entity.id, params)
.then((entity) => this.mapEntity(entity, uid));
// If relations were populated, relations count will be returned instead of the array of relations. // If relations were populated, relations count will be returned instead of the array of relations.
if (populateRelations) { if (populateRelations) {
return getDeepRelationsCount(updatedEntity, uid); return getDeepRelationsCount(updatedEntity, uid);
} }
return this.mapEntity(updatedEntity, uid); return updatedEntity;
}, },
async delete(entity, uid) { async delete(entity, uid) {
@ -236,12 +242,14 @@ module.exports = ({ strapi }) => ({
await emitEvent(ENTRY_PUBLISH, entity, uid); await emitEvent(ENTRY_PUBLISH, entity, uid);
const mappedEntity = await this.mapEntity(updatedEntity, uid);
// If relations were populated, relations count will be returned instead of the array of relations. // If relations were populated, relations count will be returned instead of the array of relations.
if (isRelationsPopulateEnabled(uid)) { if (isRelationsPopulateEnabled(uid)) {
return getDeepRelationsCount(updatedEntity, uid); return getDeepRelationsCount(mappedEntity, uid);
} }
return this.mapEntity(updatedEntity, uid); return mappedEntity;
}, },
async unpublish(entity, body = {}, uid) { async unpublish(entity, body = {}, uid) {
@ -263,12 +271,14 @@ module.exports = ({ strapi }) => ({
await emitEvent(ENTRY_UNPUBLISH, entity, uid); await emitEvent(ENTRY_UNPUBLISH, entity, uid);
const mappedEntity = await this.mapEntity(updatedEntity, uid);
// If relations were populated, relations count will be returned instead of the array of relations. // If relations were populated, relations count will be returned instead of the array of relations.
if (isRelationsPopulateEnabled(uid)) { if (isRelationsPopulateEnabled(uid)) {
return getDeepRelationsCount(updatedEntity, uid); return getDeepRelationsCount(mappedEntity, uid);
} }
return this.mapEntity(updatedEntity, uid); return mappedEntity;
}, },
async getNumberOfDraftRelations(id, uid) { async getNumberOfDraftRelations(id, uid) {