Menu

Our billing period rolls forward the first time somebody reads it, and a signup on the 31st migrates itself to the 3rd
📰

Our billing period rolls forward the first time somebody reads it, and a signup on the 31st migrates itself to the 3rd

DEV Community: typescript·Daniel Pertu·18 days ago
#M5sm6dEI
#devtotypescript#devto#dev#plan#userid
Reading 0:00
15s threshold

Every limit in Nakodo is counted since the start of the current billing period. There is no job that resets anything at midnight on the 1st, and no counter column to zero. The period is rolled forward by whoever reads it next.

src/lib/subscription.ts is 70 lines and this is the function that does it:

export async function getSubscription ( userId : string , exec : Executor = db ) { const now = new Date (); let sub = await exec . query . subscriptions . findFirst ({ where : eq ( subscriptions . userId , userId ) }); if ( ! sub ) { await exec . insert ( subscriptions ) . values ({ userId , plan : " free " , currentPeriodStart : now , currentPeriodEnd : addMonths ( now , 1 ) }) . onConflictDoNothing (); sub = ( await exec . query . subscriptions . findFirst ({ where : eq ( subscriptions . userId , userId ) })) ! ; } if ( sub . currentPeriodEnd <= now ) { let start = sub . currentPeriodStart ; let end = sub .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.