Skip to content
Merged
Changes from all commits
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
59 changes: 32 additions & 27 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 @@ -38,10 +39,6 @@ export const generateGptResponse: GenerateGptResponse<GenerateGptResponseInput,
throw new HttpError(401, 'Only authenticated users are allowed to perform this operation');
}

if (!isEligibleForResponse(context.user)) {
throw new HttpError(402, 'User has not paid or is out of credits');
}

const { hours } = ensureArgsSchemaOrThrowHttpError(generateGptResponseInputSchema, rawArgs);
const tasks = await context.entities.Task.findMany({
where: {
Expand All @@ -57,42 +54,50 @@ export const generateGptResponse: GenerateGptResponse<GenerateGptResponseInput,
throw new HttpError(500, 'Encountered a problem in communication with OpenAI');
}

// We decrement the credits 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,
},
},
});

const createResponse = context.entities.GptResponse.create({
data: {
user: { connect: { id: context.user.id } },
content: JSON.stringify(generatedSchedule),
},
});

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

// 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 your app and edit the code accordingly.
if (!isUserSubscribed(context.user)) {
if (context.user.credits > 0) {
const decrementCredit = context.entities.User.update({
where: { id: context.user.id },
data: {
credits: {
decrement: 1,
},
},
});
transactions.push(decrementCredit);
} else {
throw new HttpError(402, 'User has not paid or is out of credits');
}
}

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 =
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