Skip to content

Commit 080d811

Browse files
committed
Delete bookmarked searches in the backend
1 parent d90826e commit 080d811

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

backend/api/src/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {vote} from "api/vote";
6161
import {contact} from "api/contact";
6262
import {saveSubscription} from "api/save-subscription";
6363
import {createBookmarkedSearch} from './create-bookmarked-search'
64+
import {deleteBookmarkedSearch} from './delete-bookmarked-search'
6465

6566
// const corsOptions: CorsOptions = {
6667
// origin: ['*'], // Only allow requests from this domain
@@ -187,6 +188,7 @@ const handlers: { [k in APIPath]: APIHandler<k> } = {
187188
'set-last-online-time': setLastOnlineTime,
188189
'save-subscription': saveSubscription,
189190
'create-bookmarked-search': createBookmarkedSearch,
191+
'delete-bookmarked-search': deleteBookmarkedSearch,
190192
}
191193

192194
Object.entries(handlers).forEach(([path, handler]) => {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {APIHandler} from './helpers/endpoint'
2+
import {createSupabaseDirectClient} from 'shared/supabase/init'
3+
4+
export const deleteBookmarkedSearch: APIHandler<'delete-bookmarked-search'> = async (
5+
props,
6+
auth
7+
) => {
8+
const creator_id = auth.uid
9+
const {id} = props
10+
11+
const pg = createSupabaseDirectClient()
12+
13+
// Only allow deleting your own bookmarked searches
14+
await pg.none(
15+
`
16+
DELETE FROM bookmarked_searches
17+
WHERE id = $1 AND creator_id = $2
18+
`,
19+
[id, creator_id]
20+
)
21+
22+
return {}
23+
}

common/src/api/schema.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,15 @@ export const API = (_apiTypeCheck = {
595595
search_name: z.string().nullable().optional(),
596596
}),
597597
},
598+
'delete-bookmarked-search': {
599+
method: 'POST',
600+
authed: true,
601+
rateLimited: true,
602+
returns: {} as any,
603+
props: z.object({
604+
id: z.number(),
605+
}),
606+
},
598607
} as const)
599608

600609
export type APIPath = keyof typeof API

web/lib/supabase/searches.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ export const deleteBookmarkedSearch = async (
4646
id: number,
4747
) => {
4848
if (!id) return
49-
await run(
50-
db.from('bookmarked_searches').delete().eq('id', id)
51-
).then(() => {
52-
track('bookmarked_searches delete', {id})
53-
})
49+
await api('delete-bookmarked-search', {id})
50+
track('bookmarked_searches delete', {id})
5451
}

0 commit comments

Comments
 (0)