import { DB } from '../../../generated/indexer' import { Channel } from '../../../generated/graphql-server/src/modules/channel/channel.model' import { Category } from '../../../generated/graphql-server/src/modules/category/category.model' import { KnownLicense } from '../../../generated/graphql-server/src/modules/known-license/known-license.model' import { UserDefinedLicense } from '../../../generated/graphql-server/src/modules/user-defined-license/user-defined-license.model' import { JoystreamMediaLocation } from '../../../generated/graphql-server/src/modules/joystream-media-location/joystream-media-location.model' import { HttpMediaLocation } from '../../../generated/graphql-server/src/modules/http-media-location/http-media-location.model' import { VideoMedia } from '../../../generated/graphql-server/src/modules/video-media/video-media.model' import { Video } from '../../../generated/graphql-server/src/modules/video/video.model' import { Language } from '../../../generated/graphql-server/src/modules/language/language.model' import { VideoMediaEncoding } from '../../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model' import { License } from '../../../generated/graphql-server/src/modules/license/license.model' import { MediaLocation } from '../../../generated/graphql-server/src/modules/media-location/media-location.model' import { ICategory, IChannel, IHttpMediaLocation, IJoystreamMediaLocation, IKnownLicense, ILanguage, ILicense, IMediaLocation, IReference, IUserDefinedLicense, IVideo, IVideoMedia, IVideoMediaEncoding, IWhereCond, } from '../../types' function getEntityIdFromReferencedField(ref: IReference, entityIdBeforeTransaction: number): string { const { entityId, existing } = ref const id = existing ? entityId : entityIdBeforeTransaction + entityId return id.toString() } async function updateMediaLocationEntityPropertyValues( db: DB, where: IWhereCond, props: IMediaLocation, entityIdBeforeTransaction: number ): Promise { const { httpMediaLocation, joystreamMediaLocation } = props const record = await db.get(MediaLocation, where) if (record === undefined) throw Error(`MediaLocation entity not found: ${where.where.id}`) if (httpMediaLocation) { const id = getEntityIdFromReferencedField(httpMediaLocation, entityIdBeforeTransaction) record.httpMediaLocation = await db.get(HttpMediaLocation, { where: { id } }) } if (joystreamMediaLocation) { const id = getEntityIdFromReferencedField(joystreamMediaLocation, entityIdBeforeTransaction) record.joystreamMediaLocation = await db.get(JoystreamMediaLocation, { where: { id } }) } await db.save(record) } async function updateLicenseEntityPropertyValues( db: DB, where: IWhereCond, props: ILicense, entityIdBeforeTransaction: number ): Promise { const record = await db.get(License, where) if (record === undefined) throw Error(`License entity not found: ${where.where.id}`) const { knownLicense, userDefinedLicense } = props if (knownLicense) { const id = getEntityIdFromReferencedField(knownLicense, entityIdBeforeTransaction) record.knownLicense = await db.get(KnownLicense, { where: { id } }) } if (userDefinedLicense) { const id = getEntityIdFromReferencedField(userDefinedLicense, entityIdBeforeTransaction) record.userdefinedLicense = await db.get(UserDefinedLicense, { where: { id } }) } await db.save(record) } async function updateCategoryEntityPropertyValues(db: DB, where: IWhereCond, props: ICategory): Promise { const record = await db.get(Category, where) if (record === undefined) throw Error(`Entity not found: ${where.where.id}`) Object.assign(record, props) await db.save(record) } async function updateChannelEntityPropertyValues( db: DB, where: IWhereCond, props: IChannel, entityIdBeforeTransaction: number ): Promise { const record = await db.get(Channel, where) if (record === undefined) throw Error(`Entity not found: ${where.where.id}`) let lang: Language | undefined if (props.language !== undefined) { const id = getEntityIdFromReferencedField(props.language, entityIdBeforeTransaction) lang = await db.get(Language, { where: { id } }) if (lang === undefined) throw Error(`Language entity not found: ${id}`) props.language = undefined } Object.assign(record, props) record.language = lang || record.language await db.save(record) } async function updateVideoMediaEntityPropertyValues( db: DB, where: IWhereCond, props: IVideoMedia, entityIdBeforeTransaction: number ): Promise { const record = await db.get(VideoMedia, where) if (record === undefined) throw Error(`Entity not found: ${where.where.id}`) let enco: VideoMediaEncoding | undefined let mediaLoc: MediaLocation | undefined const { encoding, location } = props if (encoding) { const id = getEntityIdFromReferencedField(encoding, entityIdBeforeTransaction) enco = await db.get(VideoMediaEncoding, { where: { id } }) if (enco === undefined) throw Error(`VideoMediaEncoding entity not found: ${id}`) props.encoding = undefined } if (location) { const id = getEntityIdFromReferencedField(location, entityIdBeforeTransaction) mediaLoc = await db.get(MediaLocation, { where: { id } }) if (!mediaLoc) throw Error(`MediaLocation entity not found: ${id}`) props.location = undefined } Object.assign(record, props) record.encoding = enco || record.encoding record.location = mediaLoc || record.location await db.save(record) } async function updateVideoEntityPropertyValues( db: DB, where: IWhereCond, props: IVideo, entityIdBeforeTransaction: number ): Promise { const record = await db.get