Fixed fallback affiliate query causing duplicate results in channel aggregate.
This commit is contained in:
parent
83efdf59d4
commit
456b69f1ca
|
|
@ -97,18 +97,18 @@ export async function fetchEntitiesById(entityIds, options = {}, reqUser) {
|
|||
.select(
|
||||
'entities.*',
|
||||
knex.raw('row_to_json(parents) as parent'),
|
||||
knex.raw('row_to_json(affiliates) as affiliate'),
|
||||
knex.raw('coalesce(row_to_json(affiliates), row_to_json(network_affiliates)) as affiliate'),
|
||||
)
|
||||
.whereIn('entities.id', entityIds)
|
||||
.leftJoin('entities as parents', 'parents.id', 'entities.parent_id')
|
||||
.leftJoin('affiliates', knex.raw('affiliates.entity_id in (entities.id, parents.id)'))
|
||||
.leftJoin('affiliates', 'affiliates.entity_id', 'entities.id')
|
||||
.leftJoin('affiliates as network_affiliates', 'network_affiliates.entity_id', 'parents.id')
|
||||
.modify((builder) => {
|
||||
if (options.order) {
|
||||
builder.orderBy(...options.order);
|
||||
}
|
||||
})
|
||||
.orderBy(knex.raw('case when affiliates.entity_id = entities.id then 1 else 2 end'))
|
||||
.groupBy('entities.id', 'parents.id', 'affiliates.id', 'affiliates.entity_id'),
|
||||
.groupBy('entities.id', 'parents.id', 'affiliates.id', 'affiliates.entity_id', 'network_affiliates.id', 'network_affiliates.entity_id'),
|
||||
options.includeChildren ? knex('entities')
|
||||
.whereIn('entities.parent_id', entityIds)
|
||||
.whereNot('type', 'info')
|
||||
|
|
|
|||
Loading…
Reference in New Issue