Skip to content

Commit cfa5ac0

Browse files
jnsdlsMananTank
authored andcommitted
add /subscriptions endpoint
1 parent 2d92d65 commit cfa5ac0

File tree

7 files changed

+492
-69
lines changed

7 files changed

+492
-69
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { getAuthToken } from "../../app/api/lib/getAuthToken";
2+
import { API_SERVER_URL } from "../constants/env";
3+
4+
// keep in line with product SKUs in the backend
5+
type ProductSKU =
6+
| "plan:starter"
7+
| "plan:growth"
8+
| "plan:custom"
9+
| "product:ecosystem_wallets"
10+
| "product:engine_standard"
11+
| "product:engine_premium"
12+
| "usage:storage"
13+
| "usage:in_app_wallet"
14+
| "usage:aa_sponsorship"
15+
| "usage:aa_sponsorship_op_grant"
16+
| null;
17+
18+
type InvoiceLine = {
19+
// amount for this line item
20+
amount: number;
21+
// statement descriptor
22+
description: string | null;
23+
// the thirdweb product sku or null if it is not recognized
24+
thirdwebSku: ProductSKU | null;
25+
};
26+
27+
type Invoice = {
28+
// total amount excluding tax
29+
amount: number | null;
30+
// the ISO currency code (e.g. USD)
31+
currency: string;
32+
// the line items on the invoice
33+
lines: InvoiceLine[];
34+
};
35+
36+
export type TeamSubscription = {
37+
id: string;
38+
type: "PLAN" | "USAGE" | "PLAN_ADD_ON" | "PRODUCT";
39+
status:
40+
| "incomplete"
41+
| "incomplete_expired"
42+
| "trialing"
43+
| "active"
44+
| "past_due"
45+
| "canceled"
46+
| "unpaid"
47+
| "paused";
48+
currentPeriodStart: string;
49+
currentPeriodEnd: string;
50+
trialStart: string | null;
51+
trialEnd: string | null;
52+
upcomingInvoice: Invoice;
53+
};
54+
55+
export async function getTeamSubscriptions(slug: string) {
56+
const token = await getAuthToken();
57+
58+
if (!token) {
59+
return null;
60+
}
61+
62+
const teamRes = await fetch(
63+
`${API_SERVER_URL}/v1/teams/${slug}/subscriptions`,
64+
{
65+
headers: {
66+
Authorization: `Bearer ${token}`,
67+
},
68+
},
69+
);
70+
if (teamRes.ok) {
71+
return (await teamRes.json())?.result as TeamSubscription[];
72+
}
73+
return null;
74+
}
75+
76+
// util fn:
77+
78+
export function parseThirdwebSKU(sku: ProductSKU) {
79+
if (!sku) {
80+
return null;
81+
}
82+
switch (sku) {
83+
case "plan:starter":
84+
return "Starter Plan";
85+
case "plan:growth":
86+
return "Growth Plan";
87+
case "plan:custom":
88+
return "Custom Plan";
89+
case "product:ecosystem_wallets":
90+
return "Ecosystem Wallets";
91+
case "product:engine_standard":
92+
return "Engine Standard";
93+
case "product:engine_premium":
94+
return "Engine Premium";
95+
case "usage:storage":
96+
return "Storage";
97+
case "usage:in_app_wallet":
98+
return "In-App Wallet";
99+
case "usage:aa_sponsorship":
100+
return "AA Sponsorship";
101+
case "usage:aa_sponsorship_op_grant":
102+
return "AA Sponsorship Op Grant";
103+
default:
104+
return null;
105+
}
106+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import type { Meta, StoryObj } from "@storybook/react";
2+
import { addDays } from "date-fns";
3+
import {
4+
createDashboardAccountStub,
5+
teamStub,
6+
teamSubscriptionsStub,
7+
} from "stories/stubs";
8+
import {
9+
BadgeContainer,
10+
mobileViewport,
11+
} from "../../../../../../../../stories/utils";
12+
import { PlanInfoCard } from "./PlanInfoCard";
13+
14+
const meta = {
15+
title: "Billing/PlanInfoCard",
16+
component: Story,
17+
parameters: {
18+
nextjs: {
19+
appDirectory: true,
20+
},
21+
},
22+
} satisfies Meta<typeof Story>;
23+
24+
export default meta;
25+
type Story = StoryObj<typeof meta>;
26+
27+
export const Desktop: Story = {
28+
args: {},
29+
};
30+
31+
export const Mobile: Story = {
32+
args: {},
33+
parameters: {
34+
viewport: mobileViewport("iphone14"),
35+
},
36+
};
37+
38+
function Story() {
39+
const team = teamStub("foo", "growth");
40+
const zeroUsageOnDemandSubs = teamSubscriptionsStub("plan:growth");
41+
const trialPlanZeroUsageOnDemandSubs = teamSubscriptionsStub("plan:growth", {
42+
trialEnd: addDays(new Date(), 7).toISOString(),
43+
});
44+
45+
const subsWith1Usage = teamSubscriptionsStub("plan:growth", {
46+
usage: {
47+
storage: {
48+
amount: 10000,
49+
quantity: 4,
50+
},
51+
},
52+
});
53+
54+
const subsWith4Usage = teamSubscriptionsStub("plan:growth", {
55+
usage: {
56+
storage: {
57+
amount: 10000,
58+
quantity: 4,
59+
},
60+
aaSponsorshipAmount: {
61+
amount: 7500,
62+
quantity: 4,
63+
},
64+
aaSponsorshipOpGrantAmount: {
65+
amount: 2500,
66+
quantity: 4,
67+
},
68+
inAppWalletAmount: {
69+
amount: 40000,
70+
quantity: 100,
71+
},
72+
},
73+
});
74+
75+
const account = createDashboardAccountStub("foo");
76+
77+
return (
78+
<div className="container flex max-w-[1130px] flex-col gap-12 lg:p-10">
79+
<BadgeContainer label="On-demand Subscriptions with 0 usage">
80+
<PlanInfoCard
81+
team={team}
82+
subscriptions={zeroUsageOnDemandSubs}
83+
account={account}
84+
/>
85+
</BadgeContainer>
86+
87+
<BadgeContainer label="Trial Plan - On-demand Subscriptions with 0 usage">
88+
<PlanInfoCard
89+
team={team}
90+
subscriptions={trialPlanZeroUsageOnDemandSubs}
91+
account={account}
92+
/>
93+
</BadgeContainer>
94+
95+
<BadgeContainer label="On-demand Subscriptions with 1 usage">
96+
<PlanInfoCard
97+
team={team}
98+
subscriptions={subsWith1Usage}
99+
account={account}
100+
/>
101+
</BadgeContainer>
102+
103+
<BadgeContainer label="On-demand Subscriptions with 4 usage">
104+
<PlanInfoCard
105+
team={team}
106+
subscriptions={subsWith4Usage}
107+
account={account}
108+
/>
109+
</BadgeContainer>
110+
</div>
111+
);
112+
}

0 commit comments

Comments
 (0)