slot fill filter now looks at libraryTags not tags

This commit is contained in:
Stefan Zermatten
2023-05-01 18:30:13 +02:00
parent 656a079c58
commit 16de798916
3 changed files with 107 additions and 21 deletions

View File

@@ -1,11 +1,13 @@
export default function getSlotFillFilter({ slot, libraryIds }) {
if (!slot) throw 'Slot is required';
if (!libraryIds) throw 'LibraryIds is required';
let filter = {
removed: { $ne: true },
$and: []
};
if (libraryIds){
filter['ancestors.id'] = { $in: libraryIds };
}
if (slot.slotType) {
filter.$and.push({
$or: [{
@@ -38,13 +40,13 @@ export default function getSlotFillFilter({slot, libraryIds}){
let tagsOr = [];
let tagsNin = [];
if (slot.slotTags && slot.slotTags.length) {
tagsOr.push({tags: {$all: slot.slotTags}});
tagsOr.push({ libraryTags: { $all: slot.slotTags } });
}
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}});
tagsOr.push({ libraryTags: { $all: extra.tags } });
} else if (extra.operation === 'NOT') {
tagsNin.push(...extra.tags);
}
@@ -54,7 +56,7 @@ export default function getSlotFillFilter({slot, libraryIds}){
filter.$or = tagsOr;
}
if (tagsNin.length) {
filter.$and.push({tags: {$nin: tagsNin}});
filter.$and.push({ libraryTags: { $nin: tagsNin } });
}
if (!filter.$and.length) {
delete filter.$and;

View File

@@ -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 },
});
});
});

View File

@@ -15,7 +15,7 @@
"packages\\collection2\\collection2.js"
]
},
"checkJs": true,
"checkJs": false,
"allowJs": true
},
"vueCompilerOptions": {