@@ -135,7 +135,13 @@ export function createListingRepository(db) {
|
||||
|
||||
const offset = (page - 1) * limit;
|
||||
const sortOrder =
|
||||
sort === "price_low"
|
||||
sort === "updated"
|
||||
? desc(listings.updatedAt)
|
||||
: sort === "name_asc"
|
||||
? products.name
|
||||
: sort === "name_desc"
|
||||
? desc(products.name)
|
||||
: sort === "price_low"
|
||||
? listings.askingPriceMinor
|
||||
: sort === "price_high"
|
||||
? desc(listings.askingPriceMinor)
|
||||
|
||||
@@ -58,8 +58,8 @@ export function createSearchQuery({
|
||||
throw new Error("minPriceMinor must be <= maxPriceMinor");
|
||||
}
|
||||
|
||||
if (!["recent", "price_low", "price_high"].includes(sort)) {
|
||||
throw new Error("sort must be recent, price_low, or price_high");
|
||||
if (!["recent", "updated", "name_asc", "name_desc", "price_low", "price_high"].includes(sort)) {
|
||||
throw new Error("sort must be recent, updated, name_asc, name_desc, price_low, or price_high");
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -57,6 +57,9 @@ test("builds a public query filter set that hides sold items by default", () =>
|
||||
|
||||
test("accepts only the supported public search sort values", () => {
|
||||
assert.equal(createSearchQuery({ sort: "price_low" }).sort, "price_low");
|
||||
assert.equal(createSearchQuery({ sort: "updated" }).sort, "updated");
|
||||
assert.equal(createSearchQuery({ sort: "name_asc" }).sort, "name_asc");
|
||||
assert.equal(createSearchQuery({ sort: "name_desc" }).sort, "name_desc");
|
||||
assert.throws(
|
||||
() => createSearchQuery({ sort: "random" }),
|
||||
/sort must be recent/,
|
||||
|
||||
Reference in New Issue
Block a user