-
-
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import * as z from 'zod'; | ||
import type { PrismaPromise } from '@prisma/client'; | ||
import type { Task, GptResponse, User } from 'wasp/entities'; | ||
import type { | ||
GenerateGptResponse, | ||
|
@@ -65,22 +66,6 @@ export const generateGptResponse: GenerateGptResponse<GenerateGptResponseInput, | |
// credits than they have, but the damage should be pretty limited. | ||
// | ||
// Think about which option you prefer for your app and edit the code accordingly. | ||
const decrementCredit = context.entities.User.update({ | ||
where: { | ||
id: context.user.id, | ||
NOT: { | ||
OR: [ | ||
{ subscriptionStatus: SubscriptionStatus.Active }, | ||
{ subscriptionStatus: SubscriptionStatus.CancelAtPeriodEnd }, | ||
], | ||
}, | ||
}, | ||
data: { | ||
credits: { | ||
decrement: 1, | ||
}, | ||
}, | ||
}); | ||
|
||
const createResponse = context.entities.GptResponse.create({ | ||
data: { | ||
|
@@ -89,18 +74,35 @@ export const generateGptResponse: GenerateGptResponse<GenerateGptResponseInput, | |
}, | ||
}); | ||
|
||
const transactions: PrismaPromise<User | GptResponse>[] = [createResponse]; | ||
|
||
if (!isUserSubscribed(context.user)) { | ||
const decrementCredit = context.entities.User.update({ | ||
where: { id: context.user.id }, | ||
data: { | ||
credits: { | ||
decrement: 1, | ||
}, | ||
}, | ||
}); | ||
transactions.push(decrementCredit); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch, I forgot this. |
||
|
||
return generatedSchedule; | ||
}; | ||
|
||
function isEligibleForResponse(user: User) { | ||
const isUserSubscribed = | ||
return isUserSubscribed(user) || user.credits > 0; | ||
} | ||
|
||
function isUserSubscribed(user: User) { | ||
return ( | ||
user.subscriptionStatus === SubscriptionStatus.Active || | ||
user.subscriptionStatus === SubscriptionStatus.CancelAtPeriodEnd; | ||
const userHasCredits = user.credits > 0; | ||
return isUserSubscribed || userHasCredits; | ||
user.subscriptionStatus === SubscriptionStatus.CancelAtPeriodEnd | ||
); | ||
} | ||
|
||
const createTaskInputSchema = z.object({ | ||
|
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 the
decrementCredits
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:
I'm not sure which is cleaner tbh.
Uh oh!
There was an error while loading. Please reload this page.
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 :)