Skip to content

Commit

Permalink
group-call-002 - TicketFilters enum test
Browse files Browse the repository at this point in the history
  • Loading branch information
filipenevola committed Apr 13, 2024
1 parent 7776b2b commit bee8845
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
37 changes: 37 additions & 0 deletions group-call-002/TicketFilters.js
Original file line number Diff line number Diff line change
@@ -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;
},
},
};
14 changes: 14 additions & 0 deletions group-call-002/TicketFilters.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
41 changes: 11 additions & 30 deletions group-call-002/getTicketsByQuery.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 1 addition & 0 deletions group-call-002/getTicketsByQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("getTicketsByQuery", () => {
});
test(`returns all events with "Con" for VIP User`, () => {
const events = getTicketsByQuery({
quantity: 1,
userId: 1,
eventName: "Con",
});
Expand Down

0 comments on commit bee8845

Please sign in to comment.