feat: enhance API response handling and add new components for module management
This commit is contained in:
@@ -20,14 +20,43 @@ export function createFinanceApi(client: SupabaseClient<Database>) {
|
||||
|
||||
return {
|
||||
// --- SEPA Batches ---
|
||||
async listBatches(accountId: string) {
|
||||
const { data, error } = await client
|
||||
async listBatches(
|
||||
accountId: string,
|
||||
opts?: {
|
||||
search?: string;
|
||||
status?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
},
|
||||
) {
|
||||
const page = opts?.page ?? 1;
|
||||
const pageSize = opts?.pageSize ?? 25;
|
||||
|
||||
let query = client
|
||||
.from('sepa_batches')
|
||||
.select('*')
|
||||
.select('*', { count: 'exact' })
|
||||
.eq('account_id', accountId)
|
||||
.order('created_at', { ascending: false });
|
||||
|
||||
if (opts?.search) {
|
||||
query = query.ilike('description', `%${opts.search}%`);
|
||||
}
|
||||
if (opts?.status) {
|
||||
query = query.eq('status', opts.status);
|
||||
}
|
||||
|
||||
query = query.range((page - 1) * pageSize, page * pageSize - 1);
|
||||
|
||||
const { data, error, count } = await query;
|
||||
if (error) throw error;
|
||||
return data ?? [];
|
||||
|
||||
return {
|
||||
data: data ?? [],
|
||||
total: count ?? 0,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages: Math.ceil((count ?? 0) / pageSize),
|
||||
};
|
||||
},
|
||||
|
||||
async getBatch(batchId: string) {
|
||||
@@ -148,20 +177,48 @@ export function createFinanceApi(client: SupabaseClient<Database>) {
|
||||
},
|
||||
|
||||
// --- Invoices ---
|
||||
async listInvoices(accountId: string, opts?: { status?: string }) {
|
||||
async listInvoices(
|
||||
accountId: string,
|
||||
opts?: {
|
||||
search?: string;
|
||||
status?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
},
|
||||
) {
|
||||
const page = opts?.page ?? 1;
|
||||
const pageSize = opts?.pageSize ?? 25;
|
||||
|
||||
let query = client
|
||||
.from('invoices')
|
||||
.select('*')
|
||||
.select('*', { count: 'exact' })
|
||||
.eq('account_id', accountId)
|
||||
.order('issue_date', { ascending: false });
|
||||
if (opts?.status)
|
||||
|
||||
if (opts?.status) {
|
||||
query = query.eq(
|
||||
'status',
|
||||
opts.status as Database['public']['Enums']['invoice_status'],
|
||||
);
|
||||
const { data, error } = await query;
|
||||
}
|
||||
if (opts?.search) {
|
||||
query = query.or(
|
||||
`invoice_number.ilike.%${opts.search}%,recipient_name.ilike.%${opts.search}%`,
|
||||
);
|
||||
}
|
||||
|
||||
query = query.range((page - 1) * pageSize, page * pageSize - 1);
|
||||
|
||||
const { data, error, count } = await query;
|
||||
if (error) throw error;
|
||||
return data ?? [];
|
||||
|
||||
return {
|
||||
data: data ?? [],
|
||||
total: count ?? 0,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages: Math.ceil((count ?? 0) / pageSize),
|
||||
};
|
||||
},
|
||||
|
||||
async createInvoice(input: CreateInvoiceInput, userId: string) {
|
||||
|
||||
Reference in New Issue
Block a user