mirror of https://github.com/nocodb/nocodb
Raju Udava
9 months ago
committed by
GitHub
16 changed files with 1310 additions and 612 deletions
@ -0,0 +1,103 @@
|
||||
import { |
||||
Controller, |
||||
Get, |
||||
Param, |
||||
Query, |
||||
Req, |
||||
Res, |
||||
UseGuards, |
||||
} from '@nestjs/common'; |
||||
import { Request, Response } from 'express'; |
||||
import { GlobalGuard } from '~/guards/global/global.guard'; |
||||
import { DataApiLimiterGuard } from '~/guards/data-api-limiter.guard'; |
||||
import { CalendarDatasService } from '~/services/calendar-datas.service'; |
||||
import { parseHrtimeToMilliSeconds } from '~/helpers'; |
||||
|
||||
import { Acl } from '~/middlewares/extract-ids/extract-ids.middleware'; |
||||
|
||||
@Controller() |
||||
@UseGuards(DataApiLimiterGuard, GlobalGuard) |
||||
export class CalendarDatasController { |
||||
constructor(private readonly calendarDatasService: CalendarDatasService) {} |
||||
|
||||
@Get(['/api/v1/db/calendar-data/:orgs/:baseName/:tableName/views/:viewName']) |
||||
@Acl('dataList') |
||||
async dataList( |
||||
@Req() req: Request, |
||||
@Param('viewName') viewId: string, |
||||
@Query('from_date') fromDate: string, |
||||
@Query('to_date') toDate: string, |
||||
) { |
||||
return await this.calendarDatasService.getCalendarDataList({ |
||||
viewId: viewId, |
||||
query: req.query, |
||||
from_date: fromDate, |
||||
to_date: toDate, |
||||
}); |
||||
} |
||||
|
||||
@Get([ |
||||
'/api/v1/db/calendar-data/:orgs/:baseName/:tableName/views/:viewName/countByDate/', |
||||
]) |
||||
@Acl('dataList') |
||||
async calendarDataCount( |
||||
@Req() req: Request, |
||||
@Res() res: Response, |
||||
@Param('baseName') baseName: string, |
||||
@Param('tableName') tableName: string, |
||||
@Param('viewName') viewName: string, |
||||
@Query('from_date') fromDate: string, |
||||
@Query('to_date') toDate: string, |
||||
) { |
||||
const startTime = process.hrtime(); |
||||
|
||||
const data = await this.calendarDatasService.getCalendarRecordCount({ |
||||
query: req.query, |
||||
viewId: viewName, |
||||
from_date: fromDate, |
||||
to_date: toDate, |
||||
}); |
||||
|
||||
const elapsedSeconds = parseHrtimeToMilliSeconds(process.hrtime(startTime)); |
||||
res.setHeader('xc-db-response', elapsedSeconds); |
||||
res.json(data); |
||||
} |
||||
|
||||
@Get([ |
||||
'/api/v1/db/public/calendar-view/:sharedViewUuid/countByDate', |
||||
'/api/v2/public/calendar-view/:sharedViewUuid/countByDate', |
||||
]) |
||||
async countByDate( |
||||
@Req() req: Request, |
||||
@Param('sharedViewUuid') sharedViewUuid: string, |
||||
@Query('from_date') fromDate: string, |
||||
@Query('to_date') toDate: string, |
||||
) { |
||||
return await this.calendarDatasService.getPublicCalendarRecordCount({ |
||||
query: req.query, |
||||
password: req.headers?.['xc-password'] as string, |
||||
sharedViewUuid, |
||||
from_date: fromDate, |
||||
to_date: toDate, |
||||
}); |
||||
} |
||||
|
||||
@Get([ |
||||
'/api/v1/db/public/calendar-view/:sharedViewUuid', |
||||
'/api/v2/public/calendar-view/:sharedViewUuid', |
||||
]) |
||||
async getPublicCalendarDataList( |
||||
@Req() req: Request, |
||||
@Param('sharedViewUuid') sharedViewUuid: string, |
||||
@Query('from_date') fromDate: string, |
||||
@Query('to_date') toDate: string, |
||||
) { |
||||
return await this.calendarDatasService.getPublicCalendarDataList({ |
||||
query: req.query, |
||||
password: req.headers?.['xc-password'] as string, |
||||
sharedViewUuid, |
||||
from_date: fromDate, |
||||
to_date: toDate, |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,230 @@
|
||||
import { Injectable, Logger } from '@nestjs/common'; |
||||
import { ErrorMessages, ViewTypes } from 'nocodb-sdk'; |
||||
import dayjs from 'dayjs'; |
||||
import type { CalendarRangeType, FilterType } from 'nocodb-sdk'; |
||||
import { CalendarRange, Model, View } from '~/models'; |
||||
import { NcError } from '~/helpers/catchError'; |
||||
import { DatasService } from '~/services/datas.service'; |
||||
|
||||
@Injectable() |
||||
export class CalendarDatasService { |
||||
protected logger = new Logger(CalendarDatasService.name); |
||||
|
||||
constructor(protected datasService: DatasService) {} |
||||
|
||||
async getCalendarDataList(param: { |
||||
viewId: string; |
||||
query: any; |
||||
from_date: string; |
||||
to_date: string; |
||||
}) { |
||||
const { viewId, query, from_date, to_date } = param; |
||||
|
||||
if (!from_date || !to_date) |
||||
NcError.badRequest('from_date and to_date are required'); |
||||
|
||||
if (dayjs(to_date).diff(dayjs(from_date), 'days') > 42) { |
||||
NcError.badRequest('Date range should not exceed 42 days'); |
||||
} |
||||
|
||||
const view = await View.get(viewId); |
||||
|
||||
if (!view) NcError.notFound('View not found'); |
||||
|
||||
if (view.type !== ViewTypes.CALENDAR) |
||||
NcError.badRequest('View is not a calendar view'); |
||||
|
||||
const calendarRange = await CalendarRange.read(view.id); |
||||
|
||||
if (!calendarRange?.ranges?.length) NcError.badRequest('No ranges found'); |
||||
|
||||
const filterArr = await this.buildFilterArr({ |
||||
viewId, |
||||
from_date, |
||||
to_date, |
||||
}); |
||||
|
||||
query.filterArr = [...(query.filterArr ? query.filterArr : []), filterArr]; |
||||
|
||||
const model = await Model.getByIdOrName({ |
||||
id: view.fk_model_id, |
||||
}); |
||||
|
||||
return await this.datasService.dataList({ |
||||
...param, |
||||
...query, |
||||
baseName: model.base_id, |
||||
tableName: model.id, |
||||
calendarLimitOverride: 3000, // TODO: make this configurable in env
|
||||
}); |
||||
} |
||||
|
||||
async getPublicCalendarRecordCount(param: { |
||||
password: string; |
||||
query: any; |
||||
sharedViewUuid: string; |
||||
from_date: string; |
||||
to_date: string; |
||||
}) { |
||||
const { sharedViewUuid, password, query = {} } = param; |
||||
const view = await View.getByUUID(sharedViewUuid); |
||||
|
||||
if (!view) NcError.notFound('Not found'); |
||||
if (view.type !== ViewTypes.CALENDAR) { |
||||
NcError.notFound('Not found'); |
||||
} |
||||
|
||||
if (view.password && view.password !== password) { |
||||
return NcError.forbidden(ErrorMessages.INVALID_SHARED_VIEW_PASSWORD); |
||||
} |
||||
|
||||
return this.getCalendarRecordCount({ |
||||
viewId: view.id, |
||||
query, |
||||
from_date: param.from_date, |
||||
to_date: param.to_date, |
||||
}); |
||||
} |
||||
|
||||
async getPublicCalendarDataList(param: { |
||||
password: string; |
||||
query: any; |
||||
sharedViewUuid: string; |
||||
from_date: string; |
||||
to_date: string; |
||||
}) { |
||||
const { sharedViewUuid, password, query = {} } = param; |
||||
const view = await View.getByUUID(sharedViewUuid); |
||||
|
||||
if (!view) NcError.notFound('Not found'); |
||||
if (view.type !== ViewTypes.CALENDAR) { |
||||
NcError.notFound('Not found'); |
||||
} |
||||
|
||||
if (view.password && view.password !== password) { |
||||
return NcError.forbidden(ErrorMessages.INVALID_SHARED_VIEW_PASSWORD); |
||||
} |
||||
|
||||
return this.getCalendarDataList({ |
||||
viewId: view.id, |
||||
query, |
||||
from_date: param.from_date, |
||||
to_date: param.to_date, |
||||
}); |
||||
} |
||||
|
||||
async getCalendarRecordCount(param: { |
||||
viewId: string; |
||||
query: any; |
||||
from_date: string; |
||||
to_date: string; |
||||
}) { |
||||
const { viewId, query, from_date, to_date } = param; |
||||
|
||||
if (!from_date || !to_date) |
||||
NcError.badRequest('from_date and to_date are required'); |
||||
|
||||
if (dayjs(to_date).diff(dayjs(from_date), 'days') > 395) { |
||||
NcError.badRequest('Date range should not exceed 395 days'); |
||||
} |
||||
|
||||
const view = await View.get(viewId); |
||||
|
||||
if (!view) NcError.notFound('View not found'); |
||||
|
||||
if (view.type !== ViewTypes.CALENDAR) |
||||
NcError.badRequest('View is not a calendar view'); |
||||
|
||||
const { ranges } = await CalendarRange.read(view.id); |
||||
|
||||
if (!ranges.length) NcError.badRequest('No ranges found'); |
||||
|
||||
const filterArr = await this.buildFilterArr({ |
||||
viewId, |
||||
from_date, |
||||
to_date, |
||||
}); |
||||
|
||||
query.filterArr = [...(query.filterArr ? query.filterArr : []), filterArr]; |
||||
|
||||
const model = await Model.getByIdOrName({ |
||||
id: view.fk_model_id, |
||||
}); |
||||
|
||||
const data = await this.datasService.dataList({ |
||||
...param, |
||||
baseName: model.base_id, |
||||
tableName: model.id, |
||||
ignorePagination: true, |
||||
}); |
||||
|
||||
if (!data) NcError.notFound('Data not found'); |
||||
|
||||
const dates: Array<string> = []; |
||||
|
||||
const columns = await model.getColumns(); |
||||
|
||||
ranges.forEach((range: CalendarRangeType) => { |
||||
const fromCol = columns.find( |
||||
(c) => c.id === range.fk_from_column_id, |
||||
)?.title; |
||||
|
||||
data.list.forEach((date) => { |
||||
const fromDt = dayjs(date[fromCol]); |
||||
|
||||
if (fromCol && fromDt.isValid()) { |
||||
dates.push(fromDt.format('YYYY-MM-DD HH:mm:ssZ')); |
||||
} |
||||
}); |
||||
}); |
||||
|
||||
return { |
||||
count: dates.length, |
||||
dates: Array.from(new Set(dates)), |
||||
}; |
||||
} |
||||
|
||||
async buildFilterArr({ |
||||
viewId, |
||||
from_date, |
||||
to_date, |
||||
}: { |
||||
viewId: string; |
||||
from_date: string; |
||||
to_date: string; |
||||
}) { |
||||
const calendarRange = await CalendarRange.read(viewId); |
||||
if (!calendarRange?.ranges?.length) NcError.badRequest('No ranges found'); |
||||
|
||||
const filterArr: FilterType = { |
||||
is_group: true, |
||||
logical_op: 'and', |
||||
children: [], |
||||
}; |
||||
|
||||
calendarRange.ranges.forEach((range: CalendarRange) => { |
||||
const fromColumn = range.fk_from_column_id; |
||||
let rangeFilter: any = []; |
||||
if (fromColumn) { |
||||
rangeFilter = [ |
||||
{ |
||||
fk_column_id: fromColumn, |
||||
comparison_op: 'lt', |
||||
comparison_sub_op: 'exactDate', |
||||
value: to_date as string, |
||||
}, |
||||
{ |
||||
fk_column_id: fromColumn, |
||||
comparison_op: 'gt', |
||||
comparison_sub_op: 'exactDate', |
||||
value: from_date as string, |
||||
}, |
||||
]; |
||||
} |
||||
|
||||
if (rangeFilter.length > 0) filterArr.children.push(rangeFilter); |
||||
}); |
||||
|
||||
return filterArr; |
||||
} |
||||
} |
Loading…
Reference in new issue