From 901277bd5ba695ae5ea95494a09ade8508b16ded Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Fri, 16 Feb 2024 11:40:10 +0200 Subject: [PATCH] Fix search --- src/server/Server.ts | 7 +++++-- src/server/components/UserDatabase.ts | 9 +++++++-- src/server/endpoints/api/management/Users.ts | 2 +- src/server/interfaces/UserDatabaseInterface.ts | 3 ++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/server/Server.ts b/src/server/Server.ts index ac4c99e..499c8aa 100644 --- a/src/server/Server.ts +++ b/src/server/Server.ts @@ -546,8 +546,12 @@ class Server extends EventEmitter if (this.#server) { this.#logger.info('Closing server'); + let closing = false; const cleanUp = async () => { + if (closing) + return; + closing = true; await this.#mongodb.close(); await this.#mariadb?.close(); await this.#memoryStoreProvider.close(); @@ -566,9 +570,8 @@ class Server extends EventEmitter }); setTimeout(() => { - this.#logger.warn('Server close timed out, cleaning up and exiting'); + this.#logger.warn('Server close timed out, forcefully closing connections'); this.#server?.closeAllConnections(); - cleanUp(); }, 10_000); } diff --git a/src/server/components/UserDatabase.ts b/src/server/components/UserDatabase.ts index dad78e6..442ca7d 100644 --- a/src/server/components/UserDatabase.ts +++ b/src/server/components/UserDatabase.ts @@ -149,7 +149,7 @@ class UserDatabase implements UserDatabaseInterface return true; } - async fetchUsers ({ ids, page, pageSize, name, roleId, id }: UserQuery = {}): Promise + async fetchUsers ({ ids, page, pageSize, name, roleId, id, partialName }: UserQuery = {}): Promise { const query: Filter = {}; @@ -162,7 +162,12 @@ class UserDatabase implements UserDatabaseInterface // Does not scale, but works for our users collection since it should always remain relatively small if (name) - query.name = { $regex: `^${name}$`, $options: 'i' }; + { + if (partialName) + query.name = { $regex: name }; + else + query.name = { $regex: `^${name}$`, $options: 'i' }; + } const findOptions: {limit?: number, skip?: number} = {}; if (typeof page !== 'undefined' && typeof pageSize !== 'undefined') diff --git a/src/server/endpoints/api/management/Users.ts b/src/server/endpoints/api/management/Users.ts index d1e962e..660cda2 100644 --- a/src/server/endpoints/api/management/Users.ts +++ b/src/server/endpoints/api/management/Users.ts @@ -60,7 +60,7 @@ class UsersEndpoint extends ApiEndpoint if (page < 0) page = 0; - const users = await this.#users.fetchUsers({ page, pageSize, name }); + const users = await this.#users.fetchUsers({ page, pageSize, name, partialName: true }); res.json({ users: users.map(user => user.json), page: page + 1, diff --git a/src/server/interfaces/UserDatabaseInterface.ts b/src/server/interfaces/UserDatabaseInterface.ts index d086c9d..77b4612 100644 --- a/src/server/interfaces/UserDatabaseInterface.ts +++ b/src/server/interfaces/UserDatabaseInterface.ts @@ -8,7 +8,8 @@ export type Query = { ids?: (string | T | TData)[], page?: number, pageSize?: number, - name?: string | null + name?: string | null, + partialName?: boolean } export type RoleQuery = Query