Skip to content

Commit e72753c

Browse files
committed
fix update check
1 parent 07d9a33 commit e72753c

File tree

1 file changed

+66
-61
lines changed

1 file changed

+66
-61
lines changed

devcon-api/src/services/ai/open-ai/open-ai.ts

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -536,33 +536,48 @@ export const destinoApi = (() => {
536536
},
537537
generateDestinoEvent: async (event: any) => {
538538
const eventRecord = await _interface.getDestinoEvent(event.Id)
539+
const eventExists = !!eventRecord
539540

540541
// 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)) {
543543
return eventRecord.content
544544
}
545545

546546
// Otherwise generate new content
547547
console.log(`Generating new content for Destino event ${event.Id}`)
548548

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+
})
562577

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 }
564579

565-
const prompt = `
580+
const prompt = `
566581
Create an image to advertise the following event:
567582
568583
Event name: ${event.Name}
@@ -572,61 +587,51 @@ export const destinoApi = (() => {
572587
Do not use any text in the generated image. Try to avoid too many details in the image, we want to keep it simple.
573588
`
574589

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' })
577592

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+
})
586601

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
590605

591-
let imageUrl = null
606+
let imageUrl = null
592607

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+
})
599614

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`)
607622

608-
imageUrl = publicUrl
623+
imageUrl = publicUrl
624+
}
609625
}
626+
627+
upsert.image_url = imageUrl
628+
upsert.content = content
610629
}
611630

612631
// 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)
628633

629-
return result
634+
// return result
630635
},
631636
generateDestinoEvents: async () => {
632637
const events = await fetchFromSalesforce()

0 commit comments

Comments
 (0)