forked from learn-anything/learn-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateStripePlan.ts
54 lines (51 loc) · 1.53 KB
/
updateStripePlan.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Context } from "@grafbase/sdk"
import { GraphQLError } from "graphql"
import Stripe from "stripe"
import { upgradeStripeMonthlyPlanToYear } from "../edgedb/crud/user"
import { hankoIdFromToken } from "../lib/hanko-validate"
import { Resolver } from "@grafbase/generated"
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: "2023-10-16",
typescript: true
})
// TODO: should use grafbase stripe connector
// https://grafbase.com/docs/connectors
const updateStripePlanResolver: Resolver["Mutation.updateStripePlan"] = async (
parent,
args,
context,
info
) => {
try {
const hankoId = await hankoIdFromToken(context)
if (hankoId) {
const stripeSubscriptionObjectId =
await upgradeStripeMonthlyPlanToYear(hankoId)
const subscription = await stripe.subscriptions.retrieve(
// @ts-ignore
stripeSubscriptionObjectId?.stripeSubscriptionObjectId
)
// @ts-ignore
const subscriptionItemId = subscription.items.data[0].id
await stripe.subscriptions.update(
// @ts-ignore
stripeSubscriptionObjectId?.stripeSubscriptionObjectId,
{
items: [
{
id: subscriptionItemId,
price: process.env.STRIPE_YEAR_SUBSCRIPTION
}
]
}
)
return "ok"
} else {
throw new GraphQLError("Missing or invalid Authorization header")
}
} catch (err) {
console.error(err)
throw new GraphQLError(JSON.stringify(err))
}
}
export default updateStripePlanResolver