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 4 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,
GetContentTypeEntryReleases,
GetReleases,
} from '../../../shared/contracts/releases';
Expand Down Expand Up @@ -147,6 +148,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 @@ -175,4 +175,34 @@ describe('release service', () => {
expect(releases).toEqual([{ name: 'test release', action: { type: 'publish' } }]);
});
});

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');
});
});
});
31 changes: 30 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,
GetContentTypeEntryReleases,
} from '../../../shared/contracts/releases';
import type {
Expand Down Expand Up @@ -230,6 +231,34 @@ 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
if (deletedRelease.actions) {
await strapi.db.query(RELEASE_ACTION_MODEL_UID).deleteMany({
where: {
id: {
$in: deletedRelease.actions.map(({ id }) => id),
},
},
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So no matter what we delete the release right? If something goes wrong deleting the actions then what will happen? An error throws in the deleteMany, the release is already deleted, and we return an error? Seems a bit misleading in that case.

I'm wondering if the order of operations matters here. delete the actions, when that's a success delete the release? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the order you proposed is right, we should delete the actions firsts.

I also added a transaction for this, because in my opinion it's the perfect use case: We want to delete the release and all its actions only if we can delete all without problems 🤔


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 @@ -301,7 +330,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 @@ -126,6 +125,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