From c4cd542e59e039db5b37219cef5db0602dec8131 Mon Sep 17 00:00:00 2001 From: d46 Date: Thu, 7 Nov 2024 20:40:06 +0100 Subject: [PATCH] feat(chore): Channel awareness for findByCode --- .../src/service/services/facet.service.ts | 63 ++++++++++++------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/packages/core/src/service/services/facet.service.ts b/packages/core/src/service/services/facet.service.ts index f996431c64..eeeeee4c27 100644 --- a/packages/core/src/service/services/facet.service.ts +++ b/packages/core/src/service/services/facet.service.ts @@ -111,29 +111,48 @@ export class FacetService { lang?: LanguageCode, ): Promise | undefined> { const relations = ['values', 'values.facet']; - const [repository, facetCode, languageCode] = - ctxOrFacetCode instanceof RequestContext - ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - [this.connection.getRepository(ctxOrFacetCode, Facet), facetCodeOrLang, lang!] - : [ - this.connection.rawConnection.getRepository(Facet), - ctxOrFacetCode, - facetCodeOrLang as LanguageCode, - ]; + if (ctxOrFacetCode instanceof RequestContext) { + // New method using channel-aware query + return this.connection + .getRepository(ctxOrFacetCode, Facet) + .createQueryBuilder('facet') + .leftJoinAndSelect('facet.values', 'values') + .leftJoinAndSelect('values.facet', 'facetValue') + .where('facet.code = :code', { code: facetCodeOrLang }) + .andWhere('facet.channels.id = :channelId', { channelId: ctxOrFacetCode.channelId }) + .getOne() + .then(facet => { + if (facet) { + const channelLanguageCode = ctxOrFacetCode.languageCode; + return translateDeep(facet, lang || channelLanguageCode, [ + 'values', + ['values', 'facet'], + ]); + } + return undefined; + }); + } else { + // Old method + const [repository, facetCode, languageCode] = [ + this.connection.rawConnection.getRepository(Facet), + ctxOrFacetCode, + facetCodeOrLang as LanguageCode, + ]; - // TODO: Implement usage of channelLanguageCode - return repository - .findOne({ - where: { - code: facetCode, - }, - relations, - }) - .then( - facet => - (facet && translateDeep(facet, languageCode, ['values', ['values', 'facet']])) ?? - undefined, - ); + return repository + .findOne({ + where: { + code: facetCode, + }, + relations, + }) + .then(facet => { + if (facet) { + return translateDeep(facet, languageCode, ['values', ['values', 'facet']]); + } + return undefined; + }); + } } /**