Skip to content

Commit

Permalink
group-call-002 - uses create enum
Browse files Browse the repository at this point in the history
  • Loading branch information
filipenevola committed Apr 13, 2024
1 parent 9f003d6 commit 0de4fc0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
80 changes: 42 additions & 38 deletions group-call-002/TicketFilters.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createEnum } from "./createEnum";

const equals = (a, b) => {
return a === b;
};
Expand All @@ -12,49 +14,51 @@ const greaterOrEqualThan = (a, b) => {

const executor = ({ query, event, filterEnum }) => {
const propFromQuery = query[filterEnum.propName];
if (!propFromQuery) {
return true;
}

const propFromEvent = event[filterEnum.propName];
return !propFromQuery || filterEnum.operator(propFromEvent, propFromQuery);
return filterEnum.operator(propFromEvent, propFromQuery);
};

const defaultExecute = function (arg) {
return executor({ ...arg, filterEnum: this });
};
export const TicketFilters = {
BY_NAME: {
propName: "eventName",
isRequired: false,
operator: includes,
execute: defaultExecute,
},
BY_DATE: {
propName: "eventDate",
isRequired: false,
operator: equals,
execute: defaultExecute,
},
BY_TYPE: {
propName: "eventType",
isRequired: false,
operator: equals,
execute: defaultExecute,
},
BY_QUANTITY: {
propName: "quantity",
isRequired: true,
operator: greaterOrEqualThan,
execute: defaultExecute,
},
BY_STATUS: {
propName: "eventStatus",
isRequired: false,
operator: equals,
execute: defaultExecute,
},
BY_USER_VIP: {
propName: "onlyVIPUsers",
isRequired: false,
execute({ event, user }) {
return user?.isVIP || !event.onlyVIPUsers;

const isVIPUser = ({ event, user }) => {
return user?.isVIP || !event.onlyVIPUsers;
};

export const TicketFilters = createEnum(
{
BY_NAME: {
propName: "eventName",
isRequired: false,
operator: includes,
},
BY_DATE: {
propName: "eventDate",
isRequired: false,
},
BY_TYPE: {
propName: "eventType",
isRequired: false,
},
BY_QUANTITY: {
propName: "quantity",
isRequired: true,
operator: greaterOrEqualThan,
},
BY_STATUS: {
propName: "eventStatus",
isRequired: false,
},
BY_USER_VIP: {
propName: "onlyVIPUsers",
isRequired: false,
execute: isVIPUser,
},
},
};
{ defaultFields: { execute: defaultExecute, operator: equals } },
);
13 changes: 13 additions & 0 deletions group-call-002/createEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const createEnum = (obj, options = {}) =>
Object.entries(obj).reduce(
(acc, [key, value], index) => ({
...acc,
[key]: {
...options.defaultFields,
name: key,
index,
...value,
},
}),
{},
);

0 comments on commit 0de4fc0

Please sign in to comment.