11import { db } from '@sim/db'
2- import { invitation , invitationWorkspaceGrant , member } from '@sim/db/schema'
2+ import { invitation , invitationWorkspaceGrant } from '@sim/db/schema'
33import { createLogger } from '@sim/logger'
44import { and , eq } from 'drizzle-orm'
55import { type NextRequest , NextResponse } from 'next/server'
66import { z } from 'zod'
77import { AuditAction , AuditResourceType , recordAudit } from '@/lib/audit/log'
88import { getSession } from '@/lib/auth'
9+ import { isOrganizationOwnerOrAdmin } from '@/lib/billing/core/organization'
910import { cancelInvitation , getInvitationById , normalizeEmail } from '@/lib/invitations/core'
1011import { hasWorkspaceAdminAccess } from '@/lib/workspaces/permissions/utils'
1112
1213const logger = createLogger ( 'InvitationsAPI' )
1314
14- async function isOrgAdmin ( userId : string , organizationId : string ) : Promise < boolean > {
15- const [ row ] = await db
16- . select ( { role : member . role } )
17- . from ( member )
18- . where ( and ( eq ( member . userId , userId ) , eq ( member . organizationId , organizationId ) ) )
19- . limit ( 1 )
20- return row ?. role === 'owner' || row ?. role === 'admin'
21- }
22-
2315export async function GET ( request : NextRequest , { params } : { params : Promise < { id : string } > } ) {
2416 const { id } = await params
2517 const session = await getSession ( )
@@ -40,7 +32,7 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
4032
4133 let hasAdminView = false
4234 if ( inv . organizationId ) {
43- hasAdminView = await isOrgAdmin ( session . user . id , inv . organizationId )
35+ hasAdminView = await isOrganizationOwnerOrAdmin ( session . user . id , inv . organizationId )
4436 }
4537 if ( ! hasAdminView && inv . grants . length > 0 ) {
4638 const adminChecks = await Promise . all (
@@ -131,7 +123,7 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
131123 { status : 400 }
132124 )
133125 }
134- if ( ! ( await isOrgAdmin ( session . user . id , inv . organizationId ) ) ) {
126+ if ( ! ( await isOrganizationOwnerOrAdmin ( session . user . id , inv . organizationId ) ) ) {
135127 return NextResponse . json (
136128 { error : 'Only an organization owner or admin can change invitation roles' } ,
137129 { status : 403 }
@@ -181,7 +173,7 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
181173 actorId : session . user . id ,
182174 actorName : session . user . name ?? undefined ,
183175 actorEmail : session . user . email ?? undefined ,
184- action : AuditAction . ORG_INVITATION_CREATED ,
176+ action : AuditAction . ORG_INVITATION_UPDATED ,
185177 resourceType : AuditResourceType . ORGANIZATION ,
186178 resourceId : inv . organizationId ?? inv . id ,
187179 description : `Updated invitation for ${ inv . email } ` ,
@@ -220,7 +212,7 @@ export async function DELETE(
220212
221213 let canCancel = false
222214 if ( inv . organizationId ) {
223- canCancel = await isOrgAdmin ( session . user . id , inv . organizationId )
215+ canCancel = await isOrganizationOwnerOrAdmin ( session . user . id , inv . organizationId )
224216 }
225217 if ( ! canCancel && inv . grants . length > 0 ) {
226218 const adminChecks = await Promise . all (
0 commit comments