From bee884583cbccc3131b94058fe4bfaf715c758d9 Mon Sep 17 00:00:00 2001 From: filipenevola Date: Sat, 13 Apr 2024 11:54:02 -0400 Subject: [PATCH] group-call-002 - TicketFilters enum test --- group-call-002/TicketFilters.js | 37 +++++++++++++++++++++ group-call-002/TicketFilters.test.js | 14 ++++++++ group-call-002/getTicketsByQuery.js | 41 +++++++----------------- group-call-002/getTicketsByQuery.test.js | 1 + 4 files changed, 63 insertions(+), 30 deletions(-) create mode 100644 group-call-002/TicketFilters.js create mode 100644 group-call-002/TicketFilters.test.js diff --git a/group-call-002/TicketFilters.js b/group-call-002/TicketFilters.js new file mode 100644 index 0000000..4466d9d --- /dev/null +++ b/group-call-002/TicketFilters.js @@ -0,0 +1,37 @@ +export const TicketFilters = { + BY_NAME: { + propName: 'eventName', + isRequired: false, + execute({query: {eventName}, event}) { + return !eventName || event.eventName.includes(eventName); + }, + }, + BY_DATE: { + propName: 'eventDate', + isRequired: false, + execute({query: {eventDate}, event}) { + return !eventDate || event.eventDate === eventDate; + }, + }, + BY_QUANTITY: { + propName: 'quantity', + isRequired: true, + execute({query: {quantity}, event}) { + return !quantity || event.quantity >= quantity; + }, + }, + BY_STATUS: { + propName: 'status', + isRequired: false, + execute({query: {status}, event}) { + return !status || event.eventStatus === status; + }, + }, + BY_USER_VIP: { + propName: 'quantity', + isRequired: false, + execute({event, user}) { + return user?.isVIP || !event.onlyVIPUsers; + }, + }, +}; diff --git a/group-call-002/TicketFilters.test.js b/group-call-002/TicketFilters.test.js new file mode 100644 index 0000000..46a3547 --- /dev/null +++ b/group-call-002/TicketFilters.test.js @@ -0,0 +1,14 @@ +import { TicketFilters } from "./TicketFilters.js"; + +describe("TicketFilters", () => { + test("isRequired is present", () => { + Object.values(TicketFilters).forEach(({ isRequired }) => { + expect(isRequired).toBeDefined(); + }); + }); + test("propName is present", () => { + Object.values(TicketFilters).forEach(({ propName }) => { + expect(propName).toBeDefined(); + }); + }); +}); diff --git a/group-call-002/getTicketsByQuery.js b/group-call-002/getTicketsByQuery.js index 9924ede..5c88311 100644 --- a/group-call-002/getTicketsByQuery.js +++ b/group-call-002/getTicketsByQuery.js @@ -1,39 +1,20 @@ import { UsersCollection } from "./UsersCollection.js"; import { EventsCollection } from "./EventsCollection.js"; - -const Filters = { - BY_NAME: { - execute({ query: { eventName }, event }) { - return !eventName || event.eventName.includes(eventName); - }, - }, - BY_DATE: { - execute({ query: { eventDate }, event }) { - return !eventDate || event.eventDate === eventDate; - }, - }, - BY_QUANTITY: { - execute({ query: { quantity }, event }) { - return !quantity || event.quantity >= quantity; - }, - }, - BY_STATUS: { - execute({ query: { status }, event }) { - return !status || event.eventStatus === status; - }, - }, - BY_USER_VIP: { - execute({ event, user }) { - return user?.isVIP || !event.onlyVIPUsers; - }, - }, -}; +import { TicketFilters } from "./TicketFilters.js"; export function getTicketsByQuery(query) { - const user = UsersCollection.find().find((user) => user.userId === +query.userId); + Object.values(TicketFilters).forEach(({ isRequired, propName }) => { + if (isRequired && !query[propName]) { + throw new Error(`Query parameter "${propName}" is required`); + } + }); + + const user = UsersCollection.find().find( + (user) => user.userId === +query.userId, + ); return EventsCollection.find().filter((event) => { - return Object.values(Filters).every(({ execute }) => + return Object.values(TicketFilters).every(({ execute }) => execute({ event, user, diff --git a/group-call-002/getTicketsByQuery.test.js b/group-call-002/getTicketsByQuery.test.js index 8740007..ffb2262 100644 --- a/group-call-002/getTicketsByQuery.test.js +++ b/group-call-002/getTicketsByQuery.test.js @@ -11,6 +11,7 @@ describe("getTicketsByQuery", () => { }); test(`returns all events with "Con" for VIP User`, () => { const events = getTicketsByQuery({ + quantity: 1, userId: 1, eventName: "Con", });