Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add daily limits #125

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions CouponService/couponService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ type Coupon = {
id: string,
faucetConfigId: string,
maxLimitAmount: number,
consumedAmount: number,
dailyLimitAmount: number,
totalConsumedAmount: number,
todayConsumedAmount: number,
todayConsumedLastReset: number,
expiry: number,
amountPerCoupon: number,
reset: boolean,
Expand All @@ -24,7 +27,11 @@ function validateCouponData(coupon: any, couponConfig: CouponConfig): Coupon | u
coupon.faucetConfigId &&
coupon.maxLimitAmount > 0 &&
coupon.maxLimitAmount <= couponConfig.MAX_LIMIT_CAP &&
coupon.consumedAmount <= coupon.maxLimitAmount &&
coupon.dailyLimitAmount > 0 &&
coupon.dailyLimitAmount <= coupon.maxLimitAmount &&
coupon.totalConsumedAmount <= coupon.maxLimitAmount &&
coupon.todayConsumedAmount <= coupon.dailyLimitAmount &&
coupon.todayConsumedLastReset > 0 &&
coupon.expiry > 0
) {
return coupon
Expand Down Expand Up @@ -69,12 +76,12 @@ export class CouponService {
const params = new ScanCommand({
TableName: 'coupons',
})

const result = await this.documentClient?.send(params)

// Required for quick lookup of coupons in DB fetched list
const dbItemSet = new Set<string>()

// Fetches new coupons from database into memory
result?.Items?.forEach((item: Record<string, any>) => {
const coupon: Coupon | undefined = validateCouponData(item, this.couponConfig)
Expand Down Expand Up @@ -112,14 +119,14 @@ export class CouponService {
},
UpdateExpression: 'SET consumedAmount = :consumedAmount',
ExpressionAttributeValues: {
':consumedAmount': couponItem.consumedAmount,
':totalConsumedAmount': couponItem.totalConsumedAmount,
},
}

const params = new UpdateCommand(updateRequest)
await this.documentClient?.send(params)
})
}
}

async consumeCouponAmount(id: string, faucetConfigId: string, amount: number): Promise<CouponValidity> {
// Return `true` early, if coupon system is disabled (for debugging)
Expand All @@ -129,16 +136,24 @@ export class CouponService {
try {
const coupon = this.coupons.get(id)
const couponAmount = coupon?.amountPerCoupon ?? amount

if (coupon && coupon.todayConsumedLastReset < (Date.now() / 1000)) {
coupon.todayConsumedAmount = 0
coupon.todayConsumedLastReset = Date.now() / 1000
}

if (
coupon &&
coupon.faucetConfigId === faucetConfigId &&
coupon.expiry > (Date.now() / 1000) &&
coupon.consumedAmount + couponAmount < coupon.maxLimitAmount
coupon.totalConsumedAmount + couponAmount < coupon.maxLimitAmount &&
coupon.todayConsumedAmount + couponAmount < coupon.dailyLimitAmount
) {
coupon.consumedAmount += couponAmount
return { isValid: true, amount: couponAmount}
coupon.totalConsumedAmount += couponAmount
coupon.todayConsumedAmount += couponAmount
return { isValid: true, amount: couponAmount }
}
return { isValid: false, amount: couponAmount}
return { isValid: false, amount: couponAmount }
} finally {
release()
}
Expand All @@ -152,9 +167,9 @@ export class CouponService {
if (
coupon &&
coupon.expiry > (Date.now() / 1000) &&
coupon.consumedAmount - amount > 0
coupon.totalConsumedAmount - amount > 0
) {
coupon.consumedAmount -= amount
coupon.totalConsumedAmount -= amount
}
} finally {
release()
Expand Down