2021-09-26 22:08:02 +00:00
|
|
|
import { NotFoundError, resolver } from "blitz";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2021-10-01 21:04:12 +00:00
|
|
|
import db from "db";
|
|
|
|
import { updateSubscriptionPlan } from "integrations/paddle";
|
2021-09-26 22:08:02 +00:00
|
|
|
|
|
|
|
const Body = z.object({
|
2021-10-01 21:04:12 +00:00
|
|
|
planId: z.number(),
|
2021-09-26 22:08:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ planId }, ctx) => {
|
|
|
|
const subscription = await db.subscription.findFirst({ where: { organizationId: ctx.session.orgId } });
|
|
|
|
if (!subscription) {
|
|
|
|
throw new NotFoundError();
|
|
|
|
}
|
|
|
|
|
|
|
|
const subscriptionId = subscription.paddleSubscriptionId;
|
2021-10-03 16:38:39 +00:00
|
|
|
await updateSubscriptionPlan({ productId: planId, subscriptionId });
|
2021-09-26 22:08:02 +00:00
|
|
|
});
|