Skip to content

Commit 27796d1

Browse files
committed
fix: update stripe api compatibility
1 parent 107c391 commit 27796d1

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

src/routes/Navigation.svelte

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import Discord from "./Discord.svelte"
1414
import GitHub from "./GitHub.svelte"
1515
import YouTube from "./YouTube.svelte"
16+
import UserCog from "svelte-lucide/UserCog.svelte"
1617
1718
let showMenu = $state(false)
1819
let showProfile = $state(false)
@@ -62,21 +63,21 @@
6263

6364
<nav class="col-span-12 row-span-1 w-full flex-col text-sm xl:text-base">
6465
<div
65-
class="bg-surface-200/30 dark:bg-surface-800/30 flex h-full justify-between font-semibold backdrop-blur-md"
66+
class="flex h-full justify-between bg-surface-200/30 font-semibold backdrop-blur-md dark:bg-surface-800/30"
6667
>
6768
<button
6869
name="Menu"
6970
aria-label="Open the navigation menu"
70-
class="justify-left text-primary-500 my-auto flex h-full p-2 align-middle lg:hidden"
71+
class="justify-left my-auto flex h-full p-2 align-middle text-primary-500 lg:hidden"
7172
onclick={() => {
7273
showMenu = !showMenu
7374
if (showMenu) showProfile = false
7475
}}
7576
>
7677
{#if showMenu}
77-
<X class="text-surface-900-100 mx-2 my-auto" />
78+
<X class="mx-2 my-auto text-surface-900-100" />
7879
{:else}
79-
<Menu class="text-surface-900-100 mx-2 my-auto" />
80+
<Menu class="mx-2 my-auto text-surface-900-100" />
8081
{/if}
8182
<Logo selected={true} />
8283
</button>
@@ -85,7 +86,7 @@
8586
<li class="h-12">
8687
<a
8788
href="/"
88-
class="hover:text-primary-600-400 flex h-full place-content-center place-items-center"
89+
class="flex h-full place-content-center place-items-center hover:text-primary-600-400"
8990
class:text-primary-600-400={"Home" === currentPage}
9091
aria-label="Navigate to home page"
9192
>
@@ -97,7 +98,7 @@
9798
<li class="h-12">
9899
<a
99100
href={getLink(route)}
100-
class="hover:text-primary-600-400 flex h-full place-content-center place-items-center"
101+
class="flex h-full place-content-center place-items-center hover:text-primary-600-400"
101102
class:text-primary-600-400={route === currentPage}
102103
aria-label="Navigate to {route.toLowerCase()} page"
103104
>
@@ -119,7 +120,7 @@
119120
}}
120121
>
121122
{#if profile}
122-
<span class="group-hover:text-primary-500 mx-2 my-auto hidden md:block">
123+
<span class="mx-2 my-auto hidden group-hover:text-primary-500 md:block">
123124
{profile.username}
124125
</span>
125126
{/if}
@@ -145,15 +146,15 @@
145146
</div>
146147

147148
<ul
148-
class="bg-surface-200/30 dark:bg-surface-800/30 absolute z-50 w-full backdrop-blur-md lg:hidden {showMenu
149+
class="absolute z-50 w-full bg-surface-200/30 backdrop-blur-md lg:hidden dark:bg-surface-800/30 {showMenu
149150
? 'flex flex-col'
150151
: 'hidden'}"
151152
>
152153
{#each routes as route, idx (route)}
153154
<li class="h-12">
154155
<a
155156
href={getLink(route)}
156-
class="hover:text-primary-400 dark:hover:text-primary-100 flex h-full place-content-center place-items-center"
157+
class="flex h-full place-content-center place-items-center hover:text-primary-400 dark:hover:text-primary-100"
157158
class:text-primary-500={route === currentPage}
158159
class:dark:text-primary-400={route === currentPage}
159160
aria-label="Navigate to {route.toLowerCase()} page"
@@ -179,7 +180,7 @@
179180

180181
<form
181182
method="POST"
182-
class="bg-surface-200/30 dark:bg-surface-800/30 absolute z-50 w-full py-14 backdrop-blur-md {showProfile
183+
class="absolute z-50 w-full bg-surface-200/30 py-14 backdrop-blur-md dark:bg-surface-800/30 {showProfile
183184
? 'flex flex-col'
184185
: 'hidden'}"
185186
use:enhance
@@ -191,13 +192,25 @@
191192

192193
<a
193194
href="/user/{profile.id}"
194-
class="btn preset-filled-secondary-500 mx-auto flex"
195+
class="mx-auto my-2 btn flex preset-filled-secondary-500"
195196
aria-label="Open profile page"
196197
onclick={() => (showProfile = false)}
197198
>
198199
<UserRound />
199200
Profile
200201
</a>
202+
203+
{#if profile.role == "administrator"}
204+
<a
205+
href="/auth"
206+
class="mx-auto btn flex preset-filled-secondary-500"
207+
aria-label="Login as a different user"
208+
onclick={() => (showProfile = false)}
209+
>
210+
<UserCog />
211+
Login As
212+
</a>
213+
{/if}
201214
</div>
202215
</header>
203216

@@ -211,7 +224,7 @@
211224
<button
212225
name="Logout"
213226
aria-label="Logout"
214-
class="btn preset-filled-secondary-500 mx-auto"
227+
class="mx-auto btn preset-filled-secondary-500"
215228
formaction="/auth?/logout"
216229
>
217230
<LogOut />

src/routes/api/stripe/subscriptions/+server.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export const POST = async ({ request }) => {
2626
case "customer.subscription.created": {
2727
const subscriptionCreated = data.object as Stripe.Subscription
2828
if (subscriptionCreated.status !== "active") break
29+
const items = subscriptionCreated.items.data
30+
if (items.length != 1) error(409, "Subscription has multiple items only 1 was expected!")
31+
2932
console.log("INSERT profile.subscriptions: ", subscriptionCreated.id)
3033

3134
const { error: err } = await supabaseAdmin
@@ -36,15 +39,14 @@ export const POST = async ({ request }) => {
3639
user_id: subscriptionCreated.metadata.user_id,
3740
product: subscriptionCreated.items.data[0].price.product.toString(),
3841
price: subscriptionCreated.items.data[0].price.id,
39-
date_end: new Date(subscriptionCreated.cancel_at! * 1000).toISOString(),
42+
date_end: new Date(items[0].current_period_end * 1000).toISOString(),
4043
date_start: new Date(subscriptionCreated.start_date * 1000).toISOString(),
4144
cancel: subscriptionCreated.cancel_at_period_end,
4245
disabled: false
4346
})
4447

45-
if (err) {
48+
if (err)
4649
error(500, "object: " + JSON.stringify(subscriptionCreated) + "\r\n" + formatError(err))
47-
}
4850

4951
break
5052
}
@@ -54,9 +56,12 @@ export const POST = async ({ request }) => {
5456
if (subscriptionUpdated.status !== "active" && subscriptionUpdated.status !== "canceled")
5557
break
5658

59+
const items = subscriptionUpdated.items.data
60+
if (items.length != 1) error(409, "Subscription has multiple items only 1 was expected!")
61+
5762
console.log("UPDATE profile.subscription: ", subscriptionUpdated.id)
5863

59-
const date_end = new Date(subscriptionUpdated.cancel_at! * 1000).toISOString()
64+
const date_end = new Date(items[0].current_period_end * 1000).toISOString()
6065
const date_start = new Date(subscriptionUpdated.start_date * 1000).toISOString()
6166
const cancel = subscriptionUpdated.cancel_at_period_end
6267

@@ -79,9 +84,12 @@ export const POST = async ({ request }) => {
7984

8085
case "customer.subscription.deleted": {
8186
const subscriptionDeleted = data.object as Stripe.Subscription
87+
const items = subscriptionDeleted.items.data
88+
if (items.length != 1) error(409, "Subscription has multiple items only 1 was expected!")
89+
8290
console.log("DELETE profile.subscriptions: ", subscriptionDeleted.id)
8391

84-
const date_end = new Date(subscriptionDeleted.canceled_at! * 1000).toISOString()
92+
const date_end = new Date(items[0].current_period_end * 1000).toISOString()
8593
const date_start = new Date(subscriptionDeleted.start_date * 1000).toISOString()
8694
const cancel = subscriptionDeleted.cancel_at_period_end
8795

src/routes/auth/launcher/+server.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { error } from "@sveltejs/kit"
1+
import { error, json } from "@sveltejs/kit"
22
import { formatError } from "$lib/utils"
33
import { createStripeCustomer } from "$lib/server/stripe.server"
44
import { supabaseAdmin } from "$lib/server/supabase.server"
@@ -18,18 +18,7 @@ export const POST = async ({ request }) => {
1818
.single()
1919

2020
if (count) {
21-
return new Response(
22-
JSON.stringify({
23-
success: true
24-
}),
25-
{
26-
status: 200,
27-
headers: {
28-
"Content-Type": "application/json",
29-
"Access-Control-Allow-Origin": "*"
30-
}
31-
}
32-
)
21+
return json({ success: true })
3322
}
3423

3524
const {
@@ -49,7 +38,10 @@ export const POST = async ({ request }) => {
4938
discord,
5039
user.user_metadata["custom_claims"]["global_name"]
5140
)
52-
if (!stripe) error(403, "Failed to create stripe user for " + user.id)
41+
42+
if (!stripe) {
43+
error(403, "Failed to create stripe user for " + user.id)
44+
}
5345

5446
const { error: err } = await supabaseAdmin.schema("profiles").from("profiles").insert({
5547
id: user.id,
@@ -63,16 +55,5 @@ export const POST = async ({ request }) => {
6355
if (err) error(500, "Failed to INSERT profile: " + formatError(err))
6456
}
6557

66-
return new Response(
67-
JSON.stringify({
68-
success: true
69-
}),
70-
{
71-
status: 200,
72-
headers: {
73-
"Content-Type": "application/json",
74-
"Access-Control-Allow-Origin": "*"
75-
}
76-
}
77-
)
58+
return json({ success: true })
7859
}

0 commit comments

Comments
 (0)