@@ -536,33 +536,48 @@ export const destinoApi = (() => {
536
536
} ,
537
537
generateDestinoEvent : async ( event : any ) => {
538
538
const eventRecord = await _interface . getDestinoEvent ( event . Id )
539
+ const eventExists = ! ! eventRecord
539
540
540
541
// If exists and updated after last modification, return cached content
541
- if ( eventRecord && new Date ( eventRecord . updated_at ) > new Date ( event . LastModifiedDate ) ) {
542
- // console.log(`Using cached content for event ${event.Id}`)
542
+ if ( eventRecord && new Date ( eventRecord . updated_at ) < new Date ( event . LastModifiedDate ) ) {
543
543
return eventRecord . content
544
544
}
545
545
546
546
// Otherwise generate new content
547
547
console . log ( `Generating new content for Destino event ${ event . Id } ` )
548
548
549
- const eventCompletion = await openai . beta . chat . completions . parse ( {
550
- temperature : 0 ,
551
- model : 'gpt-4.1' ,
552
- messages : [
553
- {
554
- role : 'system' ,
555
- content :
556
- 'You take an event object and generate a simple summary of it in English, Spanish and Portuguese. Max 500 characters. It will be used to advertise the event.' ,
557
- } ,
558
- { role : 'user' , content : JSON . stringify ( { ...event , description : '' } ) } ,
559
- ] ,
560
- response_format : zodResponseFormat ( EventSchema , 'summary' ) ,
561
- } )
549
+ const upsert = {
550
+ event_id : event . Id ,
551
+ twitter_handle : event . Twitter ,
552
+ type_of_event : event [ 'Type of Event' ] ,
553
+ location : event . Location ,
554
+ link : event . Link ,
555
+ name : event . Name ,
556
+ date : event . Date . startDate ,
557
+ target_audience : event . TargetAudience ,
558
+ details : event . Details ,
559
+ updated_at : new Date ( ) . toISOString ( ) ,
560
+ last_modified_at : event . LastModifiedDate ,
561
+ } as any
562
+
563
+ if ( ! eventExists ) {
564
+ const eventCompletion = await openai . beta . chat . completions . parse ( {
565
+ temperature : 0 ,
566
+ model : 'gpt-4.1' ,
567
+ messages : [
568
+ {
569
+ role : 'system' ,
570
+ content :
571
+ 'You take an event object and generate a simple summary of it in English, Spanish and Portuguese. Max 500 characters. It will be used to advertise the event.' ,
572
+ } ,
573
+ { role : 'user' , content : JSON . stringify ( { ...event , description : '' } ) } ,
574
+ ] ,
575
+ response_format : zodResponseFormat ( EventSchema , 'summary' ) ,
576
+ } )
562
577
563
- const content = eventCompletion . choices [ 0 ] . message . parsed as { en : string ; es : string ; pt : string }
578
+ const content = eventCompletion . choices [ 0 ] . message . parsed as { en : string ; es : string ; pt : string }
564
579
565
- const prompt = `
580
+ const prompt = `
566
581
Create an image to advertise the following event:
567
582
568
583
Event name: ${ event . Name }
@@ -572,61 +587,51 @@ export const destinoApi = (() => {
572
587
Do not use any text in the generated image. Try to avoid too many details in the image, we want to keep it simple.
573
588
`
574
589
575
- // const referenceImagePath = path.join(__dirname, 'image-generation', 'destino.png')
576
- // const openAICompatibleImage = await toFile(fs.createReadStream(referenceImagePath), null, { type: 'image/png' })
590
+ // const referenceImagePath = path.join(__dirname, 'image-generation', 'destino.png')
591
+ // const openAICompatibleImage = await toFile(fs.createReadStream(referenceImagePath), null, { type: 'image/png' })
577
592
578
- const resultImage = await openai . images . generate ( {
579
- model : 'dall-e-3' ,
580
- prompt,
581
- // image: openAICompatibleImage,
582
- response_format : 'b64_json' ,
583
- n : 1 ,
584
- size : '1792x1024' ,
585
- } )
593
+ const resultImage = await openai . images . generate ( {
594
+ model : 'dall-e-3' ,
595
+ prompt,
596
+ // image: openAICompatibleImage,
597
+ response_format : 'b64_json' ,
598
+ n : 1 ,
599
+ size : '1792x1024' ,
600
+ } )
586
601
587
- // Save the image to a file
588
- const image_base64 = resultImage . data [ 0 ] . b64_json
589
- const image_bytes = image_base64 ? Buffer . from ( image_base64 , 'base64' ) : null
602
+ // Save the image to a file
603
+ const image_base64 = resultImage . data [ 0 ] . b64_json
604
+ const image_bytes = image_base64 ? Buffer . from ( image_base64 , 'base64' ) : null
590
605
591
- let imageUrl = null
606
+ let imageUrl = null
592
607
593
- if ( image_bytes ) {
594
- // Upload to Supabase Storage
595
- const { data : uploadData , error : uploadError } = await supabase . storage . from ( 'destino-events' ) . upload ( `${ event . Id } .png` , image_bytes , {
596
- contentType : 'image/png' ,
597
- upsert : true ,
598
- } )
608
+ if ( image_bytes ) {
609
+ // Upload to Supabase Storage
610
+ const { data : uploadData , error : uploadError } = await supabase . storage . from ( 'destino-events' ) . upload ( `${ event . Id } .png` , image_bytes , {
611
+ contentType : 'image/png' ,
612
+ upsert : true ,
613
+ } )
599
614
600
- if ( uploadError ) {
601
- console . error ( 'Error uploading image:' , uploadError )
602
- } else {
603
- // Get public URL
604
- const {
605
- data : { publicUrl } ,
606
- } = supabase . storage . from ( 'destino-events' ) . getPublicUrl ( `${ event . Id } .png` )
615
+ if ( uploadError ) {
616
+ console . error ( 'Error uploading image:' , uploadError )
617
+ } else {
618
+ // Get public URL
619
+ const {
620
+ data : { publicUrl } ,
621
+ } = supabase . storage . from ( 'destino-events' ) . getPublicUrl ( `${ event . Id } .png` )
607
622
608
- imageUrl = publicUrl
623
+ imageUrl = publicUrl
624
+ }
609
625
}
626
+
627
+ upsert . image_url = imageUrl
628
+ upsert . content = content
610
629
}
611
630
612
631
// Save to Supabase
613
- const result = await supabase . from ( 'destino_events' ) . upsert ( {
614
- event_id : event . Id ,
615
- content,
616
- twitter_handle : event . Twitter ,
617
- type_of_event : event [ 'Type of Event' ] ,
618
- location : event . Location ,
619
- link : event . Link ,
620
- name : event . Name ,
621
- date : event . Date . startDate ,
622
- target_audience : event . TargetAudience ,
623
- details : event . Details ,
624
- image_url : imageUrl ,
625
- updated_at : new Date ( ) . toISOString ( ) ,
626
- last_modified_at : event . LastModifiedDate ,
627
- } )
632
+ // const result = await supabase.from('destino_events').upsert(upsert)
628
633
629
- return result
634
+ // return result
630
635
} ,
631
636
generateDestinoEvents : async ( ) => {
632
637
const events = await fetchFromSalesforce ( )
0 commit comments