Skip to content

Commit

Permalink
feat(api): add listen report endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
apricote committed May 9, 2020
1 parent ddcdfff commit 3828b84
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Expand Up @@ -11,6 +11,7 @@ import { SourcesModule } from "./sources/sources.module";
import { UsersModule } from "./users/users.module";
import { ConfigModule } from "./config/config.module";
import { HealthCheckModule } from "./health-check/health-check.module";
import { ReportsModule } from "./reports/reports.module";

@Module({
imports: [
Expand All @@ -28,6 +29,7 @@ import { HealthCheckModule } from "./health-check/health-check.module";
MusicLibraryModule,
ListensModule,
HealthCheckModule,
ReportsModule,
],
})
export class AppModule {}
16 changes: 16 additions & 0 deletions src/reports/dto/get-listen-report.dto.ts
@@ -0,0 +1,16 @@
import { IsEnum, IsISO8601 } from "class-validator";
import { User } from "../../users/user.entity";
import { Timeframe } from "../timeframe.enum";

export class GetListenReportDto {
user: User;

@IsEnum(Timeframe)
timeFrame: Timeframe;

@IsISO8601()
timeStart: string;

@IsISO8601()
timeEnd: string;
}
14 changes: 14 additions & 0 deletions src/reports/dto/listen-report.dto.ts
@@ -0,0 +1,14 @@
import { Timeframe } from "../timeframe.enum";

export class ListenReportDto {
items: {
date: string;
count: number;
}[];

timeFrame: Timeframe;

timeStart: string;

timeEnd: string;
}
18 changes: 18 additions & 0 deletions src/reports/reports.controller.spec.ts
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ReportsController } from './reports.controller';

describe('Reports Controller', () => {
let controller: ReportsController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ReportsController],
}).compile();

controller = module.get<ReportsController>(ReportsController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
21 changes: 21 additions & 0 deletions src/reports/reports.controller.ts
@@ -0,0 +1,21 @@
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 { ListenReportDto } from "./dto/listen-report.dto";
import { ReportsService } from "./reports.service";

@Controller("api/v1/reports")
export class ReportsController {
constructor(private readonly reportsService: ReportsService) {}

@Get("listens")
@Auth()
async getListens(
@Query() options: Omit<GetListenReportDto, "user">,
@ReqUser() user: User
): Promise<ListenReportDto> {
return this.reportsService.getListens({ ...options, user });
}
}
11 changes: 11 additions & 0 deletions src/reports/reports.module.ts
@@ -0,0 +1,11 @@
import { Module } from "@nestjs/common";
import { ReportsService } from "./reports.service";
import { ReportsController } from "./reports.controller";
import { ListensModule } from "src/listens/listens.module";

@Module({
imports: [ListensModule],
providers: [ReportsService],
controllers: [ReportsController],
})
export class ReportsModule {}
18 changes: 18 additions & 0 deletions src/reports/reports.service.spec.ts
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ReportsService } from './reports.service';

describe('ReportsService', () => {
let service: ReportsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ReportsService],
}).compile();

service = module.get<ReportsService>(ReportsService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
73 changes: 73 additions & 0 deletions src/reports/reports.service.ts
@@ -0,0 +1,73 @@
import { Injectable } from "@nestjs/common";
import {
eachDayOfInterval,
eachMonthOfInterval,
eachWeekOfInterval,
eachYearOfInterval,
formatISO,
Interval,
isSameDay,
isSameMonth,
isSameWeek,
isSameYear,
parseISO,
} from "date-fns";
import { ListensService } from "../listens/listens.service";
import { GetListenReportDto } from "./dto/get-listen-report.dto";
import { ListenReportDto } from "./dto/listen-report.dto";
import { Timeframe } from "./timeframe.enum";

const timeframeToDateFns: {
[x in Timeframe]: {
eachOfInterval: (interval: Interval) => Date[];
isSame: (dateLeft: Date, dateRight: Date) => boolean;
};
} = {
[Timeframe.Day]: {
eachOfInterval: eachDayOfInterval,
isSame: isSameDay,
},
[Timeframe.Week]: {
eachOfInterval: eachWeekOfInterval,
isSame: isSameWeek,
},
[Timeframe.Month]: {
eachOfInterval: eachMonthOfInterval,
isSame: isSameMonth,
},
[Timeframe.Year]: {
eachOfInterval: eachYearOfInterval,
isSame: isSameYear,
},
};

@Injectable()
export class ReportsService {
constructor(private readonly listensService: ListensService) {}

async getListens(options: GetListenReportDto): Promise<ListenReportDto> {
const { user, timeFrame, timeStart, timeEnd } = options;

const { items: listens } = await this.listensService.getListens({
user,
filter: { time: { start: timeStart, end: timeEnd } },
page: 1,
limit: 10000000,
});

const reportInterval: Interval = {
start: parseISO(timeStart),
end: parseISO(timeEnd),
};

const { eachOfInterval, isSame } = timeframeToDateFns[timeFrame];

const reportItems = eachOfInterval(reportInterval).map((date) => {
const count = listens.filter((listen) => isSame(date, listen.playedAt))
.length;
return { date: formatISO(date), count };
});

return { items: reportItems, timeStart, timeEnd, timeFrame };
}
}
6 changes: 6 additions & 0 deletions src/reports/timeframe.enum.ts
@@ -0,0 +1,6 @@
export enum Timeframe {
Day = "day",
Week = "week",
Month = "month",
Year = "year",
}

0 comments on commit 3828b84

Please sign in to comment.