59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
import type { Operation, OperationSplit, Recurring, RecurringSplit, Settlement as SettlementRow } from "@prisma/client";
|
|
import { centsFromDecimal } from "@/domain/money";
|
|
import type { RecurringDef } from "@/domain/recurrence";
|
|
import type { Op, Settlement, Split } from "@/domain/types";
|
|
import { toIsoDate } from "@/lib/dates";
|
|
|
|
export const splitOf = (rows: { userId: string; basisPoints: number }[]): Split =>
|
|
Object.fromEntries(rows.map((s) => [s.userId, s.basisPoints]));
|
|
|
|
export const splitRows = (split: Split) =>
|
|
Object.entries(split).map(([userId, basisPoints]) => ({ userId, basisPoints }));
|
|
|
|
export function toOp(row: Operation & { splits: OperationSplit[] }): Op {
|
|
return {
|
|
id: row.id,
|
|
kind: row.kind,
|
|
amount: centsFromDecimal(row.amount),
|
|
date: toIsoDate(row.date),
|
|
label: row.label,
|
|
categoryId: row.categoryId,
|
|
accountUserId: row.accountUserId,
|
|
status: row.status,
|
|
split: splitOf(row.splits),
|
|
recurringId: row.recurringId,
|
|
allocatedToRecurringId: row.allocatedToRecurringId,
|
|
allocatedToOperationId: row.allocatedToOperationId,
|
|
allocationMode: row.allocationMode,
|
|
};
|
|
}
|
|
|
|
export function toRecurringDef(row: Recurring & { splits: RecurringSplit[] }): RecurringDef {
|
|
return {
|
|
id: row.id,
|
|
kind: row.kind,
|
|
amount: centsFromDecimal(row.amount),
|
|
label: row.label,
|
|
categoryId: row.categoryId,
|
|
accountUserId: row.accountUserId,
|
|
split: splitOf(row.splits),
|
|
frequency: row.frequency,
|
|
dayOfMonth: row.dayOfMonth,
|
|
startDate: toIsoDate(row.startDate),
|
|
endDate: row.endDate ? toIsoDate(row.endDate) : null,
|
|
paused: row.paused,
|
|
allocatedToRecurringId: row.allocatedToRecurringId,
|
|
allocationMode: row.allocationMode,
|
|
};
|
|
}
|
|
|
|
export function toSettlement(row: SettlementRow): Settlement {
|
|
return {
|
|
id: row.id,
|
|
fromUserId: row.fromUserId,
|
|
toUserId: row.toUserId,
|
|
amount: centsFromDecimal(row.amount),
|
|
date: toIsoDate(row.date),
|
|
};
|
|
}
|