Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(content-releases): Delete a Release #19000

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/core/content-releases/server/src/controllers/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
PublishRelease,
GetRelease,
Release,
DeleteRelease,
} from '../../../shared/contracts/releases';
import type { UserInfo } from '../../../shared/types';
import { getAllowedContentTypes, getService } from '../utils';
Expand Down Expand Up @@ -136,6 +137,17 @@ const releaseController = {
};
},

async delete(ctx: Koa.Context) {
const id: DeleteRelease.Request['params']['id'] = ctx.params.id;

const releaseService = getService('release', { strapi });
const release = await releaseService.delete(id);

ctx.body = {
data: release,
};
},

async publish(ctx: Koa.Context) {
const user: PublishRelease.Request['state']['user'] = ctx.state.user;
const id: PublishRelease.Request['params']['id'] = ctx.params.id;
Expand Down
16 changes: 16 additions & 0 deletions packages/core/content-releases/server/src/routes/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ export default {
],
},
},
{
method: 'DELETE',
path: '/:id',
handler: 'release.delete',
config: {
policies: [
'admin::isAuthenticatedAdmin',
{
name: 'admin::hasPermissions',
config: {
actions: ['plugin::content-releases.delete'],
},
},
],
},
},
{
method: 'POST',
path: '/:id/publish',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,34 @@ describe('release service', () => {
expect(mockUnpublishMany).toHaveBeenCalledWith([{ id: 2 }], 'contentType');
});
});

describe('delete', () => {
it('throws an error if the release does not exist', () => {
const strapiMock = {
...baseStrapiMock,
entityService: {
findOne: jest.fn().mockReturnValue(null),
},
};

// @ts-expect-error Ignore missing properties
const releaseService = createReleaseService({ strapi: strapiMock });

expect(() => releaseService.delete(1)).rejects.toThrow('No release found for id 1');
});

it('throws an error if the release is already published', () => {
const strapiMock = {
...baseStrapiMock,
entityService: {
findOne: jest.fn().mockReturnValue({ releasedAt: new Date() }),
},
};

// @ts-expect-error Ignore missing properties
const releaseService = createReleaseService({ strapi: strapiMock });

expect(() => releaseService.delete(1)).rejects.toThrow('Release already published');
});
});
});
29 changes: 28 additions & 1 deletion packages/core/content-releases/server/src/services/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
PublishRelease,
GetRelease,
Release,
DeleteRelease,
} from '../../../shared/contracts/releases';
import type {
CreateReleaseAction,
Expand Down Expand Up @@ -165,6 +166,32 @@ const createReleaseService = ({ strapi }: { strapi: LoadedStrapi }) => ({

return contentTypesMeta;
},
async delete(releaseId: DeleteRelease.Request['params']['id']) {
const release = await strapi.entityService.findOne(RELEASE_MODEL_UID, releaseId);

if (!release) {
throw new errors.NotFoundError(`No release found for id ${releaseId}`);
}

if (release.releasedAt) {
throw new errors.ValidationError('Release already published');
}

const deletedRelease = (await strapi.entityService.delete(RELEASE_MODEL_UID, releaseId, {
populate: { actions: { fields: ['id'] } },
})) as unknown as Release;

// We delete each action related to the release
deletedRelease.actions.forEach((action) => {
strapi.db.query(RELEASE_ACTION_MODEL_UID).delete({
Feranchz marked this conversation as resolved.
Show resolved Hide resolved
where: {
id: action.id,
},
});
});

return deletedRelease;
},
async publish(releaseId: PublishRelease.Request['params']['id']) {
// We need to pass the type because entityService.findOne is not returning the correct type
const releaseWithPopulatedActionEntries = (await strapi.entityService.findOne(
Expand Down Expand Up @@ -236,7 +263,7 @@ const createReleaseService = ({ strapi }: { strapi: LoadedStrapi }) => ({
}
});

// When the transaction fails it throws an error, when it is successful proceed to updating the release
// When the transaction fails it throws an error, when it is successful proceed to updating the release
const release = await strapi.entityService.update(RELEASE_MODEL_UID, releaseId, {
data: {
/*
Expand Down
20 changes: 19 additions & 1 deletion packages/core/content-releases/shared/contracts/releases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Entity } from '../types';
import type { ReleaseAction } from './release-actions';
import type { UserInfo } from '../types';
import { errors } from '@strapi/utils';
import { UID } from '@strapi/types';

export interface Release extends Entity {
name: string;
Expand Down Expand Up @@ -101,6 +100,25 @@ export declare namespace UpdateRelease {
}
}

/**
* DELETE /content-releases/:id - Delete a release
*/
export declare namespace DeleteRelease {
export interface Request {
state: {
user: UserInfo;
};
params: {
id: Release['id'];
};
}

export interface Response {
data: ReleaseDataResponse;
error?: errors.ApplicationError | errors.NotFoundError;
}
}

/**
* POST /content-releases/:releaseId/publish - Publish a release
*/
Expand Down