mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 08:19:07 +00:00
fix(content-releases): filter releases with entry attached (#19249)
* fix(content-releases): fix problem with filters of releases with entry not attached * apply marks feedback
This commit is contained in:
parent
053d9dad52
commit
2f7decb22b
@ -1,14 +1,16 @@
|
|||||||
import releaseController from '../release';
|
import releaseController from '../release';
|
||||||
|
|
||||||
const mockFindPage = jest.fn();
|
const mockFindPage = jest.fn();
|
||||||
const mockFindManyForContentTypeEntry = jest.fn();
|
const mockFindManyWithContentTypeEntryAttached = jest.fn();
|
||||||
|
const mockFindManyWithoutContentTypeEntryAttached = jest.fn();
|
||||||
const mockCountActions = jest.fn();
|
const mockCountActions = jest.fn();
|
||||||
|
|
||||||
jest.mock('../../utils', () => ({
|
jest.mock('../../utils', () => ({
|
||||||
getService: jest.fn(() => ({
|
getService: jest.fn(() => ({
|
||||||
findOne: jest.fn(() => ({ id: 1 })),
|
findOne: jest.fn(() => ({ id: 1 })),
|
||||||
findPage: mockFindPage,
|
findPage: mockFindPage,
|
||||||
findManyForContentTypeEntry: mockFindManyForContentTypeEntry,
|
findManyWithContentTypeEntryAttached: mockFindManyWithContentTypeEntryAttached,
|
||||||
|
findManyWithoutContentTypeEntryAttached: mockFindManyWithoutContentTypeEntryAttached,
|
||||||
countActions: mockCountActions,
|
countActions: mockCountActions,
|
||||||
getContentTypesDataForActions: jest.fn(),
|
getContentTypesDataForActions: jest.fn(),
|
||||||
})),
|
})),
|
||||||
@ -19,7 +21,7 @@ describe('Release controller', () => {
|
|||||||
describe('findMany', () => {
|
describe('findMany', () => {
|
||||||
it('should call findPage', async () => {
|
it('should call findPage', async () => {
|
||||||
mockFindPage.mockResolvedValue({ results: [], pagination: {} });
|
mockFindPage.mockResolvedValue({ results: [], pagination: {} });
|
||||||
mockFindManyForContentTypeEntry.mockResolvedValue([]);
|
mockFindManyWithContentTypeEntryAttached.mockResolvedValue([]);
|
||||||
const userAbility = {
|
const userAbility = {
|
||||||
can: jest.fn(),
|
can: jest.fn(),
|
||||||
};
|
};
|
||||||
@ -53,9 +55,9 @@ describe('Release controller', () => {
|
|||||||
expect(mockFindPage).toHaveBeenCalled();
|
expect(mockFindPage).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call findManyForContentTypeEntry', async () => {
|
it('should call findManyWithoutContentTypeEntryAttached', async () => {
|
||||||
mockFindPage.mockResolvedValue({ results: [], pagination: {} });
|
mockFindPage.mockResolvedValue({ results: [], pagination: {} });
|
||||||
mockFindManyForContentTypeEntry.mockResolvedValue([]);
|
mockFindManyWithContentTypeEntryAttached.mockResolvedValue([]);
|
||||||
const userAbility = {
|
const userAbility = {
|
||||||
can: jest.fn(),
|
can: jest.fn(),
|
||||||
};
|
};
|
||||||
@ -86,7 +88,7 @@ describe('Release controller', () => {
|
|||||||
// @ts-expect-error partial context
|
// @ts-expect-error partial context
|
||||||
await releaseController.findMany(ctx);
|
await releaseController.findMany(ctx);
|
||||||
|
|
||||||
expect(mockFindManyForContentTypeEntry).toHaveBeenCalled();
|
expect(mockFindManyWithoutContentTypeEntryAttached).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('create', () => {
|
describe('create', () => {
|
||||||
|
@ -40,9 +40,9 @@ const releaseController = {
|
|||||||
const hasEntryAttached: GetContentTypeEntryReleases.Request['query']['hasEntryAttached'] =
|
const hasEntryAttached: GetContentTypeEntryReleases.Request['query']['hasEntryAttached'] =
|
||||||
typeof query.hasEntryAttached === 'string' ? JSON.parse(query.hasEntryAttached) : false;
|
typeof query.hasEntryAttached === 'string' ? JSON.parse(query.hasEntryAttached) : false;
|
||||||
|
|
||||||
const data = await releaseService.findManyForContentTypeEntry(contentTypeUid, entryId, {
|
const data = hasEntryAttached
|
||||||
hasEntryAttached,
|
? await releaseService.findManyWithContentTypeEntryAttached(contentTypeUid, entryId)
|
||||||
});
|
: await releaseService.findManyWithoutContentTypeEntryAttached(contentTypeUid, entryId);
|
||||||
|
|
||||||
ctx.body = { data };
|
ctx.body = { data };
|
||||||
} else {
|
} else {
|
||||||
|
@ -279,8 +279,8 @@ describe('release service', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('findManyForContentTypeEntry', () => {
|
describe('findManyWithContentTypeEntryAttached', () => {
|
||||||
it('should format the return value correctly when hasEntryAttached is true', async () => {
|
it('should format the return value correctly', async () => {
|
||||||
const strapiMock = {
|
const strapiMock = {
|
||||||
...baseStrapiMock,
|
...baseStrapiMock,
|
||||||
db: {
|
db: {
|
||||||
@ -294,12 +294,9 @@ describe('release service', () => {
|
|||||||
|
|
||||||
// @ts-expect-error Ignore missing properties
|
// @ts-expect-error Ignore missing properties
|
||||||
const releaseService = createReleaseService({ strapi: strapiMock });
|
const releaseService = createReleaseService({ strapi: strapiMock });
|
||||||
const releases = await releaseService.findManyForContentTypeEntry(
|
const releases = await releaseService.findManyWithContentTypeEntryAttached(
|
||||||
'api::contentType.contentType',
|
'api::contentType.contentType',
|
||||||
1,
|
1
|
||||||
{
|
|
||||||
hasEntryAttached: true,
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(releases).toEqual([{ name: 'test release', action: { type: 'publish' } }]);
|
expect(releases).toEqual([{ name: 'test release', action: { type: 'publish' } }]);
|
||||||
|
@ -79,60 +79,80 @@ const createReleaseService = ({ strapi }: { strapi: LoadedStrapi }) => ({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async findManyForContentTypeEntry(
|
async findManyWithContentTypeEntryAttached(
|
||||||
contentTypeUid: GetContentTypeEntryReleases.Request['query']['contentTypeUid'],
|
contentTypeUid: GetContentTypeEntryReleases.Request['query']['contentTypeUid'],
|
||||||
entryId: GetContentTypeEntryReleases.Request['query']['entryId'],
|
entryId: GetContentTypeEntryReleases.Request['query']['entryId']
|
||||||
{
|
|
||||||
hasEntryAttached,
|
|
||||||
}: { hasEntryAttached?: GetContentTypeEntryReleases.Request['query']['hasEntryAttached'] } = {
|
|
||||||
hasEntryAttached: false,
|
|
||||||
}
|
|
||||||
) {
|
) {
|
||||||
const whereActions = hasEntryAttached
|
|
||||||
? {
|
|
||||||
// Find all Releases where the content type entry is present
|
|
||||||
actions: {
|
|
||||||
target_type: contentTypeUid,
|
|
||||||
target_id: entryId,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
: {
|
|
||||||
// Find all Releases where the content type entry is not present
|
|
||||||
$or: [
|
|
||||||
{
|
|
||||||
$not: {
|
|
||||||
actions: {
|
|
||||||
target_type: contentTypeUid,
|
|
||||||
target_id: entryId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
actions: null,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
const populateAttachedAction = hasEntryAttached
|
|
||||||
? {
|
|
||||||
// Filter the action to get only the content type entry
|
|
||||||
actions: {
|
|
||||||
where: {
|
|
||||||
target_type: contentTypeUid,
|
|
||||||
target_id: entryId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
: {};
|
|
||||||
|
|
||||||
const releases = await strapi.db.query(RELEASE_MODEL_UID).findMany({
|
const releases = await strapi.db.query(RELEASE_MODEL_UID).findMany({
|
||||||
where: {
|
where: {
|
||||||
...whereActions,
|
actions: {
|
||||||
|
target_type: contentTypeUid,
|
||||||
|
target_id: entryId,
|
||||||
|
},
|
||||||
releasedAt: {
|
releasedAt: {
|
||||||
$null: true,
|
$null: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
populate: {
|
populate: {
|
||||||
...populateAttachedAction,
|
// Filter the action to get only the content type entry
|
||||||
|
actions: {
|
||||||
|
where: {
|
||||||
|
target_type: contentTypeUid,
|
||||||
|
target_id: entryId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return releases.map((release) => {
|
||||||
|
if (release.actions?.length) {
|
||||||
|
const [actionForEntry] = release.actions;
|
||||||
|
|
||||||
|
// Remove the actions key to replace it with an action key
|
||||||
|
delete release.actions;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...release,
|
||||||
|
action: actionForEntry,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return release;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async findManyWithoutContentTypeEntryAttached(
|
||||||
|
contentTypeUid: GetContentTypeEntryReleases.Request['query']['contentTypeUid'],
|
||||||
|
entryId: GetContentTypeEntryReleases.Request['query']['entryId']
|
||||||
|
) {
|
||||||
|
// We get the list of releases where the entry is present
|
||||||
|
const releasesRelated = await strapi.db.query(RELEASE_MODEL_UID).findMany({
|
||||||
|
where: {
|
||||||
|
releasedAt: {
|
||||||
|
$null: true,
|
||||||
|
},
|
||||||
|
actions: {
|
||||||
|
target_type: contentTypeUid,
|
||||||
|
target_id: entryId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const releases = await strapi.db.query(RELEASE_MODEL_UID).findMany({
|
||||||
|
where: {
|
||||||
|
$or: [
|
||||||
|
{
|
||||||
|
id: {
|
||||||
|
$notIn: releasesRelated.map((release) => release.id),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
actions: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
releasedAt: {
|
||||||
|
$null: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user