JavascriptMick / supanuxt-saas

Simple boilerplate for SAAS. Nuxt3, Supabase, OAuth, Prisma, TRPC, Pinia, Stripe, Tailwind, OpenAI
https://nuxt3-saas-boilerplate.netlify.app/
MIT License
407 stars 55 forks source link

Useage Limits e.g. limit OpenAI Calls #12

Closed JavascriptMick closed 1 year ago

JavascriptMick commented 1 year ago

It should be possible to limit how many times you can do or access something within an account by 'counting' the number if times you have done it. Classic example would be to limit the number of OpenAI Calls on the note generator feature. OpenAI costs money, your users shouldn't be able to do it millions of times.

The Notes feature already enforces limits on the number of notes ... if(account.notes.length>= account.max_notes){ throw new AccountLimitError('Note Limit reached, no new notes can be added'); }

but limiting the number of calls is trickier because it would require a specific counter on the account...

if(account.search_count >= account.max_searches_pm){
  throw new AccountLimitError('Search Limit reached, no new searches can be made');
}

and then of course, the counter would need to be reset to 0 when the month 'rolls over'...

// rollover free plan if necessary
console.log(`checking for rollover conditions account.plan_name:${account.plan_name}, account.current_period_ends:${account.current_period_ends}`);
if(account.plan_name === config.initialPlanName && account.current_period_ends < new Date()){
  return await prisma_client.account.update({
    where: { id: account_id },
    data: {
      search_count: 0,
      current_period_ends: UtilService.addMonths(account.current_period_ends,1)
    },
    ...accountWithMembers
  })
}