Skip to content

Commit

Permalink
feat(modules): bring back caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Iqro-dev authored and Mnigos committed Jan 14, 2025
1 parent 2bf56fc commit 9db79eb
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 11 deletions.
8 changes: 7 additions & 1 deletion src/modules/history/router/history.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getQueueToken } from '@nestjs/bullmq'
import { DeepMockProxy, mockDeep } from 'vitest-mock-extended'
import { PaginateQuery } from 'nestjs-paginate'
import { MockInstance } from 'vitest'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { HistoryQueueEvents } from '../history.queue-events'

Expand Down Expand Up @@ -71,7 +72,12 @@ describe('HistoryController', () => {
},
},
],
}).compile()
})
.overrideInterceptor(CacheInterceptor)
.useValue({
intercept: vi.fn(),
})
.compile()

historyController = moduleRef.get(HistoryController)
historyQueue = moduleRef.get(getQueueToken(HISTORY_QUEUE))
Expand Down
5 changes: 4 additions & 1 deletion src/modules/history/router/history.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, UseGuards } from '@nestjs/common'
import { Controller, Get, UseGuards, UseInterceptors } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { Queue } from 'bullmq'
import {
Expand All @@ -8,6 +8,7 @@ import {
PaginatedSwaggerDocs,
paginate,
} from 'nestjs-paginate'
import { CacheInterceptor, CacheTTL } from '@nestjs/cache-manager'

import { HistoryQueueEvents } from '../history.queue-events'
import { InjectHistoryQueue } from '../decorators'
Expand All @@ -30,6 +31,8 @@ export const historyTracksPaginateConfig: PaginateConfig<HistoryTrack> = {
@Controller('users/:id/history')
@ApiTags('users/{id}/history')
@UseGuards(ValidateUserIdGuard)
@UseInterceptors(CacheInterceptor)
@CacheTTL(30)
@ApiAuth()
export class HistoryController {
constructor(
Expand Down
8 changes: 7 additions & 1 deletion src/modules/reports/router/reports.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Test, type TestingModule } from '@nestjs/testing'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { ReportsService } from '../reports.service'

Expand Down Expand Up @@ -42,7 +43,12 @@ describe('ReportsController', () => {
},
},
],
}).compile()
})
.overrideInterceptor(CacheInterceptor)
.useValue({
intercept: vi.fn(),
})
.compile()

reportsController = moduleRef.get(ReportsController)
reportsService = moduleRef.get(ReportsService)
Expand Down
10 changes: 9 additions & 1 deletion src/modules/reports/router/reports.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Controller, Get, Query, UseGuards } from '@nestjs/common'
import {
Controller,
Get,
Query,
UseGuards,
UseInterceptors,
} from '@nestjs/common'
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { ReportsService } from '../reports.service'

Expand Down Expand Up @@ -28,6 +35,7 @@ import { StatsMeasurement } from '@modules/stats/enums'
@Controller('/users/:id/reports')
@ApiTags('users/{id}/reports')
@UseGuards(ValidateUserIdGuard, TimeRangeGuard)
@UseInterceptors(CacheInterceptor)
@ApiAuth()
@ApiUser()
export class ReportsController {
Expand Down
8 changes: 7 additions & 1 deletion src/modules/stats/router/stats-rigtch.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing'
import { MockInstance } from 'vitest'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { StatsRigtchService } from '../stats-rigtch.service'
import { StatsMeasurement } from '../enums'
Expand Down Expand Up @@ -38,7 +39,12 @@ describe('StatsRigtchController', () => {
},
},
],
}).compile()
})
.overrideInterceptor(CacheInterceptor)
.useValue({
intercept: vi.fn(),
})
.compile()

statsRigtchController = moduleRef.get(StatsRigtchController)
statsRigtchService = moduleRef.get(StatsRigtchService)
Expand Down
10 changes: 9 additions & 1 deletion src/modules/stats/router/stats-rigtch.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Controller, Get, Query, UseGuards } from '@nestjs/common'
import {
Controller,
Get,
Query,
UseGuards,
UseInterceptors,
} from '@nestjs/common'
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { StatsMeasurement } from '../enums'
import { StatsRigtchService } from '../stats-rigtch.service'
Expand All @@ -23,6 +30,7 @@ import { ValidateUserIdGuard } from '@modules/users/guards'
@Controller('/users/:id/stats/rigtch')
@ApiTags('users/{id}/stats/rigtch')
@UseGuards(ValidateUserIdGuard, TimeRangeGuard)
@UseInterceptors(CacheInterceptor)
@ApiAuth()
@ApiStatsRigtchQuery()
@ApiUser()
Expand Down
8 changes: 7 additions & 1 deletion src/modules/stats/router/stats-spotify.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing'
import type { MockInstance } from 'vitest'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { StatsSpotifyController } from './stats-spotify.controller'

Expand Down Expand Up @@ -60,7 +61,12 @@ describe('UsersProfileController', () => {
},
},
],
}).compile()
})
.overrideInterceptor(CacheInterceptor)
.useValue({
intercept: vi.fn(),
})
.compile()

statsSpotifyController = moduleRef.get(StatsSpotifyController)
spotifyService = moduleRef.get(SpotifyService)
Expand Down
10 changes: 9 additions & 1 deletion src/modules/stats/router/stats-spotify.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Controller, Get, Query, UseGuards } from '@nestjs/common'
import {
Controller,
Get,
Query,
UseGuards,
UseInterceptors,
} from '@nestjs/common'
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger'
import { AccessToken } from '@spotify/web-api-ts-sdk'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { ApiAuth, RequestToken } from '@common/decorators'
import { TokenGuard } from '@common/guards'
Expand All @@ -18,6 +25,7 @@ import { TopItemQuery } from '@modules/users/dtos'
@Controller('/users/:id/stats/spotify')
@ApiTags('users/{id}/stats/spotify')
@UseGuards(ValidateUserIdGuard, TokenGuard)
@UseInterceptors(CacheInterceptor)
@ApiAuth()
@ApiUser()
export class StatsSpotifyController {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing'
import { MockInstance } from 'vitest'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { UsersRepository } from '../users.repository'

Expand Down Expand Up @@ -71,7 +72,12 @@ describe('UsersProfileController', () => {
},
},
],
}).compile()
})
.overrideInterceptor(CacheInterceptor)
.useValue({
intercept: vi.fn(),
})
.compile()

usersProfileController = moduleRef.get(UsersProfileController)
spotifyService = moduleRef.get(SpotifyService)
Expand Down
13 changes: 12 additions & 1 deletion src/modules/users/controllers/users-profile.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Controller, Get, Query, UseGuards } from '@nestjs/common'
import {
Controller,
Get,
Query,
UseGuards,
UseInterceptors,
} from '@nestjs/common'
import { ApiOperation, ApiOkResponse, ApiTags } from '@nestjs/swagger'
import { AccessToken } from '@spotify/web-api-ts-sdk'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { ApiItemQuery, ApiUser, RequestUser } from '../decorators'
import { LastItemQuery, TopItemQuery } from '../dtos'
Expand Down Expand Up @@ -59,6 +66,7 @@ export class UsersProfileController {
}

@Get('top/artists')
@UseInterceptors(CacheInterceptor)
@ApiOperation({
summary: "Getting user's top artists (cached).",
deprecated: true,
Expand Down Expand Up @@ -93,6 +101,7 @@ export class UsersProfileController {
}

@Get('top/tracks')
@UseInterceptors(CacheInterceptor)
@ApiOperation({
summary: "Getting user's top tracks (cached).",
deprecated: true,
Expand Down Expand Up @@ -127,6 +136,7 @@ export class UsersProfileController {
}

@Get('top/genres')
@UseInterceptors(CacheInterceptor)
@ApiOperation({
summary: "Getting user's top genres (cached).",
deprecated: true,
Expand All @@ -149,6 +159,7 @@ export class UsersProfileController {
}

@Get('analysis')
@UseInterceptors(CacheInterceptor)
@ApiOperation({
summary: "Getting user's analysis (cached).",
deprecated: true,
Expand Down
8 changes: 7 additions & 1 deletion src/modules/users/controllers/users.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing'
import { MockInstance } from 'vitest'
import { CacheInterceptor } from '@nestjs/cache-manager'

import { MeBody } from '../dtos'
import { UsersRepository } from '../users.repository'
Expand Down Expand Up @@ -66,7 +67,12 @@ describe('UsersController', () => {
},
},
],
}).compile()
})
.overrideInterceptor(CacheInterceptor)
.useValue({
intercept: vi.fn(),
})
.compile()

usersController = moduleRef.get(UsersController)
usersRepository = moduleRef.get(UsersRepository)
Expand Down

0 comments on commit 9db79eb

Please sign in to comment.