All list endpoints now return paginated responses:
{ data: [...], pagination: { page, limit, total, totalPages } }
Query params: ?page=1&limit=25&q=search&sort=name&order=asc
Changes:
- Added PaginationSchema in @forte/shared for consistent param parsing
- Added pagination utils (withPagination, withSort, buildSearchCondition,
paginatedResponse) in backend
- Refactored all services: AccountService, MemberService, CategoryService,
SupplierService, ProductService, InventoryUnitService
- Merged separate /search endpoints into list endpoints via ?q= param
- Removed AccountSearchSchema and ProductSearchSchema (replaced by
PaginationSchema)
- Added pagination test (5 items, page 1 limit 2, expect totalPages=3)
- Updated CLAUDE.md with API conventions
- 34 tests passing
144 lines
4.7 KiB
TypeScript
144 lines
4.7 KiB
TypeScript
import { eq, and, count } from 'drizzle-orm'
|
|
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
|
|
import { categories, suppliers } from '../db/schema/inventory.js'
|
|
import type {
|
|
CategoryCreateInput,
|
|
CategoryUpdateInput,
|
|
SupplierCreateInput,
|
|
SupplierUpdateInput,
|
|
PaginationInput,
|
|
} from '@forte/shared/schemas'
|
|
import {
|
|
withPagination,
|
|
withSort,
|
|
buildSearchCondition,
|
|
paginatedResponse,
|
|
} from '../utils/pagination.js'
|
|
|
|
export const CategoryService = {
|
|
async create(db: PostgresJsDatabase, companyId: string, input: CategoryCreateInput) {
|
|
const [category] = await db
|
|
.insert(categories)
|
|
.values({ companyId, ...input })
|
|
.returning()
|
|
return category
|
|
},
|
|
|
|
async getById(db: PostgresJsDatabase, companyId: string, id: string) {
|
|
const [category] = await db
|
|
.select()
|
|
.from(categories)
|
|
.where(and(eq(categories.id, id), eq(categories.companyId, companyId)))
|
|
.limit(1)
|
|
return category ?? null
|
|
},
|
|
|
|
async list(db: PostgresJsDatabase, companyId: string, params: PaginationInput) {
|
|
const baseWhere = and(eq(categories.companyId, companyId), eq(categories.isActive, true))
|
|
|
|
const searchCondition = params.q
|
|
? buildSearchCondition(params.q, [categories.name])
|
|
: undefined
|
|
|
|
const where = searchCondition ? and(baseWhere, searchCondition) : baseWhere
|
|
|
|
const sortableColumns: Record<string, typeof categories.name> = {
|
|
name: categories.name,
|
|
sort_order: categories.sortOrder,
|
|
created_at: categories.createdAt,
|
|
}
|
|
|
|
let query = db.select().from(categories).where(where).$dynamic()
|
|
query = withSort(query, params.sort, params.order, sortableColumns, categories.sortOrder)
|
|
query = withPagination(query, params.page, params.limit)
|
|
|
|
const [data, [{ total }]] = await Promise.all([
|
|
query,
|
|
db.select({ total: count() }).from(categories).where(where),
|
|
])
|
|
|
|
return paginatedResponse(data, total, params.page, params.limit)
|
|
},
|
|
|
|
async update(db: PostgresJsDatabase, companyId: string, id: string, input: CategoryUpdateInput) {
|
|
const [category] = await db
|
|
.update(categories)
|
|
.set({ ...input, updatedAt: new Date() })
|
|
.where(and(eq(categories.id, id), eq(categories.companyId, companyId)))
|
|
.returning()
|
|
return category ?? null
|
|
},
|
|
|
|
async softDelete(db: PostgresJsDatabase, companyId: string, id: string) {
|
|
const [category] = await db
|
|
.update(categories)
|
|
.set({ isActive: false, updatedAt: new Date() })
|
|
.where(and(eq(categories.id, id), eq(categories.companyId, companyId)))
|
|
.returning()
|
|
return category ?? null
|
|
},
|
|
}
|
|
|
|
export const SupplierService = {
|
|
async create(db: PostgresJsDatabase, companyId: string, input: SupplierCreateInput) {
|
|
const [supplier] = await db
|
|
.insert(suppliers)
|
|
.values({ companyId, ...input })
|
|
.returning()
|
|
return supplier
|
|
},
|
|
|
|
async getById(db: PostgresJsDatabase, companyId: string, id: string) {
|
|
const [supplier] = await db
|
|
.select()
|
|
.from(suppliers)
|
|
.where(and(eq(suppliers.id, id), eq(suppliers.companyId, companyId)))
|
|
.limit(1)
|
|
return supplier ?? null
|
|
},
|
|
|
|
async list(db: PostgresJsDatabase, companyId: string, params: PaginationInput) {
|
|
const baseWhere = and(eq(suppliers.companyId, companyId), eq(suppliers.isActive, true))
|
|
|
|
const searchCondition = params.q
|
|
? buildSearchCondition(params.q, [suppliers.name, suppliers.contactName, suppliers.email])
|
|
: undefined
|
|
|
|
const where = searchCondition ? and(baseWhere, searchCondition) : baseWhere
|
|
|
|
const sortableColumns: Record<string, typeof suppliers.name> = {
|
|
name: suppliers.name,
|
|
created_at: suppliers.createdAt,
|
|
}
|
|
|
|
let query = db.select().from(suppliers).where(where).$dynamic()
|
|
query = withSort(query, params.sort, params.order, sortableColumns, suppliers.name)
|
|
query = withPagination(query, params.page, params.limit)
|
|
|
|
const [data, [{ total }]] = await Promise.all([
|
|
query,
|
|
db.select({ total: count() }).from(suppliers).where(where),
|
|
])
|
|
|
|
return paginatedResponse(data, total, params.page, params.limit)
|
|
},
|
|
|
|
async update(db: PostgresJsDatabase, companyId: string, id: string, input: SupplierUpdateInput) {
|
|
const [supplier] = await db
|
|
.update(suppliers)
|
|
.set({ ...input, updatedAt: new Date() })
|
|
.where(and(eq(suppliers.id, id), eq(suppliers.companyId, companyId)))
|
|
.returning()
|
|
return supplier ?? null
|
|
},
|
|
|
|
async softDelete(db: PostgresJsDatabase, companyId: string, id: string) {
|
|
const [supplier] = await db
|
|
.update(suppliers)
|
|
.set({ isActive: false, updatedAt: new Date() })
|
|
.where(and(eq(suppliers.id, id), eq(suppliers.companyId, companyId)))
|
|
.returning()
|
|
return supplier ?? null
|
|
},
|
|
}
|