From fc75d5da153ae3c9b0379862fb55315ff2ea32e8 Mon Sep 17 00:00:00 2001 From: Riccardo Senica <46839416+RiccardoSenica@users.noreply.github.com> Date: Thu, 23 Jan 2025 20:29:42 +0000 Subject: [PATCH] feat: add -date flag --- app/api/command/route.ts | 4 ++-- utils/commands/diary.ts | 8 +++++--- utils/commands/helpers/commandParser.ts | 3 ++- utils/commands/helpers/expense.ts | 1 + utils/types.ts | 3 ++- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/api/command/route.ts b/app/api/command/route.ts index 0463d15..41d064e 100644 --- a/app/api/command/route.ts +++ b/app/api/command/route.ts @@ -4,8 +4,8 @@ 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') + if (!process.env.API_KEY) { + throw new Error('API KEY environment variable is not set'); } const body = await req.json(); diff --git a/utils/commands/diary.ts b/utils/commands/diary.ts index 9009874..1e29422 100644 --- a/utils/commands/diary.ts +++ b/utils/commands/diary.ts @@ -36,14 +36,16 @@ export async function diaryCommand( switch (parsedCommand.command) { case 'add': { - const categoryName = (parsedCommand.flags.cat as string) || process.env.DEFAULT_CATEGORY - if(!categoryName){ - throw new Error('DEFAULT_CATEGORY environment variable is not set') + 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, + date: (parsedCommand.flags.date as Date) || new Date(), categoryName }); diff --git a/utils/commands/helpers/commandParser.ts b/utils/commands/helpers/commandParser.ts index 7a8bc5a..11b133f 100644 --- a/utils/commands/helpers/commandParser.ts +++ b/utils/commands/helpers/commandParser.ts @@ -113,7 +113,8 @@ export const diaryCommands: CommandDefinition[] = [ flags: [ { name: 'desc', type: 'string', required: true }, { name: 'cost', type: 'number', required: true }, - { name: 'cat', type: 'string', required: false } + { name: 'cat', type: 'string', required: false }, + { name: 'date', type: 'date', required: false } ] }, { diff --git a/utils/commands/helpers/expense.ts b/utils/commands/helpers/expense.ts index d193c70..cdfd49a 100644 --- a/utils/commands/helpers/expense.ts +++ b/utils/commands/helpers/expense.ts @@ -17,6 +17,7 @@ export const createExpense = async (data: ExpenseType) => { data: { description: data.description, cost: data.cost, + createdAt: data.date, categoryId: category.id, deleted: false }, diff --git a/utils/types.ts b/utils/types.ts index c72f76a..2ad2202 100644 --- a/utils/types.ts +++ b/utils/types.ts @@ -35,7 +35,8 @@ export interface ShortcutsResponse { const ExpenseSchema = z.object({ description: z.string().min(1), cost: z.number().positive(), - categoryName: z.string() + categoryName: z.string(), + date: z.date() }); export type ExpenseType = z.infer;