From 3bd243778ca823b95f33d3137c000ba9f2de9ced Mon Sep 17 00:00:00 2001 From: Riccardo Senica Date: Sun, 19 Jan 2025 11:25:31 +0100 Subject: [PATCH] chore: some refactor and semplification --- README.md | 18 +++++++----- app/layout.tsx | 5 ++-- app/page.tsx | 29 ++++++++++--------- utils/commands/diary.ts | 22 ++++++++------ utils/commands/{ => helpers}/commandParser.ts | 8 ++--- utils/commands/{ => helpers}/dayLog.ts | 5 ++-- utils/{ => commands/helpers}/expense.ts | 2 +- utils/registry.ts | 2 +- 8 files changed, 50 insertions(+), 41 deletions(-) rename utils/commands/{ => helpers}/commandParser.ts (95%) rename utils/commands/{ => helpers}/dayLog.ts (94%) rename utils/{ => commands/helpers}/expense.ts (96%) diff --git a/README.md b/README.md index 8a5eb05..8a687ff 100644 --- a/README.md +++ b/README.md @@ -17,19 +17,19 @@ A personal expenses and day log tracking API that works with Siri Shortcuts. Usi #### Add an Expense ``` -add --desc "description" --cost amount --cat category +add -desc "description" -cost amount -cat category ``` -Example: `add --desc "Weekly groceries" --cost 87.50 --cat groceries` +Example: `add -desc "Weekly groceries" -cost 87.50 -cat groceries` #### Update an Expense ``` -update expenseId --desc "new description" --cost newAmount --cat newCategory +update expenseId -desc "new description" -cost newAmount -cat newCategory ``` All flags are optional - only include what you want to change. -Example: `update abc123 --cost 92.30 --cat groceries` +Example: `update abc123 -cost 92.30 -cat groceries` #### Delete an Expense @@ -42,9 +42,11 @@ Example: `delete abc123` #### Generate Report ``` -report --dateFrom "2025-01-01" --dateTo "2025-01-31" --export true +report -from dateFrom -to dateTo -export boolean ``` +Example: `report -from "2025-01-01" -to "2025-01-31" -export true` + Generates and emails an expense report for the specified period. The report includes: - Total expenses for the period @@ -56,9 +58,11 @@ The `export` flag is optional - when set to true, a JSON file with the raw data ### Day Log ``` -daylog --text "Meeting notes or daily summary" --date "2024-01-18" +daylog -text "text" -date "date" ``` +Example: `daylog -text "Meeting notes or daily summary" -date "2024-01-18"` + Adds a log entry for a specific day. The date parameter is optional and defaults to the current date. Logs are stored with UTC midnight timestamps for consistent date handling @@ -194,7 +198,7 @@ Example shortcut configuration: { "command": "expense", "parameters": { - "instruction": "add --desc \"Coffee\" --cost 3.50 --cat food" + "instruction": "add -desc \"Coffee\" -cost 3.50 -cat food" }, "apiKey": "your_api_key_here" } diff --git a/app/layout.tsx b/app/layout.tsx index 64e20de..18338df 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -2,8 +2,7 @@ import type { Metadata } from 'next'; export const metadata: Metadata = { title: 'DiaryWhisper', - description: - 'Siri-enabled diary tracker for expenses and day logs' + description: 'Siri-enabled diary tracker for expenses and day logs' }; export default function RootLayout({ @@ -16,4 +15,4 @@ export default function RootLayout({ {children} ); -} \ No newline at end of file +} diff --git a/app/page.tsx b/app/page.tsx index 4125f9f..bca059d 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -3,37 +3,38 @@ import React from 'react'; export default function Home() { - const commands = [ - 'expense add --desc "Coffee" --cost 3.50 --cat "Food"', - 'expense report --dateFrom "2024-01-01" --dateTo "2024-01-31"', - 'expense daylog --text "Added team lunch" --date "2024-01-18"' - ]; + const commands = [ + 'expense add -desc "Coffee" -cost 3.50 -cat "Food"', + 'expense report -from "2024-01-01" -to "2024-01-31"', + 'expense daylog -text "Added team lunch" -date "2024-01-18"' + ]; return ( -<> -
-
+ <> +
+

DiaryWhisper v1.0.0

Your expenses and day logs tracked via Siri Shortcuts

-
+
Loading system components...
Initializing expense database... OK
Starting expense tracking daemon... OK
System ready_
-
+

Available Commands:

{commands.map((cmd, i) => ( -
- $ {cmd} +
+ $ + {cmd}
))}
-
+
Status: OPERATIONAL Database: CONNECTED Last Backup: 2024-01-18 14:30 UTC @@ -110,4 +111,4 @@ export default function Home() { `} ); -} \ No newline at end of file +} diff --git a/utils/commands/diary.ts b/utils/commands/diary.ts index aa832c5..2a8e704 100644 --- a/utils/commands/diary.ts +++ b/utils/commands/diary.ts @@ -1,9 +1,13 @@ -import { ExpenseType, ShortcutsResponse } from '../types'; -import { CommandParser, diaryCommands } from './commandParser'; +import { ExpenseType, ShortcutsResponse } from '@utils/types'; +import { CommandParser, diaryCommands } from './helpers/commandParser'; import { Category, Expense } from '@prisma/client'; import { ExpenseReporter } from './report'; -import { createExpense, deleteExpense, updateExpense } from '@utils/expense'; -import { processDayLog } from '@utils/commands/dayLog'; +import { + createExpense, + deleteExpense, + updateExpense +} from '@utils/commands/helpers/expense'; +import { processDayLog } from '@utils/commands/helpers/dayLog'; const formatResponse = (expense: Expense & { category: Category }) => ({ id: expense.id, @@ -97,8 +101,8 @@ export async function diaryCommand( case 'report': { try { const reporter = new ExpenseReporter(); - const from = parsedCommand.flags.dateFrom as Date; - const to = parsedCommand.flags.dateTo as Date; + const from = parsedCommand.flags.from as Date; + const to = parsedCommand.flags.to as Date; const includeJson = (parsedCommand.flags.export as boolean) || false; await reporter.sendReport(from, to, includeJson); @@ -126,10 +130,10 @@ export async function diaryCommand( } case 'daylog': { - const text = parsedCommand.flags.text as string; - const date = (parsedCommand.flags.date as Date) || new Date(); + const text = parsedCommand.flags.text as string; + const date = (parsedCommand.flags.date as Date) || new Date(); - return processDayLog(text, date); + return processDayLog(text, date); } default: diff --git a/utils/commands/commandParser.ts b/utils/commands/helpers/commandParser.ts similarity index 95% rename from utils/commands/commandParser.ts rename to utils/commands/helpers/commandParser.ts index e37d2ca..7b09b2d 100644 --- a/utils/commands/commandParser.ts +++ b/utils/commands/helpers/commandParser.ts @@ -42,11 +42,11 @@ export class CommandParser { while (currentIndex < parts.length) { const flag = parts[currentIndex]; - if (!flag.startsWith('--')) { + if (!flag.startsWith('-')) { throw new Error(`Invalid flag format at: ${flag}`); } - const flagName = flag.slice(2); + const flagName = flag.slice(1); const flagDef = definition.flags.find( f => f.name === flagName || f.alias === flagName ); @@ -133,8 +133,8 @@ export const diaryCommands: CommandDefinition[] = [ { name: 'report', flags: [ - { name: 'dateFrom', type: 'date', required: true }, - { name: 'dateTo', type: 'date', required: true }, + { name: 'from', type: 'date', required: true }, + { name: 'to', type: 'date', required: true }, { name: 'export', type: 'boolean', required: false } ] }, diff --git a/utils/commands/dayLog.ts b/utils/commands/helpers/dayLog.ts similarity index 94% rename from utils/commands/dayLog.ts rename to utils/commands/helpers/dayLog.ts index 1b315cd..727fa08 100644 --- a/utils/commands/dayLog.ts +++ b/utils/commands/helpers/dayLog.ts @@ -60,7 +60,8 @@ export async function processDayLog( console.error('Error processing daylog:', error); return { success: false, - message: error instanceof Error ? error.message : 'Failed to process daylog' + message: + error instanceof Error ? error.message : 'Failed to process daylog' }; } -} \ No newline at end of file +} diff --git a/utils/expense.ts b/utils/commands/helpers/expense.ts similarity index 96% rename from utils/expense.ts rename to utils/commands/helpers/expense.ts index 8665a99..d193c70 100644 --- a/utils/expense.ts +++ b/utils/commands/helpers/expense.ts @@ -1,5 +1,5 @@ import prisma from '@prisma/prisma'; -import { ExpenseType } from './types'; +import { ExpenseType } from '@utils/types'; const createOrGetCategory = async (name: string) => { const category = await prisma.category.upsert({ diff --git a/utils/registry.ts b/utils/registry.ts index f9deff1..3b6ac74 100644 --- a/utils/registry.ts +++ b/utils/registry.ts @@ -1,7 +1,7 @@ import { ShortcutsResponse } from './types'; import { pingCommand } from './commands/ping'; import { diaryCommand } from './commands/diary'; -import { CommandParser, diaryCommands } from './commands/commandParser'; +import { CommandParser, diaryCommands } from './commands/helpers/commandParser'; type CommandHandler = ( parameters?: Record