diff --git a/src/app/api/i/[token]/pdf/route.ts b/src/app/api/i/[token]/pdf/route.ts index d775f39..362e7d6 100644 --- a/src/app/api/i/[token]/pdf/route.ts +++ b/src/app/api/i/[token]/pdf/route.ts @@ -17,7 +17,13 @@ export async function GET( with: { client: true, business: true, - items: { orderBy: (i, { asc }) => [asc(i.position)] }, + items: { + orderBy: (i, { asc }) => [ + asc(i.date), + asc(i.position), + asc(i.createdAt), + ], + }, }, }); diff --git a/src/app/dashboard/invoices/_components/invoices-data-table.tsx b/src/app/dashboard/invoices/_components/invoices-data-table.tsx index 838ec03..9308c54 100644 --- a/src/app/dashboard/invoices/_components/invoices-data-table.tsx +++ b/src/app/dashboard/invoices/_components/invoices-data-table.tsx @@ -320,6 +320,7 @@ export function InvoicesDataTable({ invoices }: InvoicesDataTableProps) { data={invoices} searchKey="invoiceNumber" searchPlaceholder="Search invoices..." + initialSorting={[{ id: "issueDate", desc: true }]} filterableColumns={filterableColumns} onRowClick={(invoice) => router.push(`/dashboard/invoices/${invoice.id}`) diff --git a/src/components/data/data-table.tsx b/src/components/data/data-table.tsx index 4d75274..91ae312 100644 --- a/src/components/data/data-table.tsx +++ b/src/components/data/data-table.tsx @@ -86,6 +86,7 @@ interface DataTableProps { selectedRows: TData[], clearSelection: () => void, ) => React.ReactNode; + initialSorting?: SortingState; } export function DataTable({ @@ -104,8 +105,9 @@ export function DataTable({ filterableColumns = [], onRowClick, selectionActions, + initialSorting = [], }: DataTableProps) { - const [sorting, setSorting] = React.useState([]); + const [sorting, setSorting] = React.useState(initialSorting); const [columnFilters, setColumnFilters] = React.useState( [], ); diff --git a/src/server/api/routers/clients.ts b/src/server/api/routers/clients.ts index 8b54a4b..c216484 100644 --- a/src/server/api/routers/clients.ts +++ b/src/server/api/routers/clients.ts @@ -74,7 +74,11 @@ export const clientsRouter = createTRPCRouter({ where: eq(clients.id, input.id), with: { invoices: { - orderBy: (invoices, { desc }) => [desc(invoices.createdAt)], + orderBy: (invoices, { desc }) => [ + desc(invoices.issueDate), + desc(invoices.dueDate), + desc(invoices.invoiceNumber), + ], }, }, }); diff --git a/src/server/api/routers/dashboard.ts b/src/server/api/routers/dashboard.ts index 730bded..474f2aa 100644 --- a/src/server/api/routers/dashboard.ts +++ b/src/server/api/routers/dashboard.ts @@ -105,7 +105,11 @@ export const dashboardRouter = createTRPCRouter({ // Recent Activity const recentInvoices = await ctx.db.query.invoices.findMany({ where: eq(invoices.createdById, userId), - orderBy: [desc(invoices.issueDate)], + orderBy: [ + desc(invoices.issueDate), + desc(invoices.dueDate), + desc(invoices.invoiceNumber), + ], limit: 5, with: { client: { diff --git a/src/server/api/routers/invoices.ts b/src/server/api/routers/invoices.ts index 30a0706..c3fd806 100644 --- a/src/server/api/routers/invoices.ts +++ b/src/server/api/routers/invoices.ts @@ -132,9 +132,19 @@ export const invoicesRouter = createTRPCRouter({ with: { business: true, client: true, - items: true, + items: { + orderBy: (items, { asc }) => [ + asc(items.date), + asc(items.position), + asc(items.createdAt), + ], + }, }, - orderBy: (invoices, { desc }) => [desc(invoices.issueDate)], + orderBy: (invoices, { desc }) => [ + desc(invoices.issueDate), + desc(invoices.dueDate), + desc(invoices.invoiceNumber), + ], }); } catch (error) { throw new TRPCError({ @@ -188,10 +198,18 @@ export const invoicesRouter = createTRPCRouter({ business: true, client: true, items: { - orderBy: (items, { asc }) => [asc(items.position)], + orderBy: (items, { asc }) => [ + asc(items.date), + asc(items.position), + asc(items.createdAt), + ], }, }, - orderBy: (invoices, { desc }) => [desc(invoices.createdAt)], + orderBy: (invoices, { desc }) => [ + desc(invoices.issueDate), + desc(invoices.dueDate), + desc(invoices.invoiceNumber), + ], }); // Return null if no draft invoice exists @@ -219,7 +237,11 @@ export const invoicesRouter = createTRPCRouter({ business: true, client: true, items: { - orderBy: (items, { asc }) => [asc(items.position)], + orderBy: (items, { asc }) => [ + asc(items.date), + asc(items.position), + asc(items.createdAt), + ], }, }, }); @@ -684,7 +706,17 @@ export const invoicesRouter = createTRPCRouter({ .query(async ({ ctx, input }) => { const invoice = await ctx.db.query.invoices.findFirst({ where: eq(invoices.publicToken, input.token), - with: { client: true, business: true, items: { orderBy: (i, { asc }) => [asc(i.position)] } }, + with: { + client: true, + business: true, + items: { + orderBy: (i, { asc }) => [ + asc(i.date), + asc(i.position), + asc(i.createdAt), + ], + }, + }, }); if (!invoice) throw new TRPCError({ code: "NOT_FOUND" }); if (invoice.publicTokenExpiresAt && new Date(invoice.publicTokenExpiresAt) < new Date()) { diff --git a/src/server/api/routers/settings.ts b/src/server/api/routers/settings.ts index 08b6ecc..67d29d2 100644 --- a/src/server/api/routers/settings.ts +++ b/src/server/api/routers/settings.ts @@ -541,9 +541,18 @@ export const settingsRouter = createTRPCRouter({ amount: true, position: true, }, - orderBy: (items, { asc }) => [asc(items.position)], + orderBy: (items, { asc }) => [ + asc(items.date), + asc(items.position), + asc(items.createdAt), + ], }, }, + orderBy: (invoices, { desc }) => [ + desc(invoices.issueDate), + desc(invoices.dueDate), + desc(invoices.invoiceNumber), + ], }); // Format the data for export diff --git a/src/server/api/routers/time-entries.ts b/src/server/api/routers/time-entries.ts index 77bebbf..2a686fe 100644 --- a/src/server/api/routers/time-entries.ts +++ b/src/server/api/routers/time-entries.ts @@ -83,7 +83,11 @@ async function addEntryToLatestInvoice( or(eq(invoices.status, "draft"), eq(invoices.status, "sent")), ), with: { items: true }, - orderBy: [desc(invoices.createdAt)], + orderBy: [ + desc(invoices.issueDate), + desc(invoices.dueDate), + desc(invoices.invoiceNumber), + ], }); if (!invoice) return null;