mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
14 changed files with 328 additions and 46 deletions
@ -0,0 +1,20 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { ViewsController } from './views.controller'; |
||||
import { ViewsService } from './views.service'; |
||||
|
||||
describe('ViewsController', () => { |
||||
let controller: ViewsController; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
controllers: [ViewsController], |
||||
providers: [ViewsService], |
||||
}).compile(); |
||||
|
||||
controller = module.get<ViewsController>(ViewsController); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(controller).toBeDefined(); |
||||
}); |
||||
}); |
@ -0,0 +1,129 @@
|
||||
import { |
||||
Body, |
||||
Controller, |
||||
Delete, |
||||
Get, |
||||
Param, |
||||
Patch, |
||||
Post, |
||||
Query, |
||||
Request, |
||||
UseGuards, |
||||
} from '@nestjs/common'; |
||||
import { AuthGuard } from '@nestjs/passport'; |
||||
import { ViewUpdateReqType } from 'nocodb-sdk'; |
||||
import { PagedResponseImpl } from '../../helpers/PagedResponse'; |
||||
import { |
||||
ExtractProjectIdMiddleware, |
||||
UseAclMiddleware, |
||||
} from '../../middlewares/extract-project-id/extract-project-id.middleware'; |
||||
import { ViewsService } from './views.service'; |
||||
|
||||
@Controller() |
||||
@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) |
||||
export class ViewsController { |
||||
constructor(private readonly viewsService: ViewsService) {} |
||||
|
||||
@Get('/api/v1/db/meta/tables/:tableId/views') |
||||
@UseAclMiddleware({ |
||||
permissionName: 'viewList', |
||||
}) |
||||
async viewList(@Param('tableId') tableId: string, @Request() req) { |
||||
return new PagedResponseImpl( |
||||
await this.viewsService.viewList({ |
||||
tableId, |
||||
user: req.user, |
||||
}), |
||||
); |
||||
} |
||||
|
||||
@Patch('/api/v1/db/meta/views/:viewId') |
||||
@UseAclMiddleware({ |
||||
permissionName: 'viewUpdate', |
||||
}) |
||||
async viewUpdate( |
||||
@Param('viewId') viewId: string, |
||||
@Body() body: ViewUpdateReqType, |
||||
) { |
||||
const result = await this.viewsService.viewUpdate({ |
||||
viewId, |
||||
view: body, |
||||
}); |
||||
return result; |
||||
} |
||||
|
||||
@Delete('/api/v1/db/meta/views/:viewId') |
||||
@UseAclMiddleware({ |
||||
permissionName: 'viewDelete', |
||||
}) |
||||
async viewDelete(@Param('viewId') viewId: string) { |
||||
const result = await this.viewsService.viewDelete({ viewId }); |
||||
return result; |
||||
} |
||||
|
||||
@Post('/api/v1/db/meta/views/:viewId/show-all') |
||||
@UseAclMiddleware({ |
||||
permissionName: 'showAllColumns', |
||||
}) |
||||
async showAllColumns( |
||||
@Param('viewId') viewId: string, |
||||
@Query('ignoreIds') ignoreIds: string[], |
||||
) { |
||||
return await this.viewsService.showAllColumns({ |
||||
viewId, |
||||
ignoreIds, |
||||
}); |
||||
} |
||||
@Post('/api/v1/db/meta/views/:viewId/hide-all') |
||||
@UseAclMiddleware({ |
||||
permissionName: 'hideAllColumns', |
||||
}) |
||||
async hideAllColumns( |
||||
@Param('viewId') viewId: string, |
||||
@Query('ignoreIds') ignoreIds: string[], |
||||
) { |
||||
return await this.viewsService.hideAllColumns({ |
||||
viewId, |
||||
ignoreIds, |
||||
}); |
||||
} |
||||
|
||||
@Post('/api/v1/db/meta/views/:viewId/share') |
||||
@UseAclMiddleware({ |
||||
permissionName: 'shareView', |
||||
}) |
||||
async shareView(@Param('viewId') viewId: string) { |
||||
return await this.viewsService.shareView({ viewId }); |
||||
} |
||||
|
||||
@Get('/api/v1/db/meta/tables/:tableId/share') |
||||
async shareViewList(@Param('tableId') tableId: string) { |
||||
return new PagedResponseImpl( |
||||
await this.viewsService.shareViewList({ |
||||
tableId, |
||||
}), |
||||
); |
||||
} |
||||
|
||||
@Patch('/api/v1/db/meta/views/:viewId/share') |
||||
@UseAclMiddleware({ |
||||
permissionName: 'shareViewUpdate', |
||||
}) |
||||
async shareViewUpdate( |
||||
@Param('viewId') viewId: string, |
||||
@Body() body: ViewUpdateReqType, |
||||
) { |
||||
return await this.viewsService.shareViewUpdate({ |
||||
viewId, |
||||
sharedView: body, |
||||
}); |
||||
} |
||||
|
||||
@Delete('/api/v1/db/meta/views/:viewId/share') |
||||
@UseAclMiddleware({ |
||||
permissionName: 'shareViewDelete', |
||||
}) |
||||
async shareViewDelete(@Param('viewId') viewId: string) { |
||||
return await this.viewsService.shareViewDelete({ viewId }); |
||||
} |
||||
} |
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common'; |
||||
import { TablesModule } from '../tables/tables.module' |
||||
import { ViewsService } from './views.service'; |
||||
import { ViewsController } from './views.controller'; |
||||
|
||||
@Module({ |
||||
controllers: [ViewsController], |
||||
providers: [ViewsService], |
||||
imports: [TablesModule], |
||||
}) |
||||
export class ViewsModule {} |
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { ViewsService } from './views.service'; |
||||
|
||||
describe('ViewsService', () => { |
||||
let service: ViewsService; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
providers: [ViewsService], |
||||
}).compile(); |
||||
|
||||
service = module.get<ViewsService>(ViewsService); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(service).toBeDefined(); |
||||
}); |
||||
}); |
@ -0,0 +1,88 @@
|
||||
import { Injectable } from '@nestjs/common'; |
||||
import { SharedViewReqType, ViewUpdateReqType } from 'nocodb-sdk'; |
||||
import { validatePayload } from '../../helpers'; |
||||
import { Model, View } from '../../models' |
||||
import { T } from 'nc-help'; |
||||
import { TablesService } from '../tables/tables.service'; |
||||
|
||||
@Injectable() |
||||
export class ViewsService { |
||||
constructor(private tablesService: TablesService) {} |
||||
|
||||
async viewList(param: { |
||||
tableId: string; |
||||
user: { |
||||
roles: Record<string, boolean>; |
||||
}; |
||||
}) { |
||||
const model = await Model.get(param.tableId); |
||||
|
||||
const viewList = await this.tablesService.xcVisibilityMetaGet({ |
||||
projectId: model.project_id, |
||||
models: [model], |
||||
}); |
||||
|
||||
// todo: user roles
|
||||
//await View.list(param.tableId)
|
||||
const filteredViewList = viewList.filter((view: any) => { |
||||
return Object.keys(param?.user?.roles).some( |
||||
(role) => param?.user?.roles[role] && !view.disabled[role], |
||||
); |
||||
}); |
||||
|
||||
return filteredViewList; |
||||
} |
||||
|
||||
async shareView(param: { viewId: string }) { |
||||
T.emit('evt', { evt_type: 'sharedView:generated-link' }); |
||||
return await View.share(param.viewId); |
||||
} |
||||
|
||||
async viewUpdate(param: { viewId: string; view: ViewUpdateReqType }) { |
||||
validatePayload( |
||||
'swagger.json#/components/schemas/ViewUpdateReq', |
||||
param.view, |
||||
); |
||||
const result = await View.update(param.viewId, param.view); |
||||
T.emit('evt', { evt_type: 'vtable:updated', show_as: result.type }); |
||||
return result; |
||||
} |
||||
|
||||
async viewDelete(param: { viewId: string }) { |
||||
await View.delete(param.viewId); |
||||
T.emit('evt', { evt_type: 'vtable:deleted' }); |
||||
return true; |
||||
} |
||||
|
||||
async shareViewUpdate(param: { |
||||
viewId: string; |
||||
sharedView: SharedViewReqType; |
||||
}) { |
||||
validatePayload( |
||||
'swagger.json#/components/schemas/SharedViewReq', |
||||
param.sharedView, |
||||
); |
||||
T.emit('evt', { evt_type: 'sharedView:updated' }); |
||||
return await View.update(param.viewId, param.sharedView); |
||||
} |
||||
|
||||
async shareViewDelete(param: { viewId: string }) { |
||||
T.emit('evt', { evt_type: 'sharedView:deleted' }); |
||||
await View.sharedViewDelete(param.viewId); |
||||
return true; |
||||
} |
||||
|
||||
async showAllColumns(param: { viewId: string; ignoreIds?: string[] }) { |
||||
await View.showAllColumns(param.viewId, param.ignoreIds || []); |
||||
return true; |
||||
} |
||||
|
||||
async hideAllColumns(param: { viewId: string; ignoreIds?: string[] }) { |
||||
await View.hideAllColumns(param.viewId, param.ignoreIds || []); |
||||
return true; |
||||
} |
||||
|
||||
async shareViewList(param: { tableId: string }) { |
||||
return await View.shareViewList(param.tableId); |
||||
} |
||||
} |
Loading…
Reference in new issue