From c6daeaf7792cd9c0e592c66591385f252d0bd54e 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 | 59 ++++++++++++------- 1 file changed, 37 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..b95608113c 100644 --- a/packages/core/src/service/services/facet.service.ts +++ b/packages/core/src/service/services/facet.service.ts @@ -111,29 +111,44 @@ 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 && lang) { + return translateDeep(facet, lang, ['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; + }); + } } /**