UNPKG

@iota/iota-names-sdk

Version:
134 lines (133 loc) 5.11 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var coupons_exports = {}; __export(coupons_exports, { COUPON_EXPIRED: () => COUPON_EXPIRED, INVALID_AVAILABLE_CLAIMS: () => INVALID_AVAILABLE_CLAIMS, INVALID_FOR_NAME_LENGTH: () => INVALID_FOR_NAME_LENGTH, INVALID_PERCENTAGE: () => INVALID_PERCENTAGE, INVALID_USER: () => INVALID_USER, INVALID_YEARS: () => INVALID_YEARS, NON_STACKING_COUPON: () => NON_STACKING_COUPON, applyCouponToPrice: () => applyCouponToPrice, applyCouponsToPrice: () => applyCouponsToPrice, hasAvailableClaims: () => hasAvailableClaims, isCouponExpired: () => isCouponExpired, isCouponValidForAddress: () => isCouponValidForAddress, isCouponValidForNameLength: () => isCouponValidForNameLength, isCouponValidForNameYears: () => isCouponValidForNameYears, isValidCouponPercentage: () => isValidCouponPercentage, validateCoupon: () => validateCoupon, validateCoupons: () => validateCoupons }); module.exports = __toCommonJS(coupons_exports); const INVALID_YEARS = "Coupon is not valid for the given number of years."; const INVALID_FOR_NAME_LENGTH = "Coupon is not valid for the given name length."; const INVALID_PERCENTAGE = "Invalid percentage amount for coupon."; const INVALID_USER = "Coupon address does not match."; const COUPON_EXPIRED = "Coupon has expired."; const INVALID_AVAILABLE_CLAIMS = "Number of claims cannot be zero."; const NON_STACKING_COUPON = "Coupon cannot be used with other coupons."; function validateCoupon(coupon, years, length, address) { try { hasAvailableClaims(coupon.rules); isCouponValidForNameYears(coupon.rules, years); if (coupon.kind === 0) { isValidCouponPercentage(coupon.amount); } isCouponValidForNameLength(coupon.rules, length); isCouponValidForAddress(coupon.rules, address); isCouponExpired(coupon.rules); } catch (error) { throw new Error( `Coupon '${coupon.couponCode}' validation failed: ${error?.message}` ); } } function validateCoupons(coupons, years, length, address) { for (const coupon of coupons) { if (!coupon.rules.can_stack && coupons.length > 1) { throw new Error( `Coupon '${coupon.couponCode}' validation failed: ${NON_STACKING_COUPON}` ); } validateCoupon(coupon, years, length, address); } } function applyCouponsToPrice(coupons, initialPrice) { if (!coupons || coupons.length === 0) { return initialPrice; } let price = initialPrice; for (const coupon of coupons) { if (!coupon.rules.can_stack && coupons.length > 1) { throw new Error("Coupons provided cannot be stacked"); } price = applyCouponToPrice(price, coupon); } return price; } function applyCouponToPrice(price, coupon) { if (!coupon) { return price; } const couponAmount = Number(coupon.amount); if (coupon.kind === 0) { let discountAmount = price * couponAmount / 100; return price - discountAmount; } else if (coupon.kind === 1) { const discountedAmount = price - couponAmount; return discountedAmount < 0 ? 0 : discountedAmount; } else { throw new Error(`Unknown coupon kind: ${coupon.kind}`); } } function hasAvailableClaims(rules) { if (rules?.available_claims !== null && rules?.available_claims !== void 0 && parseInt(rules?.available_claims) <= 0) { throw new Error(INVALID_AVAILABLE_CLAIMS); } } function isCouponValidForNameYears(rules, years) { const { from: minYears, to: maxYears } = rules.years || {}; if (minYears && maxYears && (years < minYears || years > maxYears)) { throw new Error(INVALID_YEARS); } } function isValidCouponPercentage(amount) { if (parseInt(amount) <= 0 || parseInt(amount) > 100) { throw new Error(INVALID_PERCENTAGE); } } function isCouponValidForNameLength(rules, length) { const { from: minLength, to: maxLength } = rules.length || {}; if (minLength && maxLength && (length < minLength || length > maxLength)) { throw new Error(INVALID_FOR_NAME_LENGTH); } } function isCouponValidForAddress(rules, userAddress) { if (rules.user && rules.user !== userAddress) { throw new Error(INVALID_USER); } } function isCouponExpired(rules, currentTimestamp = Date.now().toString()) { if (rules.expiration && Number(currentTimestamp) > Number(rules.expiration)) { throw new Error(COUPON_EXPIRED); } } //# sourceMappingURL=coupons.js.map