Skip to content

Commit

Permalink
fix(core): Disallow deletion of default channel (#3181)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBiegler authored Nov 15, 2024
1 parent f0607aa commit 2ed3211
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/core/e2e/channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,26 @@ describe('Channels', () => {
expect(product!.channels.map(c => c.id)).toEqual(['T_1']);
});

it('Fail to delete the default channel', async () => {
await adminClient.asSuperAdmin();

const defaultChannelId = (
await adminClient.query<Codegen.GetChannelsQuery, Codegen.GetChannelsQueryVariables>(GET_CHANNELS)
).channels.items.find(channel => channel.code === DEFAULT_CHANNEL_CODE)?.id;

expect(defaultChannelId).not.toBeUndefined();

const mutation = await adminClient.query<
Codegen.DeleteChannelMutation,
Codegen.DeleteChannelMutationVariables
>(DELETE_CHANNEL, { id: defaultChannelId! });

expect(mutation.deleteChannel).toEqual({
result: DeletionResult.NOT_DELETED,
message: 'The default Channel cannot be deleted',
});
});

describe('currencyCode support', () => {
beforeAll(async () => {
await adminClient.asSuperAdmin();
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/i18n/messages/de.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"error": {
"cannot-delete-role": "Die Rolle \"{ roleCode }\" kann nicht gelöscht werden",
"cannot-delete-default-channel": "Der Standardkanal kann nicht gelöscht werden",
"cannot-locate-customer-for-user": "Es konnte kein Kunde für den Nutzer gefunden werden",
"cannot-modify-role": "Die Rolle \"{ roleCode }\" kann nicht geändert werden",
"cannot-create-sales-for-active-order": "Es kann kein Sale für eine aktive Bestellung erstellt werden",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/i18n/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"available-currency-codes-must-include-default": "availableCurrencyCodes must include the defaultCurrencyCode ({ defaultCurrencyCode })",
"cannot-delete-role": "The role \"{ roleCode }\" cannot be deleted",
"cannot-delete-sole-superadmin": "The sole SuperAdmin cannot be deleted",
"cannot-delete-default-channel": "The default Channel cannot be deleted",
"cannot-locate-customer-for-user": "Cannot locate a Customer for the user",
"cannot-modify-role": "The role \"{ roleCode }\" cannot be modified",
"cannot-move-collection-into-self": "Cannot move a Collection into itself",
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/service/services/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,12 @@ export class ChannelService {

async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
const channel = await this.connection.getEntityOrThrow(ctx, Channel, id);
if (channel.code === DEFAULT_CHANNEL_CODE)
return {
result: DeletionResult.NOT_DELETED,
message: ctx.translate('error.cannot-delete-default-channel'),
};

const deletedChannel = new Channel(channel);
await this.connection.getRepository(ctx, Session).delete({ activeChannelId: id });
await this.connection.getRepository(ctx, Channel).delete(id);
Expand Down

0 comments on commit 2ed3211

Please sign in to comment.