API - allow search of a group using its name (#6066)
This commit is contained in:
@@ -258,6 +258,39 @@ describe("#groups.list", () => {
|
|||||||
.includes(anotherUser.id)
|
.includes(anotherUser.id)
|
||||||
).toBe(true);
|
).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should allow to find a group by its name", async () => {
|
||||||
|
const user = await buildUser();
|
||||||
|
const group = await buildGroup({
|
||||||
|
teamId: user.teamId,
|
||||||
|
});
|
||||||
|
const anotherGroup = await buildGroup({
|
||||||
|
teamId: user.teamId,
|
||||||
|
});
|
||||||
|
|
||||||
|
const unfilteredRes = await server.post("/api/groups.list", {
|
||||||
|
body: {
|
||||||
|
token: user.getJwtToken(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const body = await unfilteredRes.json();
|
||||||
|
|
||||||
|
expect(unfilteredRes.status).toEqual(200);
|
||||||
|
expect(body.data.groups.length).toEqual(2);
|
||||||
|
expect(body.data.groups[0].id).toEqual(anotherGroup.id);
|
||||||
|
expect(body.data.groups[1].id).toEqual(group.id);
|
||||||
|
|
||||||
|
const anotherRes = await server.post("/api/groups.list", {
|
||||||
|
body: {
|
||||||
|
name: group.name,
|
||||||
|
token: user.getJwtToken(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const anotherBody = await anotherRes.json();
|
||||||
|
expect(anotherRes.status).toEqual(200);
|
||||||
|
expect(anotherBody.data.groups.length).toEqual(1);
|
||||||
|
expect(anotherBody.data.groups[0].id).toEqual(group.id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("#groups.info", () => {
|
describe("#groups.info", () => {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import Router from "koa-router";
|
import Router from "koa-router";
|
||||||
import { Op } from "sequelize";
|
import { Op, WhereOptions } from "sequelize";
|
||||||
import { MAX_AVATAR_DISPLAY } from "@shared/constants";
|
import { MAX_AVATAR_DISPLAY } from "@shared/constants";
|
||||||
import auth from "@server/middlewares/authentication";
|
import auth from "@server/middlewares/authentication";
|
||||||
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
||||||
@@ -25,13 +25,24 @@ router.post(
|
|||||||
pagination(),
|
pagination(),
|
||||||
validate(T.GroupsListSchema),
|
validate(T.GroupsListSchema),
|
||||||
async (ctx: APIContext<T.GroupsListReq>) => {
|
async (ctx: APIContext<T.GroupsListReq>) => {
|
||||||
const { direction, sort, userId } = ctx.input.body;
|
const { direction, sort, userId, name } = ctx.input.body;
|
||||||
const { user } = ctx.state.auth;
|
const { user } = ctx.state.auth;
|
||||||
|
|
||||||
|
let where: WhereOptions<Group> = {
|
||||||
|
teamId: user.teamId,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (name) {
|
||||||
|
where = {
|
||||||
|
...where,
|
||||||
|
name: {
|
||||||
|
[Op.eq]: name,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const groups = await Group.filterByMember(userId).findAll({
|
const groups = await Group.filterByMember(userId).findAll({
|
||||||
where: {
|
where,
|
||||||
teamId: user.teamId,
|
|
||||||
},
|
|
||||||
order: [[sort, direction]],
|
order: [[sort, direction]],
|
||||||
offset: ctx.state.pagination.offset,
|
offset: ctx.state.pagination.offset,
|
||||||
limit: ctx.state.pagination.limit,
|
limit: ctx.state.pagination.limit,
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ export const GroupsListSchema = z.object({
|
|||||||
|
|
||||||
/** Only list groups where this user is a member */
|
/** Only list groups where this user is a member */
|
||||||
userId: z.string().uuid().optional(),
|
userId: z.string().uuid().optional(),
|
||||||
|
|
||||||
|
/** Find group with matching name */
|
||||||
|
name: z.string().optional(),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user