// routes/landlord/onboarding/profile.tsx import { define } from "../../../utils.ts"; import { page } from "fresh"; import type { SessionCookie } from "../../auth/callback.ts"; import { getUserById, updateUser } from "../../../services/userService.ts"; import { getOnboardingStatus, setOnboardingStep, } from "../../../services/onboardingService.ts"; import { User } from "../../../db/interfaces.ts"; import { createPublicId } from "../../../helpers.ts"; export const handler = define.handlers({ async GET(ctx) { const stateData = ctx.state.data as SessionCookie; const { user_id } = stateData; const publicId = stateData.user_id ? await createPublicId(stateData.user_id) : ""; if (!user_id) { return new Response("", { status: 302, headers: { Location: "/auth/login" }, }); } const status = await getOnboardingStatus(user_id); const onboardingStep = status?.onboarding_step ?? 0; if (status?.onboarded_landlord && onboardingStep === 0) { return new Response("", { status: 303, headers: { Location: "/landlord/properties/add" }, }); } const user = await getUserById(user_id); return page({ stateData, user: user ? { ...user, id: publicId } : null }); }, async POST(ctx) { const stateData = ctx.state.data as SessionCookie; const { user_id } = stateData; const publicId = stateData.user_id ? await createPublicId(stateData.user_id) : ""; if (!user_id) { return new Response("", { status: 302, headers: { Location: "/auth/login" }, }); } const status = await getOnboardingStatus(user_id); const onboardingStep = status?.onboarding_step ?? 0; if (status?.onboarded_landlord && onboardingStep === 0) { return new Response("", { status: 303, headers: { Location: "/landlord/properties/add" }, }); } const formData = await ctx.req.formData(); const phone = (formData.get("phone") || "").toString().trim(); const address_1 = (formData.get("address_1") || "").toString().trim(); const city = (formData.get("city") || "").toString().trim(); const state = (formData.get("state") || "").toString().trim(); const zip = (formData.get("zip") || "").toString().trim(); const country = (formData.get("country") || "").toString().trim(); if (!phone || !address_1 || !city || !state || !zip || !country) { const user = await getUserById(user_id); return page({ stateData, user: user ? { ...user, id: publicId } : null, error: "Phone and full address are required.", }); } await updateUser(user_id, { name: (formData.get("name") || "").toString(), email: (formData.get("email") || "").toString(), phone: phone || "", address_1: address_1 || "", address_2: (formData.get("address_2") || "").toString(), city: city || "", state: state || "", zip: zip || "", country: country || "", user_type: "landlord", onboarded_landlord: true, } as Partial); await setOnboardingStep(user_id, 2); return new Response("", { status: 302, headers: { Location: "/landlord/onboarding/property-setup" }, }); }, }); export default define.page(function LandlordProfileSetup({ data }) { const { stateData, user, error } = data as { stateData: SessionCookie; user: any; error?: string; }; return (

Complete Your Landlord Profile

Tell us about yourself to get started.

{error && (
{error}
)}
); });