fix: 404 product

This commit is contained in:
Sebastian Rindom
2021-09-14 16:25:22 +02:00
parent e7bfd6202f
commit 8c8c589b0f
+31 -12
View File
@@ -19,7 +19,12 @@ export class ProductRepository extends Repository<Product> {
}
const entitiesIds = entities.map(({ id }) => id)
const groupedRelations : { [toplevel: string]: string[]} = {}
if (entitiesIds.length === 0) {
// no need to continue
return []
}
const groupedRelations: { [toplevel: string]: string[] } = {}
for (const rel of relations) {
const [topLevel] = rel.split(".")
if (groupedRelations[topLevel]) {
@@ -32,35 +37,49 @@ export class ProductRepository extends Repository<Product> {
const entitiesIdsWithRelations = await Promise.all(
Object.entries(groupedRelations).map(([toplevel, rels]) => {
let querybuilder = this.createQueryBuilder("products")
if (toplevel === "variants") {
querybuilder = querybuilder.leftJoinAndSelect(`products.${toplevel}`, toplevel, "variants.deleted_at IS NULL")
querybuilder = querybuilder
.leftJoinAndSelect(
`products.${toplevel}`,
toplevel,
"variants.deleted_at IS NULL"
)
.orderBy({
"variants.variant_rank": "ASC",
"variants.variant_rank": "ASC",
})
} else {
querybuilder = querybuilder.leftJoinAndSelect(`products.${toplevel}`, toplevel)
querybuilder = querybuilder.leftJoinAndSelect(
`products.${toplevel}`,
toplevel
)
}
for(const rel of rels) {
for (const rel of rels) {
const [_, rest] = rel.split(".")
if (!rest) {
continue
}
// Regex matches all '.' except the rightmost
querybuilder = querybuilder.leftJoinAndSelect(rel.replace(/\.(?=[^.]*\.)/g,"__"), rel.replace(".", "__"))
querybuilder = querybuilder.leftJoinAndSelect(
rel.replace(/\.(?=[^.]*\.)/g, "__"),
rel.replace(".", "__")
)
}
return querybuilder
.where("products.deleted_at IS NULL AND products.id IN (:...entitiesIds)", { entitiesIds })
.getMany();
.where(
"products.deleted_at IS NULL AND products.id IN (:...entitiesIds)",
{ entitiesIds }
)
.getMany()
})
).then(flatten)
const entitiesAndRelations = entitiesIdsWithRelations.concat(entities)
const entitiesAndRelationsById = groupBy(entitiesAndRelations, "id")
return map(entitiesAndRelationsById, entityAndRelations =>
return map(entitiesAndRelationsById, (entityAndRelations) =>
merge({}, ...entityAndRelations)
)
}