]>
Commit | Line | Data |
---|---|---|
74d63469 GR |
1 | import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
2 | import { AccountModel } from '../account/account' | |
b876eaf1 | 3 | import { ScopeNames as VideoScopeNames, VideoModel } from './video' |
74d63469 GR |
4 | import { VideoChangeOwnership, VideoChangeOwnershipStatus } from '../../../shared/models/videos' |
5 | import { getSort } from '../utils' | |
1ca9f7c3 | 6 | import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/typings/models/video/video-change-ownership' |
453e83ea | 7 | import * as Bluebird from 'bluebird' |
74d63469 GR |
8 | |
9 | enum ScopeNames { | |
b876eaf1 C |
10 | WITH_ACCOUNTS = 'WITH_ACCOUNTS', |
11 | WITH_VIDEO = 'WITH_VIDEO' | |
74d63469 GR |
12 | } |
13 | ||
14 | @Table({ | |
15 | tableName: 'videoChangeOwnership', | |
16 | indexes: [ | |
17 | { | |
b876eaf1 | 18 | fields: [ 'videoId' ] |
74d63469 GR |
19 | }, |
20 | { | |
b876eaf1 | 21 | fields: [ 'initiatorAccountId' ] |
74d63469 GR |
22 | }, |
23 | { | |
b876eaf1 | 24 | fields: [ 'nextOwnerAccountId' ] |
74d63469 GR |
25 | } |
26 | ] | |
27 | }) | |
3acc5084 | 28 | @Scopes(() => ({ |
b876eaf1 | 29 | [ScopeNames.WITH_ACCOUNTS]: { |
74d63469 GR |
30 | include: [ |
31 | { | |
3acc5084 | 32 | model: AccountModel, |
74d63469 GR |
33 | as: 'Initiator', |
34 | required: true | |
35 | }, | |
36 | { | |
3acc5084 | 37 | model: AccountModel, |
74d63469 GR |
38 | as: 'NextOwner', |
39 | required: true | |
b876eaf1 C |
40 | } |
41 | ] | |
42 | }, | |
43 | [ScopeNames.WITH_VIDEO]: { | |
44 | include: [ | |
74d63469 | 45 | { |
b876eaf1 C |
46 | model: VideoModel.scope([ VideoScopeNames.WITH_THUMBNAILS, VideoScopeNames.WITH_FILES ]), |
47 | required: true | |
74d63469 | 48 | } |
3acc5084 | 49 | ] |
74d63469 | 50 | } |
3acc5084 | 51 | })) |
74d63469 GR |
52 | export class VideoChangeOwnershipModel extends Model<VideoChangeOwnershipModel> { |
53 | @CreatedAt | |
54 | createdAt: Date | |
55 | ||
56 | @UpdatedAt | |
57 | updatedAt: Date | |
58 | ||
59 | @AllowNull(false) | |
60 | @Column | |
61 | status: VideoChangeOwnershipStatus | |
62 | ||
63 | @ForeignKey(() => AccountModel) | |
64 | @Column | |
65 | initiatorAccountId: number | |
66 | ||
67 | @BelongsTo(() => AccountModel, { | |
68 | foreignKey: { | |
69 | name: 'initiatorAccountId', | |
70 | allowNull: false | |
71 | }, | |
72 | onDelete: 'cascade' | |
73 | }) | |
74 | Initiator: AccountModel | |
75 | ||
76 | @ForeignKey(() => AccountModel) | |
77 | @Column | |
78 | nextOwnerAccountId: number | |
79 | ||
80 | @BelongsTo(() => AccountModel, { | |
81 | foreignKey: { | |
82 | name: 'nextOwnerAccountId', | |
83 | allowNull: false | |
84 | }, | |
85 | onDelete: 'cascade' | |
86 | }) | |
87 | NextOwner: AccountModel | |
88 | ||
89 | @ForeignKey(() => VideoModel) | |
90 | @Column | |
91 | videoId: number | |
92 | ||
93 | @BelongsTo(() => VideoModel, { | |
94 | foreignKey: { | |
95 | allowNull: false | |
96 | }, | |
97 | onDelete: 'cascade' | |
98 | }) | |
99 | Video: VideoModel | |
100 | ||
101 | static listForApi (nextOwnerId: number, start: number, count: number, sort: string) { | |
5cf84858 | 102 | const query = { |
74d63469 GR |
103 | offset: start, |
104 | limit: count, | |
105 | order: getSort(sort), | |
106 | where: { | |
107 | nextOwnerAccountId: nextOwnerId | |
108 | } | |
5cf84858 C |
109 | } |
110 | ||
b876eaf1 C |
111 | return Promise.all([ |
112 | VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query), | |
453e83ea | 113 | VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll<MVideoChangeOwnershipFull>(query) |
b876eaf1 | 114 | ]).then(([ count, rows ]) => ({ total: count, data: rows })) |
74d63469 GR |
115 | } |
116 | ||
453e83ea | 117 | static load (id: number): Bluebird<MVideoChangeOwnershipFull> { |
b876eaf1 C |
118 | return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]) |
119 | .findByPk(id) | |
74d63469 GR |
120 | } |
121 | ||
1ca9f7c3 | 122 | toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership { |
74d63469 GR |
123 | return { |
124 | id: this.id, | |
125 | status: this.status, | |
126 | initiatorAccount: this.Initiator.toFormattedJSON(), | |
127 | nextOwnerAccount: this.NextOwner.toFormattedJSON(), | |
128 | video: { | |
129 | id: this.Video.id, | |
130 | uuid: this.Video.uuid, | |
131 | url: this.Video.url, | |
132 | name: this.Video.name | |
133 | }, | |
134 | createdAt: this.createdAt | |
135 | } | |
136 | } | |
137 | } |