Исправлены ошибки линтера
This commit is contained in:
parent
f2e0bd7f47
commit
ea8115cbee
2 changed files with 128 additions and 119 deletions
|
|
@ -1,58 +1,54 @@
|
|||
import { Controller, Get, Inject, Logger, Param, Query } from "@nestjs/common";
|
||||
import { CalendarService } from "./calendar.service";
|
||||
import { Controller, Get, Inject, Logger, Param, Query } from '@nestjs/common';
|
||||
import { CalendarService } from './calendar.service';
|
||||
import nano from 'nano';
|
||||
import { UNLIMITED } from "../consts/consts";
|
||||
import { UNLIMITED } from '../consts/consts';
|
||||
|
||||
@Controller('calendar')
|
||||
export class CalendarController {
|
||||
private logger = new Logger(CalendarController.name);
|
||||
private logger = new Logger(CalendarController.name);
|
||||
|
||||
constructor(
|
||||
@Inject('CALENDAR_SERVICE')
|
||||
private calendarService: CalendarService
|
||||
) {}
|
||||
constructor(
|
||||
@Inject('CALENDAR_SERVICE')
|
||||
private calendarService: CalendarService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
async get(@Param('filter') filter: any): Promise<string> {
|
||||
return await this.calendarService.getICalData(filter);
|
||||
}
|
||||
@Get()
|
||||
async get(@Param('filter') filter: any): Promise<string> {
|
||||
return await this.calendarService.getICalData(filter);
|
||||
}
|
||||
|
||||
@Get('/simple')
|
||||
async simple(
|
||||
@Query('project') project?: string,
|
||||
@Query('category') category?: string
|
||||
): Promise<string> {
|
||||
const andSection: any[] = [
|
||||
{
|
||||
"closed_on": {
|
||||
"$exists": false
|
||||
}
|
||||
}
|
||||
];
|
||||
if (project) {
|
||||
andSection.push({
|
||||
"project.name": {
|
||||
"$in": [
|
||||
project
|
||||
]
|
||||
}
|
||||
});
|
||||
}
|
||||
if (category) {
|
||||
andSection.push({
|
||||
"category.name": {
|
||||
"$in": [
|
||||
category
|
||||
]
|
||||
}
|
||||
});
|
||||
}
|
||||
const query: nano.MangoQuery = {
|
||||
selector: {
|
||||
"$and": andSection
|
||||
},
|
||||
limit: UNLIMITED
|
||||
};
|
||||
return await this.calendarService.getICalData(query);
|
||||
}
|
||||
@Get('/simple')
|
||||
async simple(
|
||||
@Query('project') project?: string,
|
||||
@Query('category') category?: string,
|
||||
): Promise<string> {
|
||||
const andSection: any[] = [
|
||||
{
|
||||
closed_on: {
|
||||
$exists: false,
|
||||
},
|
||||
},
|
||||
];
|
||||
if (project) {
|
||||
andSection.push({
|
||||
'project.name': {
|
||||
$in: [project],
|
||||
},
|
||||
});
|
||||
}
|
||||
if (category) {
|
||||
andSection.push({
|
||||
'category.name': {
|
||||
$in: [category],
|
||||
},
|
||||
});
|
||||
}
|
||||
const query: nano.MangoQuery = {
|
||||
selector: {
|
||||
$and: andSection,
|
||||
},
|
||||
limit: UNLIMITED,
|
||||
};
|
||||
return await this.calendarService.getICalData(query);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { CalendarEvent } from "../models/calendar-event";
|
||||
import { RedmineTypes } from "../models/redmine-types";
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { CalendarEvent } from '../models/calendar-event';
|
||||
import { RedmineTypes } from '../models/redmine-types';
|
||||
import * as Luxon from 'luxon';
|
||||
import { IssuesService } from "../issues/issues.service";
|
||||
import { IssuesService } from '../issues/issues.service';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
/*
|
||||
|
|
@ -36,57 +36,70 @@ END:VCALENDAR
|
|||
*/
|
||||
|
||||
export type IssueAndEvent = {
|
||||
issue: RedmineTypes.ExtendedIssue;
|
||||
event: CalendarEvent;
|
||||
issue: RedmineTypes.ExtendedIssue;
|
||||
event: CalendarEvent;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class CalendarService {
|
||||
private interval = 30 * 24 * 60 * 60 * 1000; // 30 days
|
||||
private interval = 30 * 24 * 60 * 60 * 1000; // 30 days
|
||||
|
||||
constructor(public calendarEventsKey: string, private issuesService: IssuesService) {}
|
||||
constructor(
|
||||
public calendarEventsKey: string,
|
||||
private issuesService: IssuesService,
|
||||
) {}
|
||||
|
||||
async getICalData(filter: any): Promise<string> {
|
||||
const issues = await this.issuesService.find(filter);
|
||||
const actualEvents = this.getActualEvents(issues);
|
||||
const formattedEvents = actualEvents.map((event) => {
|
||||
return this.generateICalendarEvent(event.issue, event.event);
|
||||
}).filter((event) => {
|
||||
return !!event;
|
||||
});
|
||||
const res = this.generateICalendar(formattedEvents);
|
||||
return res;
|
||||
}
|
||||
async getICalData(filter: any): Promise<string> {
|
||||
const issues = await this.issuesService.find(filter);
|
||||
const actualEvents = this.getActualEvents(issues);
|
||||
const formattedEvents = actualEvents
|
||||
.map((event) => {
|
||||
return this.generateICalendarEvent(event.issue, event.event);
|
||||
})
|
||||
.filter((event) => {
|
||||
return !!event;
|
||||
});
|
||||
const res = this.generateICalendar(formattedEvents);
|
||||
return res;
|
||||
}
|
||||
|
||||
private getActualEvents(issues: RedmineTypes.ExtendedIssue[]): IssueAndEvent[] {
|
||||
const res: IssueAndEvent[] = [];
|
||||
for (let i = 0; i < issues.length; i++) {
|
||||
const issue = issues[i];
|
||||
if (!issue[this.calendarEventsKey] || issue[this.calendarEventsKey].length <= 0) {
|
||||
continue;
|
||||
}
|
||||
const events = issue[this.calendarEventsKey];
|
||||
for (let j = 0; j < events.length; j++) {
|
||||
const event = events[j];
|
||||
if (this.actualEvent(event)) res.push({event: event, issue: issue});
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
private getActualEvents(
|
||||
issues: RedmineTypes.ExtendedIssue[],
|
||||
): IssueAndEvent[] {
|
||||
const res: IssueAndEvent[] = [];
|
||||
for (let i = 0; i < issues.length; i++) {
|
||||
const issue = issues[i];
|
||||
if (
|
||||
!issue[this.calendarEventsKey] ||
|
||||
issue[this.calendarEventsKey].length <= 0
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
const events = issue[this.calendarEventsKey];
|
||||
for (let j = 0; j < events.length; j++) {
|
||||
const event = events[j];
|
||||
if (this.actualEvent(event)) res.push({ event: event, issue: issue });
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private actualEvent(event: CalendarEvent): boolean {
|
||||
const now = Luxon.DateTime.now().toMillis();
|
||||
const from = now - this.interval;
|
||||
const to = now + this.interval;
|
||||
return Boolean(
|
||||
(from <= event.fromTimestamp && event.fromTimestamp <= to) ||
|
||||
(from <= event.toTimestamp && event.toTimestamp <= to)
|
||||
);
|
||||
}
|
||||
private actualEvent(event: CalendarEvent): boolean {
|
||||
const now = Luxon.DateTime.now().toMillis();
|
||||
const from = now - this.interval;
|
||||
const to = now + this.interval;
|
||||
return Boolean(
|
||||
(from <= event.fromTimestamp && event.fromTimestamp <= to) ||
|
||||
(from <= event.toTimestamp && event.toTimestamp <= to),
|
||||
);
|
||||
}
|
||||
|
||||
private generateICalendarEvent(issue: RedmineTypes.Issue, data: CalendarEvent): string | null {
|
||||
if (!data) return null;
|
||||
return `BEGIN:VEVENT
|
||||
private generateICalendarEvent(
|
||||
issue: RedmineTypes.Issue,
|
||||
data: CalendarEvent,
|
||||
): string | null {
|
||||
if (!data) return null;
|
||||
return `BEGIN:VEVENT
|
||||
UID:${randomUUID()}@example.com
|
||||
DTSTAMP:${this.formatTimestamp(data.fromTimestamp, data.fullDay)}
|
||||
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
|
||||
|
|
@ -94,20 +107,20 @@ DTSTART:${this.formatTimestamp(data.fromTimestamp, data.fullDay)}
|
|||
DTEND:${this.formatTimestamp(data.toTimestamp, data.fullDay)}
|
||||
SUMMARY:#${issue.id} - ${data.description} - ${issue.subject}
|
||||
END:VEVENT`;
|
||||
}
|
||||
}
|
||||
|
||||
private formatTimestamp(timestamp: number, fullDay?: boolean): string {
|
||||
let format: string = fullDay ? "yyyyMMdd" : "yyyyMMdd'T'HHmmss'Z'";
|
||||
let datetime = Luxon.DateTime.fromMillis(timestamp);
|
||||
if (!fullDay) datetime = datetime.setZone('utc');
|
||||
return datetime.toFormat(format);
|
||||
}
|
||||
private formatTimestamp(timestamp: number, fullDay?: boolean): string {
|
||||
const format: string = fullDay ? 'yyyyMMdd' : "yyyyMMdd'T'HHmmss'Z'";
|
||||
let datetime = Luxon.DateTime.fromMillis(timestamp);
|
||||
if (!fullDay) datetime = datetime.setZone('utc');
|
||||
return datetime.toFormat(format);
|
||||
}
|
||||
|
||||
private generateICalendar(events: string[]): string {
|
||||
return `BEGIN:VCALENDAR
|
||||
private generateICalendar(events: string[]): string {
|
||||
return `BEGIN:VCALENDAR
|
||||
VERSION:2.0
|
||||
PRODID:-//Example Corp.//CalDAV Client//EN
|
||||
${events.join("\n")}
|
||||
${events.join('\n')}
|
||||
END:VCALENDAR`;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue