From 9f003d669e197cb517d8dc3e43dd232e3042cd0e Mon Sep 17 00:00:00 2001 From: filipenevola Date: Sat, 13 Apr 2024 14:08:40 -0400 Subject: [PATCH] group-call-002 - compose functions --- group-call-002/TicketFilters.js | 51 +++++++++++++++++------- group-call-002/app.js | 8 +++- group-call-002/getTicketsByQuery.js | 4 +- group-call-002/getTicketsByQuery.test.js | 2 +- 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/group-call-002/TicketFilters.js b/group-call-002/TicketFilters.js index a9728c4..20d7353 100644 --- a/group-call-002/TicketFilters.js +++ b/group-call-002/TicketFilters.js @@ -1,34 +1,57 @@ +const equals = (a, b) => { + return a === b; +}; + +const includes = (a, b) => { + return a.includes(b); +}; + +const greaterOrEqualThan = (a, b) => { + return a >= b; +}; + +const executor = ({ query, event, filterEnum }) => { + const propFromQuery = query[filterEnum.propName]; + const propFromEvent = event[filterEnum.propName]; + return !propFromQuery || filterEnum.operator(propFromEvent, propFromQuery); +}; + +const defaultExecute = function (arg) { + return executor({ ...arg, filterEnum: this }); +}; export const TicketFilters = { BY_NAME: { propName: "eventName", isRequired: false, - execute({ query: { eventName }, event }) { - return !eventName || event.eventName.includes(eventName); - }, + operator: includes, + execute: defaultExecute, }, BY_DATE: { propName: "eventDate", isRequired: false, - execute({ query: { eventDate }, event }) { - return !eventDate || event.eventDate === eventDate; - }, + operator: equals, + execute: defaultExecute, + }, + BY_TYPE: { + propName: "eventType", + isRequired: false, + operator: equals, + execute: defaultExecute, }, BY_QUANTITY: { propName: "quantity", isRequired: true, - execute({ query: { quantity }, event }) { - return !quantity || event.quantity >= quantity; - }, + operator: greaterOrEqualThan, + execute: defaultExecute, }, BY_STATUS: { - propName: "status", + propName: "eventStatus", isRequired: false, - execute({ query: { status }, event }) { - return !status || event.eventStatus === status; - }, + operator: equals, + execute: defaultExecute, }, BY_USER_VIP: { - propName: "quantity", + propName: "onlyVIPUsers", isRequired: false, execute({ event, user }) { return user?.isVIP || !event.onlyVIPUsers; diff --git a/group-call-002/app.js b/group-call-002/app.js index b73f782..812dd64 100644 --- a/group-call-002/app.js +++ b/group-call-002/app.js @@ -2,6 +2,7 @@ import express from "express"; import { EventsCollection } from "./EventsCollection.js"; import { UsersCollection } from "./UsersCollection.js"; import { getTicketsByQuery } from "./getTicketsByQuery.js"; +import { TicketFilters } from "./TicketFilters.js"; const app = express(); @@ -10,7 +11,12 @@ app.get("/", (req, res) => { }); app.get("/tickets-available", (req, res) => { - return res.json(getTicketsByQuery({ ...query, status: "upcoming" })); + return res.json( + getTicketsByQuery({ + ...query, + [TicketFilters.BY_STATUS.propName]: "upcoming", + }), + ); }); app.get("/tickets", (req, res) => { diff --git a/group-call-002/getTicketsByQuery.js b/group-call-002/getTicketsByQuery.js index 5c88311..b005f10 100644 --- a/group-call-002/getTicketsByQuery.js +++ b/group-call-002/getTicketsByQuery.js @@ -14,8 +14,8 @@ export function getTicketsByQuery(query) { ); return EventsCollection.find().filter((event) => { - return Object.values(TicketFilters).every(({ execute }) => - execute({ + return Object.values(TicketFilters).every((ticketFilterEnum) => + ticketFilterEnum.execute({ event, user, query, diff --git a/group-call-002/getTicketsByQuery.test.js b/group-call-002/getTicketsByQuery.test.js index c35775a..29639e1 100644 --- a/group-call-002/getTicketsByQuery.test.js +++ b/group-call-002/getTicketsByQuery.test.js @@ -6,7 +6,7 @@ const DATA = [ input: { eventName: "Con", quantity: 101, - status: "upcoming", + eventStatus: "upcoming", }, expected: 0, },