aboutsummaryrefslogtreecommitdiff
path: root/api/src/modules/one-time-expenses/one-time-expenses.service.ts
diff options
context:
space:
mode:
authorLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-08-16 23:17:42 +0000
committerLucas Faria Mendes <lucas.fariamo08@gmail.com>2026-08-16 23:17:42 +0000
commit375482e654a01615683867e732ac9e458c7cbe74 (patch)
tree12d1b31b8deb6bf8590bcdbc2cfb52dcc553e006 /api/src/modules/one-time-expenses/one-time-expenses.service.ts
parent2c2dfba45357bbd205a59145ae55a2779c911ae0 (diff)
downloadyaba-master.tar.gz
yaba-master.zip
feat: upload yaba srcHEADmaster
Signed-off-by: Lucas Faria Mendes <lucas.fariamo08@gmail.com>
Diffstat (limited to 'api/src/modules/one-time-expenses/one-time-expenses.service.ts')
-rw-r--r--api/src/modules/one-time-expenses/one-time-expenses.service.ts102
1 files changed, 102 insertions, 0 deletions
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;
+}