Add Expenses app
A UI based on [Budget Zen](https://github.com/BrunoBernardino/budgetzen-web) but slightly updated and adjusted for bewCloud. It also features a chart with available money and spent by budgets. This is useful for envelope-based budgeting.
This commit is contained in:
38
routes/api/expenses/auto-complete.tsx
Normal file
38
routes/api/expenses/auto-complete.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { FreshContextState } from '/lib/types.ts';
|
||||
import { getExpenseSuggestions } from '/lib/data/expenses.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
suggestions: string[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (!requestBody.name || requestBody.name.length < 2) {
|
||||
return new Response('Bad request', { status: 400 });
|
||||
}
|
||||
|
||||
const suggestions = await getExpenseSuggestions(
|
||||
context.state.user.id,
|
||||
requestBody.name,
|
||||
);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, suggestions };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user