const REGULAR_PACKAGE_PRICE = 200; const REGULAR_PACKAGE_MATCHES = 6; const SUGAR_MUMMY_PRICE = 199; function createConnectionId() { return ( "CON-" + Date.now().toString(36).toUpperCase() + "-" + Math.random() .toString(36) .substring(2, 8) .toUpperCase() ); } export async function onRequestPost({ request, env }) { try { if (!env.EC_KV) { return Response.json( { error: "Database is not connected yet." }, { status: 500 } ); } const data = await request.json(); const registrationId = String(data.registrationId || "").trim(); const targetProfileId = String(data.targetProfileId || "").trim(); if (!registrationId) { return Response.json( { error: "Missing registrationId." }, { status: 400 } ); } if (!targetProfileId) { return Response.json( { error: "Missing target profile." }, { status: 400 } ); } if (registrationId === targetProfileId) { return Response.json( { error: "You cannot connect with your own profile." }, { status: 400 } ); } const personRaw = await env.EC_KV.get( `registration:${registrationId}` ); if (!personRaw) { return Response.json( { error: "Your registration was not found." }, { status: 404 } ); } const person = JSON.parse(personRaw); const targetRaw = await env.EC_KV.get( `registration:${targetProfileId}` ); if (!targetRaw) { return Response.json( { error: "The selected profile was not found." }, { status: 404 } ); } const target = JSON.parse(targetRaw); if (target.available_for_matching === false) { return Response.json( { error: "This profile is no longer available." }, { status: 400 } ); } if (target.consent_profile_visible === false) { return Response.json( { error: "This profile is not currently visible." }, { status: 400 } ); } const profileType = target.profile_type || "regular"; /* ========================================================== DUPLICATE REQUEST PROTECTION ========================================================== */ const existingConnectionId = await env.EC_KV.get( `connection_request:${registrationId}:${targetProfileId}` ); if (existingConnectionId) { const existingRaw = await env.EC_KV.get( `connection:${existingConnectionId}` ); if (existingRaw) { const existing = JSON.parse(existingRaw); return Response.json({ success: true, existing: true, connectionId: existing.id, paymentRequired: existing.status === "awaiting_payment", paymentType: existing.payment_type, amount: existing.amount, package: existing.package, matchesIncluded: existing.matches_included || 0, matchesRemaining: existing.matches_remaining ?? null, status: existing.status, contactUnlocked: existing.contact_unlocked === true, message: existing.status === "awaiting_payment" ? ( profileType === "sugar_mummy" ? "This connection is waiting for the KSh 199 payment." : "This connection is waiting for the KSh 200 package payment." ) : existing.status === "awaiting_consent" ? "This connection request has already been sent and is awaiting consent." : existing.status === "connected" ? "This connection has already been accepted." : "A connection request already exists for this profile." }); } } /* ========================================================== SUGAR MUMMY ========================================================== */ if (profileType === "sugar_mummy") { const connectionId = createConnectionId(); const connection = { id: connectionId, requester_id: registrationId, target_profile_id: targetProfileId, requester_name: person.display_name || person.name || "", target_name: target.display_name || target.name || "", profile_type: "sugar_mummy", payment_type: "sugar_mummy_connection", amount: SUGAR_MUMMY_PRICE, package: null, matches_included: 1, matches_remaining: 0, status: "awaiting_payment", contact_unlocked: false, requester_consent: true, target_consent: false, payment_id: null, payment_receipt: null, paid_at: null, created_at: new Date().toISOString(), updated_at: new Date().toISOString() }; await env.EC_KV.put( `connection:${connectionId}`, JSON.stringify(connection) ); await env.EC_KV.put( `connection_request:${registrationId}:${targetProfileId}`, connectionId ); return Response.json({ success: true, connectionId, paymentRequired: true, paymentType: "sugar_mummy_connection", amount: SUGAR_MUMMY_PRICE, package: null, matchesIncluded: 1, matchesRemaining: 0, status: "awaiting_payment", contactUnlocked: false, message: "This Sugar Mummy connection requires a KSh 199 connection fee." }); } /* ========================================================== REGULAR MATCHMAKING PACKAGE ========================================================== */ let matchesRemaining = Number( person.matches_remaining || 0 ); const packageActive = person.package_active === true && person.package === "relationship" && matchesRemaining > 0; /* ---------------------------------------------------------- ACTIVE PACKAGE ---------------------------------------------------------- If the user already paid KSh 200 and still has connection opportunities remaining, DO NOT ask them to pay again. ---------------------------------------------------------- */ if (packageActive) { matchesRemaining--; const updatedPerson = { ...person, matches_remaining: matchesRemaining, connection_count: Number( person.connection_count || 0 ) + 1, updated_at: new Date().toISOString() }; /* Keep the package active while opportunities remain. Once all 6 have been used, deactivate it. */ if ( matchesRemaining <= 0 ) { updatedPerson.package_active = false; updatedPerson.status = "Package Used"; } await env.EC_KV.put( `registration:${registrationId}`, JSON.stringify( updatedPerson ) ); const connectionId = createConnectionId(); const connection = { id: connectionId, requester_id: registrationId, target_profile_id: targetProfileId, requester_name: person.display_name || person.name || "", target_name: target.display_name || target.name || "", profile_type: "regular", payment_type: "regular_match_package", amount: 0, package: "relationship", matches_included: REGULAR_PACKAGE_MATCHES, matches_remaining: matchesRemaining, status: "awaiting_consent", contact_unlocked: false, requester_consent: true, target_consent: false, payment_id: person.payment_id || null, payment_receipt: person.payment_receipt || null, paid_at: person.package_activated_at || null, created_at: new Date().toISOString(), updated_at: new Date().toISOString() }; await env.EC_KV.put( `connection:${connectionId}`, JSON.stringify(connection) ); await env.EC_KV.put( `connection_request:${registrationId}:${targetProfileId}`, connectionId ); return Response.json({ success: true, connectionId, paymentRequired: false, paymentType: "regular_match_package", amount: 0, package: "relationship", matchesIncluded: REGULAR_PACKAGE_MATCHES, matchesRemaining, status: "awaiting_consent", contactUnlocked: false, message: matchesRemaining > 0 ? `Connection request sent. You have ${matchesRemaining} connection opportunities remaining.` : "Connection request sent. Your 6 connection opportunities have now been used." }); } /* ---------------------------------------------------------- NO ACTIVE PACKAGE ---------------------------------------------------------- The user must purchase the KSh 200 package. The first connection will be counted as one of the six once payment succeeds. ---------------------------------------------------------- */ const connectionId = createConnectionId(); const connection = { id: connectionId, requester_id: registrationId, target_profile_id: targetProfileId, requester_name: person.display_name || person.name || "", target_name: target.display_name || target.name || "", profile_type: "regular", payment_type: "regular_match_package", amount: REGULAR_PACKAGE_PRICE, package: "relationship", matches_included: REGULAR_PACKAGE_MATCHES, matches_remaining: REGULAR_PACKAGE_MATCHES, status: "awaiting_payment", contact_unlocked: false, requester_consent: true, target_consent: false, payment_id: null, payment_receipt: null, paid_at: null, created_at: new Date().toISOString(), updated_at: new Date().toISOString() }; await env.EC_KV.put( `connection:${connectionId}`, JSON.stringify(connection) ); await env.EC_KV.put( `connection_request:${registrationId}:${targetProfileId}`, connectionId ); return Response.json({ success: true, connectionId, paymentRequired: true, paymentType: "regular_match_package", amount: REGULAR_PACKAGE_PRICE, package: "relationship", matchesIncluded: REGULAR_PACKAGE_MATCHES, matchesRemaining: REGULAR_PACKAGE_MATCHES, status: "awaiting_payment", contactUnlocked: false, message: "This matchmaking package costs KSh 200 and includes up to 6 connection opportunities." }); } catch (error) { console.error( "Connection request error:", error ); return Response.json( { error: "Unable to create connection request." }, { status: 500 } ); } }