Skip to content

Commit

Permalink
feat(chore): Channel awareness for findByCode
Browse files Browse the repository at this point in the history
  • Loading branch information
d46 committed Nov 7, 2024
1 parent 961297d commit c6daeaf
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions packages/core/src/service/services/facet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,44 @@ export class FacetService {
lang?: LanguageCode,
): Promise<Translated<Facet> | 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;
});
}
}

/**
Expand Down

0 comments on commit c6daeaf

Please sign in to comment.