Update index.js
This commit is contained in:
parent
288f9abba6
commit
0adca17bbe
@ -201,9 +201,16 @@ async function addDrugInfo(item) {
|
|||||||
|
|
||||||
async function getHlwDrugInfo(ctx) {
|
async function getHlwDrugInfo(ctx) {
|
||||||
try {
|
try {
|
||||||
const page = Number.isInteger(ctx.page) && ctx.page > 0 ? ctx.page : 1;
|
// HTTP query parameters may arrive as strings. Number.isInteger('385')
|
||||||
const pageSize = Number.isInteger(ctx.pageSize) && ctx.pageSize > 0 ? ctx.pageSize : 10;
|
// is false, which previously reset every such request to page 1.
|
||||||
const query = {}
|
const requestedPage = Number(ctx.page);
|
||||||
|
const requestedPageSize = Number(ctx.pageSize);
|
||||||
|
const page = Number.isSafeInteger(requestedPage) && requestedPage > 0 ? requestedPage : 1;
|
||||||
|
const pageSize = Number.isSafeInteger(requestedPageSize) && requestedPageSize > 0 ? requestedPageSize : 10;
|
||||||
|
const query = {};
|
||||||
|
if (typeof ctx.corpId === "string" && ctx.corpId.trim()) {
|
||||||
|
query.corpId = ctx.corpId.trim();
|
||||||
|
}
|
||||||
if (typeof ctx.name === "string" && ctx.name.trim()) {
|
if (typeof ctx.name === "string" && ctx.name.trim()) {
|
||||||
query.name = new RegExp(ctx.name.trim(), "i")
|
query.name = new RegExp(ctx.name.trim(), "i")
|
||||||
};
|
};
|
||||||
@ -222,7 +229,8 @@ async function getHlwDrugInfo(ctx) {
|
|||||||
if (typeof ctx.insuranceCode === 'string' && ctx.insuranceCode.trim()) {
|
if (typeof ctx.insuranceCode === 'string' && ctx.insuranceCode.trim()) {
|
||||||
query.insurance_code = ctx.insuranceCode.trim()
|
query.insurance_code = ctx.insuranceCode.trim()
|
||||||
}
|
}
|
||||||
const res = await db.collection("drug-info").find(query).sort({ onSale: -1, createTime: -1 }).skip((page - 1) * pageSize).limit(pageSize).toArray();
|
// _id makes records with the same createTime deterministic across pages.
|
||||||
|
const res = await db.collection("drug-info").find(query).sort({ onSale: -1, createTime: -1, _id: 1 }).skip((page - 1) * pageSize).limit(pageSize).toArray();
|
||||||
const total = await db.collection("drug-info").countDocuments(query);
|
const total = await db.collection("drug-info").countDocuments(query);
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user