From 47191c0b3753b0ce02936212ccc63f45ee2f5e2a Mon Sep 17 00:00:00 2001 From: Sergei Ogorelkov Date: Fri, 10 Feb 2023 22:13:07 +0600 Subject: [PATCH] Fix query if "_class" is object with "$nin" (#2620) Signed-off-by: Sergei Ogorelkov --- server/mongo/src/storage.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/server/mongo/src/storage.ts b/server/mongo/src/storage.ts index cea75aaf60..338920020e 100644 --- a/server/mongo/src/storage.ts +++ b/server/mongo/src/storage.ts @@ -121,6 +121,24 @@ abstract class MongoAdapterBase implements DbAdapter { // Only replace if not specified if (translated._class === undefined) { translated._class = { $in: classes } + } else if (typeof translated._class === 'string') { + if (!classes.includes(translated._class)) { + translated._class = { $in: classes } + } + } else if (typeof translated._class === 'object') { + const classesIds = new Set(classes) + let descendants: Ref>[] = classes + + if (Array.isArray(translated._class.$in)) { + descendants = translated._class.$in.filter((c: Ref>) => classesIds.has(c)) + } + + if (Array.isArray(translated._class.$nin)) { + const excludedClassesIds = new Set>>(translated._class.$nin) + descendants = descendants.filter((c) => !excludedClassesIds.has(c)) + } + + translated._class = { $in: descendants } } if (baseClass !== clazz) {