aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-channel-share.ts
blob: e47c0dae7b7e26d2bafc609b3db65b7c720b0047 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as Sequelize from 'sequelize'

import { addMethodsToModel } from '../utils'
import { VideoChannelShareAttributes, VideoChannelShareInstance, VideoChannelShareMethods } from './video-channel-share-interface'

let VideoChannelShare: Sequelize.Model<VideoChannelShareInstance, VideoChannelShareAttributes>
let loadAccountsByShare: VideoChannelShareMethods.LoadAccountsByShare
let load: VideoChannelShareMethods.Load

export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
  VideoChannelShare = sequelize.define<VideoChannelShareInstance, VideoChannelShareAttributes>('VideoChannelShare',
    { },
    {
      indexes: [
        {
          fields: [ 'accountId' ]
        },
        {
          fields: [ 'videoChannelId' ]
        }
      ]
    }
  )

  const classMethods = [
    associate,
    load,
    loadAccountsByShare
  ]
  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'
  })
}

load = function (accountId: number, videoChannelId: number) {
  return VideoChannelShare.findOne({
    where: {
      accountId,
      videoChannelId
    },
    include: [
      VideoChannelShare['sequelize'].models.Account,
      VideoChannelShare['sequelize'].models.VideoChannel
    ]
  })
}

loadAccountsByShare = function (videoChannelId: number) {
  const query = {
    where: {
      videoChannelId
    },
    include: [
      {
        model: VideoChannelShare['sequelize'].models.Account,
        required: true
      }
    ]
  }

  return VideoChannelShare.findAll(query)
    .then(res => res.map(r => r.Account))
}