Skip to content

Commit

Permalink
group-call-002 - declarative filters
Browse files Browse the repository at this point in the history
  • Loading branch information
filipenevola committed Apr 2, 2024
1 parent 73c16a7 commit 7776b2b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 67 deletions.
10 changes: 7 additions & 3 deletions group-call-002/app.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
52 changes: 0 additions & 52 deletions group-call-002/getTicketsAvailable.js

This file was deleted.

12 changes: 0 additions & 12 deletions group-call-002/getTicketsAvailable.test.js

This file was deleted.

44 changes: 44 additions & 0 deletions group-call-002/getTicketsByQuery.js
Original file line number Diff line number Diff line change
@@ -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,
}),
);
});
}
19 changes: 19 additions & 0 deletions group-call-002/getTicketsByQuery.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
1 change: 1 addition & 0 deletions group-call-002/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type": "module",
"scripts": {
"start": "node app.js",
"dev:watch": "node --watch app.js",
"test": "jest"
},
"repository": {
Expand Down

0 comments on commit 7776b2b

Please sign in to comment.