Last active
July 4, 2021 06:05
-
-
Save xeptore/3845f7ac7d19d5b76f62ae9f297387d3 to your computer and use it in GitHub Desktop.
Voucher Validator Idea Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const enum Gender { | |
Male = "Male", | |
Female = "Female", | |
} | |
interface Order { | |
id: string; | |
date: Date; | |
totalPrice: number; | |
customer: { | |
id: string; | |
device: { | |
name: string; | |
os: string; | |
} | |
} | |
} | |
interface Voucher { | |
id: string; | |
orderApplicabilityAttempt: number; | |
allowedCustomerGender: Gender; | |
} | |
type ValidatorFunction = () => boolean; | |
function isInValidDateRange(orderDate: Date, voucherId: string): boolean { | |
// TODO: check whether the `orderDate` is in valid date range specified for voucher with id `voucherId`. | |
return true; | |
} | |
function isBelowMaximumAmount(orderTotalPrice: number, voucherId: string): boolean { | |
// TODO: check whether an order with total price of `orderTotalPrice` exceeds voucher with id `voucherId` maximum price | |
return true; | |
} | |
function isCustomerAccessingWithValidDevice(customerDeviceName: string, voucherId: string): boolean { | |
// TODO: check whether customer's device name is allowed to use voucher with id `voucherId` | |
return true; | |
} | |
function isValidNthOrder(orderId: string, voucherOrderAttemptApplicability: number): boolean { | |
// TODO: even query order service to get customer's order attempt number, then check whether the voucher applies to current order | |
return true; | |
} | |
function isCustomerGenderValid(customerId: string, voucherAllowedGender: Gender): boolean { | |
// TODO: query 'users service' to retrieve user's gender, then compare with voucher allowed gender | |
return true; | |
} | |
function validate(order: Order, voucher: Voucher) { | |
const validators: ReadonlyArray<ValidatorFunction> = [ | |
() => isInValidDateRange(order.date, voucher.id), | |
() => isBelowMaximumAmount(order.totalPrice, voucher.id), | |
() => isCustomerAccessingWithValidDevice(order.customer.device.name, voucher.id), | |
() => isValidNthOrder(order.id, voucher.orderApplicabilityAttempt), | |
() => isCustomerGenderValid(order.customer.id, voucher.allowedCustomerGender), | |
// Other validators can be added accordingly as needed. | |
// or even, a validator can be added conditionally | |
// or decide between two validators based on simple `order` or `voucher` | |
// properties, then the chosen validator is added to this list. | |
]; | |
return validators.every((validator) => validator()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment