From 64bcd0524d7dbf04c7c5f8927b3e1712b3a51eef Mon Sep 17 00:00:00 2001 From: Avior Date: Tue, 5 Nov 2024 19:03:12 +0100 Subject: [PATCH] fix: no data was returned when filtering with `set` (#568) --- ...data-returned-when-filtering-using-set.bru | 21 ++++++++++++ server/src/V2/endpoints/jsonEndpoints.ts | 34 +++++++++++++++---- 2 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 .bruno/fixes/567-invalid-data-returned-when-filtering-using-set.bru diff --git a/.bruno/fixes/567-invalid-data-returned-when-filtering-using-set.bru b/.bruno/fixes/567-invalid-data-returned-when-filtering-using-set.bru new file mode 100644 index 000000000..7fd289e0c --- /dev/null +++ b/.bruno/fixes/567-invalid-data-returned-when-filtering-using-set.bru @@ -0,0 +1,21 @@ +meta { + name: 567 - invalid data returned when filtering using set + type: http + seq: 9 +} + +get { + url: {{BASE_URL}}/v2/en/cards?name=umbreon&set=evolving skies + body: none + auth: none +} + +params:query { + name: umbreon + set: evolving skies +} + +assert { + res.status: eq 200 + res.body.length: lt 0 +} diff --git a/server/src/V2/endpoints/jsonEndpoints.ts b/server/src/V2/endpoints/jsonEndpoints.ts index a5e52250c..823d4902c 100644 --- a/server/src/V2/endpoints/jsonEndpoints.ts +++ b/server/src/V2/endpoints/jsonEndpoints.ts @@ -128,17 +128,37 @@ server let result: unknown switch (endpoint) { - case 'cards': + case 'cards': { + if ('set' in query) { + const tmp = query.set + delete query.set + query.$or = [{ + 'set.id': tmp + }, { + 'set.name': tmp + }] + } result = Card .find(lang, query) .map((c) => c.resume()) break - - case 'sets': + } + + case 'sets': { + if ('serie' in query) { + const tmp = query.serie + delete query.serie + query.$or = [{ + 'serie.id': tmp + }, { + 'serie.name': tmp + }] + } result = TCGSet .find(lang, query) .map((c) => c.resume()) break + } case 'series': result = Serie .find(lang, query) @@ -233,8 +253,8 @@ server } result = { name: id, - cards: Card.find(lang, {[endpointToField[endpoint]]: id}) - .map((c) => c.resume()) + cards: Card.find(lang, { [endpointToField[endpoint]]: id }) + .map((c) => c.resume()) } } if (!result) { @@ -269,8 +289,8 @@ server case 'sets': // allow the dev to use a non prefixed value like `10` instead of `010` for newer sets result = Card - // @ts-expect-error normal behavior until the filtering is more fiable - .findOne(lang, { localId: { $or: [subid.padStart(3, '0'), subid]}, $or: [{ 'set.id': id }, { 'set.name': id }] })?.full() + // @ts-expect-error normal behavior until the filtering is more fiable + .findOne(lang, { localId: { $or: [subid.padStart(3, '0'), subid] }, $or: [{ 'set.id': id }, { 'set.name': id }] })?.full() break } if (!result) {