fix: Include results from users with accents in users.list query

This commit is contained in:
Tom Moor
2024-09-12 20:46:17 -04:00
parent c60703cc5a
commit 494ef2a6cd
3 changed files with 22 additions and 11 deletions
+10 -6
View File
@@ -513,8 +513,6 @@ router.post(
});
authorize(actor, "read", document);
let users: User[] = [];
let total = 0;
let where: WhereOptions<User> = {
teamId: document.teamId,
suspendedAt: {
@@ -553,15 +551,21 @@ router.post(
...where,
[Op.and]: [
Sequelize.literal(
`unaccent(LOWER(name)) like unaccent(LOWER('%${query}%'))`
`unaccent(LOWER(name)) like unaccent(LOWER(:query))`
),
],
};
}
[users, total] = await Promise.all([
User.findAll({ where, offset, limit }),
User.count({ where }),
const replacements = { query: `%${query}%` };
const [users, total] = await Promise.all([
User.findAll({ where, replacements, offset, limit }),
User.count({
where,
// @ts-expect-error Types are incorrect for count
replacements,
}),
]);
ctx.body = {
+1 -1
View File
@@ -20,7 +20,7 @@ afterAll(() => {
describe("#users.list", () => {
it("should allow filtering by user name", async () => {
const user = await buildUser({
name: "Tester",
name: "Tèster",
});
// suspended user should not be returned
await buildUser({
+11 -4
View File
@@ -1,5 +1,5 @@
import Router from "koa-router";
import { Op, WhereOptions } from "sequelize";
import { Op, Sequelize, WhereOptions } from "sequelize";
import { UserPreference, UserRole } from "@shared/types";
import { UserRoleHelper } from "@shared/utils/UserRoleHelper";
import { UserValidation } from "@shared/validations";
@@ -124,9 +124,11 @@ router.post(
if (query) {
where = {
...where,
name: {
[Op.iLike]: `%${query}%`,
},
[Op.and]: [
Sequelize.literal(
`unaccent(LOWER(name)) like unaccent(LOWER(:query))`
),
],
};
}
@@ -144,15 +146,20 @@ router.post(
};
}
const replacements = { query: `%${query}%` };
const [users, total] = await Promise.all([
User.findAll({
where,
replacements,
order: [[sort, direction]],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
}),
User.count({
where,
// @ts-expect-error Types are incorrect for count
replacements,
}),
]);