Skip to content

Instantly share code, notes, and snippets.

@JAStanton
Last active August 18, 2022 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JAStanton/72cbd0fb882a9620a94a2d4d13f51ae6 to your computer and use it in GitHub Desktop.
Save JAStanton/72cbd0fb882a9620a94a2d4d13f51ae6 to your computer and use it in GitHub Desktop.
{
"promo": "NTDAW",
"restriction": {
"type": "DATE",
"start": "...",
"end": "...",
"tz": "America/Los_Angeles"
}
}
enum PromoValues {
NONE,
}
enum RestrictionType {
DATE,
}
async function getPaymentSpeedDefinitions(context) {
let promoType = PROMO_TYPE.NONE;
if (await tdClient.isSwitchOn(PROMOTIONS)) {
promoType = await getPromoType(context);
}
return await context.dao.paymentSpeed.findAllPOaymentSpeedDefinitions(promoType);
}
async function getPromoType(context): PromoType {
const flagResult = await tdclient.getFlag(flag_name, { carrierId: context.carrierId })
const promoDetails = flagResult.jsonValues();
if (!promoDetails) {
return PromoValues.NONE;
}
const { restriction, promoType } = promoDetails;
switch (restriction.type as RestrictionType) {
case RestrictionType.DATE:
return getDateRestrictionType(promoDetails);
case DEFAULT:
console.error('unsupported promo type', { context } );
return PromoValues.NONE;
}
}
function getDateRestrictionType({ restriction: { start, end, tz }, promoType }) {
const now = moment.tz(tz);
const start = moment.tz(tz, start);
const end = moment.tz(tz, end);
if (now.between(start, end)) {
return promoType;
}
return PromoValues.NONE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment