import { MediaRequestStatus, MediaStatus } from '@server/constants/media'; import { getRepository } from '@server/datasource'; import Media from '@server/entity/Media'; import { MediaRequest } from '@server/entity/MediaRequest'; import type { EntitySubscriberInterface, UpdateEvent } from 'typeorm'; import { EventSubscriber } from 'typeorm'; @EventSubscriber() export class MediaSubscriber implements EntitySubscriberInterface { private async updateChildRequestStatus(event: Media, is4k: boolean) { const requestRepository = getRepository(MediaRequest); const requests = await requestRepository.find({ where: { media: { id: event.id } }, }); for (const request of requests) { if ( request.is4k === is4k && request.status === MediaRequestStatus.PENDING ) { request.status = MediaRequestStatus.APPROVED; await requestRepository.save(request); } } } public beforeUpdate(event: UpdateEvent): void { if (!event.entity) { return; } if ( event.entity.status === MediaStatus.AVAILABLE && event.databaseEntity.status === MediaStatus.PENDING ) { this.updateChildRequestStatus(event.entity as Media, false); } if ( event.entity.status4k === MediaStatus.AVAILABLE && event.databaseEntity.status4k === MediaStatus.PENDING ) { this.updateChildRequestStatus(event.entity as Media, true); } } public listenTo(): typeof Media { return Media; } }