Merge pull request #3017 from specklesystems/fabians/auth-ioc-2

chore(server): auth IoC 2 - getAllPublicAppsFactory
This commit is contained in:
Alessandro Magionami 2024-09-18 09:14:15 +02:00 коммит произвёл GitHub
Родитель 8555bc2f59 cfabada3cd
Коммит ce55e5474b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
7 изменённых файлов: 70 добавлений и 39 удалений

Просмотреть файл

@ -1,9 +1,11 @@
import { DefaultAppWithUnwrappedScopes } from '@/modules/auth/defaultApps'
import { FullServerApp } from '@/modules/auth/domain/types'
import { FullServerApp, ServerAppListItem } 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 GetAllScopes = () => Promise<ScopeRecord[]>
export type RegisterDefaultApp = (app: DefaultAppWithUnwrappedScopes) => Promise<void>

Просмотреть файл

@ -3,5 +3,17 @@ import { ServerAppRecord, UserRecord } from '@/modules/core/helpers/types'
export type FullServerApp = ServerAppRecord & {
scopes: ScopeRecord[]
author: Pick<UserRecord, 'id' | 'name' | 'avatar'>
author: Pick<UserRecord, 'id' | 'name' | 'avatar'> | null
}
export type ServerAppListItem = Pick<
FullServerApp,
| 'id'
| 'name'
| 'description'
| 'redirectUrl'
| 'termsAndConditionsLink'
| 'trustByDefault'
| 'logo'
| 'author'
>

Просмотреть файл

@ -1,6 +1,5 @@
const { ForbiddenError } = require('@/modules/shared/errors')
const {
getAllPublicApps,
getAllAppsCreatedByUser,
getAllAppsAuthorizedByUser,
createApp,
@ -9,10 +8,14 @@ const {
revokeExistingAppCredentialsForUser
} = require('../../services/apps')
const { Roles } = require('@speckle/shared')
const { getAppFactory } = require('@/modules/auth/repositories/apps')
const {
getAppFactory,
getAllPublicAppsFactory
} = require('@/modules/auth/repositories/apps')
const { db } = require('@/db/knex')
const getApp = getAppFactory({ db })
const getAllPublicApps = getAllPublicAppsFactory({ db })
module.exports = {
Query: {

Просмотреть файл

@ -1,6 +1,7 @@
import { moduleLogger } from '@/logging/logging'
import { getDefaultApp } from '@/modules/auth/defaultApps'
import {
GetAllPublicApps,
GetAllScopes,
GetApp,
RegisterDefaultApp,
@ -34,7 +35,7 @@ const tables = {
tokenScopes: (db: Knex) => db<TokenScopeRecord>(TokenScopes.name)
}
const getAppRedirectUrl = (app: ServerAppRecord) => {
const getAppRedirectUrl = (app: Pick<ServerAppRecord, 'redirectUrl' | 'id'>) => {
const defaultApp = getDefaultApp({ id: app.id })
return defaultApp ? defaultApp.redirectUrl : app.redirectUrl
}
@ -65,11 +66,53 @@ export const getAppFactory =
return {
...app,
scopes: appScopes,
author: appAuthor!,
author: appAuthor || null,
redirectUrl: getAppRedirectUrl(app)
}
}
export const getAllPublicAppsFactory =
(deps: { db: Knex }): GetAllPublicApps =>
async () => {
const apps: Array<
Pick<
ServerAppRecord,
| 'id'
| 'name'
| 'description'
| 'trustByDefault'
| 'redirectUrl'
| 'logo'
| 'termsAndConditionsLink'
> &
Partial<{ authorName: string; authorId: string }>
> = await tables
.serverApps(deps.db)
.select(
'server_apps.id',
'server_apps.name',
'server_apps.description',
'server_apps.trustByDefault',
'server_apps.redirectUrl',
'server_apps.logo',
'server_apps.termsAndConditionsLink',
'users.name as authorName',
'users.id as authorId'
)
.where({ public: true })
.leftJoin('users', 'users.id', '=', 'server_apps.authorId')
.orderBy('server_apps.trustByDefault', 'DESC')
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,36 +21,6 @@ const addDefaultAppOverrides = (app) => {
}
module.exports = {
async getAllPublicApps() {
const apps = await ServerApps()
.select(
'server_apps.id',
'server_apps.name',
'server_apps.description',
'server_apps.trustByDefault',
'server_apps.redirectUrl',
'server_apps.logo',
'server_apps.termsAndConditionsLink',
'users.name as authorName',
'users.id as authorId'
)
.where({ public: true })
.leftJoin('users', 'users.id', '=', 'server_apps.authorId')
.orderBy('server_apps.trustByDefault', 'DESC')
apps.forEach((app) => {
app = addDefaultAppOverrides(app)
if (app.authorName && app.authorId) {
app.author = { name: app.authorName, id: app.authorId }
}
delete app.authorName
delete app.authorId
})
return apps
},
async getAllAppsCreatedByUser({ userId }) {
const apps = await ServerApps()
.select(

Просмотреть файл

@ -5,7 +5,6 @@ const { createUser } = require(`@/modules/core/services/users`)
const { validateToken } = require(`@/modules/core/services/tokens`)
const { beforeEachContext } = require(`@/test/hooks`)
const {
getAllPublicApps,
createApp,
updateApp,
deleteApp,
@ -20,11 +19,13 @@ const knex = require('@/db/knex')
const cryptoRandomString = require('crypto-random-string')
const {
getAppFactory,
updateDefaultAppFactory
updateDefaultAppFactory,
getAllPublicAppsFactory
} = require('@/modules/auth/repositories/apps')
const getApp = getAppFactory({ db: knex })
const updateDefaultApp = updateDefaultAppFactory({ db: knex })
const getAllPublicApps = getAllPublicAppsFactory({ db: knex })
describe('Services @apps-services', () => {
const actor = {

Просмотреть файл

@ -13,7 +13,7 @@ export type UserRecord = {
company: Nullable<string>
email: string
verified: boolean
avatar: string
avatar: Nullable<string>
profiles: Nullable<string>
/**
* Marked as optional, cause most queries delete it