integratedhub/services/financhalServices.ts
2026-01-20 09:02:26 -06:00

114 lines
2.6 KiB
TypeScript

// services/financhalServices.ts
// CRUD operations for obligations and incomes
import { eq } from "drizzle-orm";
import { incomes, obligations } from "../db/schema.ts";
import { db } from "../db/db.ts";
export interface Obligation {
id: string;
user_id: string | null;
description: string | null;
amount: number | null;
frequency: string | null;
due_date: number | null;
encrypted_data: string | null;
created_on: number | null;
updated_on: number | null;
}
export interface Income {
id: string;
user_id: string | null;
source: string | null;
amount: number | null;
frequency: string | null;
received_date: number | null;
encrypted_data: string | null;
created_on: number | null;
updated_on: number | null;
}
// Obligations CRUD
export async function createObligation(
user_id: string,
description: string | null,
amount: number | null,
frequency: string,
due_date: number,
encrypted_data: string,
) {
const now = Date.now();
await db.insert(obligations).values({
id: crypto.randomUUID(),
user_id,
description,
amount,
frequency,
due_date,
encrypted_data,
created_on: now,
updated_on: now,
});
}
export async function getObligationById(id: string) {
return await db.select().from(obligations).where(eq(obligations.id, id));
}
export async function getObligationsByUser(user_id: string) {
return await db.select().from(obligations).where(
eq(obligations.user_id, user_id),
);
}
export async function updateObligation(
id: string,
updates: Partial<Obligation>,
) {
await db.update(obligations).set(updates).where(eq(obligations.id, id));
}
export async function deleteObligation(id: string) {
await db.delete(obligations).where(eq(obligations.id, id));
}
// Incomes CRUD
export async function createIncome(
user_id: string,
source: string | null,
amount: number | null,
frequency: string,
received_date: number,
encrypted_data: string,
) {
const now = Date.now();
await db.insert(incomes).values({
id: crypto.randomUUID(),
user_id,
source,
amount,
frequency,
received_date,
encrypted_data,
created_on: now,
updated_on: now,
});
}
export async function getIncomeById(id: string) {
return await db.select().from(incomes).where(eq(incomes.id, id));
}
export async function getIncomesByUser(user_id: string) {
return await db.select().from(incomes).where(eq(incomes.user_id, user_id));
}
export async function updateIncome(id: string, updates: Partial<Income>) {
await db.update(incomes).set(updates).where(eq(incomes.id, id));
}
export async function deleteIncome(id: string) {
await db.delete(incomes).where(eq(incomes.id, id));
}