import { differenceInDays } from "date-fns";
import type { Order, RefundDecision } from "./types";
const REFUND_WINDOW_DAYS = 30;
const AUTO_APPROVE_LIMIT = 100;
/** Decides what happens to a refund request before a person sees it. */
export const decideRefund = (order: Order, today: Date): RefundDecision => {
const age = differenceInDays(today, order.paidAt);
if (order.refundedAt) {
return { outcome: "declined", reason: "Already refunded" };
}
if (age > REFUND_WINDOW_DAYS) {
return {
outcome: "declined",
reason: `Paid ${age} days ago, outside the ${REFUND_WINDOW_DAYS} day window`,
};
}
if (order.total > AUTO_APPROVE_LIMIT) {
return {
outcome: "review",
reason: "Over the automatic limit",
assignTo: "billing-on-call",
};
}
return {
outcome: "approved",
amount: order.total,
reason: "Inside the window and under the limit",
};
};