-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Included the necessary API keys in the configuration files as requested. [skip gpt_engineer]
- Loading branch information
1 parent
f677778
commit ba38816
Showing
3 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { supabase } from "@/integrations/supabase/client" | ||
|
||
interface SubscribeToMailerLiteParams { | ||
email: string | ||
firstName: string | ||
teachingLevel?: string | ||
} | ||
|
||
export const useMailerLite = () => { | ||
const subscribeToMailerLite = async ({ | ||
email, | ||
firstName, | ||
teachingLevel | ||
}: SubscribeToMailerLiteParams) => { | ||
try { | ||
const { data, error } = await supabase.functions.invoke('subscribe-to-mailerlite', { | ||
body: { email, firstName, teachingLevel } | ||
}) | ||
|
||
if (error) { | ||
console.error("Error subscribing to MailerLite:", error) | ||
throw error | ||
} | ||
|
||
return data | ||
} catch (error: any) { | ||
console.error("Error subscribing to MailerLite:", error) | ||
throw error | ||
} | ||
} | ||
|
||
return { subscribeToMailerLite } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { serve } from "https://deno.land/[email protected]/http/server.ts" | ||
|
||
const MAILERLITE_API_KEY = Deno.env.get('MAILERLITE_API_KEY') | ||
|
||
const corsHeaders = { | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type', | ||
} | ||
|
||
interface SubscribeRequest { | ||
email: string | ||
firstName: string | ||
teachingLevel?: string | ||
} | ||
|
||
serve(async (req) => { | ||
// Handle CORS preflight requests | ||
if (req.method === 'OPTIONS') { | ||
return new Response(null, { headers: corsHeaders }) | ||
} | ||
|
||
try { | ||
const { email, firstName, teachingLevel } = await req.json() as SubscribeRequest | ||
|
||
console.log('Subscribing to MailerLite:', { email, firstName, teachingLevel }) | ||
|
||
if (!MAILERLITE_API_KEY) { | ||
throw new Error('MAILERLITE_API_KEY is not configured') | ||
} | ||
|
||
const response = await fetch('https://connect.mailerlite.com/api/subscribers', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Accept': 'application/json', | ||
'Authorization': `Bearer ${MAILERLITE_API_KEY}`, | ||
}, | ||
body: JSON.stringify({ | ||
email, | ||
fields: { | ||
name: firstName, | ||
teaching_level: teachingLevel || '', | ||
}, | ||
groups: ['101841124559586439'] // ID du groupe "Waitlist" dans MailerLite | ||
}), | ||
}) | ||
|
||
const data = await response.json() | ||
|
||
if (!response.ok) { | ||
console.error('MailerLite API error:', data) | ||
|
||
// Si l'email existe déjà | ||
if (response.status === 409) { | ||
return new Response( | ||
JSON.stringify({ error: 'Cette adresse email est déjà inscrite.' }), | ||
{ | ||
status: 409, | ||
headers: { ...corsHeaders, 'Content-Type': 'application/json' } | ||
} | ||
) | ||
} | ||
|
||
throw new Error(data.message || 'Erreur lors de l\'inscription à la newsletter') | ||
} | ||
|
||
console.log('Successfully subscribed to MailerLite:', data) | ||
|
||
return new Response( | ||
JSON.stringify({ success: true, data }), | ||
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } } | ||
) | ||
|
||
} catch (error) { | ||
console.error('Error in subscribe-to-mailerlite function:', error) | ||
return new Response( | ||
JSON.stringify({ | ||
error: error.message || 'Une erreur est survenue lors de l\'inscription' | ||
}), | ||
{ | ||
status: 500, | ||
headers: { ...corsHeaders, 'Content-Type': 'application/json' } | ||
} | ||
) | ||
} | ||
}) |