import { prisma } from '@/db'; import { OrganizationWhereInput } from '@/generated/prisma/models'; import { authMiddleware } from '@/lib/middleware'; import { parseError } from '@/utils/helper'; import { createServerFn } from '@tanstack/react-start'; import { houseListSchema } from './house.schema'; export const getAllHouse = createServerFn({ method: 'GET' }) .middleware([authMiddleware]) .inputValidator(houseListSchema) .handler(async ({ data }) => { try { const { page, limit, keyword } = data; const skip = (page - 1) * limit; const where: OrganizationWhereInput = { OR: [ { name: { contains: keyword, mode: 'insensitive', }, }, ], }; const [list, total]: [OrganizationWithMembers[], number] = await Promise.all([ await prisma.organization.findMany({ where, orderBy: { createdAt: 'desc' }, include: { members: { select: { role: true, user: { select: { name: true, email: true, image: true, }, }, }, }, }, take: limit, skip, }), await prisma.organization.count({ where }), ]); const totalPage = Math.ceil(+total / limit); return { result: list, pagination: { currentPage: page, totalPage, totalItem: total, }, }; } catch (error) { console.error(error); const { message, code } = parseError(error); throw { message, code }; } });