From f2c0b7787543a275ca80ce7ee0dce598d0a8eb4d Mon Sep 17 00:00:00 2001 From: DarkPhoenix2704 Date: Tue, 20 Feb 2024 07:15:49 +0000 Subject: [PATCH] wip --- .../src/controllers/calendars.controller.ts | 27 ++++++------ packages/nocodb/src/helpers/getAst.ts | 5 ++- .../nocodb/src/services/calendars.service.ts | 42 ++++++++++++++++--- packages/nocodb/src/services/datas.service.ts | 39 +++++++++++++++++ 4 files changed, 91 insertions(+), 22 deletions(-) diff --git a/packages/nocodb/src/controllers/calendars.controller.ts b/packages/nocodb/src/controllers/calendars.controller.ts index e0a5bff8de..6bb9b315ef 100644 --- a/packages/nocodb/src/controllers/calendars.controller.ts +++ b/packages/nocodb/src/controllers/calendars.controller.ts @@ -1,19 +1,8 @@ -import { - Body, - Controller, - Get, - HttpCode, - Param, - Patch, - Post, - Req, - UseGuards, -} from '@nestjs/common'; +import { Body, Controller, Get, HttpCode, Param, Patch, Post, Req, UseGuards } from '@nestjs/common'; import { Request } from 'express'; import { ViewCreateReqType } from 'nocodb-sdk'; import { GlobalGuard } from '~/guards/global/global.guard'; import { CalendarsService } from '~/services/calendars.service'; -import { Acl } from '~/middlewares/extract-ids/extract-ids.middleware'; import { MetaApiLimiterGuard } from '~/guards/meta-api-limiter.guard'; @Controller() @@ -25,7 +14,7 @@ export class CalendarsController { '/api/v1/db/meta/calendars/:calendarViewId', '/api/v2/meta/calendars/:calendarViewId', ]) - @Acl('calendarViewGet') + // @Acl('calendarViewGet') async calendarViewGet(@Param('calendarViewId') calendarViewId: string) { return await this.calendarsService.calendarViewGet({ calendarViewId, @@ -37,7 +26,7 @@ export class CalendarsController { '/api/v2/meta/tables/:tableId/calendars', ]) @HttpCode(200) - @Acl('calendarViewCreate') + // @Acl('calendarViewCreate') async calendarViewCreate( @Param('tableId') tableId: string, @Body() body: ViewCreateReqType, @@ -60,7 +49,6 @@ export class CalendarsController { async calendarViewUpdate( @Param('calendarViewId') calendarViewId: string, @Body() body, - @Req() req: Request, ) { return await this.calendarsService.calendarViewUpdate({ @@ -69,4 +57,13 @@ export class CalendarsController { req, }); } + + + @Get('/api/v1/db/data/calendars/:calendarViewId/count') + async calendarRecordCountGet(@Param('calendarViewId') calendarViewId: string, @Req() req: Request) { + return await this.calendarsService.calendarRecordCountGet({ + calendarViewId, + req + }); + } } diff --git a/packages/nocodb/src/helpers/getAst.ts b/packages/nocodb/src/helpers/getAst.ts index 4b046fbbf8..01328ac2a4 100644 --- a/packages/nocodb/src/helpers/getAst.ts +++ b/packages/nocodb/src/helpers/getAst.ts @@ -13,7 +13,7 @@ import type { Model, } from '~/models'; import { NcError } from '~/helpers/catchError'; -import { GalleryView, KanbanView, View } from '~/models'; +import { CalendarView, GalleryView, KanbanView, View } from '~/models'; const getAst = async ({ query, @@ -49,6 +49,9 @@ const getAst = async ({ } else if (view && view.type === ViewTypes.KANBAN) { const kanban = await KanbanView.get(view.id); coverImageId = kanban.fk_cover_image_col_id; + } else if (view && view.type === ViewTypes.CALENDAR) { + const calendar = await CalendarView.get(view.id); + coverImageId = calendar.fk_cover_image_col_id; } if (!model.columns?.length) await model.getColumns(); diff --git a/packages/nocodb/src/services/calendars.service.ts b/packages/nocodb/src/services/calendars.service.ts index b38146656b..b3100cd016 100644 --- a/packages/nocodb/src/services/calendars.service.ts +++ b/packages/nocodb/src/services/calendars.service.ts @@ -1,16 +1,14 @@ import { Injectable } from '@nestjs/common'; +import type { CalendarUpdateReqType, UserType, ViewCreateReqType } from 'nocodb-sdk'; import { AppEvents, ViewTypes } from 'nocodb-sdk'; -import type { - CalendarUpdateReqType, - UserType, - ViewCreateReqType, -} from 'nocodb-sdk'; import type { NcRequest } from '~/interface/config'; import { AppHooksService } from '~/services/app-hooks/app-hooks.service'; import { validatePayload } from '~/helpers'; import { NcError } from '~/helpers/catchError'; import { CalendarView, View } from '~/models'; import CalendarRange from '~/models/CalendarRange'; +import { Model, Source } from '~/ee/models'; +import NcConnectionMgrv2 from '~/ee/utils/common/NcConnectionMgrv2'; @Injectable() export class CalendarsService { @@ -24,7 +22,7 @@ export class CalendarsService { const calendarRanges = await CalendarRange.read(param.calendarViewId); return { ...calendarView, - calendar_range: calendarRanges, + calendar_range: calendarRanges.ranges, }; } @@ -83,4 +81,36 @@ export class CalendarsService { return res; } + + async calendarRecordCountGet(param: { + calendarViewId: string; + req: any + }) { + const { query } = param.req; + console.log('query', query) + + const view = await View.get(param.calendarViewId); + if (!view) { + NcError.badRequest('View not found'); + } + + const source = await Source.get(view.source_id); + + const baseModel = await Model.getBaseModelSQL({ + id: view.fk_model_id, + viewId: view?.id, + dbDriver: await NcConnectionMgrv2.get(source), + }); + + + const data = await baseModel.groupByAndAggregate( + 'Date', + 'count', + { + groupByColumnName: 'Date', + }, + ); + console.log(data); + return data; + } } diff --git a/packages/nocodb/src/services/datas.service.ts b/packages/nocodb/src/services/datas.service.ts index 2301299505..247fdf5f3c 100644 --- a/packages/nocodb/src/services/datas.service.ts +++ b/packages/nocodb/src/services/datas.service.ts @@ -966,4 +966,43 @@ export class DatasService { return column; } + + async getDataAggregateBy(param: { + viewId: string; + query?: any; + aggregateColumnName: string; + aggregateFunction: string; + groupByColumnName?: string; + ignoreFilters?: boolean; + sort?: { + column_name: string; + direction: 'asc' | 'desc'; + }; + }) { + const {viewId, query = {} } = param; + + const view = await View.get(viewId) + + const source = await Source.get(view.source_id); + + const baseModel = await Model.getBaseModelSQL({ + id: view.fk_model_id, + viewId: view?.id, + dbDriver: await NcConnectionMgrv2.get(source), + }); + + const data = await baseModel.groupByAndAggregate( + param.aggregateColumnName, + param.aggregateFunction, + { + groupByColumnName: param.groupByColumnName, + sortBy: param.sort, + ...query, + }, + ); + + return new PagedResponseImpl(data, { + ...query, + }); + } }