Skip to content

Commit

Permalink
fix: read notes
Browse files Browse the repository at this point in the history
- reworked the dashboard so it's less overwhelming. it now has tabs for each "category" of thing you are looking at.
- added front-end for editing stripe's business name (aka doing business as)
- made the default for new connect accounts to have their business name as their discord username
  • Loading branch information
Torwent committed Jan 9, 2024
1 parent 0ad8e1f commit d23f6ad
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 80 deletions.
32 changes: 32 additions & 0 deletions src/lib/backend/data.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export async function createStripeConnectAccount(
},
business_profile: {
mcc: "5734",
name: scripter.profiles.username,
support_url: "https://waspscripts.com/scripters/" + scripter.url,
url: "https://waspscripts.com/scripters/" + scripter.url,
support_email: "[email protected]"
Expand Down Expand Up @@ -352,6 +353,37 @@ export async function finishStripeAccountSetup(baseURL: string, account: string)
return accountLink.url
}

export async function getStripeAccount(id: string) {
let stripeAccount: Stripe.Response<Stripe.Account> | null = null

try {
stripeAccount = await stripe.accounts.retrieve(id)
} catch (error) {
console.error(
"An error occurred when calling the Stripe API to create an account session",
error
)
}

return stripeAccount
}

export async function updateStripeAccount(id: string, dba: string) {
let stripeAccount: Stripe.Response<Stripe.Account> | null = null

try {
stripeAccount = await stripe.accounts.update(id, { business_profile: { name: dba } })
} catch (error) {
console.error(
"An error occurred when calling the Stripe API to create an account session",
error
)
return false
}

return true
}

export async function getStripeSession(account: string) {
let accountSession: Stripe.Response<Stripe.AccountSession> | null = null

Expand Down
4 changes: 4 additions & 0 deletions src/lib/backend/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ export const countryCodeSchema = z.object({
code: z.string().length(2, "Country codes have to be only 2 letters")
})

export const dbaSchema = z.object({
dba: z.string().min(3, "Your name needs to be longer")
})

export type PriceSchema = z.infer<typeof priceSchema>
export type BundleSchema = z.infer<typeof newBundleSchema>
export type NewScriptSchema = z.infer<typeof newScriptSchema>
Expand Down
3 changes: 1 addition & 2 deletions src/routes/dashboard/Table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
multipleSubmits: "prevent",
clearOnSubmit: "errors-and-message",
validators: schema,
resetForm: true,
invalidateAll: true
resetForm: true
})
const btnText = action.includes("dd") ? "Add" : "Save"
Expand Down
52 changes: 45 additions & 7 deletions src/routes/dashboard/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import {
finishStripeAccountSetup,
updateStripePrice,
updateStripeProduct,
getStripeSession
getStripeSession,
getStripeAccount,
updateStripeAccount
} from "$lib/backend/data.server"
import {
bundleArraySchema,
countryCodeSchema,
dbaSchema,
newBundleSchema,
newScriptArraySchema,
scriptArraySchema
Expand All @@ -35,6 +38,7 @@ export const load = async (event) => {

const promises = await Promise.all([
superValidate(event, countryCodeSchema),
superValidate(event, dbaSchema),
superValidate(event, bundleArraySchema),
superValidate(event, newBundleSchema),
superValidate(event, scriptArraySchema),
Expand All @@ -43,15 +47,21 @@ export const load = async (event) => {
])

event.depends("dashboard:stripe_session")
const stripeSession = promises[5].stripe ? await getStripeSession(promises[5].stripe) : null

const scripter = promises[6]

const stripeAccount = scripter.stripe ? await getStripeAccount(scripter.stripe) : null
const stripeSession = scripter.stripe ? await getStripeSession(scripter.stripe) : null

return {
countryForm: promises[0],
bundlesForm: promises[1],
newBundleForm: promises[2],
scriptsForm: promises[3],
newScriptForm: promises[4],
stripeSession: stripeSession
dbaForm: promises[1],
bundlesForm: promises[2],
newBundleForm: promises[3],
scriptsForm: promises[4],
newScriptForm: promises[5],
stripeAccount,
stripeSession
}
}

Expand Down Expand Up @@ -108,6 +118,34 @@ export const actions = {
return
},

displayName: async ({
request,
locals: { supabaseServer, getSession, getProfile },
url: { origin },
params: { slug }
}) => {
const promises = await Promise.all([getSession(), getProfile(), request.formData()])
const profile = promises[1]

if (!promises[0] || !profile) {
return await doLogin(supabaseServer, origin, new URLSearchParams("login&provider=discord"))
}

if (!UUID_V4_REGEX.test(slug)) throw error(403, "Invalid dashboard UUID.")
if (profile.id !== slug && !profile.roles.administrator)
throw error(403, "You cannot access another scripter dashboard.")

const scripter = await getScripterDashboard(supabaseServer, slug)

const form = await superValidate(promises[2], dbaSchema)
if (!form.valid) return setError(form, "", "The name you set is not valid!")
if (!scripter.stripe) return setError(form, "", "The user is missing a stripe profile!")

const success = await updateStripeAccount(scripter.stripe, form.data.dba)
if (!success) return setError(form, "", "Failed to update stripe's business name")
return
},

bundleEdit: async ({
request,
locals: { supabaseServer, getSession, getProfile },
Expand Down
Loading

0 comments on commit d23f6ad

Please sign in to comment.