From f7d337f3d88f705068b98a040d7c775e9c76d5df Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Wed, 29 May 2019 13:38:07 +0200 Subject: [PATCH] fix(core): Do not throw when deleting Facet with no FacetValues Fixes #105 --- packages/core/e2e/facet.e2e-spec.ts | 14 ++++++++++++++ .../core/src/service/services/facet.service.ts | 12 +++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/core/e2e/facet.e2e-spec.ts b/packages/core/e2e/facet.e2e-spec.ts index 6d46f53367..14a7233a97 100644 --- a/packages/core/e2e/facet.e2e-spec.ts +++ b/packages/core/e2e/facet.e2e-spec.ts @@ -310,6 +310,20 @@ describe('Facet resolver', () => { }); expect(result3.product!.facetValues).toEqual([]); }); + + it('deleteFacet with no FacetValues works', async () => { + const { createFacet } = await client.query(CREATE_FACET, { + input: { + code: 'test', + isPrivate: false, + translations: [ + { languageCode: LanguageCode.en, name: 'Test' }, + ], + }, + }); + const result = await client.query(DELETE_FACET, { id: createFacet.id, force: false }); + expect(result.deleteFacet.result).toBe(DeletionResult.DELETED); + }); }); }); diff --git a/packages/core/src/service/services/facet.service.ts b/packages/core/src/service/services/facet.service.ts index 8830e7f922..1d3c97db59 100644 --- a/packages/core/src/service/services/facet.service.ts +++ b/packages/core/src/service/services/facet.service.ts @@ -94,9 +94,15 @@ export class FacetService { async delete(ctx: RequestContext, id: ID, force: boolean = false): Promise { const facet = await getEntityOrThrow(this.connection, Facet, id, { relations: ['values'] }); - const { productCount, variantCount } = await this.facetValueService.checkFacetValueUsage( - facet.values.map(fv => fv.id), - ); + let productCount = 0; + let variantCount = 0; + if (facet.values.length) { + const counts = await this.facetValueService.checkFacetValueUsage( + facet.values.map(fv => fv.id), + ); + productCount = counts.productCount; + variantCount = counts.variantCount; + } const isInUse = !!(productCount || variantCount); const both = !!(productCount && variantCount) ? 'both' : 'single';