diff --git a/server/src/repositories/ownerRepository.ts b/server/src/repositories/ownerRepository.ts index 66dc09a82..b85769273 100644 --- a/server/src/repositories/ownerRepository.ts +++ b/server/src/repositories/ownerRepository.ts @@ -203,27 +203,29 @@ const searchOwners = async ( perPage?: number ): Promise> => { const filterQuery = db(ownerTable) - .whereRaw( - `upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`, - q - ) - .orWhereRaw( - `upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`, - q?.split(' ').reverse().join(' ') - ); - - const filteredCount: number = await db(ownerTable) - .whereRaw( - `upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`, - q - ) - .orWhereRaw( - `upper(unaccent(full_name)) like '%' || upper(unaccent(?)) || '%'`, - q?.split(' ').reverse().join(' ') - ) - .count('id') - .first() - .then((_) => Number(_?.count)); + .select('*') + .whereRaw( + `immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`, + [`%${q}%`] + ) + .orWhereRaw( + `immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`, + [`%${q.split(' ').reverse().join(' ')}%`] + ) + .orderBy('id', 'desc'); + +const filteredCount = await db(ownerTable) + .whereRaw( + `immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`, + [`%${q}%`] + ) + .orWhereRaw( + `immutable_unaccent(full_name) ILIKE immutable_unaccent(?)`, + [`%${q.split(' ').reverse().join(' ')}%`] + ) + .count('id') + .first() + .then((row) => Number(row?.count)); const totalCount = await db(ownerTable) .count('id')