Skip to content

Commit

Permalink
refactor(modules/reports): change listening days response type
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Sep 9, 2024
1 parent c083a3c commit 2f3e52c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 60 deletions.
64 changes: 28 additions & 36 deletions src/modules/reports/reports.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,20 @@ describe('ReportsService', () => {
.spyOn(historyTracksRepository, 'countByUserAndBetweenDates')
.mockResolvedValue(COUNT)

expect(
await reportsService.getListeningDays(
{
before,
after,
measurement: StatsMeasurement.PLAYS,
},
userMock
)
).toEqual({
1: COUNT,
2: COUNT,
3: COUNT,
4: COUNT,
5: COUNT,
6: COUNT,
7: COUNT,
})
const result = await reportsService.getListeningDays(
{
before,
after,
measurement: StatsMeasurement.PLAYS,
},
userMock
)

for (const [index, { date, value, dayIndex }] of result.entries()) {
expect(date).toBeInstanceOf(Date)
expect(value).toBe(COUNT)
expect(dayIndex).toBe(index + 1)
}

expect(countByUserAndBetweenDatesSpy).toHaveBeenCalledTimes(DAYS)
})
Expand All @@ -103,24 +99,20 @@ describe('ReportsService', () => {
.spyOn(historyTracksRepository, 'findByUserAndBetweenDates')
.mockResolvedValue(historyTracksMock)

expect(
await reportsService.getListeningDays(
{
before,
after,
measurement: StatsMeasurement.PLAY_TIME,
},
userMock
)
).toEqual({
1: TOTAL_DURATION,
2: TOTAL_DURATION,
3: TOTAL_DURATION,
4: TOTAL_DURATION,
5: TOTAL_DURATION,
6: TOTAL_DURATION,
7: TOTAL_DURATION,
})
const result = await reportsService.getListeningDays(
{
before,
after,
measurement: StatsMeasurement.PLAY_TIME,
},
userMock
)

for (const [index, { date, value, dayIndex }] of result.entries()) {
expect(date).toBeInstanceOf(Date)
expect(value).toBe(TOTAL_DURATION)
expect(dayIndex).toBe(index + 1)
}

expect(findByUserAndBetweenDatesSpy).toHaveBeenCalledTimes(DAYS)
})
Expand Down
28 changes: 18 additions & 10 deletions src/modules/reports/reports.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ReportsTotalItemsQuery,
ReportsListeningQuery,
} from './router/dtos'
import { ListeningDaysDocument } from './router/docs'

import { HistoryTracksRepository } from '@modules/history/tracks'
import type { User } from '@modules/users'
Expand All @@ -23,19 +24,22 @@ export class ReportsService {
const timeRangeTimestamp = before.getTime() - after.getTime()
const timeRangeDays = Math.floor(timeRangeTimestamp / (1000 * 60 * 60 * 24))

const listeningDaysObject: Record<number, number> = {}
const listeningDaysArray: ListeningDaysDocument[] = []

for (let index = 0; index < timeRangeDays; index++) {
const afterParam = new Date(after.getTime() + 1000 * 60 * 60 * 24 * index)
const beforeParam = new Date(afterParam.getTime() + 1000 * 60 * 60 * 24)

if (measurement === StatsMeasurement.PLAYS) {
listeningDaysObject[index + 1] =
await this.historyTracksRepository.countByUserAndBetweenDates(
listeningDaysArray.push({
date: afterParam,
dayIndex: index + 1,
value: await this.historyTracksRepository.countByUserAndBetweenDates(
user.id,
afterParam,
beforeParam
)
),
})
} else {
const historyTracks =
await this.historyTracksRepository.findByUserAndBetweenDates(
Expand All @@ -49,15 +53,19 @@ export class ReportsService {

const tracksDurations = historyTracks.map(({ track }) => track.duration)

listeningDaysObject[index + 1] = tracksDurations.reduce(
(previousDuration, currentDuration) =>
previousDuration + currentDuration,
0
)
listeningDaysArray.push({
date: afterParam,
dayIndex: index + 1,
value: tracksDurations.reduce(
(previousDuration, currentDuration) =>
previousDuration + currentDuration,
0
),
})
}
}

return listeningDaysObject
return listeningDaysArray
}

async getListeningHours(
Expand Down
54 changes: 41 additions & 13 deletions src/modules/reports/router/reports.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,55 @@ describe('ReportsController', () => {

describe('ListeningCharts', () => {
describe('getListeningDays', () => {
const listeningDaysObject = {
1: 10,
2: 10,
3: 10,
4: 10,
5: 10,
6: 10,
7: 10,
}
const listeningDaysArray = [
{
date: new Date('2024-08-22T08:59:38.340Z'),
dayIndex: 1,
value: 10,
},
{
date: new Date('2024-08-23T08:59:38.340Z'),
dayIndex: 2,
value: 10,
},
{
date: new Date('2024-08-24T08:59:38.340Z'),
dayIndex: 3,
value: 10,
},
{
date: new Date('2024-08-25T08:59:38.340Z'),
dayIndex: 4,
value: 10,
},
{
date: new Date('2024-08-26T08:59:38.340Z'),
dayIndex: 5,
value: 10,
},
{
date: new Date('2024-08-27T08:59:38.340Z'),
dayIndex: 6,
value: 10,
},
{
date: new Date('2024-08-28T08:59:38.340Z'),
dayIndex: 7,
value: 10,
},
]

test('should get listening days with plays measurement', async () => {
const getListeningDaysSpy = vi
.spyOn(reportsService, 'getListeningDays')
.mockResolvedValue(listeningDaysObject)
.mockResolvedValue(listeningDaysArray)

expect(
await reportsController.getListeningDays(userMock, {
before,
after,
})
).toEqual(listeningDaysObject)
).toEqual(listeningDaysArray)

expect(getListeningDaysSpy).toHaveBeenCalledWith(
{
Expand All @@ -94,15 +122,15 @@ describe('ReportsController', () => {
test('should get listening days with play time measurement', async () => {
const getListeningDaysSpy = vi
.spyOn(reportsService, 'getListeningDays')
.mockResolvedValue(listeningDaysObject)
.mockResolvedValue(listeningDaysArray)

expect(
await reportsController.getListeningDays(userMock, {
before,
after,
measurement: StatsMeasurement.PLAY_TIME,
})
).toEqual(listeningDaysObject)
).toEqual(listeningDaysArray)

expect(getListeningDaysSpy).toHaveBeenCalledWith(
{
Expand Down
6 changes: 5 additions & 1 deletion src/modules/reports/router/reports.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger'
import { ReportsService } from '../reports.service'

import { ReportsListeningQuery, ReportsTotalItemsQuery } from './dtos'
import { TotalItemsDocument } from './docs'
import { ListeningDaysDocument, TotalItemsDocument } from './docs'
import {
ApiReportsListeningQuery,
ApiReportsTotalItemsQuery,
Expand Down Expand Up @@ -40,6 +40,10 @@ export class ReportsController {
description:
"Getting user's listening days, used for creating charts (cached).",
})
@ApiOkResponse({
description: MANY_SUCCESSFULLY_RETRIEVED('listening days'),
type: [ListeningDaysDocument],
})
@ApiReportsListeningQuery()
getListeningDays(
@RequestUser() user: User,
Expand Down

0 comments on commit 2f3e52c

Please sign in to comment.