Skip to content

Commit 720f367

Browse files
committed
fix: update post controller to handle jsonb to text cast
1 parent 1534eb6 commit 720f367

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎src/api/post/content-types/post/schema.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"Ethereum",
6363
"Crypto 101:Crypto_101",
6464
"Bitcoin",
65-
"Crypto pro:Crypto_pro",
65+
"Crypto Pro:Crypto_Pro",
6666
"Thought Leadership:Thought_Leadership",
6767
"Governance Newsletters:Governance_Newsletters",
6868
"Newsletter"

‎src/api/post/controllers/post.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
11
import { factories } from '@strapi/strapi'
22

3-
export default factories.createCoreController('api::post.post')
3+
interface PostFilters {
4+
type?: { $contains?: string; $containsi?: string }
5+
[key: string]: unknown
6+
}
7+
8+
interface PostQuery {
9+
filters?: PostFilters
10+
pagination?: { page?: number; pageSize?: number }
11+
[key: string]: unknown
12+
}
13+
14+
export default factories.createCoreController('api::post.post', ({ strapi }) => ({
15+
async find(ctx) {
16+
const query = ctx.query as PostQuery
17+
18+
const { type, ...restFilters } = query.filters ?? {}
19+
20+
// Support both $contains and $containsi from the frontend
21+
const typeFilter = type?.$contains ?? type?.$containsi
22+
23+
if (!typeFilter) return super.find(ctx)
24+
25+
// strapi-plugin-multi-select stores `type` as jsonb in PostgreSQL.
26+
// LIKE/ILIKE fail on jsonb ("operator does not exist: jsonb ~~ unknown").
27+
// We resolve matching IDs via a raw ::text cast, then hand off to super.find()
28+
// so that populate, pagination, fields, and sort all work normally.
29+
//
30+
// Hyphens in the search term are replaced with underscores because _ is a
31+
// single-char wildcard in LIKE patterns, making %crypto_pro% match both
32+
// "Crypto_pro" and "crypto-pro" regardless of separator convention.
33+
const pattern = `%${String(typeFilter).replace(/-/g, '_')}%`
34+
35+
const rows = await strapi.db
36+
.connection('posts')
37+
.whereNotNull('published_at')
38+
.whereRaw('type::text ILIKE ?', [pattern])
39+
.select<{ id: number }[]>('id')
40+
41+
if (rows.length === 0) {
42+
return ctx.send({
43+
data: [],
44+
meta: { pagination: { page: 1, pageSize: query.pagination?.pageSize ?? 25, pageCount: 0, total: 0 } },
45+
})
46+
}
47+
48+
ctx.query = {
49+
...query,
50+
filters: {
51+
...restFilters,
52+
id: { $in: rows.map((r) => r.id) },
53+
},
54+
}
55+
56+
return super.find(ctx)
57+
},
58+
}))

0 commit comments

Comments
 (0)