-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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); |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
// 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. |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
Description
Fixes #418