chore: some refactor and semplification

This commit is contained in:
2025-01-19 11:25:31 +01:00
parent d473941773
commit 3bd243778c
8 changed files with 50 additions and 41 deletions

View File

@@ -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:

View File

@@ -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 }
]
},

View File

@@ -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'
};
}
}
}

View File

@@ -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({

View File

@@ -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<string, string>