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 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) { for (const rel of relations) {
const [topLevel] = rel.split(".") const [topLevel] = rel.split(".")
if (groupedRelations[topLevel]) { if (groupedRelations[topLevel]) {
@@ -32,35 +37,49 @@ export class ProductRepository extends Repository<Product> {
const entitiesIdsWithRelations = await Promise.all( const entitiesIdsWithRelations = await Promise.all(
Object.entries(groupedRelations).map(([toplevel, rels]) => { Object.entries(groupedRelations).map(([toplevel, rels]) => {
let querybuilder = this.createQueryBuilder("products") let querybuilder = this.createQueryBuilder("products")
if (toplevel === "variants") { 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({ .orderBy({
"variants.variant_rank": "ASC", "variants.variant_rank": "ASC",
}) })
} else { } 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(".") const [_, rest] = rel.split(".")
if (!rest) { if (!rest) {
continue continue
} }
// Regex matches all '.' except the rightmost // Regex matches all '.' except the rightmost
querybuilder = querybuilder.leftJoinAndSelect(rel.replace(/\.(?=[^.]*\.)/g,"__"), rel.replace(".", "__")) querybuilder = querybuilder.leftJoinAndSelect(
rel.replace(/\.(?=[^.]*\.)/g, "__"),
rel.replace(".", "__")
)
} }
return querybuilder return querybuilder
.where("products.deleted_at IS NULL AND products.id IN (:...entitiesIds)", { entitiesIds }) .where(
.getMany(); "products.deleted_at IS NULL AND products.id IN (:...entitiesIds)",
{ entitiesIds }
)
.getMany()
}) })
).then(flatten) ).then(flatten)
const entitiesAndRelations = entitiesIdsWithRelations.concat(entities) const entitiesAndRelations = entitiesIdsWithRelations.concat(entities)
const entitiesAndRelationsById = groupBy(entitiesAndRelations, "id") const entitiesAndRelationsById = groupBy(entitiesAndRelations, "id")
return map(entitiesAndRelationsById, entityAndRelations => return map(entitiesAndRelationsById, (entityAndRelations) =>
merge({}, ...entityAndRelations) merge({}, ...entityAndRelations)
) )
} }