From 21009085c44c84409ce142de812e63b8b6bb21c9 Mon Sep 17 00:00:00 2001 From: Kristaps Fabians Geikins Date: Tue, 10 Sep 2024 15:50:01 +0300 Subject: [PATCH] chore(server): accessrequests IoC 5 - getUsersPendingAccessRequestFactory --- .../accessrequests/repositories/index.ts | 33 +++++++++++-------- .../modules/accessrequests/services/stream.ts | 4 +-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/server/modules/accessrequests/repositories/index.ts b/packages/server/modules/accessrequests/repositories/index.ts index 1ee38a6a2b..2107768849 100644 --- a/packages/server/modules/accessrequests/repositories/index.ts +++ b/packages/server/modules/accessrequests/repositories/index.ts @@ -1,4 +1,3 @@ -import { db } from '@/db/knex' import { ServerAccessRequests, Streams } from '@/modules/core/dbSchema' import { InvalidArgumentError } from '@/modules/shared/errors' import { Nullable } from '@/modules/shared/helpers/typeHelper' @@ -132,18 +131,24 @@ export const createNewRequestFactory = return results[0] } -export async function getUsersPendingAccessRequest< - T extends AccessRequestType = AccessRequestType, - I extends Nullable = Nullable ->(userId: string, resourceType: T, resourceId: I) { - if (!userId || !resourceType) { - throw new InvalidArgumentError('User ID or resource type missing') - } +export const getUsersPendingAccessRequestFactory = + (deps: { db: Knex }) => + async < + T extends AccessRequestType = AccessRequestType, + I extends Nullable = Nullable + >( + userId: string, + resourceType: T, + resourceId: I + ) => { + if (!userId || !resourceType) { + throw new InvalidArgumentError('User ID or resource type missing') + } - const q = baseQueryFactory({ db })(resourceType) - .andWhere(ServerAccessRequests.col.requesterId, userId) - .andWhere(ServerAccessRequests.col.resourceId, resourceId) - .first() + const q = baseQueryFactory({ db: deps.db })(resourceType) + .andWhere(ServerAccessRequests.col.requesterId, userId) + .andWhere(ServerAccessRequests.col.resourceId, resourceId) + .first() - return await q -} + return await q + } diff --git a/packages/server/modules/accessrequests/services/stream.ts b/packages/server/modules/accessrequests/services/stream.ts index 356041a473..8220c58989 100644 --- a/packages/server/modules/accessrequests/services/stream.ts +++ b/packages/server/modules/accessrequests/services/stream.ts @@ -11,7 +11,7 @@ import { generateId, getPendingAccessRequestFactory, getPendingAccessRequestsFactory, - getUsersPendingAccessRequest, + getUsersPendingAccessRequestFactory, ServerAccessRequestRecord, StreamAccessRequestRecord } from '@/modules/accessrequests/repositories' @@ -42,7 +42,7 @@ export async function getUserProjectAccessRequest( userId: string, projectId: string ): Promise> { - const req = await getUsersPendingAccessRequest( + const req = await getUsersPendingAccessRequestFactory({ db })( userId, AccessRequestType.Stream, projectId