106 lines
3.4 KiB
TypeScript
106 lines
3.4 KiB
TypeScript
// routes/api/create/rent_schedule.ts
|
|
import { define } from "../../../utils.ts";
|
|
import { createRentSchedule } from "../../../services/paymentService.ts";
|
|
import { SessionCookie } from "../../auth/callback.ts";
|
|
|
|
export const handler = define.handlers({
|
|
async POST(ctx) {
|
|
try {
|
|
const stateData: SessionCookie = ctx.state.data;
|
|
|
|
const contentType = ctx.req.headers.get("content-type") || "";
|
|
let data: Record<string, string | number | string[]> = {};
|
|
let sender: string | null = null;
|
|
let isJsonRequest = false;
|
|
|
|
// Handle both JSON and form data
|
|
if (contentType.includes("application/json")) {
|
|
isJsonRequest = true;
|
|
const jsonData = await ctx.req.json();
|
|
data = jsonData;
|
|
} else {
|
|
const formData = await ctx.req.formData();
|
|
sender = formData.get("sender")?.toString() || null;
|
|
|
|
data = {
|
|
property_id: formData.get("property_id")?.toString() || "",
|
|
rent_amount: parseFloat(
|
|
formData.get("rent_amount")?.toString() || "0",
|
|
),
|
|
due_day: parseInt(formData.get("due_day")?.toString() || "1"),
|
|
late_fee_amount: parseFloat(
|
|
formData.get("late_fee_amount")?.toString() || "0",
|
|
),
|
|
grace_period_days: parseInt(
|
|
formData.get("grace_period_days")?.toString() || "0",
|
|
),
|
|
payment_methods_allowed:
|
|
formData.get("payment_methods_allowed")?.toString().split(",") ||
|
|
["card", "ach"],
|
|
};
|
|
}
|
|
|
|
const property_id = data.property_id as string;
|
|
const rent_amount = data.rent_amount as number;
|
|
const due_day = data.due_day as number;
|
|
const late_fee_amount = data.late_fee_amount as number || 0;
|
|
const grace_period_days = data.grace_period_days as number || 0;
|
|
const payment_methods_allowed =
|
|
data.payment_methods_allowed as string[] || ["card", "ach"];
|
|
|
|
if (!property_id || !rent_amount || !due_day) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: "Property ID, rent amount, and due day are required",
|
|
}),
|
|
{
|
|
status: 400,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
}
|
|
|
|
const result = await createRentSchedule(
|
|
property_id,
|
|
rent_amount,
|
|
due_day,
|
|
late_fee_amount,
|
|
grace_period_days,
|
|
payment_methods_allowed,
|
|
);
|
|
|
|
// Return appropriate response based on request type
|
|
if (isJsonRequest) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
rent_schedule: result[0],
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
},
|
|
);
|
|
} else {
|
|
// Redirect back to the sender URL
|
|
const senderPath = sender?.toString() || "/";
|
|
let redirectUrl = senderPath;
|
|
try {
|
|
if (senderPath.startsWith("/")) {
|
|
const url = new URL(ctx.req.url);
|
|
redirectUrl = url.origin + senderPath;
|
|
}
|
|
} catch (_) {
|
|
// fallback: just use senderPath
|
|
}
|
|
return Response.redirect(redirectUrl, 303);
|
|
}
|
|
} catch (e) {
|
|
return new Response(JSON.stringify({ error: e }), {
|
|
status: 500,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
},
|
|
});
|