From 0de4fc0496245fd015c45459cfbff5ee70f0f0ec Mon Sep 17 00:00:00 2001 From: filipenevola Date: Sat, 13 Apr 2024 15:30:09 -0400 Subject: [PATCH] group-call-002 - uses create enum --- group-call-002/TicketFilters.js | 80 +++++++++++++++++---------------- group-call-002/createEnum.js | 13 ++++++ 2 files changed, 55 insertions(+), 38 deletions(-) create mode 100644 group-call-002/createEnum.js diff --git a/group-call-002/TicketFilters.js b/group-call-002/TicketFilters.js index 20d7353..50a7bd3 100644 --- a/group-call-002/TicketFilters.js +++ b/group-call-002/TicketFilters.js @@ -1,3 +1,5 @@ +import { createEnum } from "./createEnum"; + const equals = (a, b) => { return a === b; }; @@ -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 } }, +); diff --git a/group-call-002/createEnum.js b/group-call-002/createEnum.js new file mode 100644 index 0000000..6f6dc30 --- /dev/null +++ b/group-call-002/createEnum.js @@ -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, + }, + }), + {}, + );