| 1 | import { NextResponse } from "next/server"; |
| 2 | |
| 3 | type JourneyType = "PERSONAL_LOAN" | "GOLD_LOAN"; |
| 4 | |
| 5 | type HostedSessionRequest = { |
| 6 | borrowerProviderID?: string; |
| 7 | journeyType?: JourneyType; |
| 8 | profile?: { |
| 9 | contactNumber?: string; |
| 10 | pan?: string; |
| 11 | panName?: string; |
| 12 | dob?: string; |
| 13 | gender?: "male" | "female" | "transgender"; |
| 14 | personalemail?: string; |
| 15 | }; |
| 16 | workProfile?: { |
| 17 | employmentType?: "salaried" | "selfEmployed"; |
| 18 | officialemail?: string; |
| 19 | income?: string | number; |
| 20 | incomeType?: "monthly" | "annual"; |
| 21 | companyName?: string; |
| 22 | udyamNumber?: string; |
| 23 | }; |
| 24 | address?: { |
| 25 | addressL1?: string; |
| 26 | addressL2?: string; |
| 27 | city?: string; |
| 28 | state?: string; |
| 29 | pincode?: string; |
| 30 | }; |
| 31 | journey?: { |
| 32 | endUse?: string; |
| 33 | bureauConsent?: boolean; |
| 34 | }; |
| 35 | goldLoan?: { |
| 36 | userType?: "individual" | "non-individual"; |
| 37 | constitution?: string | null; |
| 38 | jewelleryWeightGrams?: string | number; |
| 39 | purity?: "24K" | "22K" | "21K" | "18K" | "14K" | "9K"; |
| 40 | endUse?: |
| 41 | | "marriage" |
| 42 | | "familyFunctions" |
| 43 | | "medicalTreatmentAndEmergencies" |
| 44 | | "travelEducationExpenses" |
| 45 | | "businessExpansion" |
| 46 | | "agricultureAndFarmRelatedNeeds" |
| 47 | | "purchaseOfEquipment" |
| 48 | | "other"; |
| 49 | bureauConsent?: boolean; |
| 50 | aaID?: string; |
| 51 | requestedAmount?: string | number; |
| 52 | requestedTenureMonths?: number; |
| 53 | }; |
| 54 | }; |
| 55 | |
| 56 | type HostedSessionResponse = { |
| 57 | embedURL: string; |
| 58 | }; |
| 59 | |
| 60 | export async function POST(request: Request) { |
| 61 | const payload = (await request.json().catch(() => null)) as |
| 62 | | HostedSessionRequest |
| 63 | | null; |
| 64 | |
| 65 | const borrowerProviderID = payload?.borrowerProviderID?.trim(); |
| 66 | const journeyType = payload?.journeyType; |
| 67 | |
| 68 | if (!borrowerProviderID) { |
| 69 | return NextResponse.json( |
| 70 | { error: "Missing borrowerProviderID." }, |
| 71 | { status: 400 }, |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | if (journeyType !== "PERSONAL_LOAN" && journeyType !== "GOLD_LOAN") { |
| 76 | return NextResponse.json( |
| 77 | { error: "journeyType must be PERSONAL_LOAN or GOLD_LOAN." }, |
| 78 | { status: 400 }, |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | const platformBaseURL = process.env.PLATFORM_BASE_URL; |
| 83 | const platformAPIKey = process.env.PLATFORM_API_KEY; |
| 84 | |
| 85 | if (!platformBaseURL || !platformAPIKey) { |
| 86 | return NextResponse.json( |
| 87 | { error: "Hosted Experience configuration is missing." }, |
| 88 | { status: 500 }, |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | const response = await fetch(`${platformBaseURL}/api/lab/sessions`, { |
| 93 | method: "POST", |
| 94 | headers: { |
| 95 | Authorization: `Bearer ${platformAPIKey}`, |
| 96 | "Content-Type": "application/json", |
| 97 | }, |
| 98 | body: JSON.stringify({ |
| 99 | borrowerProviderID, |
| 100 | journeyType, |
| 101 | profile: payload?.profile, |
| 102 | workProfile: payload?.workProfile, |
| 103 | address: payload?.address, |
| 104 | journey: payload?.journey, |
| 105 | goldLoan: payload?.goldLoan, |
| 106 | }), |
| 107 | cache: "no-store", |
| 108 | }); |
| 109 | |
| 110 | if (!response.ok) { |
| 111 | const details = await response.json().catch(() => ({})); |
| 112 | return NextResponse.json( |
| 113 | { error: "Failed to create Hosted Experience URL.", details }, |
| 114 | { status: response.status }, |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | const data = (await response.json()) as HostedSessionResponse; |
| 119 | |
| 120 | return NextResponse.json( |
| 121 | { embedURL: data.embedURL }, |
| 122 | { |
| 123 | headers: { |
| 124 | "Cache-Control": "no-store", |
| 125 | }, |
| 126 | }, |
| 127 | ); |
| 128 | } |