From 16de798916795f563647338ef5e17b98689ccb59 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Mon, 1 May 2023 18:30:13 +0200 Subject: [PATCH] slot fill filter now looks at libraryTags not tags --- .../methods/getSlotFillFilter.js | 42 +++++----- .../methods/getSlotFillFilter.test.js | 84 +++++++++++++++++++ app/jsconfig.json | 2 +- 3 files changed, 107 insertions(+), 21 deletions(-) create mode 100644 app/imports/api/creature/creatureProperties/methods/getSlotFillFilter.test.js diff --git a/app/imports/api/creature/creatureProperties/methods/getSlotFillFilter.js b/app/imports/api/creature/creatureProperties/methods/getSlotFillFilter.js index d4670c95..a25e7bf4 100644 --- a/app/imports/api/creature/creatureProperties/methods/getSlotFillFilter.js +++ b/app/imports/api/creature/creatureProperties/methods/getSlotFillFilter.js @@ -1,16 +1,18 @@ -export default function getSlotFillFilter({slot, libraryIds}){ +export default function getSlotFillFilter({ slot, libraryIds }) { + + if (!slot) throw 'Slot is required'; + if (!libraryIds) throw 'LibraryIds is required'; + let filter = { - removed: {$ne: true}, + removed: { $ne: true }, $and: [] }; - if (libraryIds){ - filter['ancestors.id'] = {$in: libraryIds}; - } - if (slot.slotType){ + filter['ancestors.id'] = { $in: libraryIds }; + if (slot.slotType) { filter.$and.push({ $or: [{ type: slot.slotType - },{ + }, { type: 'slotFiller', slotFillerType: slot.slotType, }] @@ -19,44 +21,44 @@ export default function getSlotFillFilter({slot, libraryIds}){ filter.$and.push({ $or: [{ type: 'classLevel', - },{ + }, { type: 'slotFiller', slotFillerType: 'classLevel', }] }); - if (slot.variableName) { + if (slot.variableName) { filter.variableName = slot.variableName; } // Only search for levels the class needs if (slot.missingLevels && slot.missingLevels.length) { - filter.level = {$in: slot.missingLevels}; + filter.level = { $in: slot.missingLevels }; } else { filter.level = (slot.level || 0) + 1; } } let tagsOr = []; let tagsNin = []; - if (slot.slotTags && slot.slotTags.length){ - tagsOr.push({tags: {$all: slot.slotTags}}); + if (slot.slotTags && slot.slotTags.length) { + tagsOr.push({ libraryTags: { $all: slot.slotTags } }); } - if (slot.extraTags && slot.extraTags.length){ + if (slot.extraTags && slot.extraTags.length) { slot.extraTags.forEach(extra => { if (!extra.tags || !extra.tags.length) return; - if (extra.operation === 'OR'){ - tagsOr.push({tags: {$all: extra.tags}}); - } else if (extra.operation === 'NOT'){ + if (extra.operation === 'OR') { + tagsOr.push({ libraryTags: { $all: extra.tags } }); + } else if (extra.operation === 'NOT') { tagsNin.push(...extra.tags); } }); } - if (tagsOr.length){ + if (tagsOr.length) { filter.$or = tagsOr; } - if (tagsNin.length){ - filter.$and.push({tags: {$nin: tagsNin}}); + if (tagsNin.length) { + filter.$and.push({ libraryTags: { $nin: tagsNin } }); } - if (!filter.$and.length){ + if (!filter.$and.length) { delete filter.$and; } return filter; diff --git a/app/imports/api/creature/creatureProperties/methods/getSlotFillFilter.test.js b/app/imports/api/creature/creatureProperties/methods/getSlotFillFilter.test.js new file mode 100644 index 00000000..2e4048d2 --- /dev/null +++ b/app/imports/api/creature/creatureProperties/methods/getSlotFillFilter.test.js @@ -0,0 +1,84 @@ +import { assert } from 'chai'; +import getSlotFillFilter from '/imports/api/creature/creatureProperties/methods/getSlotFillFilter.js'; + +describe('Slot fill filter', function () { + + it('Gives error if arguments aren\'t provided', function () { + assert.throws( + () => getSlotFillFilter(undefined), + null, null, 'Passing undefined should give an error' + ); + assert.throws( + () => getSlotFillFilter({ + slot: { slotTags: ['tag1'] }, + }), + null, null, 'Passing no libraryIds should give an error' + ); + assert.throws( + () => getSlotFillFilter({ + libraryIds: ['libraryId1'], + }), + null, null, 'Passing no slot should give an error' + ); + }); + + it('filters using basic slot tags', function () { + const filter = getSlotFillFilter({ + slot: { + slotTags: ['tag1', 'tag2'] + }, + libraryIds: ['libraryId1', 'libraryId2'] + }); + assert.deepStrictEqual(filter, { + $or: [{ + libraryTags: { $all: ['tag1', 'tag2'] } + }], + 'ancestors.id': { $in: ['libraryId1', 'libraryId2'] }, + removed: { $ne: true }, + }); + }); + + it('filters using slot type', function () { + const filter = getSlotFillFilter({ + slot: { + slotTags: ['tag1', 'tag2'], + slotType: 'feature', + }, + libraryIds: ['libraryId1', 'libraryId2'] + }); + assert.deepStrictEqual(filter.$and, [{ + $or: [{ + type: 'feature' + }, { + type: 'slotFiller', + slotFillerType: 'feature', + }], + }]); + }); + + it('filters using extra tags', function () { + const filter = getSlotFillFilter({ + slot: { + slotTags: ['tag1', 'tag2'], + extraTags: [ + { operation: 'OR', tags: ['tag3', 'tag4'] }, + { operation: 'NOT', tags: ['tag5', 'tag6'] }, + { operation: 'NOT', tags: ['tag7', 'tag8'] }, + ], + }, + libraryIds: ['libraryId1', 'libraryId2'] + }); + assert.deepStrictEqual(filter, { + $or: [ + { libraryTags: { $all: ['tag1', 'tag2'] } }, + { libraryTags: { $all: ['tag3', 'tag4'] } }, + ], + $and: [ + { libraryTags: { $nin: ['tag5', 'tag6', 'tag7', 'tag8'] } }, + ], + 'ancestors.id': { $in: ['libraryId1', 'libraryId2'] }, + removed: { $ne: true }, + }); + }); + +}); \ No newline at end of file diff --git a/app/jsconfig.json b/app/jsconfig.json index 9843a174..82b81ff2 100644 --- a/app/jsconfig.json +++ b/app/jsconfig.json @@ -15,7 +15,7 @@ "packages\\collection2\\collection2.js" ] }, - "checkJs": true, + "checkJs": false, "allowJs": true }, "vueCompilerOptions": {