mirror of
https://github.com/Motion-Games/MotionWebStudio.git
synced 2026-04-21 09:00:53 +02:00
stripe
This commit is contained in:
104
supabase/functions/check-subscriptions/index.ts
Normal file
104
supabase/functions/check-subscriptions/index.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
|
||||
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.39.3'
|
||||
|
||||
declare const Deno: any;
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||||
}
|
||||
|
||||
Deno.serve(async (req: any) => {
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response('ok', { headers: corsHeaders })
|
||||
}
|
||||
|
||||
try {
|
||||
const supabase = createClient(
|
||||
Deno.env.get('SUPABASE_URL') ?? '',
|
||||
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
|
||||
)
|
||||
|
||||
// Fetch active subscriptions
|
||||
const { data: subscriptions, error } = await supabase
|
||||
.from('maintenance_subscriptions')
|
||||
.select('*, order:orders(*)')
|
||||
.eq('status', 'active');
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
const notificationsToSend: any[] = [];
|
||||
const today = new Date();
|
||||
|
||||
// Mock email sending function for now
|
||||
const sendEmailMock = async (payload: any) => {
|
||||
console.log("Mock email sent:", payload);
|
||||
// In production, you would invoke the 'resend' function here
|
||||
};
|
||||
|
||||
if (subscriptions) {
|
||||
for (const sub of subscriptions) {
|
||||
const billingDate = new Date(sub.next_billing_date);
|
||||
const daysUntil = Math.ceil((billingDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24));
|
||||
|
||||
// Logic: Notify if <= 30 days and not notified recently (e.g. in last 14 days)
|
||||
let shouldNotify = false;
|
||||
|
||||
// If it's already overdue or coming up in 30 days
|
||||
if (daysUntil <= 30) {
|
||||
if (!sub.last_notified_at) {
|
||||
shouldNotify = true;
|
||||
} else {
|
||||
const lastNotified = new Date(sub.last_notified_at);
|
||||
const daysSinceLastNotification = Math.ceil((today.getTime() - lastNotified.getTime()) / (1000 * 60 * 60 * 24));
|
||||
// Only notify if 2 weeks passed since last notification
|
||||
if (daysSinceLastNotification > 14) {
|
||||
shouldNotify = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldNotify) {
|
||||
const payload = {
|
||||
to: sub.client_email,
|
||||
subject: `Emlékeztető: Éves díj esedékes (${daysUntil} nap)`,
|
||||
daysLeft: daysUntil,
|
||||
domain: sub.order?.details?.domainName || 'Weboldal',
|
||||
subscriptionId: sub.id
|
||||
};
|
||||
|
||||
notificationsToSend.push(payload);
|
||||
|
||||
// Itt hívnánk meg a sendEmail függvényt
|
||||
await sendEmailMock(payload);
|
||||
|
||||
// Update last_notified_at to keep data consistent
|
||||
const { error: updateError } = await supabase
|
||||
.from('maintenance_subscriptions')
|
||||
.update({ last_notified_at: new Date().toISOString() })
|
||||
.eq('id', sub.id);
|
||||
|
||||
if (updateError) {
|
||||
console.error(`Failed to update subscription ${sub.id}:`, updateError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({ success: true, notifications: notificationsToSend }),
|
||||
{
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: 200,
|
||||
}
|
||||
)
|
||||
} catch (err: any) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: err.message }),
|
||||
{
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
status: 400,
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user