Skip to content

Commit

Permalink
group-call-002 - compose functions
Browse files Browse the repository at this point in the history
  • Loading branch information
filipenevola committed Apr 13, 2024
1 parent 0fa5b01 commit 9f003d6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
51 changes: 37 additions & 14 deletions group-call-002/TicketFilters.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 7 additions & 1 deletion group-call-002/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions group-call-002/getTicketsByQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion group-call-002/getTicketsByQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const DATA = [
input: {
eventName: "Con",
quantity: 101,
status: "upcoming",
eventStatus: "upcoming",
},
expected: 0,
},
Expand Down

0 comments on commit 9f003d6

Please sign in to comment.