Skip to content

Commit b74fb9e

Browse files
committed
fix: resolve TypeScript issues by improving null checks and type handling
1 parent 97ca7ac commit b74fb9e

7 files changed

Lines changed: 39 additions & 9 deletions

File tree

app/pages/index.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ async function tryDemo() {
8787
// Activate the demo org before navigating
8888
const orgsResult = await authClient.organization.list()
8989
const orgs = orgsResult.data
90-
if (orgs && orgs.length > 0) {
91-
await authClient.organization.setActive({ organizationId: orgs[0].id })
90+
const firstOrg = orgs?.[0]
91+
if (firstOrg) {
92+
await authClient.organization.setActive({ organizationId: firstOrg.id })
9293
}
9394
9495
// Hard navigation to avoid hydration mismatches between dark landing and light dashboard

app/pages/onboarding/create-org.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ const autoSwitched = ref(false)
2929
watch([orgs, isOrgsLoading], async ([orgList, loading]) => {
3030
if (loading || autoSwitched.value || showCreateForm.value) return
3131
if (orgList.length === 1) {
32+
const firstOrg = orgList[0]
33+
if (!firstOrg) return
34+
3235
autoSwitched.value = true
3336
isLoading.value = true
3437
try {
35-
await switchOrg(orgList[0].id)
38+
await switchOrg(firstOrg.id)
3639
}
3740
catch {
3841
isLoading.value = false

server/api/__sitemap__/urls.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { eq } from 'drizzle-orm'
2+
import { queryCollection } from '@nuxt/content/server'
23
import { job } from '../../database/schema'
34

45
/**

server/middleware/demo-guard.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@ export default defineEventHandler(async (event) => {
5252

5353
// Check if the current session belongs to the demo org
5454
const session = await auth.api.getSession({ headers: event.headers })
55-
if (!session?.session.activeOrganizationId) return
55+
const activeOrganizationId = session
56+
? (session.session as { activeOrganizationId?: string }).activeOrganizationId
57+
: undefined
58+
59+
if (!activeOrganizationId) return
5660

5761
const guardedOrgId = await getDemoOrgId()
5862
if (!guardedOrgId) return
5963

60-
if (session.session.activeOrganizationId === guardedOrgId) {
64+
if (activeOrganizationId === guardedOrgId) {
6165
throw createError({
6266
statusCode: 403,
6367
statusMessage: 'Demo mode — this action is disabled. Deploy your own instance to get full access.',

server/plugins/migrations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export default defineNitroPlugin(async () => {
1818

1919
try {
2020
// pg_try_advisory_lock returns true if lock acquired, false if another process holds it
21-
const [{ locked }] = await db.execute<{ locked: boolean }>(
21+
const lockResult = await db.execute<{ locked: boolean }>(
2222
`SELECT pg_try_advisory_lock(${MIGRATION_LOCK_ID}) as locked`
2323
)
24+
const locked = lockResult[0]?.locked ?? false
2425

2526
if (!locked) {
2627
console.log('[Applirank] Another instance is running migrations, skipping')

server/utils/db.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ function getDB(): DB {
4141
export const db: DB = new Proxy({} as DB, {
4242
get(_, prop) {
4343
const instance = getDB()
44-
const value = (instance as Record<string, unknown>)[prop]
44+
45+
if (typeof prop !== 'string') {
46+
return Reflect.get(instance as object, prop)
47+
}
48+
49+
const value = (instance as unknown as Record<string, unknown>)[prop]
4550
// Bind methods so they keep the correct `this` context
4651
return typeof value === 'function' ? value.bind(instance) : value
4752
},

server/utils/requireAuth.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import type { H3Event } from 'h3'
22

3+
type AuthSession = NonNullable<Awaited<ReturnType<typeof auth.api.getSession>>>
4+
type AuthSessionWithActiveOrg = Omit<AuthSession, 'session'> & {
5+
session: AuthSession['session'] & {
6+
activeOrganizationId: string
7+
}
8+
}
9+
310
/**
411
* Require an authenticated session with an active organization.
512
* Throws 401 if not authenticated, 403 if no active organization selected.
@@ -16,9 +23,17 @@ export async function requireAuth(event: H3Event) {
1623
throw createError({ statusCode: 401, statusMessage: 'Unauthorized' })
1724
}
1825

19-
if (!session.session.activeOrganizationId) {
26+
const activeOrganizationId = (session.session as { activeOrganizationId?: string }).activeOrganizationId
27+
28+
if (!activeOrganizationId) {
2029
throw createError({ statusCode: 403, statusMessage: 'No active organization' })
2130
}
2231

23-
return session
32+
return {
33+
...session,
34+
session: {
35+
...session.session,
36+
activeOrganizationId,
37+
},
38+
} as AuthSessionWithActiveOrg
2439
}

0 commit comments

Comments
 (0)