chore: some refactor and semplification
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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 }
|
||||
]
|
||||
},
|
||||
@@ -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'
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
67
utils/commands/helpers/expense.ts
Normal file
67
utils/commands/helpers/expense.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import prisma from '@prisma/prisma';
|
||||
import { ExpenseType } from '@utils/types';
|
||||
|
||||
const createOrGetCategory = async (name: string) => {
|
||||
const category = await prisma.category.upsert({
|
||||
where: { name },
|
||||
update: {},
|
||||
create: { name }
|
||||
});
|
||||
return category;
|
||||
};
|
||||
|
||||
export const createExpense = async (data: ExpenseType) => {
|
||||
const category = await createOrGetCategory(data.categoryName);
|
||||
|
||||
const newExpense = await prisma.expense.create({
|
||||
data: {
|
||||
description: data.description,
|
||||
cost: data.cost,
|
||||
categoryId: category.id,
|
||||
deleted: false
|
||||
},
|
||||
include: {
|
||||
category: true
|
||||
}
|
||||
});
|
||||
|
||||
return newExpense;
|
||||
};
|
||||
|
||||
export const updateExpense = async (id: string, data: Partial<ExpenseType>) => {
|
||||
let categoryId = undefined;
|
||||
|
||||
if (data.categoryName) {
|
||||
const category = await createOrGetCategory(data.categoryName);
|
||||
categoryId = category.id;
|
||||
}
|
||||
|
||||
const updatedExpense = await prisma.expense.update({
|
||||
where: {
|
||||
id,
|
||||
deleted: false
|
||||
},
|
||||
data: {
|
||||
...(data.description && { description: data.description }),
|
||||
...(data.cost && { cost: data.cost }),
|
||||
...(categoryId && { categoryId })
|
||||
},
|
||||
include: {
|
||||
category: true
|
||||
}
|
||||
});
|
||||
|
||||
return updatedExpense;
|
||||
};
|
||||
|
||||
export const deleteExpense = async (id: string) => {
|
||||
await prisma.expense.update({
|
||||
where: {
|
||||
id,
|
||||
deleted: false
|
||||
},
|
||||
data: {
|
||||
deleted: true
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user