95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
import { serve } from 'https://deno.land/std@0.168.0/http/server.ts';
|
||
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2';
|
||
|
||
const corsHeaders = {
|
||
'Access-Control-Allow-Origin': '*',
|
||
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
||
};
|
||
|
||
serve(async (req) => {
|
||
if (req.method === 'OPTIONS') {
|
||
return new Response('ok', { headers: corsHeaders });
|
||
}
|
||
|
||
try {
|
||
const supabaseClient = createClient(
|
||
Deno.env.get('SUPABASE_URL') ?? '',
|
||
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
|
||
);
|
||
|
||
const { requestId } = await req.json();
|
||
|
||
if (!requestId) {
|
||
return new Response(
|
||
JSON.stringify({ error: 'Request ID gerekli' }),
|
||
{ status: 400, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
||
);
|
||
}
|
||
|
||
// İstek bilgisini al
|
||
const { data: exportRequest, error: fetchError } = await supabaseClient
|
||
.from('data_export_requests')
|
||
.select('*')
|
||
.eq('id', requestId)
|
||
.single();
|
||
|
||
if (fetchError || !exportRequest) {
|
||
return new Response(
|
||
JSON.stringify({ error: 'İstek bulunamadı' }),
|
||
{ status: 404, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
||
);
|
||
}
|
||
|
||
// İsteği processing olarak işaretle
|
||
await supabaseClient
|
||
.from('data_export_requests')
|
||
.update({ status: 'processing' })
|
||
.eq('id', requestId);
|
||
|
||
// Kullanıcı verilerini dışa aktar
|
||
const { data: exportData, error: exportError } = await supabaseClient
|
||
.rpc('export_user_data', {
|
||
p_user_id: exportRequest.user_id,
|
||
});
|
||
|
||
if (exportError) {
|
||
// Hata durumunda failed olarak işaretle
|
||
await supabaseClient
|
||
.from('data_export_requests')
|
||
.update({ status: 'failed' })
|
||
.eq('id', requestId);
|
||
|
||
return new Response(
|
||
JSON.stringify({ error: 'Veri dışa aktarılamadı' }),
|
||
{ status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
||
);
|
||
}
|
||
|
||
// 7 gün sonra sona erecek şekilde ayarla
|
||
const expiresAt = new Date();
|
||
expiresAt.setDate(expiresAt.getDate() + 7);
|
||
|
||
// İsteği completed olarak işaretle ve veriyi kaydet
|
||
await supabaseClient
|
||
.from('data_export_requests')
|
||
.update({
|
||
status: 'completed',
|
||
export_data: exportData,
|
||
completed_at: new Date().toISOString(),
|
||
expires_at: expiresAt.toISOString(),
|
||
})
|
||
.eq('id', requestId);
|
||
|
||
return new Response(
|
||
JSON.stringify({ success: true, message: 'Veri dışa aktarma tamamlandı' }),
|
||
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
||
);
|
||
} catch (error) {
|
||
console.error('Hata:', error);
|
||
return new Response(
|
||
JSON.stringify({ error: error.message }),
|
||
{ status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
||
);
|
||
}
|
||
});
|