update: changes to backend endpoints in orderBy and search query usage
This commit is contained in:
parent
251d74d659
commit
dfa3cd9f03
|
|
@ -1,4 +1,4 @@
|
|||
import { and, eq, ilike, or, sql } from "drizzle-orm";
|
||||
import { and, eq, ilike, or, sql, desc } from "drizzle-orm";
|
||||
import { Hono } from "hono";
|
||||
import checkPermission from "../../middlewares/checkPermission";
|
||||
import { z } from "zod";
|
||||
|
|
@ -49,16 +49,10 @@
|
|||
async (c) => {
|
||||
const { page, limit, q } = c.req.valid("query");
|
||||
|
||||
const totalCountQuery = sql<number>`(SELECT count(*) FROM ${assessments})`;
|
||||
|
||||
const result = await db
|
||||
// Query untuk menghitung total jumlah item (totalCountQuery)
|
||||
const assessmentCountQuery = await db
|
||||
.select({
|
||||
idPermohonan: assessments.id,
|
||||
namaResponden: users.name,
|
||||
namaPerusahaan: respondents.companyName,
|
||||
status: assessments.status,
|
||||
tanggal: assessments.createdAt,
|
||||
fullCount: totalCountQuery,
|
||||
count: sql<number>`count(*)`,
|
||||
})
|
||||
.from(assessments)
|
||||
.leftJoin(respondents, eq(assessments.respondentId, respondents.id))
|
||||
|
|
@ -68,10 +62,37 @@
|
|||
? or(
|
||||
ilike(users.name, `%${q}%`),
|
||||
ilike(respondents.companyName, `%${q}%`),
|
||||
sql`CAST(${assessments.status} AS TEXT) ILIKE ${'%' + q + '%'}`,
|
||||
eq(assessments.id, q)
|
||||
)
|
||||
: undefined
|
||||
);
|
||||
|
||||
const totalItems = Number(assessmentCountQuery[0]?.count) || 0;
|
||||
|
||||
// Query utama untuk mendapatkan data permohonan assessment
|
||||
const result = await db
|
||||
.select({
|
||||
idPermohonan: assessments.id,
|
||||
namaResponden: users.name,
|
||||
namaPerusahaan: respondents.companyName,
|
||||
status: assessments.status,
|
||||
tanggal: assessments.createdAt,
|
||||
})
|
||||
.from(assessments)
|
||||
.leftJoin(respondents, eq(assessments.respondentId, respondents.id))
|
||||
.leftJoin(users, eq(respondents.userId, users.id))
|
||||
.where(
|
||||
q
|
||||
? or(
|
||||
ilike(users.name, `%${q}%`),
|
||||
ilike(respondents.companyName, `%${q}%`),
|
||||
sql`CAST(${assessments.status} AS TEXT) ILIKE ${'%' + q + '%'}`,
|
||||
eq(assessments.id, q)
|
||||
)
|
||||
: undefined
|
||||
)
|
||||
.orderBy(desc(assessments.createdAt))
|
||||
.offset(page * limit)
|
||||
.limit(limit);
|
||||
|
||||
|
|
@ -85,10 +106,8 @@
|
|||
})),
|
||||
_metadata: {
|
||||
currentPage: page,
|
||||
totalPages: Math.ceil(
|
||||
(Number(result[0]?.fullCount) ?? 0) / limit
|
||||
),
|
||||
totalItems: Number(result[0]?.fullCount) ?? 0,
|
||||
totalPages: Math.ceil(totalItems / limit),
|
||||
totalItems,
|
||||
perPage: limit,
|
||||
},
|
||||
});
|
||||
|
|
@ -104,7 +123,6 @@
|
|||
|
||||
const queryResult = await db
|
||||
.select({
|
||||
// id: assessments.id,
|
||||
tanggal: assessments.createdAt,
|
||||
nama: users.name,
|
||||
posisi: respondents.position,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user