Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions apps/web/app/api/finance/customer-invoices/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { NextRequest, NextResponse } from 'next/server'
import { getCustomerInvoices, getCustomerInvoiceById, createCustomerInvoice, updateCustomerInvoice, deleteCustomerInvoice } from '@/lib/repositories/finance-transactions'

/**
* GET /api/finance/customer-invoices/[id]
* Get customer invoice by ID
*/
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const invoice = await getCustomerInvoiceById(id)

if (!invoice) {
return NextResponse.json({ error: 'Customer invoice not found' }, { status: 404 })
}

return NextResponse.json({ data: invoice })
} catch (error) {
console.error('Error fetching customer invoice:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to fetch customer invoice' },
{ status: 500 }
)
}
}

/**
* PUT /api/finance/customer-invoices/[id]
* Update customer invoice
*/
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const body = await request.json()
const invoice = await updateCustomerInvoice(id, body)

return NextResponse.json({ data: invoice })
} catch (error) {
console.error('Error updating customer invoice:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to update customer invoice' },
{ status: 500 }
)
}
}

/**
* DELETE /api/finance/customer-invoices/[id]
* Delete customer invoice
*/
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
await deleteCustomerInvoice(id)
return NextResponse.json({ message: 'Customer invoice deleted successfully' })
} catch (error) {
console.error('Error deleting customer invoice:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to delete customer invoice' },
{ status: 500 }
)
}
}
56 changes: 56 additions & 0 deletions apps/web/app/api/finance/customer-invoices/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { NextRequest, NextResponse } from 'next/server'
import {
getCustomerInvoices,
getCustomerInvoiceById,
createCustomerInvoice,
updateCustomerInvoice,
deleteCustomerInvoice
} from '@/lib/repositories/finance-transactions'

/**
* GET /api/finance/customer-invoices
* Get all customer invoices (Accounts Receivable)
*/
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams
const entityId = searchParams.get('entity_id') || undefined
const status = searchParams.get('status') || undefined

const invoices = await getCustomerInvoices(entityId, status)
return NextResponse.json({ data: invoices })
} catch (error) {
console.error('Error fetching customer invoices:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to fetch customer invoices' },
{ status: 500 }
)
}
}

/**
* POST /api/finance/customer-invoices
* Create new customer invoice with line items
*/
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const { lines = [], ...invoiceData } = body

const invoice = await createCustomerInvoice(
invoiceData,
lines.map((line: any, index: number) => ({
...line,
line_number: index + 1,
}))
)

return NextResponse.json({ data: invoice }, { status: 201 })
} catch (error) {
console.error('Error creating customer invoice:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to create customer invoice' },
{ status: 500 }
)
}
}
72 changes: 72 additions & 0 deletions apps/web/app/api/finance/customers/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { NextRequest, NextResponse } from 'next/server'
import { getCustomerById, updateCustomer, deleteCustomer } from '@/lib/repositories/finance-transactions'

/**
* GET /api/finance/customers/[id]
* Get customer by ID
*/
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const customer = await getCustomerById(id)

if (!customer) {
return NextResponse.json({ error: 'Customer not found' }, { status: 404 })
}

return NextResponse.json({ data: customer })
} catch (error) {
console.error('Error fetching customer:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to fetch customer' },
{ status: 500 }
)
}
}

/**
* PUT /api/finance/customers/[id]
* Update customer
*/
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const body = await request.json()
const customer = await updateCustomer(id, body)

return NextResponse.json({ data: customer })
} catch (error) {
console.error('Error updating customer:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to update customer' },
{ status: 500 }
)
}
}

/**
* DELETE /api/finance/customers/[id]
* Delete customer
*/
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
await deleteCustomer(id)
return NextResponse.json({ message: 'Customer deleted successfully' })
} catch (error) {
console.error('Error deleting customer:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to delete customer' },
{ status: 500 }
)
}
}
41 changes: 41 additions & 0 deletions apps/web/app/api/finance/customers/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { NextRequest, NextResponse } from 'next/server'
import { getCustomers, createCustomer, updateCustomer, deleteCustomer } from '@/lib/repositories/finance-transactions'

/**
* GET /api/finance/customers
* Get all customers for current tenant
*/
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams
const entityId = searchParams.get('entity_id') || undefined

const customers = await getCustomers(entityId)
return NextResponse.json({ data: customers })
} catch (error) {
console.error('Error fetching customers:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to fetch customers' },
{ status: 500 }
)
}
}

/**
* POST /api/finance/customers
* Create new customer
*/
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const customer = await createCustomer(body)

return NextResponse.json({ data: customer }, { status: 201 })
} catch (error) {
console.error('Error creating customer:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to create customer' },
{ status: 500 }
)
}
}
28 changes: 25 additions & 3 deletions apps/web/app/api/finance/journal/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,34 @@ export async function POST(request: NextRequest) {
)
}

const { lines, ...entryData } = body;

// Auto-generate entry number if not provided
let entryNumber = entryData.entry_number;
if (!entryNumber) {
const date = new Date(entryData.transaction_date || new Date());
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const ts = Date.now().toString().slice(-5);
entryNumber = `JE-${year}${month}-${ts}`;
}

const linesWithTenant = lines.map((line: any) => ({
...line,
tenant_id: tenantId,
created_by: '812558af-8be8-4c53-b581-e6a4f1c91147',
updated_by: '812558af-8be8-4c53-b581-e6a4f1c91147'
}));

const entry = await createJournalEntry(
{
...body,
prepared_by: '00000000-0000-0000-0000-000000000000' // System user
...entryData,
entry_number: entryNumber,
prepared_by: '812558af-8be8-4c53-b581-e6a4f1c91147',
created_by: '812558af-8be8-4c53-b581-e6a4f1c91147',
tenant_id: tenantId
},
body.lines
linesWithTenant
)
return NextResponse.json(entry, { status: 201 })
} catch (error) {
Expand Down
72 changes: 72 additions & 0 deletions apps/web/app/api/finance/payments/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { NextRequest, NextResponse } from 'next/server'
import { getPaymentById, updatePayment, deletePayment } from '@/lib/repositories/finance-transactions'

/**
* GET /api/finance/payments/[id]
* Get payment by ID
*/
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const payment = await getPaymentById(id)

if (!payment) {
return NextResponse.json({ error: 'Payment not found' }, { status: 404 })
}

return NextResponse.json({ data: payment })
} catch (error) {
console.error('Error fetching payment:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to fetch payment' },
{ status: 500 }
)
}
}

/**
* PUT /api/finance/payments/[id]
* Update payment
*/
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const body = await request.json()
const payment = await updatePayment(id, body)

return NextResponse.json({ data: payment })
} catch (error) {
console.error('Error updating payment:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to update payment' },
{ status: 500 }
)
}
}

/**
* DELETE /api/finance/payments/[id]
* Delete payment
*/
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
await deletePayment(id)
return NextResponse.json({ message: 'Payment deleted successfully' })
} catch (error) {
console.error('Error deleting payment:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to delete payment' },
{ status: 500 }
)
}
}
42 changes: 42 additions & 0 deletions apps/web/app/api/finance/payments/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NextRequest, NextResponse } from 'next/server'
import { getPayments, getPaymentById, createPayment, updatePayment, deletePayment } from '@/lib/repositories/finance-transactions'

/**
* GET /api/finance/payments
* Get all payments
*/
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams
const entityId = searchParams.get('entity_id') || undefined
const status = searchParams.get('status') || undefined

const payments = await getPayments(entityId, status)
return NextResponse.json({ data: payments })
} catch (error) {
console.error('Error fetching payments:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to fetch payments' },
{ status: 500 }
)
}
}

/**
* POST /api/finance/payments
* Create new payment
*/
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const payment = await createPayment(body)

return NextResponse.json({ data: payment }, { status: 201 })
} catch (error) {
console.error('Error creating payment:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to create payment' },
{ status: 500 }
)
}
}
Loading