Исправлены ошибки линтера

This commit is contained in:
Pavel Gnedov 2023-11-07 02:26:54 +07:00
parent f2e0bd7f47
commit ea8115cbee
2 changed files with 128 additions and 119 deletions

View file

@ -1,58 +1,54 @@
import { Controller, Get, Inject, Logger, Param, Query } from "@nestjs/common"; import { Controller, Get, Inject, Logger, Param, Query } from '@nestjs/common';
import { CalendarService } from "./calendar.service"; import { CalendarService } from './calendar.service';
import nano from 'nano'; import nano from 'nano';
import { UNLIMITED } from "../consts/consts"; import { UNLIMITED } from '../consts/consts';
@Controller('calendar') @Controller('calendar')
export class CalendarController { export class CalendarController {
private logger = new Logger(CalendarController.name); private logger = new Logger(CalendarController.name);
constructor( constructor(
@Inject('CALENDAR_SERVICE') @Inject('CALENDAR_SERVICE')
private calendarService: CalendarService private calendarService: CalendarService,
) {} ) {}
@Get() @Get()
async get(@Param('filter') filter: any): Promise<string> { async get(@Param('filter') filter: any): Promise<string> {
return await this.calendarService.getICalData(filter); return await this.calendarService.getICalData(filter);
} }
@Get('/simple') @Get('/simple')
async simple( async simple(
@Query('project') project?: string, @Query('project') project?: string,
@Query('category') category?: string @Query('category') category?: string,
): Promise<string> { ): Promise<string> {
const andSection: any[] = [ const andSection: any[] = [
{ {
"closed_on": { closed_on: {
"$exists": false $exists: false,
} },
} },
]; ];
if (project) { if (project) {
andSection.push({ andSection.push({
"project.name": { 'project.name': {
"$in": [ $in: [project],
project },
] });
} }
}); if (category) {
} andSection.push({
if (category) { 'category.name': {
andSection.push({ $in: [category],
"category.name": { },
"$in": [ });
category }
] const query: nano.MangoQuery = {
} selector: {
}); $and: andSection,
} },
const query: nano.MangoQuery = { limit: UNLIMITED,
selector: { };
"$and": andSection return await this.calendarService.getICalData(query);
}, }
limit: UNLIMITED }
};
return await this.calendarService.getICalData(query);
}
}

View file

@ -1,8 +1,8 @@
import { Injectable, Logger } from "@nestjs/common"; import { Injectable } from '@nestjs/common';
import { CalendarEvent } from "../models/calendar-event"; import { CalendarEvent } from '../models/calendar-event';
import { RedmineTypes } from "../models/redmine-types"; import { RedmineTypes } from '../models/redmine-types';
import * as Luxon from 'luxon'; import * as Luxon from 'luxon';
import { IssuesService } from "../issues/issues.service"; import { IssuesService } from '../issues/issues.service';
import { randomUUID } from 'crypto'; import { randomUUID } from 'crypto';
/* /*
@ -36,57 +36,70 @@ END:VCALENDAR
*/ */
export type IssueAndEvent = { export type IssueAndEvent = {
issue: RedmineTypes.ExtendedIssue; issue: RedmineTypes.ExtendedIssue;
event: CalendarEvent; event: CalendarEvent;
}; };
@Injectable() @Injectable()
export class CalendarService { 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,
async getICalData(filter: any): Promise<string> { private issuesService: IssuesService,
const issues = await this.issuesService.find(filter); ) {}
const actualEvents = this.getActualEvents(issues);
const formattedEvents = actualEvents.map((event) => { async getICalData(filter: any): Promise<string> {
return this.generateICalendarEvent(event.issue, event.event); const issues = await this.issuesService.find(filter);
}).filter((event) => { const actualEvents = this.getActualEvents(issues);
return !!event; const formattedEvents = actualEvents
}); .map((event) => {
const res = this.generateICalendar(formattedEvents); return this.generateICalendarEvent(event.issue, event.event);
return res; })
} .filter((event) => {
return !!event;
private getActualEvents(issues: RedmineTypes.ExtendedIssue[]): IssueAndEvent[] { });
const res: IssueAndEvent[] = []; const res = this.generateICalendar(formattedEvents);
for (let i = 0; i < issues.length; i++) { return res;
const issue = issues[i]; }
if (!issue[this.calendarEventsKey] || issue[this.calendarEventsKey].length <= 0) {
continue; private getActualEvents(
} issues: RedmineTypes.ExtendedIssue[],
const events = issue[this.calendarEventsKey]; ): IssueAndEvent[] {
for (let j = 0; j < events.length; j++) { const res: IssueAndEvent[] = [];
const event = events[j]; for (let i = 0; i < issues.length; i++) {
if (this.actualEvent(event)) res.push({event: event, issue: issue}); const issue = issues[i];
} if (
} !issue[this.calendarEventsKey] ||
return res; issue[this.calendarEventsKey].length <= 0
} ) {
continue;
private actualEvent(event: CalendarEvent): boolean { }
const now = Luxon.DateTime.now().toMillis(); const events = issue[this.calendarEventsKey];
const from = now - this.interval; for (let j = 0; j < events.length; j++) {
const to = now + this.interval; const event = events[j];
return Boolean( if (this.actualEvent(event)) res.push({ event: event, issue: issue });
(from <= event.fromTimestamp && event.fromTimestamp <= to) || }
(from <= event.toTimestamp && event.toTimestamp <= to) }
); return res;
} }
private generateICalendarEvent(issue: RedmineTypes.Issue, data: CalendarEvent): string | null { private actualEvent(event: CalendarEvent): boolean {
if (!data) return null; const now = Luxon.DateTime.now().toMillis();
return `BEGIN:VEVENT 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
UID:${randomUUID()}@example.com UID:${randomUUID()}@example.com
DTSTAMP:${this.formatTimestamp(data.fromTimestamp, data.fullDay)} DTSTAMP:${this.formatTimestamp(data.fromTimestamp, data.fullDay)}
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com 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)} DTEND:${this.formatTimestamp(data.toTimestamp, data.fullDay)}
SUMMARY:#${issue.id} - ${data.description} - ${issue.subject} SUMMARY:#${issue.id} - ${data.description} - ${issue.subject}
END:VEVENT`; END:VEVENT`;
} }
private formatTimestamp(timestamp: number, fullDay?: boolean): string { private formatTimestamp(timestamp: number, fullDay?: boolean): string {
let format: string = fullDay ? "yyyyMMdd" : "yyyyMMdd'T'HHmmss'Z'"; const format: string = fullDay ? 'yyyyMMdd' : "yyyyMMdd'T'HHmmss'Z'";
let datetime = Luxon.DateTime.fromMillis(timestamp); let datetime = Luxon.DateTime.fromMillis(timestamp);
if (!fullDay) datetime = datetime.setZone('utc'); if (!fullDay) datetime = datetime.setZone('utc');
return datetime.toFormat(format); return datetime.toFormat(format);
} }
private generateICalendar(events: string[]): string { private generateICalendar(events: string[]): string {
return `BEGIN:VCALENDAR return `BEGIN:VCALENDAR
VERSION:2.0 VERSION:2.0
PRODID:-//Example Corp.//CalDAV Client//EN PRODID:-//Example Corp.//CalDAV Client//EN
${events.join("\n")} ${events.join('\n')}
END:VCALENDAR`; END:VCALENDAR`;
} }
} }