mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 08:19:07 +00:00
Merge pull request #16894 from strapi/feature/bulk-publish
This commit is contained in:
commit
2a76cb378f
@ -42,7 +42,7 @@ describe('Content-Manager', () => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test('Publish a content-type', async () => {
|
||||
test('Publish a single entity for a content-type', async () => {
|
||||
const uid = 'api::test.test';
|
||||
const entity = { id: 1, publishedAt: null };
|
||||
await entityManager.publish(entity, {}, uid);
|
||||
@ -60,7 +60,7 @@ describe('Content-Manager', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('Publish many content-types', async () => {
|
||||
test('Publish many entities for a content-type', async () => {
|
||||
const uid = 'api::test.test';
|
||||
const entities = [
|
||||
{ id: 1, publishedAt: null },
|
||||
@ -80,7 +80,12 @@ describe('Content-Manager', () => {
|
||||
},
|
||||
data: { publishedAt: expect.any(Date) },
|
||||
});
|
||||
|
||||
expect(strapi.entityService.findMany).toHaveBeenCalledWith(uid, {
|
||||
filters: {
|
||||
id: { $in: [1, 2] },
|
||||
},
|
||||
populate: {},
|
||||
});
|
||||
expect(strapi.eventHub.emit.mock.calls).toEqual([
|
||||
[
|
||||
'entry.publish',
|
||||
@ -92,6 +97,37 @@ describe('Content-Manager', () => {
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
test('Publish many entities for a content-type with mixed publishedAt state', async () => {
|
||||
const uid = 'api::test.test';
|
||||
const entities = [
|
||||
{ id: 1, publishedAt: new Date() },
|
||||
{ id: 2, publishedAt: null },
|
||||
];
|
||||
|
||||
strapi.entityService.findMany.mockResolvedValueOnce([{ id: 2, publishedAt: new Date() }]);
|
||||
|
||||
await entityManager.publishMany(entities, uid);
|
||||
|
||||
expect(strapi.db.query().updateMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
id: { $in: [2] },
|
||||
},
|
||||
data: { publishedAt: expect.any(Date) },
|
||||
});
|
||||
expect(strapi.entityService.findMany).toHaveBeenCalledWith(uid, {
|
||||
filters: {
|
||||
id: { $in: [2] },
|
||||
},
|
||||
populate: {},
|
||||
});
|
||||
expect(strapi.eventHub.emit.mock.calls).toEqual([
|
||||
[
|
||||
'entry.publish',
|
||||
{ model: fakeModel.modelName, entry: { id: 2, publishedAt: expect.any(Date) } },
|
||||
],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Unpublish', () => {
|
||||
@ -120,7 +156,7 @@ describe('Content-Manager', () => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test('Unpublish a content-type', async () => {
|
||||
test('Unpublish a single entity for a content-type', async () => {
|
||||
const uid = 'api::test.test';
|
||||
const entity = { id: 1, publishedAt: new Date() };
|
||||
await entityManager.unpublish(entity, {}, uid);
|
||||
@ -138,7 +174,7 @@ describe('Content-Manager', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('Unpublish many content-types', async () => {
|
||||
test('Unpublish many entities for a content-type', async () => {
|
||||
const uid = 'api::test.test';
|
||||
const entities = [
|
||||
{ id: 1, publishedAt: new Date() },
|
||||
@ -158,10 +194,44 @@ describe('Content-Manager', () => {
|
||||
},
|
||||
data: { publishedAt: null },
|
||||
});
|
||||
expect(strapi.entityService.findMany).toHaveBeenCalledWith(uid, {
|
||||
filters: {
|
||||
id: { $in: [1, 2] },
|
||||
},
|
||||
populate: {},
|
||||
});
|
||||
expect(strapi.eventHub.emit.mock.calls).toEqual([
|
||||
['entry.unpublish', { model: fakeModel.modelName, entry: { id: 1, publishedAt: null } }],
|
||||
['entry.unpublish', { model: fakeModel.modelName, entry: { id: 2, publishedAt: null } }],
|
||||
]);
|
||||
});
|
||||
|
||||
test('Unpublish many entities for a content-type with a mixed publishedAt state', async () => {
|
||||
const uid = 'api::test.test';
|
||||
const entities = [
|
||||
{ id: 1, publishedAt: new Date() },
|
||||
{ id: 2, publishedAt: null },
|
||||
];
|
||||
|
||||
strapi.entityService.findMany.mockResolvedValueOnce([{ id: 1, publishedAt: null }]);
|
||||
|
||||
await entityManager.unpublishMany(entities, uid);
|
||||
|
||||
expect(strapi.db.query().updateMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
id: { $in: [1] },
|
||||
},
|
||||
data: { publishedAt: null },
|
||||
});
|
||||
expect(strapi.entityService.findMany).toHaveBeenCalledWith(uid, {
|
||||
filters: {
|
||||
id: { $in: [1] },
|
||||
},
|
||||
populate: {},
|
||||
});
|
||||
expect(strapi.eventHub.emit.mock.calls).toEqual([
|
||||
['entry.unpublish', { model: fakeModel.modelName, entry: { id: 1, publishedAt: null } }],
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -289,21 +289,21 @@ module.exports = ({ strapi }) => ({
|
||||
const entitiesToPublish = entities
|
||||
.filter((entity) => !entity[PUBLISHED_AT_ATTRIBUTE])
|
||||
.map((entity) => entity.id);
|
||||
const where = { id: { $in: entitiesToPublish } };
|
||||
|
||||
const filters = { id: { $in: entitiesToPublish } };
|
||||
const data = {
|
||||
[PUBLISHED_AT_ATTRIBUTE]: new Date(),
|
||||
};
|
||||
const populate = isRelationsPopulateEnabled(uid)
|
||||
? getDeepPopulate(uid, {})
|
||||
: getDeepPopulate(uid, { countMany: true, countOne: true });
|
||||
|
||||
// Everything is valid, publish
|
||||
const publishedEntitiesCount = await strapi.db.query(uid).updateMany({
|
||||
where,
|
||||
where: filters,
|
||||
data,
|
||||
});
|
||||
// Get the updated entities since updateMany only returns the count
|
||||
const publishedEntities = await strapi.entityService.findMany(uid, { where, populate });
|
||||
const publishedEntities = await strapi.entityService.findMany(uid, { filters, populate });
|
||||
// Emit the publish event for all updated entities
|
||||
await Promise.all(publishedEntities.map((entity) => emitEvent(ENTRY_PUBLISH, entity, uid)));
|
||||
|
||||
@ -315,9 +315,13 @@ module.exports = ({ strapi }) => ({
|
||||
if (!entities.length) {
|
||||
return null;
|
||||
}
|
||||
// Unpublish everything regardles of publish draft state
|
||||
const entitiesToUnpublish = entities.map((entity) => entity.id);
|
||||
const where = { id: { $in: entitiesToUnpublish } };
|
||||
|
||||
// Only unpublish entities with a published_at date
|
||||
const entitiesToUnpublish = entities
|
||||
.filter((entity) => entity[PUBLISHED_AT_ATTRIBUTE])
|
||||
.map((entity) => entity.id);
|
||||
|
||||
const filters = { id: { $in: entitiesToUnpublish } };
|
||||
const data = {
|
||||
[PUBLISHED_AT_ATTRIBUTE]: null,
|
||||
};
|
||||
@ -327,11 +331,11 @@ module.exports = ({ strapi }) => ({
|
||||
|
||||
// No need to validate, unpublish
|
||||
const unpublishedEntitiesCount = await strapi.db.query(uid).updateMany({
|
||||
where,
|
||||
where: filters,
|
||||
data,
|
||||
});
|
||||
// Get the updated entities since updateMany only returns the count
|
||||
const unpublishedEntities = await strapi.entityService.findMany(uid, { where, populate });
|
||||
const unpublishedEntities = await strapi.entityService.findMany(uid, { filters, populate });
|
||||
// Emit the unpublish event for all updated entities
|
||||
await Promise.all(unpublishedEntities.map((entity) => emitEvent(ENTRY_UNPUBLISH, entity, uid)));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user