Skip to content

Commit

Permalink
feat(modules/stats/rigtch): add offset query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnigos committed Sep 11, 2024
1 parent 5ea426c commit b2fe50a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export const ApiStatsRigtchQuery = () =>
required: false,
description: 'The amount of tracks to be returned.',
}),
ApiQuery({
name: 'offset',
type: Number,
required: false,
description: 'The offset of the items returned.',
}),
ApiQuery({
name: 'measurement',
type: String,
Expand Down
6 changes: 6 additions & 0 deletions src/modules/stats/router/dtos/stats-rigtch-query.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export class StatsRigtchQuery {
)
readonly limit?: number

@IsOptional()
@IsNumber()
@Max(100)
@Transform(({ value }: { value: number }) => (value > 100 ? 100 : value))
readonly offset?: number

@IsOptional()
@IsEnum(StatsMeasurement)
readonly measurement?: StatsMeasurement
Expand Down
21 changes: 14 additions & 7 deletions src/modules/stats/router/stats-rigtch.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { albumMock, artistMock, trackMock, userMock } from '@common/mocks'
import { UsersRepository } from '@modules/users'

describe('StatsRigtchController', () => {
const limit = 10
const offset = 0
const date = new Date()

let moduleRef: TestingModule
Expand Down Expand Up @@ -64,7 +66,6 @@ describe('StatsRigtchController', () => {
})

test('should get top tracks', async () => {
const limit = 10
const result = Array.from({ length: limit }, () => trackMock)

getTopTracksSpy.mockResolvedValue(result)
Expand All @@ -73,8 +74,9 @@ describe('StatsRigtchController', () => {
await statsRigtchController.getTopTracks(userMock, {
before: date,
after: date,
limit: 10,
limit,
measurement: StatsMeasurement.PLAYS,
offset,
})
).toEqual(result)

Expand All @@ -84,6 +86,7 @@ describe('StatsRigtchController', () => {
after: date,
limit,
measurement: StatsMeasurement.PLAYS,
offset,
},
userMock
)
Expand All @@ -98,7 +101,6 @@ describe('StatsRigtchController', () => {
})

test('should get top artists', async () => {
const limit = 10
const result = Array.from({ length: limit }, () => artistMock)

getTopArtistsSpy.mockResolvedValue(result)
Expand All @@ -107,8 +109,9 @@ describe('StatsRigtchController', () => {
await statsRigtchController.getTopArtists(userMock, {
before: date,
after: date,
limit: 10,
limit,
measurement: StatsMeasurement.PLAYS,
offset,
})
).toEqual(result)

Expand All @@ -117,6 +120,7 @@ describe('StatsRigtchController', () => {
before: date,
after: date,
limit,
offset,
measurement: StatsMeasurement.PLAYS,
},
userMock
Expand All @@ -132,7 +136,6 @@ describe('StatsRigtchController', () => {
})

test('should get top albums', async () => {
const limit = 10
const result = Array.from({ length: limit }, () => albumMock)

getTopAlbumsSpy.mockResolvedValue(result)
Expand All @@ -141,7 +144,8 @@ describe('StatsRigtchController', () => {
await statsRigtchController.getTopAlbums(userMock, {
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAYS,
})
).toEqual(result)
Expand All @@ -151,6 +155,7 @@ describe('StatsRigtchController', () => {
before: date,
after: date,
limit,
offset,
measurement: StatsMeasurement.PLAYS,
},
userMock
Expand All @@ -175,7 +180,8 @@ describe('StatsRigtchController', () => {
await statsRigtchController.getTopGenres(userMock, {
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAYS,
})
).toEqual(result)
Expand All @@ -185,6 +191,7 @@ describe('StatsRigtchController', () => {
before: date,
after: date,
limit,
offset,
measurement: StatsMeasurement.PLAYS,
},
userMock
Expand Down
8 changes: 8 additions & 0 deletions src/modules/stats/router/stats-rigtch.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class StatsRigtchController {
before = new Date(),
after,
limit = 10,
offset = 0,
measurement = StatsMeasurement.PLAYS,
}: StatsRigtchQuery
) {
Expand All @@ -63,6 +64,7 @@ export class StatsRigtchController {
after,
limit,
measurement,
offset,
},
user
)
Expand All @@ -85,6 +87,7 @@ export class StatsRigtchController {
before = new Date(),
after,
limit = 10,
offset = 0,
measurement = StatsMeasurement.PLAYS,
}: StatsRigtchQuery
) {
Expand All @@ -94,6 +97,7 @@ export class StatsRigtchController {
after,
limit,
measurement,
offset,
},
user
)
Expand All @@ -116,6 +120,7 @@ export class StatsRigtchController {
before = new Date(),
after,
limit = 10,
offset = 0,
measurement = StatsMeasurement.PLAYS,
}: StatsRigtchQuery
) {
Expand All @@ -125,6 +130,7 @@ export class StatsRigtchController {
after,
limit,
measurement,
offset,
},
user
)
Expand All @@ -147,6 +153,7 @@ export class StatsRigtchController {
before = new Date(),
after,
limit = 10,
offset = 0,
measurement = StatsMeasurement.PLAYS,
}: StatsRigtchQuery
) {
Expand All @@ -156,6 +163,7 @@ export class StatsRigtchController {
after,
limit,
measurement,
offset,
},
user
)
Expand Down
66 changes: 50 additions & 16 deletions src/modules/stats/stats-rigtch.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import type { Track } from '@modules/items/tracks'
vi.mock('@common/utils')

describe('StatsRigtchService', () => {
const limit = 10
const offset = 0
const date = new Date()

let moduleRef: TestingModule
Expand Down Expand Up @@ -109,7 +111,8 @@ describe('StatsRigtchService', () => {
{
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAYS,
},
userMock
Expand All @@ -127,7 +130,10 @@ describe('StatsRigtchService', () => {
date,
relations
)
expect(getMostFrequentItemsSpy).toHaveBeenCalled()
expect(getMostFrequentItemsSpy).toHaveBeenCalledWith(
expect.anything(),
limit + offset
)
expect(getMostListenedItemsByDurationSpy).not.toHaveBeenCalled()
})

Expand All @@ -145,7 +151,8 @@ describe('StatsRigtchService', () => {
{
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAY_TIME,
},
userMock
Expand All @@ -163,7 +170,10 @@ describe('StatsRigtchService', () => {
date,
relations
)
expect(getMostListenedItemsByDurationSpy).toHaveBeenCalled()
expect(getMostListenedItemsByDurationSpy).toHaveBeenCalledWith(
expect.anything(),
limit + offset
)
})
})

Expand Down Expand Up @@ -196,7 +206,8 @@ describe('StatsRigtchService', () => {
{
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAYS,
},
userMock
Expand All @@ -214,7 +225,10 @@ describe('StatsRigtchService', () => {
date,
relations
)
expect(getMostFrequentItemsSpy).toHaveBeenCalled()
expect(getMostFrequentItemsSpy).toHaveBeenCalledWith(
expect.anything(),
limit + offset
)
})

test('should get top artists with play time measurement', async () => {
Expand All @@ -232,7 +246,8 @@ describe('StatsRigtchService', () => {
{
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAY_TIME,
},
userMock
Expand All @@ -250,7 +265,10 @@ describe('StatsRigtchService', () => {
date,
relations
)
expect(getMostListenedItemsByDurationSpy).toHaveBeenCalled()
expect(getMostListenedItemsByDurationSpy).toHaveBeenCalledWith(
expect.anything(),
limit + offset
)
})
})

Expand Down Expand Up @@ -285,7 +303,8 @@ describe('StatsRigtchService', () => {
{
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAYS,
},
userMock
Expand All @@ -303,7 +322,10 @@ describe('StatsRigtchService', () => {
date,
relations
)
expect(getMostFrequentItemsSpy).toHaveBeenCalled()
expect(getMostFrequentItemsSpy).toHaveBeenCalledWith(
expect.anything(),
limit + offset
)
})

test('should get top albums with play time measurement', async () => {
Expand All @@ -320,7 +342,8 @@ describe('StatsRigtchService', () => {
{
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAY_TIME,
},
userMock
Expand All @@ -338,7 +361,10 @@ describe('StatsRigtchService', () => {
date,
relations
)
expect(getMostListenedItemsByDurationSpy).toHaveBeenCalled()
expect(getMostListenedItemsByDurationSpy).toHaveBeenCalledWith(
expect.anything(),
limit + offset
)
})
})

Expand Down Expand Up @@ -372,7 +398,8 @@ describe('StatsRigtchService', () => {
{
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAYS,
},
userMock
Expand All @@ -390,7 +417,10 @@ describe('StatsRigtchService', () => {
date,
relations
)
expect(getMostFrequentItemsSpy).toHaveBeenCalled()
expect(getMostFrequentItemsSpy).toHaveBeenCalledWith(
expect.anything(),
limit + offset
)
})

test('should get top genres with play time measurement', async () => {
Expand All @@ -407,7 +437,8 @@ describe('StatsRigtchService', () => {
{
before: date,
after: date,
limit: 10,
limit,
offset,
measurement: StatsMeasurement.PLAY_TIME,
},
userMock
Expand All @@ -425,7 +456,10 @@ describe('StatsRigtchService', () => {
date,
relations
)
expect(getMostListenedItemsByDurationSpy).toHaveBeenCalled()
expect(getMostListenedItemsByDurationSpy).toHaveBeenCalledWith(
expect.anything(),
limit + offset
)
})
})
})
Loading

0 comments on commit b2fe50a

Please sign in to comment.