Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions lib/db/derivedtables/component-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export interface BaseComponent {
in_stock: boolean
is_basic: boolean
is_preferred: boolean
is_extended_promotional: boolean
attributes: Record<string, string>
}
5 changes: 5 additions & 0 deletions routes/api/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 @@ -64,6 +65,9 @@ export default withWinterSpec({
if (req.query.is_preferred) {
query = query.where("preferred", "=", 1)
}
if (req.query.is_extended_promotional) {
query = query.where("preferred", "=", 1).where("basic", "=", 0)
}

const baseQuery = query
let fallbackLikeTokens: string[] = []
Expand Down Expand Up @@ -189,6 +193,7 @@ export default withWinterSpec({
package: c.package,
is_basic: Boolean(c.basic),
is_preferred: Boolean(c.preferred),
is_extended_promotional: Boolean(c.preferred) && !Boolean(c.basic),
description: c.description,
stock: c.stock,
price: extractSmallQuantityPrice(c.price),
Expand Down
20 changes: 20 additions & 0 deletions routes/components/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 +40,10 @@ export default withWinterSpec({
"price",
"extra",
"basic",
"preferred",
sql<number>`CASE WHEN preferred = 1 AND basic = 0 THEN 1 ELSE 0 END`.as(
"is_extended_promotional",
),
])
.limit(limit)
.orderBy("stock", "desc")
Expand All @@ -58,6 +63,9 @@ export default withWinterSpec({
if (req.query.is_preferred) {
query = query.where("preferred", "=", 1)
}
if (req.query.is_extended_promotional) {
query = query.where("preferred", "=", 1).where("basic", "=", 0)
}

if (req.query.search) {
const search = req.query.search // TypeScript now knows this is defined within this block
Expand All @@ -82,6 +90,7 @@ export default withWinterSpec({
package: c.package,
is_basic: Boolean(c.basic),
is_preferred: Boolean(c.preferred),
is_extended_promotional: Boolean(c.is_extended_promotional),
description: c.description,
stock: c.stock,
price: extractSmallQuantityPrice(c.price),
Expand Down Expand Up @@ -126,6 +135,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
27 changes: 27 additions & 0 deletions tests/routes/api/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,30 @@ test("GET /api/search supports '0402 LED'", async () => {
expect(res.data.components.every((c: any) => c.package === "0402")).toBe(true)
expect(res.data.components.some((c: any) => c.lcsc === 965793)).toBe(true)
})

test("GET /api/search returns is_extended_promotional field", async () => {
const { axios } = await getTestServer()
const res = await axios.get("/api/search?q=resistor&limit=10")

expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)
expect(res.data.components.length).toBeGreaterThan(0)

const component = res.data.components[0]
expect(component).toHaveProperty("is_extended_promotional")
expect(typeof component.is_extended_promotional).toBe("boolean")
})

test("GET /api/search with is_extended_promotional=true returns only extended promotional components", async () => {
const { axios } = await getTestServer()
const res = await axios.get("/api/search?is_extended_promotional=true&limit=50")

expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)

for (const component of res.data.components) {
expect(component.is_preferred).toBe(true)
expect(component.is_basic).toBe(false)
expect(component.is_extended_promotional).toBe(true)
}
})
27 changes: 27 additions & 0 deletions tests/routes/components/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,30 @@ test("GET /components/list with json param returns component data", async () =>
expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)
})

test("GET /components/list returns is_extended_promotional field", async () => {
const { axios } = await getTestServer()
const res = await axios.get("/components/list?json=true")

expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)
expect(res.data.components.length).toBeGreaterThan(0)

const component = res.data.components[0]
expect(component).toHaveProperty("is_extended_promotional")
expect(typeof component.is_extended_promotional).toBe("boolean")
})

test("GET /components/list with is_extended_promotional=true returns only extended promotional components", async () => {
const { axios } = await getTestServer()
const res = await axios.get("/components/list?json=true&is_extended_promotional=true")

expect(res.data).toHaveProperty("components")
expect(Array.isArray(res.data.components)).toBe(true)

for (const component of res.data.components) {
expect(component.is_preferred).toBe(true)
expect(component.is_basic).toBe(false)
expect(component.is_extended_promotional).toBe(true)
}
})
Loading