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