mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
6 changed files with 178 additions and 1 deletions
@ -0,0 +1,20 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { PublicMetasController } from './public-metas.controller'; |
||||
import { PublicMetasService } from './public-metas.service'; |
||||
|
||||
describe('PublicMetasController', () => { |
||||
let controller: PublicMetasController; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
controllers: [PublicMetasController], |
||||
providers: [PublicMetasService], |
||||
}).compile(); |
||||
|
||||
controller = module.get<PublicMetasController>(PublicMetasController); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(controller).toBeDefined(); |
||||
}); |
||||
}); |
@ -0,0 +1,27 @@
|
||||
import { Controller, Get, Request, Param } from '@nestjs/common'; |
||||
import { PublicMetasService } from './public-metas.service'; |
||||
|
||||
@Controller('public-metas') |
||||
export class PublicMetasController { |
||||
constructor(private readonly publicMetasService: PublicMetasService) {} |
||||
|
||||
@Get('/api/v1/db/public/shared-view/:sharedViewUuid/meta') |
||||
async viewMetaGet( |
||||
@Request() req, |
||||
@Param('sharedViewUuid') sharedViewUuid: string, |
||||
) { |
||||
return await this.publicMetasService.viewMetaGet({ |
||||
password: req.headers?.['xc-password'] as string, |
||||
sharedViewUuid: sharedViewUuid, |
||||
}); |
||||
} |
||||
|
||||
@Get('/api/v1/db/public/shared-base/:sharedBaseUuid/meta') |
||||
async publicSharedBaseGet( |
||||
@Param('sharedBaseUuid') sharedBaseUuid: string, |
||||
): Promise<any> { |
||||
return await this.publicMetasService.publicSharedBaseGet({ |
||||
sharedBaseUuid, |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'; |
||||
import { PublicMetasService } from './public-metas.service'; |
||||
import { PublicMetasController } from './public-metas.controller'; |
||||
|
||||
@Module({ |
||||
controllers: [PublicMetasController], |
||||
providers: [PublicMetasService] |
||||
}) |
||||
export class PublicMetasModule {} |
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { PublicMetasService } from './public-metas.service'; |
||||
|
||||
describe('PublicMetasService', () => { |
||||
let service: PublicMetasService; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
providers: [PublicMetasService], |
||||
}).compile(); |
||||
|
||||
service = module.get<PublicMetasService>(PublicMetasService); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(service).toBeDefined(); |
||||
}); |
||||
}); |
@ -0,0 +1,102 @@
|
||||
import { Injectable } from '@nestjs/common'; |
||||
import { |
||||
ErrorMessages, |
||||
LinkToAnotherRecordType, |
||||
RelationTypes, |
||||
UITypes, |
||||
} from 'nocodb-sdk'; |
||||
import { NcError } from '../../helpers/catchError'; |
||||
import { |
||||
Base, |
||||
Column, |
||||
LinkToAnotherRecordColumn, |
||||
Model, |
||||
Project, |
||||
View, |
||||
} from '../../models'; |
||||
|
||||
@Injectable() |
||||
export class PublicMetasService { |
||||
async viewMetaGet(param: { sharedViewUuid: string; password: string }) { |
||||
const view: View & { |
||||
relatedMetas?: { [ket: string]: Model }; |
||||
client?: string; |
||||
} = await View.getByUUID(param.sharedViewUuid); |
||||
|
||||
if (!view) NcError.notFound('Not found'); |
||||
|
||||
if (view.password && view.password !== param.password) { |
||||
NcError.forbidden(ErrorMessages.INVALID_SHARED_VIEW_PASSWORD); |
||||
} |
||||
|
||||
await view.getFilters(); |
||||
await view.getSorts(); |
||||
|
||||
await view.getViewWithInfo(); |
||||
await view.getColumns(); |
||||
await view.getModelWithInfo(); |
||||
await view.model.getColumns(); |
||||
|
||||
const base = await Base.get(view.model.base_id); |
||||
view.client = base.type; |
||||
|
||||
// todo: return only required props
|
||||
delete view['password']; |
||||
|
||||
view.model.columns = view.columns |
||||
.filter((c) => { |
||||
const column = view.model.columnsById[c.fk_column_id]; |
||||
return ( |
||||
c.show || |
||||
(column.rqd && !column.cdf && !column.ai) || |
||||
column.pk || |
||||
view.model.columns.some( |
||||
(c1) => |
||||
c1.uidt === UITypes.LinkToAnotherRecord && |
||||
(<LinkToAnotherRecordColumn>c1.colOptions).type === |
||||
RelationTypes.BELONGS_TO && |
||||
view.columns.some((vc) => vc.fk_column_id === c1.id && vc.show) && |
||||
(<LinkToAnotherRecordColumn>c1.colOptions).fk_child_column_id === |
||||
c.fk_column_id, |
||||
) |
||||
); |
||||
}) |
||||
.map( |
||||
(c) => |
||||
new Column({ |
||||
...c, |
||||
...view.model.columnsById[c.fk_column_id], |
||||
} as any), |
||||
) as any; |
||||
|
||||
const relatedMetas = {}; |
||||
|
||||
// load related table metas
|
||||
for (const col of view.model.columns) { |
||||
if (UITypes.LinkToAnotherRecord === col.uidt) { |
||||
const colOpt = await col.getColOptions<LinkToAnotherRecordType>(); |
||||
relatedMetas[colOpt.fk_related_model_id] = await Model.getWithInfo({ |
||||
id: colOpt.fk_related_model_id, |
||||
}); |
||||
if (colOpt.type === 'mm') { |
||||
relatedMetas[colOpt.fk_mm_model_id] = await Model.getWithInfo({ |
||||
id: colOpt.fk_mm_model_id, |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
|
||||
view.relatedMetas = relatedMetas; |
||||
|
||||
return view; |
||||
} |
||||
async publicSharedBaseGet(param: { sharedBaseUuid: string }): Promise<any> { |
||||
const project = await Project.getByUuid(param.sharedBaseUuid); |
||||
|
||||
if (!project) { |
||||
NcError.notFound(); |
||||
} |
||||
|
||||
return { project_id: project.id }; |
||||
} |
||||
} |
Loading…
Reference in new issue