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
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions template/app/src/demo-ai-app/operations.ts
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,
Expand Down Expand Up @@ -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: {
Expand All @@ -89,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 :)


if (!isUserSubscribed(context.user)) {
const decrementCredit = context.entities.User.update({
where: { id: context.user.id },
data: {
credits: {
decrement: 1,
},
},
});
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.

}

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.


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({
Expand Down