Skip to content

Commit

Permalink
feat(platform): install app page
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Jan 31, 2023
1 parent 5bc7e9f commit 9f3a089
Show file tree
Hide file tree
Showing 21 changed files with 514 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/platform-server/src/db/mysql/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class User extends BaseEntity {
}

@ObjectType()
export class Application extends PickType(User, ['username', 'createdAt']) {
export class Application extends PickType(User, ['username', 'avatarUrl', 'createdAt']) {
@Field(() => Int)
id!: number
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test.serial('create application', async (t) => {
const response = await gqlClient.query({
query: applicationQuery,
variables: {
id: createResponse.createApplication.application.id,
name: createResponse.createApplication.application.username,
},
})

Expand Down
16 changes: 13 additions & 3 deletions packages/platform-server/src/modules/application/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ limitations under the License.

import { Args, ID, Int, Mutation, ObjectType, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql'

import { Project, Application } from '@perfsee/platform-server/db'
import { Project, Application, User } from '@perfsee/platform-server/db'
import { UserError } from '@perfsee/platform-server/error'
import { PaginationInput, PaginatedType, paginate, Paginated } from '@perfsee/platform-server/graphql'

import { Auth } from '../auth'
Expand Down Expand Up @@ -74,8 +75,17 @@ export class ApplicationResolver {
constructor(private readonly service: ApplicationService) {}

@Query(() => Application)
async application(@Args({ name: 'id', type: () => Int }) id: number) {
return this.service.loader.load(id)
async application(
@Args({ name: 'id', type: () => Int, nullable: true }) id?: number,
@Args({ name: 'name', type: () => String, nullable: true }) name?: string,
) {
if (id) {
return this.service.loader.load(id)
} else if (name) {
return User.findOneByOrFail({ username: name, isApp: true })
} else {
throw new UserError('missing query condition')
}
}

@Query(() => PaginatedApplications)
Expand Down
15 changes: 14 additions & 1 deletion packages/platform-server/src/modules/project/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,21 @@ export class ProjectResolver {
description: 'search project with git namespace/name',
})
query: string | undefined,
@Args({
name: 'permission',
nullable: true,
type: () => Permission,
description: 'filter project with permission',
})
permission: Permission | undefined,
): Promise<PaginatedType<Project>> {
const [projects, totalCount] = await this.projectService.getProjects(user, paginationInput, query, starred)
const [projects, totalCount] = await this.projectService.getProjects(
user,
paginationInput,
query,
starred,
permission,
)
return paginate(projects, 'id', paginationInput, totalCount)
}

Expand Down
22 changes: 18 additions & 4 deletions packages/platform-server/src/modules/project/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ export class ProjectService {
.getMany()
}

async getProjects(user: User, { first, skip, after }: PaginationInput, query?: string, starOnly = false) {
async getProjects(
user: User,
{ first, skip, after }: PaginationInput,
query?: string,
starOnly = false,
permission = Permission.Read,
) {
const queryBuilder = Project.createQueryBuilder('project')

// only allow to query projects that the user starred
Expand All @@ -103,15 +109,23 @@ export class ProjectService {
}

// only allow to query projects that the user can see
const allowProjectIds = await this.permissionProvider.userAllowList(user, Permission.Read)
const allowProjectIds = await this.permissionProvider.userAllowList(user, permission)
if (allowProjectIds.length) {
queryBuilder.andWhere(
new Brackets((builder) => {
builder.andWhereInIds(allowProjectIds).orWhere('is_public is true')
if (permission === Permission.Read) {
// read permission include public
builder.andWhereInIds(allowProjectIds).orWhere('is_public is true')
} else {
builder.andWhereInIds(allowProjectIds)
}
}),
)
} else {
queryBuilder.andWhere('is_public is true')
if (permission === Permission.Read) {
// read permission include public
queryBuilder.andWhere('is_public is true')
}
}

// fuzzy matching
Expand Down
4 changes: 4 additions & 0 deletions packages/platform/src/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import {
DesktopOutlined,
ApiOutlined,
GlobalOutlined,
FileSearchOutlined,
FileProtectOutlined,
} from '@ant-design/icons'
import { registerIcons } from '@fluentui/react'
import { PlugConnectedIcon, PlugDisconnectedIcon, HideIcon, RedEyeIcon } from '@fluentui/react-icons-mdl2'
Expand Down Expand Up @@ -125,6 +127,8 @@ registerIcons({
desktop: <DesktopOutlined />,
ping: <ApiOutlined />,
global: <GlobalOutlined />,
fileSearch: <FileSearchOutlined />,
fileProtect: <FileProtectOutlined />,

// for password text field
Hide: <HideIcon />,
Expand Down
1 change: 1 addition & 0 deletions packages/platform/src/modules/apps/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './installer'
110 changes: 110 additions & 0 deletions packages/platform/src/modules/apps/installer.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2022 ByteDance and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Module, EffectModule, Effect, ImmerReducer } from '@sigi/core'
import { Draft } from 'immer'
import { Observable } from 'rxjs'
import { switchMap, map, startWith, endWith } from 'rxjs/operators'

import { createErrorCatcher, GraphQLClient } from '@perfsee/platform/common'
import { applicationQuery, ApplicationQuery, authorizeApplicationMutation, Permission } from '@perfsee/schema'

export type Application = ApplicationQuery['application']

interface State {
application: Application | null
loading: boolean
installing: boolean
installSuccess: boolean
}

@Module('ApplicationInstallerModule')
export class ApplicationInstallerModule extends EffectModule<State> {
defaultState = {
application: null,
loading: false,
installing: false,
installSuccess: false,
}

constructor(private readonly client: GraphQLClient) {
super()
}

@Effect()
getApplication(payload$: Observable<{ appName: string }>) {
return payload$.pipe(
switchMap(({ appName }) =>
this.client
.query({
query: applicationQuery,
variables: { name: appName },
})
.pipe(
map((data) => {
return this.getActions().setApplication(data.application)
}),
startWith(this.getActions().setLoading(true)),
endWith(this.getActions().setLoading(false)),
createErrorCatcher('Failed to fetch application'),
),
),
)
}

@Effect()
authNewApplications(payload$: Observable<{ projectId: string; applicationId: number; permissions: Permission[] }>) {
return payload$.pipe(
switchMap(({ projectId, applicationId, permissions }) =>
this.client
.query({
query: authorizeApplicationMutation,
variables: {
projectId,
applicationId,
permissions,
},
})
.pipe(
createErrorCatcher('Failed to auth new applications.'),
map(() => this.getActions().setInstallSuccess(true)),
startWith(this.getActions().setInstalling(true)),
endWith(this.getActions().setInstalling(false)),
),
),
)
}

@ImmerReducer()
setApplication(state: Draft<State>, payload: Application) {
state.application = payload
}

@ImmerReducer()
setLoading(state: Draft<State>, loading: boolean) {
state.loading = loading
}

@ImmerReducer()
setInstallSuccess(state: Draft<State>, installSuccess: boolean) {
state.installSuccess = installSuccess
}

@ImmerReducer()
setInstalling(state: Draft<State>, installing: boolean) {
state.installing = installing
}
}

0 comments on commit 9f3a089

Please sign in to comment.