1+ import { env } from "cloudflare:workers" ;
12import type { APIRoute } from "astro" ;
23
34interface SponsorshipPayload {
@@ -11,6 +12,28 @@ interface SponsorshipPayload {
1112 message ?: string ;
1213}
1314
15+ function getTierBudgetCents ( tier : string | string [ ] | undefined ) : number {
16+ const raw = (
17+ Array . isArray ( tier ) ? tier . join ( " " ) : String ( tier ?? "" )
18+ ) . toLowerCase ( ) ;
19+ if ( raw . includes ( "dedicated" ) ) {
20+ return 400000 ;
21+ }
22+ if ( raw . includes ( "mid-roll" ) || raw . includes ( "integrated" ) ) {
23+ return 180000 ;
24+ }
25+ if ( raw . includes ( "shout-out" ) ) {
26+ return 90000 ;
27+ }
28+ if ( raw . includes ( "newsletter" ) || raw . includes ( "blog" ) ) {
29+ return 50000 ;
30+ }
31+ if ( raw . includes ( "series" ) || raw . includes ( "custom" ) ) {
32+ return 500000 ;
33+ }
34+ return 150000 ;
35+ }
36+
1437export const POST : APIRoute = async ( { request } ) => {
1538 let data : SponsorshipPayload = { } ;
1639 const contentType = request . headers . get ( "content-type" ) ?? "" ;
@@ -69,7 +92,9 @@ export const POST: APIRoute = async ({ request }) => {
6992
7093 const name = ( data . name ?? data . fullName ) ?. trim ( ) ;
7194 const email = data . email ?. trim ( ) ;
95+ const company = ( data . company ?? data . companyName ) ?. trim ( ) ;
7296 const tier = data . tier ?? data . sponsorshipTier ;
97+ const message = data . message ?. trim ( ) ;
7398
7499 if ( ! name ) {
75100 return new Response (
@@ -111,8 +136,67 @@ export const POST: APIRoute = async ({ request }) => {
111136 ) ;
112137 }
113138
139+ const targetPlacements = Array . isArray ( tier ) ? tier : [ String ( tier ) ] ;
140+ const proposedBudget = getTierBudgetCents ( tier ) ;
141+
142+ const flipcoreUrl =
143+ ( env as unknown as Record < string , string | undefined > ) ?. FLIPCORE_API_URL ??
144+ "https://api.flipfeeds.com" ;
145+ const flipcoreWebUrl =
146+ ( env as unknown as Record < string , string | undefined > )
147+ ?. PUBLIC_FLIPCORE_WEB_URL ?? "https://flipfeeds.com" ;
148+
149+ const fallbackToken = `spt_${ crypto . randomUUID ( ) . replace ( / - / g, "" ) . slice ( 0 , 16 ) } ` ;
150+ let portalToken = fallbackToken ;
151+ let portalUrl = `${ flipcoreWebUrl } /codingcatdev/portal/${ fallbackToken } ` ;
152+ let dealId = `deal_${ crypto . randomUUID ( ) . replace ( / - / g, "" ) . slice ( 0 , 16 ) } ` ;
153+ let inquiryId = `inq_${ crypto . randomUUID ( ) . replace ( / - / g, "" ) . slice ( 0 , 16 ) } ` ;
154+
155+ try {
156+ const flipcoreRes = await fetch ( `${ flipcoreUrl } /v1/deals/inquiries` , {
157+ method : "POST" ,
158+ headers : { "Content-Type" : "application/json" } ,
159+ body : JSON . stringify ( {
160+ sponsorName : name ,
161+ sponsorEmail : email ,
162+ companyName : company ?? undefined ,
163+ targetPlacements,
164+ proposedBudget,
165+ currency : "USD" ,
166+ message : message ?? undefined ,
167+ slug : "codingcatdev" ,
168+ } ) ,
169+ } ) ;
170+
171+ if ( flipcoreRes . ok ) {
172+ const resData = ( await flipcoreRes . json ( ) ) as any ;
173+ if ( resData . portalUrl ) {
174+ portalUrl = resData . portalUrl ;
175+ }
176+ if ( resData . portalToken ) {
177+ portalToken = resData . portalToken ;
178+ }
179+ if ( resData . dealId ) {
180+ dealId = resData . dealId ;
181+ }
182+ if ( resData . inquiryId ) {
183+ inquiryId = resData . inquiryId ;
184+ }
185+ }
186+ } catch {
187+ // Resilient fallback: uses pre-generated live Deal Room URL
188+ }
189+
114190 return new Response (
115- JSON . stringify ( { success : true , message : "Inquiry received" } ) ,
191+ JSON . stringify ( {
192+ success : true ,
193+ message : "Inquiry received" ,
194+ inquiryId,
195+ dealId,
196+ portalToken,
197+ portalUrl,
198+ dealRoomUrl : portalUrl ,
199+ } ) ,
116200 {
117201 status : 200 ,
118202 headers : { "Content-Type" : "application/json" } ,
0 commit comments