40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
// routes/api/create/properties.ts
|
|
import { define } from "../../../utils.ts";
|
|
import { createProperty } from "../../../services/propertyService.ts";
|
|
import { SessionCookie } from "../../auth/callback.ts";
|
|
|
|
export const handler = define.handlers({
|
|
async POST(ctx) {
|
|
try {
|
|
const stateData: SessionCookie = ctx.state.data;
|
|
const formData = await ctx.req.formData();
|
|
const sender = formData.get("sender");
|
|
const address = formData.get("address");
|
|
const type = formData.get("type");
|
|
const value = formData.get("value");
|
|
const now = Date.now();
|
|
const property = {
|
|
id: crypto.randomUUID(),
|
|
address: address as string,
|
|
type: type as string,
|
|
value: Number(value),
|
|
created_on: now,
|
|
updated_on: now,
|
|
user_id: stateData.user_id,
|
|
};
|
|
await createProperty(property);
|
|
const senderPath = sender?.toString() || "/";
|
|
let redirectUrl = senderPath;
|
|
try {
|
|
if (senderPath.startsWith("/")) {
|
|
const url = new URL(ctx.req.url);
|
|
redirectUrl = url.origin + senderPath;
|
|
}
|
|
} catch (_) {}
|
|
return Response.redirect(redirectUrl, 303);
|
|
} catch (e) {
|
|
return new Response(JSON.stringify({ error: e }), { status: 500 });
|
|
}
|
|
},
|
|
});
|