Add account creation endpoint and dynamic REST client environment config

This commit is contained in:
GitHub Copilot
2025-05-06 09:13:03 +00:00
parent a8ec2734c8
commit bac13b30c9
3 changed files with 112 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
import type { APIRoute } from 'astro';
import { accountService } from '../../../data/db.service';
import { AccountStatus, AccountType, accountService } from '../../../data/db.service';
import type { Account } from '../../../types';
export const GET: APIRoute = async () => {
try {
@@ -21,3 +22,44 @@ export const GET: APIRoute = async () => {
});
}
};
export const POST: APIRoute = async ({ request }) => {
try {
const accountData = (await request.json()) as Omit<Account, 'id' | 'createdAt' | 'updatedAt'>;
// Validate required fields
if (!accountData.name || !accountData.bankName || !accountData.accountNumber) {
return new Response(
JSON.stringify({
error: 'Missing required fields: name, bankName, and accountNumber are required',
}),
{
status: 400,
headers: { 'Content-Type': 'application/json' },
},
);
}
// Set default values if not provided
const data = {
...accountData,
balance: accountData.balance || 0,
type: accountData.type || AccountType.CHECKING,
status: accountData.status || AccountStatus.ACTIVE,
};
// Create the account
const newAccount = await accountService.create(data);
return new Response(JSON.stringify(newAccount), {
status: 201,
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
console.error('Error creating account:', error);
return new Response(JSON.stringify({ error: 'Failed to create account' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
};