feat: Allow filtering searches by 'source'
fix: Do not show API searches in recent list in app
This commit is contained in:
@@ -4,6 +4,7 @@ export default function presentSearchQuery(searchQuery: SearchQuery) {
|
||||
return {
|
||||
id: searchQuery.id,
|
||||
query: searchQuery.query,
|
||||
source: searchQuery.source,
|
||||
createdAt: searchQuery.createdAt,
|
||||
answer: searchQuery.answer,
|
||||
score: searchQuery.score,
|
||||
|
||||
@@ -21,3 +21,13 @@ export const SearchesUpdateSchema = BaseSchema.extend({
|
||||
});
|
||||
|
||||
export type SearchesUpdateReq = z.infer<typeof SearchesUpdateSchema>;
|
||||
|
||||
export const SearchesListSchema = BaseSchema.extend({
|
||||
body: z
|
||||
.object({
|
||||
source: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
});
|
||||
|
||||
export type SearchesListReq = z.infer<typeof SearchesListSchema>;
|
||||
|
||||
@@ -24,6 +24,7 @@ describe("#searches.list", () => {
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
query: "bar",
|
||||
source: "api",
|
||||
}),
|
||||
]);
|
||||
});
|
||||
@@ -42,6 +43,18 @@ describe("#searches.list", () => {
|
||||
expect(queries).toContain("foo");
|
||||
expect(queries).toContain("bar");
|
||||
});
|
||||
|
||||
it("should allow filtering by source", async () => {
|
||||
const res = await server.post("/api/searches.list", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
source: "api",
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#searches.update", () => {
|
||||
|
||||
@@ -10,23 +10,32 @@ import * as T from "./schema";
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post("searches.list", auth(), pagination(), async (ctx: APIContext) => {
|
||||
const { user } = ctx.state.auth;
|
||||
router.post(
|
||||
"searches.list",
|
||||
auth(),
|
||||
validate(T.SearchesListSchema),
|
||||
pagination(),
|
||||
async (ctx: APIContext<T.SearchesListReq>) => {
|
||||
const { user } = ctx.state.auth;
|
||||
const source = ctx.input.body?.source;
|
||||
|
||||
const searches = await SearchQuery.findAll({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
order: [["createdAt", "DESC"]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
const searches = await SearchQuery.findAll({
|
||||
where: {
|
||||
...(source ? { source } : {}),
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
},
|
||||
order: [["createdAt", "DESC"]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data: searches.map(presentSearchQuery),
|
||||
};
|
||||
});
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data: searches.map(presentSearchQuery),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
router.post(
|
||||
"searches.update",
|
||||
|
||||
Reference in New Issue
Block a user