From 5dfc9a4cbf9c4a76741379e11affaf7c81ff46eb Mon Sep 17 00:00:00 2001 From: Daniel Biegler Date: Sun, 3 Nov 2024 21:00:50 +0100 Subject: [PATCH 1/2] fix(core): Disallow deletion of default channel --- packages/core/e2e/channel.e2e-spec.ts | 20 +++++++++++++++++++ packages/core/src/i18n/messages/de.json | 1 + packages/core/src/i18n/messages/en.json | 1 + .../src/service/services/channel.service.ts | 6 ++++++ 4 files changed, 28 insertions(+) diff --git a/packages/core/e2e/channel.e2e-spec.ts b/packages/core/e2e/channel.e2e-spec.ts index 085c1ec2ef..92582bc374 100644 --- a/packages/core/e2e/channel.e2e-spec.ts +++ b/packages/core/e2e/channel.e2e-spec.ts @@ -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(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.result).toEqual({ + result: DeletionResult.NOT_DELETED, + message: 'The default Channel cannot be deleted', + }); + }); + describe('currencyCode support', () => { beforeAll(async () => { await adminClient.asSuperAdmin(); diff --git a/packages/core/src/i18n/messages/de.json b/packages/core/src/i18n/messages/de.json index 0e8f7a693d..c618cca2c5 100644 --- a/packages/core/src/i18n/messages/de.json +++ b/packages/core/src/i18n/messages/de.json @@ -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", diff --git a/packages/core/src/i18n/messages/en.json b/packages/core/src/i18n/messages/en.json index b63615971f..c3b66e4ad7 100644 --- a/packages/core/src/i18n/messages/en.json +++ b/packages/core/src/i18n/messages/en.json @@ -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", diff --git a/packages/core/src/service/services/channel.service.ts b/packages/core/src/service/services/channel.service.ts index 57e49e7ee8..0427310604 100644 --- a/packages/core/src/service/services/channel.service.ts +++ b/packages/core/src/service/services/channel.service.ts @@ -457,6 +457,12 @@ export class ChannelService { async delete(ctx: RequestContext, id: ID): Promise { 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); From b246c54f372d02009d6fcdbff72ccf9909bafc46 Mon Sep 17 00:00:00 2001 From: Daniel Biegler Date: Sun, 3 Nov 2024 21:25:24 +0100 Subject: [PATCH 2/2] fix(core): Fix test (compared the wrong field) --- packages/core/e2e/channel.e2e-spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/e2e/channel.e2e-spec.ts b/packages/core/e2e/channel.e2e-spec.ts index 92582bc374..6c4c1d2d04 100644 --- a/packages/core/e2e/channel.e2e-spec.ts +++ b/packages/core/e2e/channel.e2e-spec.ts @@ -384,7 +384,7 @@ describe('Channels', () => { Codegen.DeleteChannelMutationVariables >(DELETE_CHANNEL, { id: defaultChannelId! }); - expect(mutation.deleteChannel.result).toEqual({ + expect(mutation.deleteChannel).toEqual({ result: DeletionResult.NOT_DELETED, message: 'The default Channel cannot be deleted', });