Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/nestjs-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
"handlebars": "4.7.8",
"helmet": "7.1.0",
"http-proxy-middleware": "3.0.3",
"immer": "10.0.4",
"ioredis": "5.4.1",
"is-port-reachable": "3.1.0",
"joi": "17.12.2",
Expand Down
76 changes: 76 additions & 0 deletions apps/nestjs-backend/src/features/dashboard/chart.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '@teable/db-main-prisma';
import { ChartType, DataSource, THEMES_KEYS, DEFAULT_SERIES_ARRAY } from '@teable/openapi';

@Injectable()
export class ChartService {
constructor(private readonly prismaService: PrismaService) {}

async getDefaultPluginOptions(baseId: string, pluginId: string, tableId?: string) {
if (pluginId !== 'plgchartV2') {
return;
}

const tables = await this.prismaService.txClient().tableMeta.findMany({
where: {
baseId,
deletedTime: null,
},
select: {
id: true,
},
orderBy: {
order: 'asc',
},
});

if (tables.length === 0) {
return;
}

const defaultTableId = tableId || tables[0].id;

const fields = await this.prismaService.txClient().field.findMany({
where: {
tableId: defaultTableId,
deletedTime: null,
},
select: {
id: true,
},
});

const viewIds = await this.prismaService.txClient().view.findMany({
where: {
tableId: defaultTableId,
deletedTime: null,
},
select: {
id: true,
},
});

const defaultViewId = viewIds[0].id;

const defaultFieldId = fields[0].id;

return {
chartType: ChartType.Bar,
dataSource: DataSource.Table,
query: {
tableId: defaultTableId,
viewId: defaultViewId,
orderBy: { on: 'xAxis', order: 'asc' },
xAxis: defaultFieldId,
filter: null,
groupBy: null,
seriesArray: DEFAULT_SERIES_ARRAY,
},
appearance: {
theme: THEMES_KEYS.BLUE,
legendVisible: true,
labelVisible: false,
},
};
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Module } from '@nestjs/common';
import { BaseModule } from '../base/base.module';
import { CollaboratorModule } from '../collaborator/collaborator.module';
import { ChartService } from './chart.service';
import { DashboardController } from './dashboard.controller';
import { DashboardService } from './dashboard.service';

@Module({
imports: [CollaboratorModule, BaseModule],
providers: [DashboardService],
providers: [DashboardService, ChartService],
controllers: [DashboardController],
exports: [DashboardService],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ import { CustomHttpException } from '../../custom.exception';
import type { IClsStore } from '../../types/cls';
import { BaseImportService } from '../base/base-import.service';
import { CollaboratorService } from '../collaborator/collaborator.service';
import { ChartService } from './chart.service';

@Injectable()
export class DashboardService {
constructor(
private readonly prismaService: PrismaService,
private readonly cls: ClsService<IClsStore>,
private readonly collaboratorService: CollaboratorService,
private readonly baseImportService: BaseImportService
private readonly baseImportService: BaseImportService,
private readonly chartService: ChartService
) {}

async getDashboard(baseId: string): Promise<IGetDashboardListVo> {
Expand Down Expand Up @@ -223,6 +225,9 @@ export class DashboardService {
const userId = this.cls.get('user.id');
await this.validatePluginPublished(baseId, ro.pluginId);

// chartV2 need default options
const defaultOptions = await this.chartService.getDefaultPluginOptions(baseId, ro.pluginId);

return this.prismaService.$tx(async () => {
const newInstallPlugin = await this.prismaService.txClient().pluginInstall.create({
data: {
Expand All @@ -233,6 +238,7 @@ export class DashboardService {
name: ro.name,
pluginId: ro.pluginId,
createdBy: userId,
storage: defaultOptions ? JSON.stringify(defaultOptions) : null,
},
select: {
id: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Module } from '@nestjs/common';
import { BaseModule } from '../base/base.module';
import { CollaboratorModule } from '../collaborator/collaborator.module';
import { ChartService } from '../dashboard/chart.service';
import { DashboardModule } from '../dashboard/dashboard.module';
import { PluginPanelController } from './plugin-panel.controller';
import { PluginPanelService } from './plugin-panel.service';

@Module({
imports: [CollaboratorModule, BaseModule],
imports: [CollaboratorModule, BaseModule, DashboardModule],
controllers: [PluginPanelController],
exports: [PluginPanelService],
providers: [PluginPanelService],
providers: [PluginPanelService, ChartService],
})
export class PluginPanelModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ import { CustomHttpException } from '../../custom.exception';
import type { IClsStore } from '../../types/cls';
import { BaseImportService } from '../base/base-import.service';
import { CollaboratorService } from '../collaborator/collaborator.service';
import { ChartService } from '../dashboard/chart.service';

@Injectable()
export class PluginPanelService {
constructor(
private readonly prismaService: PrismaService,
private readonly cls: ClsService<IClsStore>,
private readonly collaboratorService: CollaboratorService,
private readonly baseImportService: BaseImportService
private readonly baseImportService: BaseImportService,
private readonly chartService: ChartService
) {}

createPluginPanel(tableId: string, createPluginPanelRo: IPluginPanelCreateRo) {
Expand Down Expand Up @@ -166,7 +168,7 @@ export class PluginPanelService {
};
}

private async getBaseId(tableId: string) {
async getBaseId(tableId: string) {
const base = await this.prismaService.tableMeta.findUnique({
where: {
id: tableId,
Expand All @@ -193,6 +195,11 @@ export class PluginPanelService {
const { pluginId, name } = installPluginPanelRo;
const currentUser = this.cls.get('user.id');
const baseId = await this.getBaseId(tableId);
const defaultOptions = await this.chartService.getDefaultPluginOptions(
baseId,
pluginId,
tableId
);
return this.prismaService.$tx(async (prisma) => {
const plugin = await prisma.plugin.findUnique({
where: {
Expand All @@ -215,6 +222,7 @@ export class PluginPanelService {
position: PluginPosition.Panel,
positionId: pluginPanelId,
createdBy: currentUser,
storage: defaultOptions ? JSON.stringify(defaultOptions) : null,
},
select: {
id: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import {
getDashboardInstallPluginQueryRoSchema,
getPluginPanelInstallPluginQueryRoSchema,
IGetDashboardInstallPluginQueryRo,
IGetPluginPanelInstallPluginQueryRo,
type IBaseQueryVo,
ITestSqlRo,
testSqlRoSchema,
IGetDashboardInstallPluginQueryRo,
} from '@teable/openapi';
import type { IBaseQueryVoV2, IBaseQueryVo, IBaseTableSchemaVo } from '@teable/openapi';
import { ZodValidationPipe } from '../../../../zod.validation.pipe';
import { Permissions } from '../../../auth/decorators/permissions.decorator';
import { ResourceMeta } from '../../../auth/decorators/resource_meta.decorator';
Expand Down Expand Up @@ -50,4 +52,46 @@ export class PluginChartController {
cellFormat
);
}

@Get(':pluginInstallId/dashboard/:positionId/query/v2')
@Permissions('base|read')
@ResourceMeta('baseId', 'query')
getDashboardPluginQueryV2(
@Param('pluginInstallId') pluginInstallId: string,
@Param('positionId') positionId: string,
@Query('baseId') baseId: string
): Promise<IBaseQueryVoV2> {
return this.pluginChartService.getDashboardPluginQueryV2(baseId, pluginInstallId, positionId);
}

@Get(':pluginInstallId/plugin-panel/:positionId/query/v2')
@Permissions('table|read')
@ResourceMeta('tableId', 'query')
getPluginPanelPluginQueryV2(
@Param('pluginInstallId') pluginInstallId: string,
@Param('positionId') positionId: string,
@Query('tableId') tableId: string
): Promise<IBaseQueryVoV2> {
return this.pluginChartService.getPluginPanelPluginQueryV2(
tableId,
pluginInstallId,
positionId
);
}

@Post('test-sql')
@Permissions('base|read')
@ResourceMeta('baseId', 'body')
testSql(
@Body(new ZodValidationPipe(testSqlRoSchema)) testSqlRo: ITestSqlRo
): Promise<IBaseQueryVoV2> {
return this.pluginChartService.testSql(testSqlRo);
}

@Get(':baseId/schema')
@Permissions('base|read')
@ResourceMeta('baseId', 'params')
getSchemaByBaseId(@Param('baseId') baseId: string): Promise<IBaseTableSchemaVo> {
return this.pluginChartService.getSchemaByBaseId(baseId);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { Module } from '@nestjs/common';
import { BaseModule } from '../../../base/base.module';
import { BaseSqlExecutorModule } from '../../../base-sql-executor/base-sql-executor.module';
import { DashboardModule } from '../../../dashboard/dashboard.module';
import { FieldModule } from '../../../field/field.module';
import { PluginPanelModule } from '../../../plugin-panel/plugin-panel.module';
import { RecordModule } from '../../../record/record.module';
import { PluginChartController } from './plugin-chart.controller';
import { PluginChartService } from './plugin-chart.service';

@Module({
imports: [PluginPanelModule, DashboardModule, BaseModule],
imports: [
PluginPanelModule,
DashboardModule,
BaseModule,
RecordModule,
FieldModule,
BaseSqlExecutorModule,
],
providers: [PluginChartService],
exports: [PluginChartService],
controllers: [PluginChartController],
Expand Down
Loading
Loading