-
Notifications
You must be signed in to change notification settings - Fork 5
/
route.tsx
79 lines (66 loc) · 2.45 KB
/
route.tsx
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { json, LoaderFunction, MetaFunction, redirect } from "@remix-run/node"
import { useLoaderData, useOutletContext } from "@remix-run/react"
import invariant from "tiny-invariant"
import { AccountPlanView } from "./view"
import ErrorBoundaryView from "~/components/ErrorBoundaryView/ErrorBoundaryView"
import { initPortalClient } from "~/models/portal/portal.server"
import { Account, D2Stats, PortalApp, User } from "~/models/portal/sdk"
import { Stripe, stripe } from "~/models/stripe/stripe.server"
import { AppIdOutletContext } from "~/routes/account.$accountId.$appId/route"
import { getErrorMessage } from "~/utils/catchError"
import { getRequiredServerEnvVar } from "~/utils/environment"
import { seo_title_append } from "~/utils/seo"
import { requireUser } from "~/utils/user.server"
export const meta: MetaFunction = () => {
return [
{
title: `Account Plan ${seo_title_append}`,
},
]
}
export type AccountAppRelays = Pick<D2Stats, "totalCount"> &
Pick<PortalApp, "name" | "appEmoji">
export type AccountPlanLoaderData = {
account: Account
subscription?: Stripe.Subscription
accountAppsRelays: AccountAppRelays[]
user: User
}
export const loader: LoaderFunction = async ({ request, params }) => {
const { accountId } = params
invariant(accountId, "account id not found")
if (getRequiredServerEnvVar("FLAG_STRIPE_PAYMENT") === "false") {
return redirect(`/account/${accountId}/settings`)
}
const user = await requireUser(request)
const portal = initPortalClient({ token: user.accessToken })
try {
let subscription
let account
const accountAppsRelays: AccountAppRelays[] = []
account = await portal.getUserAccount({ accountID: accountId, accepted: true })
if (account.getUserAccount.integrations?.stripeSubscriptionID) {
subscription = await stripe.subscriptions.retrieve(
account.getUserAccount.integrations.stripeSubscriptionID,
)
}
return json<AccountPlanLoaderData>({
account: account.getUserAccount as Account,
subscription,
accountAppsRelays,
user: user.user,
})
} catch (error) {
throw new Response(getErrorMessage(error), {
status: 500,
})
}
}
export default function AccountPlanDetails() {
const data = useLoaderData<AccountPlanLoaderData>()
const { userRole } = useOutletContext<AppIdOutletContext>()
return <AccountPlanView {...data} userRole={userRole} />
}
export function ErrorBoundary() {
return <ErrorBoundaryView />
}