2020-10-28 18:47:14 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { assoc, has, prop, omit } = require('lodash/fp');
|
2021-04-29 13:51:12 +02:00
|
|
|
const strapiUtils = require('@strapi/utils');
|
2020-10-28 18:47:14 +01:00
|
|
|
|
|
|
|
const { sanitizeEntity } = strapiUtils;
|
|
|
|
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 emitEvent = (event, fn) => async (entity, model) => {
|
|
|
|
const result = await fn(entity, model);
|
|
|
|
|
|
|
|
const modelDef = strapi.getModel(model);
|
|
|
|
|
|
|
|
strapi.eventHub.emit(event, {
|
|
|
|
model: modelDef.modelName,
|
|
|
|
entry: sanitizeEntity(result, { model: modelDef }),
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
const findCreatorRoles = entity => {
|
|
|
|
const createdByPath = `${CREATED_BY_ATTRIBUTE}.id`;
|
|
|
|
|
|
|
|
if (has(createdByPath, entity)) {
|
|
|
|
const creatorId = prop(createdByPath, entity);
|
2021-06-22 17:13:11 +02:00
|
|
|
return strapi.query('strapi::role').findMany({ where: { users: { id: creatorId } } });
|
2020-10-28 18:47:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2020-11-02 12:40:35 +01:00
|
|
|
async assocCreatorRoles(entity) {
|
2020-10-28 18:47:14 +01:00
|
|
|
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) {
|
|
|
|
const params = { ...opts, populate };
|
|
|
|
|
|
|
|
return strapi.entityService.find(uid, { params });
|
2020-10-28 18:47:14 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
findPage(opts, uid, populate) {
|
|
|
|
const params = { ...opts, populate };
|
|
|
|
|
|
|
|
return strapi.entityService.findPage(uid, { params });
|
2020-11-02 22:10:51 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
findWithRelationCounts(opts, uid, populate) {
|
|
|
|
const params = { ...opts, populate };
|
|
|
|
|
|
|
|
return strapi.entityService.findWithRelationCounts(uid, { params });
|
2020-12-16 15:28:11 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
search(opts, uid, populate) {
|
|
|
|
const params = { ...opts, populate };
|
|
|
|
|
|
|
|
return strapi.entityService.search(uid, { params });
|
2020-10-28 18:47:14 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
searchPage(opts, uid, populate) {
|
|
|
|
const params = { ...opts, populate };
|
|
|
|
|
|
|
|
return strapi.entityService.searchPage(uid, { params });
|
2020-11-02 22:10:51 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
searchWithRelationCounts(opts, uid, populate) {
|
|
|
|
const params = { ...opts, populate };
|
|
|
|
|
|
|
|
return strapi.entityService.searchWithRelationCounts(uid, { params });
|
2020-12-16 15:28:11 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
count(opts, uid) {
|
|
|
|
const params = { ...opts };
|
|
|
|
|
|
|
|
return strapi.entityService.count(uid, { params });
|
2020-11-02 12:40:35 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
async findOne(id, uid, populate) {
|
|
|
|
const params = { filters: { id }, populate };
|
|
|
|
return strapi.entityService.findOne(uid, { params });
|
2020-10-28 18:47:14 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
async findOneWithCreatorRoles(id, uid, populate) {
|
|
|
|
const entity = await this.findOne(id, uid, populate);
|
2020-10-28 18:47:14 +01:00
|
|
|
|
|
|
|
if (!entity) {
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
2020-11-02 12:40:35 +01:00
|
|
|
return this.assocCreatorRoles(entity);
|
2020-10-28 18:47:14 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
async create(body, uid) {
|
|
|
|
const modelDef = strapi.getModel(uid);
|
2020-11-03 10:00:50 +01:00
|
|
|
const publishData = { ...body };
|
2020-10-28 18:47:14 +01:00
|
|
|
|
|
|
|
if (hasDraftAndPublish(modelDef)) {
|
|
|
|
publishData[PUBLISHED_AT_ATTRIBUTE] = null;
|
|
|
|
}
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
return strapi.entityService.create(uid, { data: publishData });
|
2020-10-28 18:47:14 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
update(entity, body, uid) {
|
2020-10-28 18:47:14 +01:00
|
|
|
const params = { id: entity.id };
|
2020-11-03 10:00:50 +01:00
|
|
|
const publishData = omitPublishedAtField(body);
|
2020-10-28 18:47:14 +01:00
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
return strapi.entityService.update(uid, { params, data: publishData });
|
2020-10-28 18:47:14 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
delete(entity, uid) {
|
|
|
|
return strapi.entityService.delete(uid, entity.id);
|
2020-10-28 18:47:14 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
findAndDelete(opts, uid) {
|
|
|
|
const params = { ...opts };
|
|
|
|
|
|
|
|
return strapi.entityService.delete(uid, { params });
|
2020-11-03 10:00:50 +01:00
|
|
|
},
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
publish: emitEvent(ENTRY_PUBLISH, async (entity, uid) => {
|
2020-11-02 19:39:48 +01:00
|
|
|
if (entity[PUBLISHED_AT_ATTRIBUTE]) {
|
|
|
|
throw strapi.errors.badRequest('already.published');
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the entity is valid for publication
|
2021-06-30 20:00:03 +02:00
|
|
|
await strapi.entityValidator.validateEntityCreation(strapi.getModel(uid), entity);
|
2020-11-02 19:39:48 +01:00
|
|
|
|
2020-10-28 18:47:14 +01:00
|
|
|
const data = { [PUBLISHED_AT_ATTRIBUTE]: new Date() };
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
return strapi.entityService.update(uid, entity.id, { data });
|
2020-10-28 18:47:14 +01:00
|
|
|
}),
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
unpublish: emitEvent(ENTRY_UNPUBLISH, (entity, uid) => {
|
2020-11-02 19:39:48 +01:00
|
|
|
if (!entity[PUBLISHED_AT_ATTRIBUTE]) {
|
|
|
|
throw strapi.errors.badRequest('already.draft');
|
|
|
|
}
|
|
|
|
|
2020-10-28 18:47:14 +01:00
|
|
|
const data = { [PUBLISHED_AT_ATTRIBUTE]: null };
|
|
|
|
|
2021-06-30 20:00:03 +02:00
|
|
|
return strapi.entityService.update(uid, entity.id, { data });
|
2020-10-28 18:47:14 +01:00
|
|
|
}),
|
|
|
|
};
|