From 7776b2bb3135f16005d03090cad23805f717de8c Mon Sep 17 00:00:00 2001 From: filipenevola Date: Tue, 2 Apr 2024 14:25:05 -0400 Subject: [PATCH] group-call-002 - declarative filters --- group-call-002/app.js | 10 +++-- group-call-002/getTicketsAvailable.js | 52 ---------------------- group-call-002/getTicketsAvailable.test.js | 12 ----- group-call-002/getTicketsByQuery.js | 44 ++++++++++++++++++ group-call-002/getTicketsByQuery.test.js | 19 ++++++++ group-call-002/package.json | 1 + 6 files changed, 71 insertions(+), 67 deletions(-) delete mode 100644 group-call-002/getTicketsAvailable.js delete mode 100644 group-call-002/getTicketsAvailable.test.js create mode 100644 group-call-002/getTicketsByQuery.js create mode 100644 group-call-002/getTicketsByQuery.test.js diff --git a/group-call-002/app.js b/group-call-002/app.js index d33713a..b73f782 100644 --- a/group-call-002/app.js +++ b/group-call-002/app.js @@ -1,16 +1,20 @@ import express from "express"; import { EventsCollection } from "./EventsCollection.js"; import { UsersCollection } from "./UsersCollection.js"; -import { getTicketsAvailable } from "./getTicketsAvailable.js"; +import { getTicketsByQuery } from "./getTicketsByQuery.js"; const app = express(); app.get("/", (req, res) => { - res.send("Hello World!"); + res.send("Hello World"); }); app.get("/tickets-available", (req, res) => { - return res.json(getTicketsAvailable(req.query)); + return res.json(getTicketsByQuery({ ...query, status: "upcoming" })); +}); + +app.get("/tickets", (req, res) => { + return res.json(getTicketsByQuery(req.query)); }); const port = 3000 || process.env.PORT; diff --git a/group-call-002/getTicketsAvailable.js b/group-call-002/getTicketsAvailable.js deleted file mode 100644 index c7ee226..0000000 --- a/group-call-002/getTicketsAvailable.js +++ /dev/null @@ -1,52 +0,0 @@ -import { UsersCollection } from "./UsersCollection.js"; -import { EventsCollection } from "./EventsCollection.js"; - -function filterByName(eventName, event) { - return !eventName || event.eventName.includes(eventName); -} - -function filterByDate(eventDate, event) { - return !eventDate || event.eventDate === eventDate; -} - -function filterByQuantity(quantity, event) { - return !quantity || event.quantity >= quantity; -} - -function filterByUpcoming(event) { - return event.eventStatus === "upcoming"; -} - -function filterByVIP(user, event) { - return user?.isVIP || !event.onlyVIPUsers; -} - -function filterAvailableEvents({ - eventName, - event, - eventDate, - quantity, - user, -}) { - return ( - filterByName(eventName, event) && - filterByDate(eventDate, event) && - filterByQuantity(quantity, event) && - filterByUpcoming(event) && - filterByVIP(user, event) - ); -} - -export function getTicketsAvailable({userId, eventName, eventDate, quantity}) { - const user = UsersCollection.find().find((user) => user.userId === userId); - - return EventsCollection.find().filter((event) => { - return filterAvailableEvents({ - eventName, - event, - eventDate, - quantity, - user, - }); - }); -} diff --git a/group-call-002/getTicketsAvailable.test.js b/group-call-002/getTicketsAvailable.test.js deleted file mode 100644 index 0a567fb..0000000 --- a/group-call-002/getTicketsAvailable.test.js +++ /dev/null @@ -1,12 +0,0 @@ -import { getTicketsAvailable } from "./getTicketsAvailable.js"; - -describe("getTicketsAvailable", () => { - test("returns empty array when there are no events", () => { - const events = getTicketsAvailable({ eventName: "Con", quantity: 101 }); - expect(events.length).toBe(0); - }); - test("returns one event", () => { - const events = getTicketsAvailable({ userId: 1, eventName: "Con", quantity: 99 }); - expect(events.length).toBe(1); - }); -}); diff --git a/group-call-002/getTicketsByQuery.js b/group-call-002/getTicketsByQuery.js new file mode 100644 index 0000000..9924ede --- /dev/null +++ b/group-call-002/getTicketsByQuery.js @@ -0,0 +1,44 @@ +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; + }, + }, +}; + +export function getTicketsByQuery(query) { + const user = UsersCollection.find().find((user) => user.userId === +query.userId); + + return EventsCollection.find().filter((event) => { + return Object.values(Filters).every(({ execute }) => + execute({ + event, + user, + query, + }), + ); + }); +} diff --git a/group-call-002/getTicketsByQuery.test.js b/group-call-002/getTicketsByQuery.test.js new file mode 100644 index 0000000..8740007 --- /dev/null +++ b/group-call-002/getTicketsByQuery.test.js @@ -0,0 +1,19 @@ +import { getTicketsByQuery } from "./getTicketsByQuery.js"; + +describe("getTicketsByQuery", () => { + test("returns empty array when there are no events", () => { + const events = getTicketsByQuery({ + eventName: "Con", + quantity: 101, + status: "upcoming", + }); + expect(events.length).toBe(0); + }); + test(`returns all events with "Con" for VIP User`, () => { + const events = getTicketsByQuery({ + userId: 1, + eventName: "Con", + }); + expect(events.length).toBe(2); + }); +}); diff --git a/group-call-002/package.json b/group-call-002/package.json index 094c843..dcdd105 100644 --- a/group-call-002/package.json +++ b/group-call-002/package.json @@ -6,6 +6,7 @@ "type": "module", "scripts": { "start": "node app.js", + "dev:watch": "node --watch app.js", "test": "jest" }, "repository": {