From 375482e654a01615683867e732ac9e458c7cbe74 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Sun, 16 Aug 2026 20:17:42 -0300 Subject: feat: upload yaba src Signed-off-by: Lucas Faria Mendes --- .../one-time-expenses/one-time-expenses.service.ts | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 api/src/modules/one-time-expenses/one-time-expenses.service.ts (limited to 'api/src/modules/one-time-expenses/one-time-expenses.service.ts') diff --git a/api/src/modules/one-time-expenses/one-time-expenses.service.ts b/api/src/modules/one-time-expenses/one-time-expenses.service.ts new file mode 100644 index 0000000..c55640d --- /dev/null +++ b/api/src/modules/one-time-expenses/one-time-expenses.service.ts @@ -0,0 +1,102 @@ +import { and, between, eq } from 'drizzle-orm'; +import { db } from '@/db/client'; +import { oneTimeExpenses } from '@/db/schema'; +import type { + CreateOneTimeExpenseInput, + UpdateOneTimeExpenseInput, +} from './one-time-expenses.schema'; + +export async function findOneTimeExpensesByUser( + userId: number, + filters?: { month?: number; year?: number }, +) { + const conditions = [eq(oneTimeExpenses.user_id, userId)]; + + // Filtra pelo mês/ano usando range de datas no formato YYYY-MM-DD + if (filters?.month !== undefined && filters?.year !== undefined) { + const year = filters.year; + const month = filters.month; + const startDate = `${year}-${String(month).padStart(2, '0')}-01`; + // Último dia do mês: pega o dia 0 do mês seguinte + const lastDay = new Date(year, month, 0).getDate(); + const endDate = `${year}-${String(month).padStart(2, '0')}-${String(lastDay).padStart(2, '0')}`; + conditions.push(between(oneTimeExpenses.expense_date, startDate, endDate)); + } else if (filters?.year !== undefined) { + const year = filters.year; + conditions.push( + between(oneTimeExpenses.expense_date, `${year}-01-01`, `${year}-12-31`), + ); + } + + return db + .select() + .from(oneTimeExpenses) + .where(and(...conditions)) + .orderBy(oneTimeExpenses.expense_date); +} + +export async function findOneTimeExpenseById(userId: number, id: number) { + const result = await db + .select() + .from(oneTimeExpenses) + .where( + and(eq(oneTimeExpenses.user_id, userId), eq(oneTimeExpenses.id, id)), + ) + .limit(1); + return result[0] ?? null; +} + +export async function createOneTimeExpense( + userId: number, + data: CreateOneTimeExpenseInput, +) { + const [created] = await db + .insert(oneTimeExpenses) + .values({ + user_id: userId, + name: data.name, + amount: data.amount, + category: data.category, + type: data.type, + expense_date: data.expense_date, + notes: data.notes ?? null, + }) + .returning(); + return created; +} + +export async function updateOneTimeExpense( + userId: number, + id: number, + data: UpdateOneTimeExpenseInput, +) { + const now = new Date().toISOString(); + + const [updated] = await db + .update(oneTimeExpenses) + .set({ + ...(data.name !== undefined && { name: data.name }), + ...(data.amount !== undefined && { amount: data.amount }), + ...(data.category !== undefined && { category: data.category }), + ...(data.type !== undefined && { type: data.type }), + ...(data.expense_date !== undefined && { expense_date: data.expense_date }), + ...(data.notes !== undefined && { notes: data.notes }), + updated_at: now, + }) + .where( + and(eq(oneTimeExpenses.user_id, userId), eq(oneTimeExpenses.id, id)), + ) + .returning(); + + return updated ?? null; +} + +export async function deleteOneTimeExpense(userId: number, id: number) { + const [deleted] = await db + .delete(oneTimeExpenses) + .where( + and(eq(oneTimeExpenses.user_id, userId), eq(oneTimeExpenses.id, id)), + ) + .returning(); + return deleted ?? null; +} -- cgit v1.2.3