|
| 1 | +// POST /api/goals - Create a new savings goal |
| 2 | + |
| 3 | +import { NextRequest, NextResponse } from 'next/server'; |
| 4 | +import { buildCreateGoalTx } from '@/lib/contracts/savings-goals'; |
| 5 | +import { getSessionFromRequest, getPublicKeyFromSession } from '@/lib/auth/session'; |
| 6 | +import { |
| 7 | + createValidationError, |
| 8 | + createAuthenticationError, |
| 9 | + handleUnexpectedError |
| 10 | +} from '@/lib/errors/api-errors'; |
| 11 | +import { |
| 12 | + validateAmount, |
| 13 | + validateFutureDate, |
| 14 | + validateGoalName |
| 15 | +} from '@/lib/validation/savings-goals'; |
| 16 | +import { ApiSuccessResponse } from '@/lib/types/savings-goals'; |
| 17 | + |
| 18 | +export async function POST(request: NextRequest) { |
| 19 | + try { |
| 20 | + // Authenticate user |
| 21 | + const session = getSessionFromRequest(request); |
| 22 | + if (!session) { |
| 23 | + return createAuthenticationError('Authentication required', 'Please provide a valid session'); |
| 24 | + } |
| 25 | + |
| 26 | + let publicKey: string; |
| 27 | + try { |
| 28 | + publicKey = getPublicKeyFromSession(session); |
| 29 | + } catch (error) { |
| 30 | + return createAuthenticationError('Invalid session', 'Session does not contain a valid public key'); |
| 31 | + } |
| 32 | + |
| 33 | + // Parse request body |
| 34 | + let body; |
| 35 | + try { |
| 36 | + body = await request.json(); |
| 37 | + } catch (error) { |
| 38 | + return createValidationError('Invalid request body', 'Request body must be valid JSON'); |
| 39 | + } |
| 40 | + |
| 41 | + const { name, targetAmount, targetDate } = body; |
| 42 | + |
| 43 | + // Validate name |
| 44 | + if (!name) { |
| 45 | + return createValidationError('Missing required field', 'Goal name is required'); |
| 46 | + } |
| 47 | + const nameValidation = validateGoalName(name); |
| 48 | + if (!nameValidation.isValid) { |
| 49 | + return createValidationError('Invalid goal name', nameValidation.error); |
| 50 | + } |
| 51 | + |
| 52 | + // Validate target amount |
| 53 | + if (targetAmount === undefined || targetAmount === null) { |
| 54 | + return createValidationError('Missing required field', 'Target amount is required'); |
| 55 | + } |
| 56 | + const amountValidation = validateAmount(targetAmount); |
| 57 | + if (!amountValidation.isValid) { |
| 58 | + return createValidationError('Invalid target amount', amountValidation.error); |
| 59 | + } |
| 60 | + |
| 61 | + // Validate target date |
| 62 | + if (!targetDate) { |
| 63 | + return createValidationError('Missing required field', 'Target date is required'); |
| 64 | + } |
| 65 | + const dateValidation = validateFutureDate(targetDate); |
| 66 | + if (!dateValidation.isValid) { |
| 67 | + return createValidationError('Invalid target date', dateValidation.error); |
| 68 | + } |
| 69 | + |
| 70 | + // Build transaction |
| 71 | + const result = await buildCreateGoalTx(publicKey, name, targetAmount, targetDate); |
| 72 | + |
| 73 | + // Return success response |
| 74 | + const response: ApiSuccessResponse = { |
| 75 | + xdr: result.xdr |
| 76 | + }; |
| 77 | + |
| 78 | + return NextResponse.json(response, { status: 200 }); |
| 79 | + |
| 80 | + } catch (error) { |
| 81 | + return handleUnexpectedError(error); |
| 82 | + } |
| 83 | +} |
0 commit comments