From 6589e8a81573c01369bc86f40dcd4d93919c80a7 Mon Sep 17 00:00:00 2001 From: Riccardo Senica <46839416+RiccardoSenica@users.noreply.github.com> Date: Tue, 21 Jan 2025 05:36:43 +0000 Subject: [PATCH] feat: use more default values --- .env.example | 3 ++- app/api/command/route.ts | 4 ++++ utils/commands/diary.ts | 14 ++++++-------- utils/commands/helpers/commandParser.ts | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.env.example b/.env.example index 07710bb..8b65a9e 100644 --- a/.env.example +++ b/.env.example @@ -16,4 +16,5 @@ POSTGRES_URL_NO_SSL= POSTGRES_USER= RECIPIENT_EMAIL= RESEND_API_KEY= -SENDER_EMAIL= \ No newline at end of file +SENDER_EMAIL= +DEFAULT_CATEGORY= \ No newline at end of file diff --git a/app/api/command/route.ts b/app/api/command/route.ts index 1f6adf6..0463d15 100644 --- a/app/api/command/route.ts +++ b/app/api/command/route.ts @@ -4,6 +4,10 @@ import { NextResponse } from 'next/server'; export async function POST(req: Request) { try { + if(!process.env.API_KEY){ + throw new Error('API KEY environment variable is not set') + } + const body = await req.json(); const result = RequestSchema.safeParse(body); diff --git a/utils/commands/diary.ts b/utils/commands/diary.ts index 43dde42..9009874 100644 --- a/utils/commands/diary.ts +++ b/utils/commands/diary.ts @@ -25,7 +25,7 @@ export async function diaryCommand( if (!parameters || !parameters['instruction']) { return { success: false, - message: 'Message parameter is missing.' + message: 'Instruction parameter is missing.' }; } @@ -36,17 +36,15 @@ export async function diaryCommand( switch (parsedCommand.command) { case 'add': { - if (!parsedCommand.flags.cat) { - return { - success: false, - message: 'Category is required' - }; + const categoryName = (parsedCommand.flags.cat as string) || process.env.DEFAULT_CATEGORY + if(!categoryName){ + throw new Error('DEFAULT_CATEGORY environment variable is not set') } const expense = await createExpense({ description: parsedCommand.flags.desc as string, cost: parsedCommand.flags.cost as number, - categoryName: parsedCommand.flags.cat as string + categoryName }); const formatted = formatResponse(expense); @@ -102,7 +100,7 @@ export async function diaryCommand( try { const reporter = new ExpenseReporter(); const from = parsedCommand.flags.from as Date; - const to = parsedCommand.flags.to as Date; + const to = (parsedCommand.flags.to as Date) || new Date(); const includeJson = (parsedCommand.flags.export as boolean) || false; await reporter.sendReport(from, to, includeJson); diff --git a/utils/commands/helpers/commandParser.ts b/utils/commands/helpers/commandParser.ts index af05d9b..7a8bc5a 100644 --- a/utils/commands/helpers/commandParser.ts +++ b/utils/commands/helpers/commandParser.ts @@ -134,7 +134,7 @@ export const diaryCommands: CommandDefinition[] = [ name: 'report', flags: [ { name: 'from', type: 'date', required: true }, - { name: 'to', type: 'date', required: true }, + { name: 'to', type: 'date', required: false }, { name: 'export', type: 'boolean', required: false } ] },