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 c4cd542
Showing 1 changed file with 41 additions and 22 deletions.
63 changes: 41 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,48 @@ 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) {
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;
});
}
}

/**
Expand Down

0 comments on commit c4cd542

Please sign in to comment.