Browse Source

feat: public shared meta apis

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 2 years ago
parent
commit
f04c8d3d6e
  1. 3
      packages/nocodb-nest/src/app.module.ts
  2. 20
      packages/nocodb-nest/src/modules/public-metas/public-metas.controller.spec.ts
  3. 27
      packages/nocodb-nest/src/modules/public-metas/public-metas.controller.ts
  4. 9
      packages/nocodb-nest/src/modules/public-metas/public-metas.module.ts
  5. 18
      packages/nocodb-nest/src/modules/public-metas/public-metas.service.spec.ts
  6. 102
      packages/nocodb-nest/src/modules/public-metas/public-metas.service.ts

3
packages/nocodb-nest/src/app.module.ts

@ -38,9 +38,10 @@ import { AuditsModule } from './modules/audits/audits.module';
import { DatasModule } from './modules/datas/datas.module'; import { DatasModule } from './modules/datas/datas.module';
import { DataAliasModule } from './modules/data-alias/data-alias.module'; import { DataAliasModule } from './modules/data-alias/data-alias.module';
import { ApiDocsModule } from './modules/api-docs/api-docs.module'; import { ApiDocsModule } from './modules/api-docs/api-docs.module';
import { PublicMetasModule } from './modules/public-metas/public-metas.module';
@Module({ @Module({
imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule, FormsModule, GridsModule, KanbansModule, GalleriesModule, FormColumnsModule, GridColumnsModule, MapsModule, ProjectUsersModule, ModelVisibilitiesModule, HookFiltersModule, ApiTokensModule, AttachmentsModule, OrgLcenseModule, OrgTokensModule, OrgUsersModule, MetaDiffsModule, AuditsModule, DatasModule, DataAliasModule, ApiDocsModule], imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule, FormsModule, GridsModule, KanbansModule, GalleriesModule, FormColumnsModule, GridColumnsModule, MapsModule, ProjectUsersModule, ModelVisibilitiesModule, HookFiltersModule, ApiTokensModule, AttachmentsModule, OrgLcenseModule, OrgTokensModule, OrgUsersModule, MetaDiffsModule, AuditsModule, DatasModule, DataAliasModule, ApiDocsModule, PublicMetasModule],
controllers: [], controllers: [],
providers: [Connection, MetaService, JwtStrategy, ExtractProjectIdMiddleware], providers: [Connection, MetaService, JwtStrategy, ExtractProjectIdMiddleware],
exports: [Connection, MetaService], exports: [Connection, MetaService],

20
packages/nocodb-nest/src/modules/public-metas/public-metas.controller.spec.ts

@ -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();
});
});

27
packages/nocodb-nest/src/modules/public-metas/public-metas.controller.ts

@ -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,
});
}
}

9
packages/nocodb-nest/src/modules/public-metas/public-metas.module.ts

@ -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 {}

18
packages/nocodb-nest/src/modules/public-metas/public-metas.service.spec.ts

@ -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();
});
});

102
packages/nocodb-nest/src/modules/public-metas/public-metas.service.ts

@ -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…
Cancel
Save