diff --git a/src/now-playing/now-playing.service.ts b/src/now-playing/now-playing.service.ts index 63db931..45c4edb 100644 --- a/src/now-playing/now-playing.service.ts +++ b/src/now-playing/now-playing.service.ts @@ -50,16 +50,15 @@ export class NowPlayingService { let updatedCount = 0; for (const np of nowPlaying) { - const stationId = - this.recordSync.getStationIdByNowPlayingId(np.id); - if (!stationId) continue; + const mapping = this.recordSync.getStationByNowPlayingId(np.id); + if (!mapping) continue; const coverUrl = np.track.image600 ?? np.track.image200 ?? np.track.image100; const updated = await this.prisma.nowPlaying.upsert({ - where: { stationId }, + where: { stationId: mapping.dbId }, create: { - stationId, + stationId: mapping.dbId, song: np.track.song, artist: np.track.artist, coverUrl, @@ -71,7 +70,7 @@ export class NowPlayingService { }, }); - this.gateway.broadcastNowPlaying(stationId, { + this.gateway.broadcastNowPlaying(mapping.stationId.toString(), { song: np.track.song, artist: np.track.artist, coverUrl, diff --git a/src/now-playing/record-station-sync.service.ts b/src/now-playing/record-station-sync.service.ts index 62e6848..4e25e31 100644 --- a/src/now-playing/record-station-sync.service.ts +++ b/src/now-playing/record-station-sync.service.ts @@ -25,10 +25,15 @@ interface RecordNowPlayingItem { track: RecordTrack; } +export interface StationMapping { + dbId: string; + stationId: number; +} + @Injectable() export class RecordStationSyncService implements OnModuleInit { private readonly logger = new Logger(RecordStationSyncService.name); - private nowPlayingIdToStationId = new Map(); + private nowPlayingIdToStation = new Map(); constructor(private readonly prisma: PrismaService) {} @@ -78,7 +83,7 @@ export class RecordStationSyncService implements OnModuleInit { const ourStations = await this.prisma.station.findMany(); let matched = 0; - const newMap = new Map(); + const newMap = new Map(); for (let i = 0; i < recordStations.length; i++) { const rs = recordStations[i]; @@ -105,20 +110,20 @@ export class RecordStationSyncService implements OnModuleInit { `https://www.radiorecord.ru/upload/stations_images/${rs.prefix}_image600_colored_fill.png`, }, }); - newMap.set(np.id, match.id); + newMap.set(np.id, { dbId: match.id, stationId: match.stationId }); matched++; } } - this.nowPlayingIdToStationId = newMap; + this.nowPlayingIdToStation = newMap; this.logger.log( `Matched and updated ${matched} stations, mapped ${newMap.size} now-playing IDs`, ); return matched; } - getStationIdByNowPlayingId(nowPlayingId: number): string | undefined { - return this.nowPlayingIdToStationId.get(nowPlayingId); + getStationByNowPlayingId(nowPlayingId: number): StationMapping | undefined { + return this.nowPlayingIdToStation.get(nowPlayingId); } private normalizeStreamUrl(url?: string): string | null {