197 lines
5.3 KiB
JavaScript
Raw Normal View History

'use strict';
2022-10-05 18:42:50 +02:00
const { assoc, has, prop, omit } = require('lodash/fp');
2021-04-29 13:51:12 +02:00
const strapiUtils = require('@strapi/utils');
2021-10-20 17:30:05 +02:00
const { ApplicationError } = require('@strapi/utils').errors;
2022-10-05 18:42:50 +02:00
const { getDeepPopulate, getDeepPopulateDraftCount } = require('./utils/populate');
const { sumDraftCounts } = require('./utils/draft');
2022-10-05 18:42:50 +02:00
const { hasDraftAndPublish } = strapiUtils.contentTypes;
const { PUBLISHED_AT_ATTRIBUTE, CREATED_BY_ATTRIBUTE } = strapiUtils.contentTypes.constants;
const { ENTRY_PUBLISH, ENTRY_UNPUBLISH } = strapiUtils.webhook.webhookEvents;
const omitPublishedAtField = omit(PUBLISHED_AT_ATTRIBUTE);
const wrapWithEmitEvent = (event, fn) => async (entity, body, model) => {
const result = await fn(entity, body, model);
const modelDef = strapi.getModel(model);
2021-11-10 17:08:54 +01:00
const sanitizedEntity = await strapiUtils.sanitize.sanitizers.defaultSanitizeOutput(
modelDef,
entity
);
strapi.eventHub.emit(event, {
model: modelDef.modelName,
entry: sanitizedEntity,
});
return result;
};
2022-08-08 23:33:39 +02:00
const findCreatorRoles = (entity) => {
const createdByPath = `${CREATED_BY_ATTRIBUTE}.id`;
if (has(createdByPath, entity)) {
const creatorId = prop(createdByPath, entity);
return strapi.query('admin::role').findMany({ where: { users: { id: creatorId } } });
}
return [];
};
2022-08-08 23:33:39 +02:00
const addCreatedByRolesPopulate = (populate) => {
2021-09-24 15:40:02 +02:00
return {
...populate,
createdBy: {
populate: ['roles'],
},
};
};
/**
* @type {import('./entity-manager').default}
*/
2021-07-13 18:46:36 +02:00
module.exports = ({ strapi }) => ({
async assocCreatorRoles(entity) {
if (!entity) {
return entity;
}
const roles = await findCreatorRoles(entity);
return assoc(`${CREATED_BY_ATTRIBUTE}.roles`, roles, entity);
},
2021-06-30 20:00:03 +02:00
find(opts, uid, populate) {
2021-07-28 15:32:21 +02:00
const params = { ...opts, populate: getDeepPopulate(uid, populate) };
2021-06-30 20:00:03 +02:00
return strapi.entityService.findMany(uid, params);
},
2021-06-30 20:00:03 +02:00
findPage(opts, uid, populate) {
2022-08-26 10:41:31 +02:00
const params = { ...opts, populate: getDeepPopulate(uid, populate, { maxLevel: 1 }) };
2021-06-30 20:00:03 +02:00
return strapi.entityService.findPage(uid, params);
},
2022-05-19 14:47:23 +02:00
findWithRelationCountsPage(opts, uid, populate) {
2022-08-26 10:41:31 +02:00
const counterPopulate = getDeepPopulate(uid, populate, { countMany: true, maxLevel: 1 });
const params = { ...opts, populate: addCreatedByRolesPopulate(counterPopulate) };
2021-06-30 20:00:03 +02:00
2022-05-19 14:47:23 +02:00
return strapi.entityService.findWithRelationCountsPage(uid, params);
2020-12-16 15:28:11 +01:00
},
2022-08-26 10:41:31 +02:00
findOneWithCreatorRolesAndCount(id, uid, populate) {
const counterPopulate = getDeepPopulate(uid, populate, { countMany: true, countOne: true });
2022-08-26 10:41:31 +02:00
const params = { populate: addCreatedByRolesPopulate(counterPopulate) };
return strapi.entityService.findOne(uid, id, params);
},
2021-06-30 20:00:03 +02:00
async findOne(id, uid, populate) {
2021-07-30 20:45:51 +02:00
const params = { populate: getDeepPopulate(uid, populate) };
2021-07-08 18:15:32 +02:00
return strapi.entityService.findOne(uid, id, params);
},
2021-06-30 20:00:03 +02:00
async findOneWithCreatorRoles(id, uid, populate) {
const entity = await this.findOne(id, uid, populate);
if (!entity) {
return entity;
}
return this.assocCreatorRoles(entity);
},
2021-06-30 20:00:03 +02:00
async create(body, uid) {
const modelDef = strapi.getModel(uid);
const publishData = { ...body };
if (hasDraftAndPublish(modelDef)) {
publishData[PUBLISHED_AT_ATTRIBUTE] = null;
}
const params = {
data: publishData,
populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }),
};
2021-07-08 18:15:32 +02:00
return strapi.entityService.create(uid, params);
},
2021-06-30 20:00:03 +02:00
update(entity, body, uid) {
const publishData = omitPublishedAtField(body);
const params = {
data: publishData,
populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }),
};
2021-07-08 18:15:32 +02:00
return strapi.entityService.update(uid, entity.id, params);
},
2021-06-30 20:00:03 +02:00
delete(entity, uid) {
const params = { populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }) };
2021-07-08 18:15:32 +02:00
return strapi.entityService.delete(uid, entity.id, params);
},
2021-07-08 18:15:32 +02:00
// FIXME: handle relations
2021-07-05 23:31:23 +02:00
deleteMany(opts, uid) {
2021-06-30 20:00:03 +02:00
const params = { ...opts };
return strapi.entityService.deleteMany(uid, params);
},
publish: wrapWithEmitEvent(ENTRY_PUBLISH, async (entity, body = {}, uid) => {
if (entity[PUBLISHED_AT_ATTRIBUTE]) {
2021-10-20 17:30:05 +02:00
throw new ApplicationError('already.published');
}
// validate the entity is valid for publication
await strapi.entityValidator.validateEntityCreation(
strapi.getModel(uid),
entity,
undefined,
entity
);
const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: new Date() };
const params = {
data,
populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }),
};
2021-07-08 18:15:32 +02:00
return strapi.entityService.update(uid, entity.id, params);
}),
unpublish: wrapWithEmitEvent(ENTRY_UNPUBLISH, (entity, body = {}, uid) => {
if (!entity[PUBLISHED_AT_ATTRIBUTE]) {
2021-10-20 17:30:05 +02:00
throw new ApplicationError('already.draft');
}
const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: null };
const params = {
data,
populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }),
};
2021-07-08 18:15:32 +02:00
return strapi.entityService.update(uid, entity.id, params);
}),
2022-10-05 18:42:50 +02:00
async getNumberOfDraftRelations(id, uid) {
const { populate, hasRelations } = getDeepPopulateDraftCount(uid);
if (!hasRelations) {
return 0;
}
const entity = await strapi.entityService.findOne(uid, id, { populate });
return sumDraftCounts(entity, uid);
},
2021-07-13 18:46:36 +02:00
});