fix redirect to checkout when the org has not a subscription

This commit is contained in:
2026-01-07 14:30:29 -03:00
parent d222872000
commit ded57b43f0
7 changed files with 173 additions and 113 deletions

View File

@@ -3,6 +3,13 @@ import { type LoaderFunctionArgs, createContext } from 'react-router'
import { userContext } from '@repo/auth/context'
import { request as req } from '@repo/util/request'
import type { Address } from '@/routes/_.$orgid.enrollments.buy/review'
export type Subscription = {
billing_day: number
payment_method: 'PIX' | 'BANK_SLIP' | 'MANUAL'
}
export type Workspace = {
id: string
name: string
@@ -12,6 +19,8 @@ export type Workspace = {
export type WorkspaceContextProps = {
activeWorkspace: Workspace
workspaces: Workspace[]
subscription: Subscription | null
address: Address | null
}
export const workspaceContext = createContext<WorkspaceContextProps>()
@@ -43,7 +52,33 @@ export const workspaceMiddleware = async (
({ id }) => id === org_id
) as Workspace
context.set(workspaceContext, { activeWorkspace, workspaces })
const [subscription, address] = await Promise.all([
req({
url: `/orgs/${activeWorkspace.id}/subscription`,
request,
context
})
.then((r) => r.json())
.then(emptyObjectToNull),
req({
url: `/orgs/${activeWorkspace.id}/address`,
request,
context
})
.then((r) => r.json())
.then(emptyObjectToNull)
])
context.set(workspaceContext, {
activeWorkspace,
workspaces,
subscription,
address
})
return await next()
}
const emptyObjectToNull = (data: any) =>
data && Object.keys(data).length === 0 ? null : data