TypeScriptBusiness logicDifficulty 3/3
// lib/transfers.ts
import { db } from "@/lib/db";
export async function transfer(fromId: string, toId: string, amountMinor: number) {
const from = await db.account.findUnique({ where: { id: fromId } });
if (!from || from.balanceMinor < amountMinor) {
throw new Error("Insufficient funds");
}
await db.account.update({
where: { id: fromId },
data: { balanceMinor: { decrement: amountMinor } },
});
await db.account.update({
where: { id: toId },
data: { balanceMinor: { increment: amountMinor } },
});
}