Skip to content

Commit

Permalink
refactor(api): extract TMDB package
Browse files Browse the repository at this point in the history
After this, we can extract the TMDB mock server into a separate
application.

Related to #83
  • Loading branch information
JoosepAlviste committed Jan 11, 2024
1 parent 1406f5b commit a426389
Show file tree
Hide file tree
Showing 29 changed files with 2,701 additions and 548 deletions.
2 changes: 2 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@sentry/node": "^7.56.0",
"@sentry/tracing": "^7.56.0",
"@serieslist/logger": "workspace:*",
"@serieslist/tmdb": "workspace:*",
"bcryptjs": "^2.4.3",
"bullmq": "^4.1.0",
"dataloader": "^2.2.2",
Expand Down Expand Up @@ -82,6 +83,7 @@
"@types/node": "^18.15.3",
"@types/pg": "^8.6.6",
"@vitest/coverage-v8": "^1.1.1",
"just-filter-object": "^3.2.0",
"kysely-codegen": "^0.10.0",
"madge": "^6.0.0",
"nock": "^13.3.1",
Expand Down
5 changes: 0 additions & 5 deletions apps/api/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ export const config = {
url: process.env.APP_URL!,
},

tmdb: {
url: process.env.TMDB_URL!,
apiToken: process.env.TMDB_API_TOKEN!,
},

sentry: {
enabled: process.env.NODE_ENV === 'production',
dsn: process.env.API_SENTRY_DSN!,
Expand Down
36 changes: 16 additions & 20 deletions apps/api/src/features/series/__tests__/series.schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {
tmdbSeasonFactory,
tmdbEpisodeFactory,
tmdbSeriesDetailsFactory,
mockTMDBDetailsRequest,
mockTMDBSearchRequest,
mockTMDBSeasonRequest,
} from '@serieslist/tmdb'
import { type NotWorthIt } from '@serieslist/type-utils'
import { parseISO, subDays } from 'date-fns'
import { type Selectable } from 'kysely'
import { nanoid } from 'nanoid'

import {
tmdbEpisodeFactory,
tmdbSeasonFactory,
tmdbSeriesDetailsFactory,
} from '#/features/tmdb'
import { userFactory } from '#/features/users'
import { type User } from '#/generated/db'
import { graphql } from '#/generated/gql'
Expand All @@ -26,12 +29,6 @@ import { seasonFactory } from '../season.factory'
import { seriesFactory } from '../series.factory'
import { userSeriesStatusFactory } from '../userSeriesStatus.factory'

import {
mockTMDbDetailsRequest,
mockTMDbSearchRequest,
mockTMDbSeasonRequest,
} from './scopes'

describe('features/series/series.schema', () => {
describe('seriesSearch query', () => {
const executeSearch = (keyword = 'testing') =>
Expand All @@ -56,7 +53,7 @@ describe('features/series/series.schema', () => {
})

it('searches series from the TMDB API', async () => {
const scope = mockTMDbSearchRequest('testing', {
const scope = mockTMDBSearchRequest('testing', {
results: [
tmdbSeriesDetailsFactory.build({
name: 'Testing Series',
Expand Down Expand Up @@ -84,7 +81,7 @@ describe('features/series/series.schema', () => {

it('returns an empty list if the TMDB request fails', async () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const scope = mockTMDbSearchRequest('testing', {
const scope = mockTMDBSearchRequest('testing', {
incorrect: 'response format',
} as NotWorthIt)

Expand All @@ -102,7 +99,7 @@ describe('features/series/series.schema', () => {
first_air_date: '2022-04-05',
poster_path: 'foo.jpg',
})
mockTMDbSearchRequest('testing', {
mockTMDBSearchRequest('testing', {
results: [tmdbSeries],
})

Expand All @@ -128,7 +125,7 @@ describe('features/series/series.schema', () => {
title,
})

mockTMDbSearchRequest('testing', {
mockTMDBSearchRequest('testing', {
results: [
tmdbSeriesDetailsFactory.build({
name: title,
Expand Down Expand Up @@ -196,7 +193,7 @@ describe('features/series/series.schema', () => {
syncedAt: null,
})

const scope = mockTMDbDetailsRequest(
const scope = mockTMDBDetailsRequest(
series.tmdbId,
tmdbSeriesDetailsFactory.build({
id: series.tmdbId,
Expand All @@ -222,7 +219,7 @@ describe('features/series/series.schema', () => {
syncedAt: subDays(new Date(Date.now()), 3),
})

const scope = mockTMDbDetailsRequest(
const scope = mockTMDBDetailsRequest(
series.tmdbId,
tmdbSeriesDetailsFactory.build(),
)
Expand All @@ -237,11 +234,10 @@ describe('features/series/series.schema', () => {
syncedAt: null,
})

mockTMDbDetailsRequest(
mockTMDBDetailsRequest(
series.tmdbId,
tmdbSeriesDetailsFactory.build({
id: series.tmdbId,
overview: 'Updated plot',
number_of_seasons: 1,
seasons: [
tmdbSeasonFactory.build({
Expand All @@ -259,7 +255,7 @@ describe('features/series/series.schema', () => {
episode_number: 2,
name: 'Episode 2',
})
const seriesSeasonScope = mockTMDbSeasonRequest(
const seriesSeasonScope = mockTMDBSeasonRequest(
{
tmdbId: series.tmdbId,
seasonNumber: 1,
Expand Down
55 changes: 27 additions & 28 deletions apps/api/src/features/series/__tests__/series.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {
tmdbNotFoundResponseFactory,
tmdbSeasonFactory,
mockTMDBDetailsRequest,
mockTMDBSeasonRequest,
} from '@serieslist/tmdb'
import { type LiterallyAnything } from '@serieslist/type-utils'
import { format, subDays } from 'date-fns'

import { seriesProgressFactory } from '#/features/seriesProgress'
import {
tmdbEpisodeFactory,
tmdbNotFoundResponseFactory,
tmdbSeasonFactory,
tmdbSeriesDetailsFactory,
} from '#/features/tmdb'
import { tmdbEpisodeFactory, tmdbSeriesDetailsFactory } from '#/features/tmdb'
import { userFactory } from '#/features/users'
import { db } from '#/lib/db'
import {
Expand All @@ -26,8 +27,6 @@ import {
syncSeriesDetails,
} from '../series.service'

import { mockTMDbDetailsRequest, mockTMDbSeasonRequest } from './scopes'

describe('features/series/series.service', () => {
describe('syncSeasonsAndEpisodes', () => {
it('imports new episodes into existing seasons', async () => {
Expand All @@ -41,10 +40,10 @@ describe('features/series/series.service', () => {
number: 1,
})

const newTMDbEpisode = tmdbEpisodeFactory.build({
const newTMDBEpisode = tmdbEpisodeFactory.build({
episode_number: 2,
})
mockTMDbSeasonRequest(
mockTMDBSeasonRequest(
{
tmdbId: series.tmdbId,
seasonNumber: 1,
Expand All @@ -56,7 +55,7 @@ describe('features/series/series.service', () => {
{},
{ transient: { savedEpisode: existingEpisode } },
),
newTMDbEpisode,
newTMDBEpisode,
],
}),
)
Expand All @@ -82,7 +81,7 @@ describe('features/series/series.service', () => {
.execute()
expect(episodes).toHaveLength(2)
expect(episodes[0]?.id).toBe(existingEpisode.id)
expect(episodes[1]?.tmdbId).toBe(newTMDbEpisode.id)
expect(episodes[1]?.tmdbId).toBe(newTMDBEpisode.id)
})

it('updates existing episodes', async () => {
Expand All @@ -105,7 +104,7 @@ describe('features/series/series.service', () => {
transient: { savedEpisode: s1e1 },
},
)
mockTMDbSeasonRequest(
mockTMDBSeasonRequest(
{
tmdbId: series.tmdbId,
seasonNumber: 1,
Expand Down Expand Up @@ -143,7 +142,7 @@ describe('features/series/series.service', () => {
number: 1,
})

mockTMDbSeasonRequest(
mockTMDBSeasonRequest(
{
tmdbId: series.tmdbId,
seasonNumber: 1,
Expand Down Expand Up @@ -207,10 +206,10 @@ describe('features/series/series.service', () => {
nextEpisodeId: null,
})

const newTMDbEpisode = tmdbEpisodeFactory.build({
const newTMDBEpisode = tmdbEpisodeFactory.build({
episode_number: 2,
})
mockTMDbSeasonRequest(
mockTMDBSeasonRequest(
{
tmdbId: series.tmdbId,
seasonNumber: 1,
Expand All @@ -219,7 +218,7 @@ describe('features/series/series.service', () => {
season_number: 1,
episodes: [
tmdbEpisodeFactory.build({}, { transient: { savedEpisode: s1e1 } }),
newTMDbEpisode,
newTMDBEpisode,
tmdbEpisodeFactory.build({
episode_number: 3,
}),
Expand All @@ -236,7 +235,7 @@ describe('features/series/series.service', () => {

const newEpisode = await db
.selectFrom('episode')
.where('tmdbId', '=', newTMDbEpisode.id)
.where('tmdbId', '=', newTMDBEpisode.id)
.selectAll()
.executeTakeFirstOrThrow()
const seriesProgresses = await db
Expand All @@ -254,7 +253,7 @@ describe('features/series/series.service', () => {
seasons: [{ season }],
} = await createSeriesWithEpisodesAndSeasons([0])

mockTMDbSeasonRequest(
mockTMDBSeasonRequest(
{ tmdbId: series.tmdbId, seasonNumber: season.number },
tmdbNotFoundResponseFactory.build(),
)
Expand All @@ -279,7 +278,7 @@ describe('features/series/series.service', () => {
seasons: [{ season }],
} = await createSeriesWithEpisodesAndSeasons([0])

mockTMDbSeasonRequest(
mockTMDBSeasonRequest(
{ tmdbId: series.tmdbId, seasonNumber: season.number },
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
{ not: 'correct' } as LiterallyAnything,
Expand Down Expand Up @@ -312,7 +311,7 @@ describe('features/series/series.service', () => {
],
} = await createSeriesWithEpisodesAndSeasons([2])

mockTMDbSeasonRequest(
mockTMDBSeasonRequest(
{ tmdbId: series.tmdbId, seasonNumber: season.number },
tmdbSeasonFactory.build({
season_number: season.number,
Expand Down Expand Up @@ -346,7 +345,7 @@ describe('features/series/series.service', () => {
it('does not sync seasons if there are none', async () => {
const series = await seriesFactory.create()

mockTMDbDetailsRequest(
mockTMDBDetailsRequest(
series.tmdbId,
tmdbSeriesDetailsFactory.build({
id: series.tmdbId,
Expand All @@ -364,7 +363,7 @@ describe('features/series/series.service', () => {
it('deletes a series if it has been deleted in TMDB', async () => {
const series = await seriesFactory.create()

mockTMDbDetailsRequest(series.tmdbId, tmdbNotFoundResponseFactory.build())
mockTMDBDetailsRequest(series.tmdbId, tmdbNotFoundResponseFactory.build())

const returnedSeries = await syncSeriesDetails({
ctx: createContext(),
Expand All @@ -385,7 +384,7 @@ describe('features/series/series.service', () => {
const series = await seriesFactory.create()

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
mockTMDbDetailsRequest(series.tmdbId, {
mockTMDBDetailsRequest(series.tmdbId, {
not: 'correct',
} as LiterallyAnything)

Expand Down Expand Up @@ -427,7 +426,7 @@ describe('features/series/series.service', () => {
syncedAt: subDays(new Date(), 8),
})

const scope = mockTMDbDetailsRequest(
const scope = mockTMDBDetailsRequest(
series.tmdbId,
tmdbSeriesDetailsFactory.build(
{
Expand Down Expand Up @@ -456,7 +455,7 @@ describe('features/series/series.service', () => {
syncedAt: null,
})

const scope = mockTMDbDetailsRequest(
const scope = mockTMDBDetailsRequest(
series.tmdbId,
tmdbSeriesDetailsFactory.build(
{},
Expand All @@ -474,7 +473,7 @@ describe('features/series/series.service', () => {
syncedAt: subDays(new Date(), 6),
})

const scope = mockTMDbDetailsRequest(
const scope = mockTMDBDetailsRequest(
series.tmdbId,
tmdbSeriesDetailsFactory.build(
{},
Expand All @@ -493,7 +492,7 @@ describe('features/series/series.service', () => {
})

const seriesToSyncScopes = seriesToSync.map((series) =>
mockTMDbDetailsRequest(
mockTMDBDetailsRequest(
series.tmdbId,
tmdbSeriesDetailsFactory.build(
{},
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/features/series/series.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const createMany = ({
return ctx.db.insertInto('series').values(series).returningAll().execute()
}

export const updateOneByTMDbId = ({
export const updateOneByTMDBId = ({
ctx,
tmdbId,
series,
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/features/series/series.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { tmdbService } from '@serieslist/tmdb'
import { addDays, isFuture, subDays } from 'date-fns'
import index from 'just-index'
import unique from 'just-unique'
import { type Insertable } from 'kysely'

import { seriesProgressService } from '#/features/seriesProgress'
import { tmdbService } from '#/features/tmdb'
import { type Season } from '#/generated/db'
import {
type SeriesUpdateStatusInput,
Expand Down Expand Up @@ -255,7 +255,7 @@ export const syncSeriesDetails = async ({
return null
}

const savedSeries = await seriesRepository.updateOneByTMDbId({
const savedSeries = await seriesRepository.updateOneByTMDBId({
ctx,
tmdbId,
series: {
Expand Down
2 changes: 0 additions & 2 deletions apps/api/src/features/tmdb/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './types'
export * as tmdbService from './tmdb.service'
export * from './tmdbSeriesDetails.factory'
Loading

0 comments on commit a426389

Please sign in to comment.