Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ src/
update.ts - Update loan by ID
pay.ts - Make a loan payment
mark-paid.ts - Pay the next pending installment
unmark-paid.ts - Reverse a loan payment (default: most recent)
delete.ts - Delete loan by ID
stats/
summary.ts - Financial summary
Expand Down Expand Up @@ -161,6 +162,7 @@ Login flow writes human-readable output to stderr (not JSON) since it is interac
| GET | /api/loans | loans list |
| POST | /api/loans | loans create |
| POST | /api/loans/:id/pay | loans pay |
| POST | /api/loans/:id/reverse-payment | loans unmark-paid |
| PUT | /api/loans/:id | loans update |
| DELETE | /api/loans/:id | loans delete |
| GET | /api/stats/summary | stats summary |
Expand Down
44 changes: 44 additions & 0 deletions src/commands/loans/unmark-paid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Command } from "commander";
import { apiRequest } from "../../lib/api-client.js";
import { output } from "../../lib/output.js";

interface LoanPayment {
id: string;
paidAt: string;
payAmount: number;
loanAmount: number;
payCurrency: string;
}

interface LoanDetails {
id: string;
payments: LoanPayment[];
}

export const unmarkPaidLoanCommand = new Command("unmark-paid")
.description("Reverse a loan payment (default: most recent)")
.argument("<id>", "Loan ID")
.option("--payment-id <id>", "Specific payment ID to reverse")
.addHelpText(
"after",
"\nExamples:\n lucas loans unmark-paid <loan-id>\n lucas loans unmark-paid <loan-id> --payment-id <payment-id>\n",
)
.action(async (id: string, opts: { paymentId?: string }) => {
let paymentId = opts.paymentId;

if (!paymentId) {
const loan = await apiRequest<LoanDetails>("GET", `/api/loans/${id}`);
if (!loan.payments || loan.payments.length === 0) {
output.error("No payments found for this loan", 404, { loanId: id });
}
const sorted = [...loan.payments].sort(
(a, b) => new Date(b.paidAt).getTime() - new Date(a.paidAt).getTime(),
);
paymentId = sorted[0].id;
}

const data = await apiRequest("POST", `/api/loans/${id}/reverse-payment`, {
paymentId,
});
output.success(data);
});
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { createLoanCommand } from "./commands/loans/create.js";
import { updateLoanCommand } from "./commands/loans/update.js";
import { payLoanCommand } from "./commands/loans/pay.js";
import { markPaidLoanCommand } from "./commands/loans/mark-paid.js";
import { unmarkPaidLoanCommand } from "./commands/loans/unmark-paid.js";
import { deleteLoanCommand } from "./commands/loans/delete.js";

// Stats
Expand Down Expand Up @@ -107,6 +108,7 @@ loans.addCommand(createLoanCommand);
loans.addCommand(updateLoanCommand);
loans.addCommand(payLoanCommand);
loans.addCommand(markPaidLoanCommand);
loans.addCommand(unmarkPaidLoanCommand);
loans.addCommand(deleteLoanCommand);

// Grupo: stats
Expand Down
Loading