fix: pull user from locals and theme picker default

This commit is contained in:
Rotimi Best
2026-06-13 09:55:34 +01:00
parent 977176dbe2
commit 094683b551
3 changed files with 63 additions and 17 deletions
@@ -147,8 +147,10 @@
</button>
<div
style={`border-color: ${isCustomTheme ? $currentOrg.theme : 'dark:border-neutral-700'};`}
class="group relative h-auto w-fit cursor-pointer rounded-full border-2"
style={isCustomTheme ? `border-color: ${$currentOrg.theme};` : ''}
class="group relative h-auto w-fit cursor-pointer rounded-full border-2 {!isCustomTheme
? 'custom-theme-picker--empty'
: ''}"
>
<!-- plus icon positioned over the color picker -->
<div
@@ -232,4 +234,12 @@
:global(.color::before) {
display: none;
}
:global(.custom-theme-picker--empty .color) {
background-color: #e5e7eb !important;
}
:global(.dark .custom-theme-picker--empty .color) {
background-color: #374151 !important;
}
</style>
@@ -3,16 +3,34 @@ import { TOKEN_PACK } from '@cio/utils/plans';
import { dev } from '$app/environment';
import { env } from '$env/dynamic/private';
import type { RequestEvent } from './$types';
import { classroomio, getApiHeaders } from '$lib/utils/services/api';
export const GET = async ({ url, locals }: RequestEvent) => {
const { user, profile, organizations } = locals;
export const GET = async ({ url, locals, cookies }: RequestEvent) => {
const { user } = locals;
if (!user || !profile) {
if (!user) {
return new Response('Unauthorized', { status: 401 });
}
const orgId = url.searchParams.get('orgId');
const org = organizations?.find((candidate) => candidate.id === orgId);
if (!orgId) {
return new Response('Missing orgId', { status: 400 });
}
const accountResponse = await classroomio.account.$get(undefined, getApiHeaders(cookies));
if (!accountResponse.ok) {
return new Response('Failed to fetch account data', { status: 500 });
}
const accountData = await accountResponse.json();
if (!accountData.success) {
return new Response('Failed to fetch account data', { status: 500 });
}
const org = accountData.organizations?.find((candidate) => candidate.id === orgId);
if (!org?.siteName) {
return new Response('You are not a member of this organization', { status: 403 });
@@ -37,7 +55,7 @@ export const GET = async ({ url, locals }: RequestEvent) => {
});
const totalPriceCents = TOKEN_PACK.PRICE_USD_CENTS * quantity;
const customerEmail = (profile.email ?? user.email)?.replace('@test.com', '+test@digdippa.com');
const customerEmail = (accountData.profile?.email ?? user.email)?.replace('@test.com', '+test@digdippa.com');
const checkout = await polar.checkouts.create({
products: [productId],
@@ -51,13 +69,13 @@ export const GET = async ({ url, locals }: RequestEvent) => {
]
},
customerEmail,
customerName: profile.fullname || undefined,
customerName: accountData.profile?.fullname || undefined,
successUrl: `${url.origin}/org/${org.siteName}/settings/ai-credits?tokens=success`,
metadata: {
kind: 'token_pack',
orgId: org.id,
orgSlug: org.siteName,
triggeredBy: profile.id,
triggeredBy: accountData.profile?.id ?? user.id,
tokensPerUnit: String(TOKEN_PACK.TOKENS_PER_UNIT),
quantity: String(quantity)
}
@@ -3,6 +3,7 @@ import { PLANS } from '@cio/utils/plans';
import { dev } from '$app/environment';
import { env } from '$env/dynamic/private';
import type { RequestEvent } from './$types';
import { classroomio, getApiHeaders } from '$lib/utils/services/api';
const SUBSCRIPTION_PRODUCT_IDS = new Set(
Object.values(PLANS)
@@ -13,17 +14,34 @@ const SUBSCRIPTION_PRODUCT_IDS = new Set(
.filter((id): id is string => Boolean(id))
);
export const GET = async ({ url, locals }: RequestEvent) => {
const { user, profile, organizations } = locals;
export const GET = async ({ url, locals, cookies }: RequestEvent) => {
const { user } = locals;
if (!user || !profile) {
if (!user) {
return new Response('Unauthorized', { status: 401 });
}
const orgId = url.searchParams.get('orgId');
const org = organizations?.find((candidate) => candidate.id === orgId);
if (!org?.siteName || !org.member) {
if (!orgId) {
return new Response('Missing orgId', { status: 400 });
}
const accountResponse = await classroomio.account.$get(undefined, getApiHeaders(cookies));
if (!accountResponse.ok) {
return new Response('Failed to fetch account data', { status: 500 });
}
const accountData = await accountResponse.json();
if (!accountData.success) {
return new Response('Failed to fetch account data', { status: 500 });
}
const org = accountData.organizations?.find((candidate) => candidate.id === orgId);
if (!org?.siteName || !org.memberId) {
return new Response('You are not a member of this organization', { status: 403 });
}
@@ -44,13 +62,13 @@ export const GET = async ({ url, locals }: RequestEvent) => {
const checkout = await polar.checkouts.create({
products: [productId],
customerEmail: profile.email ?? user.email,
customerName: profile.fullname || undefined,
customerEmail: accountData.profile?.email ?? user.email,
customerName: accountData.profile?.fullname || undefined,
successUrl: `${url.origin}/org/${org.siteName}?upgrade=true&confirmation=true`,
metadata: {
orgId: org.id,
orgSlug: org.siteName,
triggeredBy: String(org.member.id)
triggeredBy: String(org.memberId)
}
});