|
| 1 | +/** |
| 2 | + * POST /api/escrow/create |
| 3 | + * |
| 4 | + * Deploy a new escrow contract for a project. |
| 5 | + * The authenticated user must be the client (project owner). |
| 6 | + * |
| 7 | + * Body: |
| 8 | + * projectId string (UUID) |
| 9 | + * freelancerId string (UUID) |
| 10 | + * freelancerWalletAddress string |
| 11 | + * totalAmount string e.g. "500.00" |
| 12 | + * currency string e.g. "USDC" |
| 13 | + * terms? string |
| 14 | + * milestones? Array<{ title, amount, description?, dueDate? }> |
| 15 | + */ |
| 16 | + |
| 17 | +import { NextRequest, NextResponse } from 'next/server' |
| 18 | +import { withAuth } from '@/lib/auth/middleware' |
| 19 | +import { sql } from '@/lib/db' |
| 20 | +import { |
| 21 | + escrowService, |
| 22 | + EscrowError, |
| 23 | + escrowErrorToHttpStatus, |
| 24 | + EscrowAlreadyExistsError, |
| 25 | +} from '@/lib/escrow' |
| 26 | + |
| 27 | +export const POST = withAuth(async (request: NextRequest, auth) => { |
| 28 | + let body: Record<string, unknown> |
| 29 | + try { |
| 30 | + body = await request.json() |
| 31 | + } catch { |
| 32 | + return NextResponse.json( |
| 33 | + { error: 'Request body must be valid JSON', code: 'INVALID_JSON' }, |
| 34 | + { status: 400 } |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + // --- Resolve authenticated wallet to a DB user --- |
| 39 | + const users = await sql` |
| 40 | + SELECT id FROM users WHERE wallet_address = ${auth.walletAddress} LIMIT 1 |
| 41 | + ` |
| 42 | + if (users.length === 0) { |
| 43 | + return NextResponse.json( |
| 44 | + { error: 'Authenticated wallet has no platform account', code: 'USER_NOT_FOUND' }, |
| 45 | + { status: 401 } |
| 46 | + ) |
| 47 | + } |
| 48 | + const clientId = users[0].id as string |
| 49 | + |
| 50 | + try { |
| 51 | + const result = await escrowService.createEscrow({ |
| 52 | + projectId: body.projectId as string, |
| 53 | + clientId, |
| 54 | + freelancerId: body.freelancerId as string, |
| 55 | + clientWalletAddress: auth.walletAddress, |
| 56 | + freelancerWalletAddress: body.freelancerWalletAddress as string, |
| 57 | + totalAmount: body.totalAmount as string, |
| 58 | + currency: (body.currency as string) ?? 'USDC', |
| 59 | + terms: body.terms as string | undefined, |
| 60 | + milestones: body.milestones as Array<{ |
| 61 | + title: string |
| 62 | + amount: string |
| 63 | + description?: string |
| 64 | + dueDate?: string |
| 65 | + }> | undefined, |
| 66 | + }) |
| 67 | + |
| 68 | + return NextResponse.json( |
| 69 | + { |
| 70 | + contractId: result.contract.id, |
| 71 | + projectId: result.contract.projectId, |
| 72 | + escrowAddress: result.contract.escrowAddress, |
| 73 | + deployTxHash: result.deployTxHash, |
| 74 | + status: result.contract.status, |
| 75 | + escrowStatus: result.contract.escrowStatus, |
| 76 | + totalAmount: result.contract.totalAmount, |
| 77 | + currency: result.contract.currency, |
| 78 | + milestonesCreated: result.milestonesCreated, |
| 79 | + createdAt: result.contract.createdAt, |
| 80 | + }, |
| 81 | + { status: 201 } |
| 82 | + ) |
| 83 | + } catch (err) { |
| 84 | + if (err instanceof EscrowAlreadyExistsError) { |
| 85 | + return NextResponse.json( |
| 86 | + { |
| 87 | + error: err.message, |
| 88 | + code: err.code, |
| 89 | + existingContractId: err.existingContractId, |
| 90 | + }, |
| 91 | + { status: 409 } |
| 92 | + ) |
| 93 | + } |
| 94 | + if (err instanceof EscrowError) { |
| 95 | + return NextResponse.json( |
| 96 | + { error: err.message, code: err.code }, |
| 97 | + { status: escrowErrorToHttpStatus(err) } |
| 98 | + ) |
| 99 | + } |
| 100 | + console.error('[escrow/create] Unexpected error:', err) |
| 101 | + return NextResponse.json( |
| 102 | + { error: 'Internal server error', code: 'INTERNAL_ERROR' }, |
| 103 | + { status: 500 } |
| 104 | + ) |
| 105 | + } |
| 106 | +}) |
0 commit comments