1 import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
2 import { AccountModel } from '../account/account'
3 import { ScopeNames as VideoScopeNames, VideoModel } from './video'
4 import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos'
5 import { getSort } from '../utils'
6 import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/typings/models/video/video-change-ownership'
7 import * as Bluebird from 'bluebird'
10 WITH_ACCOUNTS = 'WITH_ACCOUNTS',
11 WITH_VIDEO = 'WITH_VIDEO'
15 tableName: 'videoChangeOwnership',
21 fields: [ 'initiatorAccountId' ]
24 fields: [ 'nextOwnerAccountId' ]
29 [ScopeNames.WITH_ACCOUNTS]: {
43 [ScopeNames.WITH_VIDEO]: {
46 model: VideoModel.scope([ VideoScopeNames.WITH_THUMBNAILS, VideoScopeNames.WITH_FILES ]),
52 export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel> {
61 status: VideoChangeOwnershipStatus
63 @ForeignKey(() => AccountModel)
65 initiatorAccountId: number
67 @BelongsTo(() => AccountModel, {
69 name: 'initiatorAccountId',
74 Initiator: AccountModel
76 @ForeignKey(() => AccountModel)
78 nextOwnerAccountId: number
80 @BelongsTo(() => AccountModel, {
82 name: 'nextOwnerAccountId',
87 NextOwner: AccountModel
89 @ForeignKey(() => VideoModel)
93 @BelongsTo(() => VideoModel, {
101 static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
105 order: getSort(sort),
107 nextOwnerAccountId: nextOwnerId
112 VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query),
113 VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll<MVideoChangeOwnershipFull>(query)
114 ]).then(([ count, rows ]) => ({ total: count, data: rows }))
117 static load (id: number): Bluebird<MVideoChangeOwnershipFull> {
118 return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ])
122 toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership {
126 initiatorAccount: this.Initiator.toFormattedJSON(),
127 nextOwnerAccount: this.NextOwner.toFormattedJSON(),
130 uuid: this.Video.uuid,
132 name: this.Video.name
134 createdAt: this.createdAt