Skip to content

Commit

Permalink
fix(core): Do not throw when deleting Facet with no FacetValues
Browse files Browse the repository at this point in the history
Fixes #105
  • Loading branch information
michaelbromley committed May 29, 2019
1 parent d8b6c47 commit f7d337f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 14 additions & 0 deletions packages/core/e2e/facet.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,20 @@ describe('Facet resolver', () => {
});
expect(result3.product!.facetValues).toEqual([]);
});

it('deleteFacet with no FacetValues works', async () => {
const { createFacet } = await client.query<CreateFacet.Mutation, CreateFacet.Variables>(CREATE_FACET, {
input: {
code: 'test',
isPrivate: false,
translations: [
{ languageCode: LanguageCode.en, name: 'Test' },
],
},
});
const result = await client.query<DeleteFacet.Mutation, DeleteFacet.Variables>(DELETE_FACET, { id: createFacet.id, force: false });
expect(result.deleteFacet.result).toBe(DeletionResult.DELETED);
});
});
});

Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/service/services/facet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ export class FacetService {

async delete(ctx: RequestContext, id: ID, force: boolean = false): Promise<DeletionResponse> {
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';
Expand Down

0 comments on commit f7d337f

Please sign in to comment.