85 lines
2.0 KiB
TypeScript
85 lines
2.0 KiB
TypeScript
import 'server-only';
|
|
import type { SupabaseClient } from '@supabase/supabase-js';
|
|
|
|
import type { Database } from '@kit/supabase/database';
|
|
|
|
export function createEventExportService(client: SupabaseClient<Database>) {
|
|
return new EventExportService(client);
|
|
}
|
|
|
|
class EventExportService {
|
|
constructor(private readonly client: SupabaseClient<Database>) {}
|
|
|
|
async exportRegistrationsCsv(eventId: string): Promise<string> {
|
|
const { data: registrations, error } = await this.client
|
|
.from('event_registrations')
|
|
.select('*')
|
|
.eq('event_id', eventId)
|
|
.order('last_name');
|
|
|
|
if (error) throw error;
|
|
if (!registrations?.length) return '';
|
|
|
|
const headers = [
|
|
'Vorname',
|
|
'Nachname',
|
|
'E-Mail',
|
|
'Telefon',
|
|
'Geburtsdatum',
|
|
'Status',
|
|
'Anmeldedatum',
|
|
];
|
|
|
|
const rows = registrations.map((r) =>
|
|
[
|
|
r.first_name,
|
|
r.last_name,
|
|
r.email ?? '',
|
|
r.phone ?? '',
|
|
r.date_of_birth ?? '',
|
|
r.status,
|
|
r.created_at ?? '',
|
|
]
|
|
.map((v) => `"${String(v).replace(/"/g, '""')}"`)
|
|
.join(';'),
|
|
);
|
|
|
|
return [headers.join(';'), ...rows].join('\n');
|
|
}
|
|
|
|
async exportEventsCsv(
|
|
accountId: string,
|
|
filters?: { status?: string },
|
|
): Promise<string> {
|
|
let query = this.client
|
|
.from('events')
|
|
.select('*')
|
|
.eq('account_id', accountId)
|
|
.order('event_date', { ascending: false });
|
|
|
|
if (filters?.status) {
|
|
query = query.eq('status', filters.status as any);
|
|
}
|
|
|
|
const { data: events, error } = await query;
|
|
if (error) throw error;
|
|
if (!events?.length) return '';
|
|
|
|
const headers = ['Name', 'Status', 'Datum', 'Ort', 'Kapazitat'];
|
|
|
|
const rows = events.map((e) =>
|
|
[
|
|
e.name,
|
|
e.status,
|
|
e.event_date ?? '',
|
|
e.location ?? '',
|
|
e.capacity?.toString() ?? '',
|
|
]
|
|
.map((v) => `"${String(v).replace(/"/g, '""')}"`)
|
|
.join(';'),
|
|
);
|
|
|
|
return [headers.join(';'), ...rows].join('\n');
|
|
}
|
|
}
|