Skip to content

[Demo App] decrement credits only if user doesn't have subscription plan #425

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

vincanger
Copy link
Collaborator

Description

Fixes #418

@vincanger vincanger requested a review from sodic May 8, 2025 11:16
Copy link
Collaborator

@sodic sodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, one small question.

console.log('Decrementing credits and saving response');
prisma.$transaction([decrementCredit, createResponse]);
await prisma.$transaction(transactions);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, I forgot this.

@@ -81,18 +74,35 @@ export const generateGptResponse: GenerateGptResponse<GenerateGptResponseInput,
},
});

const transactions: PrismaPromise<User | GptResponse>[] = [createResponse];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the explicit type annotation here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It complains when i try to push thedecrementCredits transaction if I already define it with [createResponse].

Instead, I could define an empty array and then push both transactions without needing the explicit type annotation, like this:

  const transactions = [];
  transactions.push(createResponse);
  
  if (!isUserSubscribed(context.user)) {
    //...
    transactions.push(decrementCredit);
  }

I'm not sure which is cleaner tbh.

Copy link
Collaborator

@sodic sodic Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess it's subjective, but I prefer the way you did it anyway.

Although we might be better off moving everything up and doing it differently. See the other comment for more :)

Comment on lines +61 to +68
// We decrement the credits for users without an active subscription
// after using up tokens to get a daily plan from Chat GPT.
//
// This way, users don't feel cheated if something goes wrong.
// On the flipside, users can theoretically abuse this and spend more
// credits than they have, but the damage should be pretty limited.
//
// Think about which option you prefer for you and edit the code accordingly.
const decrementCredit = context.entities.User.update({
where: { id: context.user.id },
data: {
credits: {
decrement: 1,
},
},
});
// Think about which option you prefer for your app and edit the code accordingly.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is now in the wrong place I think

},
},
});
transactions.push(decrementCredit);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we do this right away, instead of checking the credits and the subscription twice.

This is what I mean:

const transactions = [];
if (!isUserSubscribed(user)) {
  if (user.credits > 0) {
    const decrementCredit = ...
    transactions.push(decrementCredit);
  } else {
    throw new HttpError(402, 'User has not paid or is out of credits');
  }
}

We could then kick out the isEligibleForResponse function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Demo AI app] Only decrement credits if there isn't an active subscription
2 participants