90 lines
3.1 KiB
TypeScript
90 lines
3.1 KiB
TypeScript
import { createClient } from '@supabase/supabase-js'
|
|
import { requireUser } from '../_shared/auth.ts'
|
|
import { handleCorsPreflightRequest } from '../_shared/cors.ts'
|
|
import { createJsonResponder, JsonBodyError, readJsonBody } from '../_shared/json-response.ts'
|
|
import {
|
|
mapTeamRpcError,
|
|
parseTeamAcceptInput,
|
|
TeamContractError,
|
|
} from '../_shared/team-contract.ts'
|
|
|
|
const REQUEST_LIMIT_BYTES = 4 * 1024
|
|
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
|
|
interface AcceptRpcResult {
|
|
team_id: string
|
|
role: 'owner' | 'admin' | 'member'
|
|
duplicate: boolean
|
|
already_member: boolean
|
|
}
|
|
|
|
const jsonResponse = createJsonResponder({ noStore: true, pragmaNoCache: true })
|
|
|
|
async function readJson(req: Request): Promise<unknown> {
|
|
try {
|
|
return await readJsonBody(req, { maxBytes: REQUEST_LIMIT_BYTES, requireContentType: true })
|
|
} catch (error) {
|
|
if (error instanceof JsonBodyError) {
|
|
throw new TeamContractError(error.code, error.status)
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
function createUserClient(req: Request) {
|
|
return createClient(
|
|
Deno.env.get('SUPABASE_URL') ?? '',
|
|
Deno.env.get('SUPABASE_ANON_KEY') ?? '',
|
|
{
|
|
auth: { persistSession: false, autoRefreshToken: false },
|
|
global: { headers: { Authorization: req.headers.get('Authorization') ?? '' } },
|
|
},
|
|
)
|
|
}
|
|
|
|
function normalizeAcceptResult(value: unknown): AcceptRpcResult {
|
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
throw new TeamContractError('invalid_accept_response', 502)
|
|
}
|
|
const row = value as Record<string, unknown>
|
|
if (
|
|
typeof row.team_id !== 'string'
|
|
|| !UUID_PATTERN.test(row.team_id)
|
|
|| (row.role !== 'owner' && row.role !== 'admin' && row.role !== 'member')
|
|
|| typeof row.duplicate !== 'boolean'
|
|
|| typeof row.already_member !== 'boolean'
|
|
) {
|
|
throw new TeamContractError('invalid_accept_response', 502)
|
|
}
|
|
return row as unknown as AcceptRpcResult
|
|
}
|
|
|
|
Deno.serve(async (req: Request) => {
|
|
const preflight = handleCorsPreflightRequest(req)
|
|
if (preflight) return preflight
|
|
if (req.method !== 'POST') return jsonResponse({ error: 'method_not_allowed' }, 405)
|
|
|
|
try {
|
|
await requireUser(req)
|
|
const input = parseTeamAcceptInput(await readJson(req))
|
|
const { data, error } = await createUserClient(req).rpc('accept_team_invite', {
|
|
invite_token: input.token,
|
|
})
|
|
if (error) throw mapTeamRpcError('accept', error)
|
|
const accepted = normalizeAcceptResult(data)
|
|
return jsonResponse({
|
|
team_id: accepted.team_id,
|
|
role: accepted.role,
|
|
already_member: accepted.already_member,
|
|
duplicate: accepted.duplicate,
|
|
})
|
|
} catch (error) {
|
|
if (error instanceof TeamContractError) {
|
|
return jsonResponse({ error: error.code, message: error.code }, error.status)
|
|
}
|
|
if (error && typeof error === 'object' && 'status' in error && 'message' in error) {
|
|
return jsonResponse({ error: 'unauthorized', message: 'unauthorized' }, 401)
|
|
}
|
|
return jsonResponse({ error: 'team_accept_failed', message: 'team_accept_failed' }, 500)
|
|
}
|
|
})
|