From 32b58d38cbb989faa15ac172950d8d4b39f70d74 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 15 Jun 2024 08:20:05 +0000 Subject: [PATCH] refactor(server): build network/studio media without storing them in resource cache --- server/src/resources/company/entity.ts | 16 ++++++++ server/src/resources/company/mapper.ts | 4 +- server/src/resources/company/types.ts | 2 +- server/src/services/cache/cache-service.ts | 37 +++++++------------ server/src/services/cache/types.ts | 4 -- .../src/services/company/company-service.ts | 2 +- .../services/company/provider/tmdb/tmdb.ts | 4 +- 7 files changed, 35 insertions(+), 34 deletions(-) diff --git a/server/src/resources/company/entity.ts b/server/src/resources/company/entity.ts index 385ed460..65c62b34 100644 --- a/server/src/resources/company/entity.ts +++ b/server/src/resources/company/entity.ts @@ -21,6 +21,22 @@ export class CompanyEntity extends BaseEntity { return new CompanyEntity({ id, props }); } + get artwork(): { logo?: { url: string; source: string } } { + return this.props.artwork; + } + + get name(): string { + return this.props.name; + } + + get providers(): { tmdbId: number } { + return this.props.providers; + } + + get source(): string { + return this.props.source; + } + /** * Validates the Company entity. */ diff --git a/server/src/resources/company/mapper.ts b/server/src/resources/company/mapper.ts index 31e8f2cf..04dd32e5 100644 --- a/server/src/resources/company/mapper.ts +++ b/server/src/resources/company/mapper.ts @@ -37,7 +37,7 @@ export class CompanyMapper props: { name: record.name, artwork: record.artwork, - provider: record.provider, + providers: record.provider, source: record.source, }, createdAt: record.createdAt, @@ -53,7 +53,7 @@ export class CompanyMapper toResponse(entity: CompanyEntity): CompanyResponseProps { const copy = entity.getProps(); return { - id: copy.provider.tmdbId, + id: copy.providers.tmdbId, name: copy.name, logo_path: copy.artwork.logo?.url || '', }; diff --git a/server/src/resources/company/types.ts b/server/src/resources/company/types.ts index 12dbc076..4a8a1e47 100644 --- a/server/src/resources/company/types.ts +++ b/server/src/resources/company/types.ts @@ -11,7 +11,7 @@ export type CompanyProps = { source: string; }; }; - provider: { + providers: { tmdbId: number; }; source: string; diff --git a/server/src/services/cache/cache-service.ts b/server/src/services/cache/cache-service.ts index a9446dec..f7d27913 100644 --- a/server/src/services/cache/cache-service.ts +++ b/server/src/services/cache/cache-service.ts @@ -79,31 +79,28 @@ export class CacheService { this.networkService.getNetworks(), this.companyService.getCompanies(), ]); - const [showDiscovery, movieDiscovery] = await Bluebird.all([ - Bluebird.map(networks, async (network) => - this.showService.getDiscover({ - filterByNetworkId: network.providers.tmdbId, - }), + await Bluebird.all([ + Bluebird.all( + networks.map(async (network) => + this.showService.getDiscover({ + filterByNetworkId: network.providers.tmdbId, + }), + ), ), - Bluebird.map(companies, async (company) => - this.movieService.getDiscover({ - filterByCompanyId: parseInt(company.id, 10), - }), + Bluebird.all( + companies.map(async (company) => + this.movieService.getDiscover({ + filterByCompanyId: company.providers.tmdbId, + }), + ), ), ]); - return { movies: movies.map((movie) => movie.getProps()), shows: shows.map((show) => show.getProps()), people: people.map((person) => person.getProps()), networks: networks.map((network) => network.getProps()), companies: companies.map((company) => company.getProps()), - showDiscovery: showDiscovery - .flat() - .map((show) => show.getProps()), - movieDiscovery: movieDiscovery - .flat() - .map((movie) => movie.getProps()), }; }, this.defaultCacheTTL, @@ -118,12 +115,6 @@ export class CacheService { companies: results.companies.map((company) => CompanyEntity.create(company), ), - showDiscovery: results.showDiscovery.map((show) => - ShowEntity.create(show), - ), - movieDiscovery: results.movieDiscovery.map((movie) => - MovieEntity.create(movie), - ), }; } catch (error) { this.logger.error({ error }, 'Error storing trending data'); @@ -133,8 +124,6 @@ export class CacheService { people: [], networks: [], companies: [], - showDiscovery: [], - movieDiscovery: [], }; } } diff --git a/server/src/services/cache/types.ts b/server/src/services/cache/types.ts index 96a82f15..2f1f7d19 100644 --- a/server/src/services/cache/types.ts +++ b/server/src/services/cache/types.ts @@ -15,8 +15,6 @@ export type CommonResourcesCacheResponse = { people: PersonProps[]; networks: NetworkProps[]; companies: CompanyProps[]; - showDiscovery: ShowProps[]; - movieDiscovery: MovieProps[]; }; /** @@ -28,6 +26,4 @@ export type CommonResourcesResponse = { people: PersonEntity[]; networks: NetworkEntity[]; companies: CompanyEntity[]; - showDiscovery: ShowEntity[]; - movieDiscovery: MovieEntity[]; }; diff --git a/server/src/services/company/company-service.ts b/server/src/services/company/company-service.ts index 1230db2d..1265481e 100644 --- a/server/src/services/company/company-service.ts +++ b/server/src/services/company/company-service.ts @@ -40,7 +40,7 @@ export class CompanyService { ); return companies.map((company) => { this.logger.debug( - { companyId: company.provider.tmdbId, name: company.name }, + { companyId: company.providers.tmdbId, name: company.name }, `got company details`, ); return CompanyEntity.create(company); diff --git a/server/src/services/company/provider/tmdb/tmdb.ts b/server/src/services/company/provider/tmdb/tmdb.ts index ebf4fdc1..d58a23fc 100644 --- a/server/src/services/company/provider/tmdb/tmdb.ts +++ b/server/src/services/company/provider/tmdb/tmdb.ts @@ -52,8 +52,8 @@ export class CompanyTmdbProvider implements CompanyDetailsProvider { } : undefined, }, - provider: { - tmdbId: response.id!, + providers: { + tmdbId: response.id || id, }, source: 'tmdb', };