2017-11-15 17:56:21 +01:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
|
|
|
import { addMethodsToModel } from '../utils'
|
2017-11-16 15:55:01 +01:00
|
|
|
import { VideoChannelShareAttributes, VideoChannelShareInstance, VideoChannelShareMethods } from './video-channel-share-interface'
|
2017-11-15 17:56:21 +01:00
|
|
|
|
|
|
|
let VideoChannelShare: Sequelize.Model<VideoChannelShareInstance, VideoChannelShareAttributes>
|
2017-11-16 15:55:01 +01:00
|
|
|
let loadAccountsByShare: VideoChannelShareMethods.LoadAccountsByShare
|
2017-11-27 14:44:51 +01:00
|
|
|
let load: VideoChannelShareMethods.Load
|
2017-11-15 17:56:21 +01:00
|
|
|
|
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
VideoChannelShare = sequelize.define<VideoChannelShareInstance, VideoChannelShareAttributes>('VideoChannelShare',
|
|
|
|
{ },
|
|
|
|
{
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'accountId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'videoChannelId' ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const classMethods = [
|
2017-11-16 15:55:01 +01:00
|
|
|
associate,
|
2017-11-27 14:44:51 +01:00
|
|
|
load,
|
2017-11-16 15:55:01 +01:00
|
|
|
loadAccountsByShare
|
2017-11-15 17:56:21 +01:00
|
|
|
]
|
|
|
|
addMethodsToModel(VideoChannelShare, classMethods)
|
|
|
|
|
|
|
|
return VideoChannelShare
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
|
|
|
function associate (models) {
|
|
|
|
VideoChannelShare.belongsTo(models.Account, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'accountId',
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
|
|
|
|
VideoChannelShare.belongsTo(models.VideoChannel, {
|
|
|
|
foreignKey: {
|
|
|
|
name: 'videoChannelId',
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
}
|
2017-11-16 15:55:01 +01:00
|
|
|
|
2017-11-30 11:31:15 +01:00
|
|
|
load = function (accountId: number, videoChannelId: number, t: Sequelize.Transaction) {
|
2017-11-27 14:44:51 +01:00
|
|
|
return VideoChannelShare.findOne({
|
|
|
|
where: {
|
|
|
|
accountId,
|
|
|
|
videoChannelId
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
VideoChannelShare['sequelize'].models.Account,
|
|
|
|
VideoChannelShare['sequelize'].models.VideoChannel
|
2017-11-30 11:31:15 +01:00
|
|
|
],
|
|
|
|
transaction: t
|
2017-11-27 14:44:51 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-30 11:31:15 +01:00
|
|
|
loadAccountsByShare = function (videoChannelId: number, t: Sequelize.Transaction) {
|
2017-11-16 15:55:01 +01:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoChannelId
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: VideoChannelShare['sequelize'].models.Account,
|
|
|
|
required: true
|
|
|
|
}
|
2017-11-30 11:31:15 +01:00
|
|
|
],
|
|
|
|
transaction: t
|
2017-11-16 15:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return VideoChannelShare.findAll(query)
|
|
|
|
.then(res => res.map(r => r.Account))
|
|
|
|
}
|