mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
refactor: use TimeOptions for Listens report
This commit is contained in:
parent
5bc76f23d0
commit
67ea28aec7
7 changed files with 51 additions and 59 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { IsEnum, IsISO8601 } from "class-validator";
|
||||
import { IsEnum, ValidateNested } from "class-validator";
|
||||
import { User } from "../../users/user.entity";
|
||||
import { Timeframe } from "../timeframe.enum";
|
||||
import { ReportTimeDto } from "./report-time.dto";
|
||||
|
||||
export class GetListenReportDto {
|
||||
user: User;
|
||||
|
|
@ -8,9 +9,6 @@ export class GetListenReportDto {
|
|||
@IsEnum(Timeframe)
|
||||
timeFrame: Timeframe;
|
||||
|
||||
@IsISO8601()
|
||||
timeStart: string;
|
||||
|
||||
@IsISO8601()
|
||||
timeEnd: string;
|
||||
@ValidateNested()
|
||||
time: ReportTimeDto;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,10 +5,4 @@ export class ListenReportDto {
|
|||
date: string;
|
||||
count: number;
|
||||
}[];
|
||||
|
||||
timeFrame: Timeframe;
|
||||
|
||||
timeStart: string;
|
||||
|
||||
timeEnd: string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@ import { Controller, Get, Query } from "@nestjs/common";
|
|||
import { Auth } from "src/auth/decorators/auth.decorator";
|
||||
import { ReqUser } from "../auth/decorators/req-user.decorator";
|
||||
import { User } from "../users/user.entity";
|
||||
import { GetListenReportDto } from "./dto/get-listen-report.dto";
|
||||
import { GetTopArtistsReportDto } from "./dto/get-top-artists-report.dto";
|
||||
import { ListenReportDto } from "./dto/listen-report.dto";
|
||||
import { ReportTimeDto } from "./dto/report-time.dto";
|
||||
import { TopArtistsReportDto } from "./dto/top-artists-report.dto";
|
||||
import { ReportsService } from "./reports.service";
|
||||
import { Timeframe } from "./timeframe.enum";
|
||||
|
||||
@Controller("api/v1/reports")
|
||||
export class ReportsController {
|
||||
|
|
@ -15,10 +15,11 @@ export class ReportsController {
|
|||
@Get("listens")
|
||||
@Auth()
|
||||
async getListens(
|
||||
@Query() options: GetListenReportDto,
|
||||
@Query() time: ReportTimeDto,
|
||||
@Query("timeFrame") timeFrame: Timeframe,
|
||||
@ReqUser() user: User
|
||||
): Promise<ListenReportDto> {
|
||||
return this.reportsService.getListens({ ...options, user });
|
||||
return this.reportsService.getListens({ user, timeFrame, time });
|
||||
}
|
||||
|
||||
@Get("top-artists")
|
||||
|
|
|
|||
|
|
@ -65,14 +65,9 @@ export class ReportsService {
|
|||
constructor(private readonly listensService: ListensService) {}
|
||||
|
||||
async getListens(options: GetListenReportDto): Promise<ListenReportDto> {
|
||||
const { user, timeFrame, timeStart, timeEnd } = options;
|
||||
const { user, timeFrame, time: timePreset } = options;
|
||||
|
||||
// Function should eventually be rewritten to accept a timepreset
|
||||
const interval = this.getIntervalFromPreset({
|
||||
timePreset: TimePreset.CUSTOM,
|
||||
customTimeStart: timeStart,
|
||||
customTimeEnd: timeEnd,
|
||||
});
|
||||
const interval = this.getIntervalFromPreset(timePreset);
|
||||
|
||||
const { items: listens } = await this.listensService.getListens({
|
||||
user,
|
||||
|
|
@ -81,20 +76,15 @@ export class ReportsService {
|
|||
limit: PAGINATION_LIMIT_UNLIMITED,
|
||||
});
|
||||
|
||||
const reportInterval: Interval = {
|
||||
start: parseISO(timeStart),
|
||||
end: parseISO(timeEnd),
|
||||
};
|
||||
|
||||
const { eachOfInterval, isSame } = timeframeToDateFns[timeFrame];
|
||||
|
||||
const reportItems = eachOfInterval(reportInterval).map((date) => {
|
||||
const reportItems = eachOfInterval(interval).map((date) => {
|
||||
const count = listens.filter((listen) => isSame(date, listen.playedAt))
|
||||
.length;
|
||||
return { date: formatISO(date), count };
|
||||
});
|
||||
|
||||
return { items: reportItems, timeStart, timeEnd, timeFrame };
|
||||
return { items: reportItems };
|
||||
}
|
||||
|
||||
async getTopArtists(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue