chore(server): auth IoC 3 - getAllAppsCreatedByUserFactory (#3018)

* chore(server): auth IoC 3 - getAllAppsCreatedByUserFactory

* minor fix
This commit is contained in:
Kristaps Fabians Geikins
2024-09-19 09:58:13 +03:00
committed by GitHub
parent ea6ece6b5e
commit 40cc8caa6f
5 changed files with 39 additions and 32 deletions
@@ -1,11 +1,19 @@
import { DefaultAppWithUnwrappedScopes } from '@/modules/auth/defaultApps'
import { FullServerApp, ServerAppListItem } from '@/modules/auth/domain/types'
import {
FullServerApp,
ServerAppListItem,
UserServerApp
} from '@/modules/auth/domain/types'
import { ScopeRecord } from '@/modules/auth/helpers/types'
export type GetApp = (params: { id: string }) => Promise<FullServerApp | null>
export type GetAllPublicApps = () => Promise<ServerAppListItem[]>
export type GetAllAppsCreatedByUser = (params: {
userId: string
}) => Promise<UserServerApp[]>
export type GetAllScopes = () => Promise<ScopeRecord[]>
export type RegisterDefaultApp = (app: DefaultAppWithUnwrappedScopes) => Promise<void>
+5 -2
View File
@@ -1,11 +1,14 @@
import { ScopeRecord } from '@/modules/auth/helpers/types'
import { ServerAppRecord, UserRecord } from '@/modules/core/helpers/types'
export type FullServerApp = ServerAppRecord & {
scopes: ScopeRecord[]
export type UserServerApp = ServerAppRecord & {
author: Pick<UserRecord, 'id' | 'name' | 'avatar'> | null
}
export type FullServerApp = UserServerApp & {
scopes: ScopeRecord[]
}
export type ServerAppListItem = Pick<
FullServerApp,
| 'id'
@@ -1,6 +1,5 @@
const { ForbiddenError } = require('@/modules/shared/errors')
const {
getAllAppsCreatedByUser,
getAllAppsAuthorizedByUser,
createApp,
updateApp,
@@ -10,12 +9,14 @@ const {
const { Roles } = require('@speckle/shared')
const {
getAppFactory,
getAllPublicAppsFactory
getAllPublicAppsFactory,
getAllAppsCreatedByUserFactory
} = require('@/modules/auth/repositories/apps')
const { db } = require('@/db/knex')
const getApp = getAppFactory({ db })
const getAllPublicApps = getAllPublicAppsFactory({ db })
const getAllAppsCreatedByUser = getAllAppsCreatedByUserFactory({ db })
module.exports = {
Query: {
@@ -1,6 +1,7 @@
import { moduleLogger } from '@/logging/logging'
import { getDefaultApp } from '@/modules/auth/defaultApps'
import {
GetAllAppsCreatedByUser,
GetAllPublicApps,
GetAllScopes,
GetApp,
@@ -113,6 +114,27 @@ export const getAllPublicAppsFactory =
}))
}
export const getAllAppsCreatedByUserFactory =
(deps: { db: Knex }): GetAllAppsCreatedByUser =>
async ({ userId }) => {
const apps: Array<
ServerAppRecord & Partial<{ authorName: string; authorId: string }>
> = await tables
.serverApps(deps.db)
.select('server_apps.*', 'users.name as authorName', 'users.id as authorId')
.where({ authorId: userId })
.leftJoin('users', 'users.id', '=', 'server_apps.authorId')
return apps.map((app) => ({
...app,
redirectUrl: getAppRedirectUrl(app),
author:
app.authorId && app.authorName
? { name: app.authorName, id: app.authorId, avatar: null }
: null
}))
}
export const getAllScopesFactory =
(deps: { db: Knex }): GetAllScopes =>
async () => {
@@ -21,33 +21,6 @@ const addDefaultAppOverrides = (app) => {
}
module.exports = {
async getAllAppsCreatedByUser({ userId }) {
const apps = await ServerApps()
.select(
'server_apps.id',
'server_apps.secret',
'server_apps.name',
'server_apps.description',
'server_apps.redirectUrl',
'server_apps.logo',
'server_apps.termsAndConditionsLink',
'users.name as authorName',
'users.id as authorId'
)
.where({ authorId: userId })
.leftJoin('users', 'users.id', '=', 'server_apps.authorId')
apps.forEach((app) => {
if (app.authorName) {
app.author = { name: app.authorName, id: app.authorId }
}
delete app.authorName
delete app.authorId
})
return apps
},
async getAllAppsAuthorizedByUser({ userId }) {
const query = knex.raw(
`