]>
Commit | Line | Data |
---|---|---|
d8465018 C |
1 | import * as Sequelize from 'sequelize' |
2 | ||
3 | import { addMethodsToModel } from '../utils' | |
d7d5611c | 4 | import { VideoChannelShareAttributes, VideoChannelShareInstance, VideoChannelShareMethods } from './video-channel-share-interface' |
d8465018 C |
5 | |
6 | let VideoChannelShare: Sequelize.Model<VideoChannelShareInstance, VideoChannelShareAttributes> | |
d7d5611c | 7 | let loadAccountsByShare: VideoChannelShareMethods.LoadAccountsByShare |
4e50b6a1 | 8 | let load: VideoChannelShareMethods.Load |
d8465018 C |
9 | |
10 | export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | |
11 | VideoChannelShare = sequelize.define<VideoChannelShareInstance, VideoChannelShareAttributes>('VideoChannelShare', | |
12 | { }, | |
13 | { | |
14 | indexes: [ | |
15 | { | |
16 | fields: [ 'accountId' ] | |
17 | }, | |
18 | { | |
19 | fields: [ 'videoChannelId' ] | |
20 | } | |
21 | ] | |
22 | } | |
23 | ) | |
24 | ||
25 | const classMethods = [ | |
d7d5611c | 26 | associate, |
4e50b6a1 | 27 | load, |
d7d5611c | 28 | loadAccountsByShare |
d8465018 C |
29 | ] |
30 | addMethodsToModel(VideoChannelShare, classMethods) | |
31 | ||
32 | return VideoChannelShare | |
33 | } | |
34 | ||
35 | // ------------------------------ METHODS ------------------------------ | |
36 | ||
37 | function associate (models) { | |
38 | VideoChannelShare.belongsTo(models.Account, { | |
39 | foreignKey: { | |
40 | name: 'accountId', | |
41 | allowNull: false | |
42 | }, | |
43 | onDelete: 'cascade' | |
44 | }) | |
45 | ||
46 | VideoChannelShare.belongsTo(models.VideoChannel, { | |
47 | foreignKey: { | |
48 | name: 'videoChannelId', | |
49 | allowNull: true | |
50 | }, | |
51 | onDelete: 'cascade' | |
52 | }) | |
53 | } | |
d7d5611c | 54 | |
25ed141c | 55 | load = function (accountId: number, videoChannelId: number, t: Sequelize.Transaction) { |
4e50b6a1 C |
56 | return VideoChannelShare.findOne({ |
57 | where: { | |
58 | accountId, | |
59 | videoChannelId | |
60 | }, | |
61 | include: [ | |
62 | VideoChannelShare['sequelize'].models.Account, | |
63 | VideoChannelShare['sequelize'].models.VideoChannel | |
25ed141c C |
64 | ], |
65 | transaction: t | |
4e50b6a1 C |
66 | }) |
67 | } | |
68 | ||
25ed141c | 69 | loadAccountsByShare = function (videoChannelId: number, t: Sequelize.Transaction) { |
d7d5611c C |
70 | const query = { |
71 | where: { | |
72 | videoChannelId | |
73 | }, | |
74 | include: [ | |
75 | { | |
76 | model: VideoChannelShare['sequelize'].models.Account, | |
77 | required: true | |
78 | } | |
25ed141c C |
79 | ], |
80 | transaction: t | |
d7d5611c C |
81 | } |
82 | ||
83 | return VideoChannelShare.findAll(query) | |
84 | .then(res => res.map(r => r.Account)) | |
85 | } |