Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions routes/api/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default withWinterSpec({
limit: z.string().optional(),
is_basic: z.boolean().optional(),
is_preferred: z.boolean().optional(),
is_extended_promotional: z.boolean().optional(),
}),
jsonResponse: z.any(),
} as const)(async (req, ctx) => {
Expand All @@ -48,6 +49,11 @@ export default withWinterSpec({
if (req.query.is_preferred) {
query = query.where("preferred", "=", 1)
}
if (req.query.is_extended_promotional) {
query = query.where(
sql<boolean>`json_extract(extra, '$.is_extended_promotional') = 1`,
)
}

if (req.query.q) {
const rawSearchTerm = req.query.q.trim()
Expand Down Expand Up @@ -80,6 +86,7 @@ export default withWinterSpec({
package: c.package,
is_basic: Boolean(c.basic),
is_preferred: Boolean(c.preferred),
is_extended_promotional: isExtendedPromotional(c.extra),
description: c.description,
stock: c.stock,
price: extractSmallQuantityPrice(c.price),
Expand Down
29 changes: 29 additions & 0 deletions routes/components/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ const extractSmallQuantityPrice = (price: string | null) => {
}
}

const isExtendedPromotional = (extra: string | null): boolean => {
if (!extra) return false
try {
const extraObj = JSON.parse(extra)
return Boolean(extraObj?.is_extended_promotional)
} catch {
return false
}
}

export default withWinterSpec({
auth: "none",
methods: ["GET"],
Expand All @@ -23,6 +33,7 @@ export default withWinterSpec({
search: z.string().optional(),
is_basic: z.boolean().optional(),
is_preferred: z.boolean().optional(),
is_extended_promotional: z.boolean().optional(),
}),
jsonResponse: z.any(),
} as const)(async (req, ctx) => {
Expand All @@ -39,6 +50,7 @@ export default withWinterSpec({
"price",
"extra",
"basic",
"preferred",
])
.limit(limit)
.orderBy("stock", "desc")
Expand All @@ -58,6 +70,11 @@ export default withWinterSpec({
if (req.query.is_preferred) {
query = query.where("preferred", "=", 1)
}
if (req.query.is_extended_promotional) {
query = query.where(
sql<boolean>`json_extract(extra, '$.is_extended_promotional') = 1`,
)
}

if (req.query.search) {
const search = req.query.search // TypeScript now knows this is defined within this block
Expand All @@ -82,6 +99,7 @@ export default withWinterSpec({
package: c.package,
is_basic: Boolean(c.basic),
is_preferred: Boolean(c.preferred),
is_extended_promotional: isExtendedPromotional(c.extra),
description: c.description,
stock: c.stock,
price: extractSmallQuantityPrice(c.price),
Expand Down Expand Up @@ -126,6 +144,17 @@ export default withWinterSpec({
/>
</label>
</div>
<div>
<label>
Extended Promotional:
<input
type="checkbox"
name="is_extended_promotional"
value="true"
checked={req.query.is_extended_promotional}
/>
</label>
</div>
<button type="submit">Filter</button>
</form>

Expand Down