Skip to content

Commit

Permalink
Merge pull request #2934 from specklesystems/fabians/accessreq-ioc-5
Browse files Browse the repository at this point in the history
chore(server): accessrequests IoC 5 - getUsersPendingAccessRequestFac…
  • Loading branch information
alemagio authored Sep 11, 2024
2 parents eb3f34f + 2100908 commit 1908e97
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
33 changes: 19 additions & 14 deletions packages/server/modules/accessrequests/repositories/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -132,18 +131,24 @@ export const createNewRequestFactory =
return results[0]
}

export async function getUsersPendingAccessRequest<
T extends AccessRequestType = AccessRequestType,
I extends Nullable<string> = Nullable<string>
>(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<string> = Nullable<string>
>(
userId: string,
resourceType: T,
resourceId: I
) => {
if (!userId || !resourceType) {
throw new InvalidArgumentError('User ID or resource type missing')
}

const q = baseQueryFactory({ db })<T, I>(resourceType)
.andWhere(ServerAccessRequests.col.requesterId, userId)
.andWhere(ServerAccessRequests.col.resourceId, resourceId)
.first()
const q = baseQueryFactory({ db: deps.db })<T, I>(resourceType)
.andWhere(ServerAccessRequests.col.requesterId, userId)
.andWhere(ServerAccessRequests.col.resourceId, resourceId)
.first()

return await q
}
return await q
}
4 changes: 2 additions & 2 deletions packages/server/modules/accessrequests/services/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
generateId,
getPendingAccessRequestFactory,
getPendingAccessRequestsFactory,
getUsersPendingAccessRequest,
getUsersPendingAccessRequestFactory,
ServerAccessRequestRecord,
StreamAccessRequestRecord
} from '@/modules/accessrequests/repositories'
Expand Down Expand Up @@ -42,7 +42,7 @@ export async function getUserProjectAccessRequest(
userId: string,
projectId: string
): Promise<Nullable<StreamAccessRequestRecord>> {
const req = await getUsersPendingAccessRequest(
const req = await getUsersPendingAccessRequestFactory({ db })(
userId,
AccessRequestType.Stream,
projectId
Expand Down

0 comments on commit 1908e97

Please sign in to comment.