Madhuri Sandbhor e1b9d9adee
enhancement(CM): bulk delete and unpublish migration to v5 (#20235)
* fix: added bulk actions

* chore: migrate bulk publish & unpublish to v5

* chore: change findLocales type to accept arrays

* chore: fix lint error

* chore: migrate bulkDelete to v5 (#20161)

* chore: migrate bulkDelete to v5

* chore: change findLocales type to accept strings array

* fix: docs prettier styles

* chore: remove console.log

* enhancement: migrate countManyDraftRelations to v5

* feat: bulk delete with locale selection option

* fix: bulk unpublish working without locale support

* test: e2e tests updated

* fix: documents api change, deleteMany tags updated, test skipped

* chore: renaming

* test: reverting e2e tests for delete action changes

* fix: reverting publish action changes

* fix: hide add to release as its not ready to be migrated

* fix: error message updated

* fix: tests updated for useDocumentAction

* fix: updated request params

* fix: reverting wrong commit message

Co-authored-by: Josh <37798644+joshuaellis@users.noreply.github.com>

* fix: comments updated

* chore: renaming

* chore: add type to bulk action actions (#20256)

* chore: actionType renamed to just type

* fix: dont show unpublish button on contentTypes without draft and publish

* fix: set a fixed width for FieldActionWrapper on UID input

---------

Co-authored-by: Fernando Chavez <fernando.chavez@strapi.io>
Co-authored-by: Josh <37798644+joshuaellis@users.noreply.github.com>
2024-05-09 12:35:48 +02:00

470 lines
10 KiB
TypeScript

import { errors } from '@strapi/utils';
import { RequestHandler, rest } from 'msw';
import { COLLECTION_TYPES, SINGLE_TYPES } from '../src/constants/collections';
import { historyHandlers } from '../src/history/tests/server';
import { mockData } from './mockData';
export const handlers: RequestHandler[] = [
/**
*
* CONTENT_MANAGER
*
*/
rest.put('/content-manager/content-types/:model/configuration', (req, res, ctx) => {
return res(ctx.status(200));
}),
rest.get('/content-manager/content-types/:model/configuration', (req, res, ctx) => {
const configuration =
req.params.model === 'api::homepage.homepage'
? mockData.contentManager.singleTypeConfiguration
: mockData.contentManager.collectionTypeConfiguration;
return res(
ctx.json({
data: configuration,
})
);
}),
rest.get('/content-manager/:collectionType/:uid/:id', (req, res, ctx) => {
const { id, collectionType, uid } = req.params;
if (id === 'configuration') {
return;
}
if (
id === '12345' &&
collectionType === COLLECTION_TYPES &&
uid === mockData.contentManager.contentType
) {
return res(
ctx.json({
data: {
documentId: id,
id: 1,
name: 'Entry 1',
createdAt: '',
updatedAt: '',
publishedAt: '',
},
})
);
} else {
return res(
ctx.status(404),
ctx.json({ error: new errors.NotFoundError('Document not found') })
);
}
}),
rest.put('/content-manager/:collectionType/:uid/:id', async (req, res, ctx) => {
const { id, collectionType, uid } = req.params;
const data = await req.json();
if (
id === '12345' &&
collectionType === COLLECTION_TYPES &&
uid === mockData.contentManager.contentType
) {
return res(
ctx.json({
data: {
documentId: id,
id: 1,
name: 'Entry 1',
...data,
createdAt: '',
updatedAt: '',
publishedAt: '',
},
})
);
}
return res(
ctx.status(404),
ctx.json({
error: new errors.NotFoundError('Document not found'),
})
);
}),
rest.post('/content-manager/:collectionType/:uid', async (req, res, ctx) => {
const data = await req.json();
if (
req.params.collectionType !== SINGLE_TYPES &&
req.params.collectionType !== COLLECTION_TYPES
) {
return;
}
return res(
ctx.json({
data: {
documentId: '12345',
id: 1,
...data,
},
})
);
}),
rest.post('/content-manager/:collectionType/:uid/clone/:id', async (req, res, ctx) => {
const data = await req.json();
return res(
ctx.json({
data: {
documentId: '67890',
id: 2,
...data,
},
})
);
}),
rest.post('/content-manager/:collectionType/:uid/:id/actions/discard', (req, res, ctx) => {
const { id } = req.params;
if (id === '12345') {
return res(
ctx.status(200),
ctx.json({
documentId: id,
id: 1,
title: 'test',
})
);
}
return res(
ctx.status(404),
ctx.json({
error: new errors.NotFoundError('Document not found'),
})
);
}),
rest.post('/content-manager/:collectionType/:uid/:id/actions/publish', async (req, res, ctx) => {
const { id } = req.params;
if (id === '12345') {
return res(
ctx.status(200),
ctx.json({
documentId: id,
id: 1,
title: 'test',
publishedAt: '2024-01-23T16:23:38.948Z',
})
);
}
return res(
ctx.status(404),
ctx.json({
error: new errors.NotFoundError('Document not found'),
})
);
}),
rest.post(
'/content-manager/:collectionType/:uid/:id/actions/unpublish',
async (req, res, ctx) => {
const { id } = req.params;
if (id === '12345') {
return res(
ctx.status(200),
ctx.json({
documentId: id,
id: 1,
title: 'test',
publishedAt: null,
})
);
}
return res(
ctx.status(404),
ctx.json({
error: new errors.NotFoundError('Document not found'),
})
);
}
),
rest.post(
'/content-manager/collection-types/:uid/actions/bulkUnpublish',
async (req, res, ctx) => {
const { documentIds } = await req.json();
if (documentIds.length === 2) {
return res(
ctx.status(200),
ctx.json({
count: 2,
})
);
}
return res(
ctx.status(404),
ctx.json({
error: new errors.NotFoundError('Document not found'),
})
);
}
),
rest.delete('/content-manager/:collectionType/:uid/:id', (req, res, ctx) => {
const { id } = req.params;
if (id === '12345') {
return res(
ctx.status(200),
ctx.json({
documentId: id,
id: 1,
title: 'test',
})
);
}
return res(
ctx.status(404),
ctx.json({
error: new errors.NotFoundError('Document not found'),
})
);
}),
rest.post('/content-manager/collection-types/:uid/actions/bulkDelete', async (req, res, ctx) => {
const { documentIds } = await req.json();
if (documentIds.length === 2) {
return res(
ctx.status(200),
ctx.json({
count: 2,
})
);
}
return res(
ctx.status(404),
ctx.json({
error: new errors.NotFoundError('Document not found'),
})
);
}),
rest.get('/content-manager/init', (req, res, ctx) => {
return res(
ctx.json({
data: {
components: mockData.contentManager.components,
contentTypes: mockData.contentManager.contentTypes,
},
})
);
}),
rest.post('/content-manager/uid/generate', async (req, res, ctx) => {
const body = await req.json();
return res(
ctx.json({
data: body?.data?.target ?? 'regenerated',
})
);
}),
rest.post('/content-manager/uid/check-availability', async (req, res, ctx) => {
const body = await req.json();
return res(
ctx.json({
isAvailable: body?.value === 'available',
})
);
}),
rest.get('/content-manager/collection-types/:contentType', (req, res, ctx) => {
return res(
ctx.json({
results: [
{
documentId: '12345',
id: 1,
name: 'Entry 1',
publishedAt: null,
notrepeat_req: {},
},
{
documentId: '67890',
id: 2,
name: 'Entry 2',
publishedAt: null,
notrepeat_req: {},
},
{
documentId: 'abcde',
id: 3,
name: 'Entry 3',
publishedAt: null,
notrepeat_req: {},
},
],
})
);
}),
rest.get('/content-manager/content-types', (req, res, ctx) =>
res(
ctx.json({
data: [
{
uid: 'admin::collectionType',
isDisplayed: true,
apiID: 'permission',
kind: 'collectionType',
},
{
uid: 'admin::collectionTypeNotDispalyed',
isDisplayed: false,
apiID: 'permission',
kind: 'collectionType',
},
{
uid: 'admin::singleType',
isDisplayed: true,
kind: 'singleType',
},
{
uid: 'admin::singleTypeNotDispalyed',
isDisplayed: false,
kind: 'singleType',
},
],
})
)
),
rest.get('/content-manager/components', (req, res, ctx) =>
res(
ctx.json({
data: [
{
uid: 'basic.relation',
isDisplayed: true,
apiID: 'relation',
category: 'basic',
info: {
displayName: 'Relation',
},
options: {},
attributes: {
id: {
type: 'integer',
},
categories: {
type: 'relation',
relation: 'oneToMany',
target: 'api::category.category',
targetModel: 'api::category.category',
relationType: 'oneToMany',
},
},
},
],
})
)
),
rest.post(
'/content-manager/collection-types/:contentType/actions/bulkPublish',
(req, res, ctx) => {
return res(
ctx.json({
data: {
count: 3,
},
})
);
}
),
rest.get(
'/content-manager/collection-types/:contentType/actions/countManyEntriesDraftRelations',
(req, res, ctx) => {
return res(
ctx.json({
data: 0,
})
);
}
),
rest.get('/content-manager/relations/:model/:id/:fieldName', (req, res, ctx) => {
return res(
ctx.json({
results: [
{
id: 1,
documentId: 'apples',
locale: 'en',
status: 'draft',
name: 'Relation entity 1',
},
{
id: 2,
documentId: 'bananas',
locale: 'en',
status: 'published',
name: 'Relation entity 2',
},
{
id: 3,
documentId: 'pears',
locale: 'en',
status: 'modified',
name: 'Relation entity 3',
},
],
pagination: {
page: 1,
pageCount: 1,
total: 3,
},
})
);
}),
rest.get('/content-manager/relations/:model/:fieldName', (req, res, ctx) => {
return res(
ctx.json({
results: [
{
id: 1,
documentId: 'apples',
locale: 'en',
status: 'draft',
name: 'Relation entity 1',
},
{
id: 2,
documentId: 'bananas',
locale: 'en',
status: 'published',
name: 'Relation entity 2',
},
{
id: 3,
documentId: 'pears',
locale: 'en',
status: 'modified',
name: 'Relation entity 3',
},
],
pagination: {
page: 1,
pageCount: 1,
total: 3,
},
})
);
}),
/**
* Content History
*/
...historyHandlers,
];