From db4d240f0a87afe3b37f8f508e4f5f4e5c87f205 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Fri, 14 Apr 2023 17:49:01 +0600 Subject: [PATCH] TSK-1182 Filter "Is not" doesn't work (#2985) --- packages/presentation/src/status.ts | 52 +++++++++++++++++++---------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/packages/presentation/src/status.ts b/packages/presentation/src/status.ts index d5bc20c981..bfdb337ca7 100644 --- a/packages/presentation/src/status.ts +++ b/packages/presentation/src/status.ts @@ -199,6 +199,22 @@ export class StatusMiddleware extends BasePresentationMiddleware implements Pres return result } + private categorizeStatus (mgr: StatusManager, attr: AnyAttribute, target: Array>): Array> { + for (const sid of [...target]) { + const s = mgr.byId.get(sid) + if (s !== undefined) { + const statuses = mgr.statuses.filter( + (it) => + it.ofAttribute === attr._id && + it.name.toLowerCase().trim() === s.name.toLowerCase().trim() && + it._id !== s._id + ) + target.push(...statuses.map((it) => it._id)) + } + } + return target.filter((it, idx, arr) => arr.indexOf(it) === idx) + } + private async updateQueryOptions( allAttrs: Map, h: Hierarchy, @@ -211,34 +227,36 @@ export class StatusMiddleware extends BasePresentationMiddleware implements Pres if (attr.type._class === core.class.RefTo && h.isDerived((attr.type as RefTo).to, core.class.Status)) { const mgr = await this.getManager() let target: Array> = [] + let targetNin: Array> = [] statusFields.push(attr) const v = (query as any)[attr.name] if (v !== undefined) { // Only add filter if we have filer inside. - if (v?.$in !== undefined) { - target.push(...v.$in) + if (typeof v === 'string') { + target.push(v as Ref) } else { - target.push(v) + if (v.$in !== undefined) { + target.push(...v.$in) + } else if (v.$nin !== undefined) { + targetNin.push(...v.$nin) + } else if (v.$ne !== undefined) { + targetNin.push(v.$ne) + } } // Find all similar name statues for same attribute name. - for (const sid of [...target]) { - const s = mgr.byId.get(sid) - if (s !== undefined) { - const statuses = mgr.statuses.filter( - (it) => - it.ofAttribute === attr._id && - it.name.toLowerCase().trim() === s.name.toLowerCase().trim() && - it._id !== s._id - ) - if (statuses !== undefined) { - target.push(...statuses.map((it) => it._id)) - } + target = this.categorizeStatus(mgr, attr, target) + targetNin = this.categorizeStatus(mgr, attr, targetNin) + if (target.length > 0 || targetNin.length > 0) { + ;(query as any)[attr.name] = {} + if (target.length > 0) { + ;(query as any)[attr.name].$in = target + } + if (targetNin.length > 0) { + ;(query as any)[attr.name].$nin = targetNin } } - target = target.filter((it, idx, arr) => arr.indexOf(it) === idx) - ;(query as any)[attr.name] = { $in: target } if (finalOptions.lookup !== undefined) { // Remove lookups by status field