aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/account-video-rate.ts
blob: d92834bbbd24152ef7b495dbf973e37ee23bc68c (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
/*
  Account rates per video.
*/
import { values } from 'lodash'
import * as Sequelize from 'sequelize'

import { VIDEO_RATE_TYPES } from '../../initializers'

import { addMethodsToModel } from '../utils'
import {
  AccountVideoRateInstance,
  AccountVideoRateAttributes,

  AccountVideoRateMethods
} from './account-video-rate-interface'

let AccountVideoRate: Sequelize.Model<AccountVideoRateInstance, AccountVideoRateAttributes>
let load: AccountVideoRateMethods.Load

export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
  AccountVideoRate = sequelize.define<AccountVideoRateInstance, AccountVideoRateAttributes>('AccountVideoRate',
    {
      type: {
        type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)),
        allowNull: false
      }
    },
    {
      indexes: [
        {
          fields: [ 'videoId', 'accountId' ],
          unique: true
        }
      ]
    }
  )

  const classMethods = [
    associate,

    load
  ]
  addMethodsToModel(AccountVideoRate, classMethods)

  return AccountVideoRate
}

// ------------------------------ STATICS ------------------------------

function associate (models) {
  AccountVideoRate.belongsTo(models.Video, {
    foreignKey: {
      name: 'videoId',
      allowNull: false
    },
    onDelete: 'CASCADE'
  })

  AccountVideoRate.belongsTo(models.Account, {
    foreignKey: {
      name: 'accountId',
      allowNull: false
    },
    onDelete: 'CASCADE'
  })
}

load = function (accountId: number, videoId: number, transaction: Sequelize.Transaction) {
  const options: Sequelize.FindOptions<AccountVideoRateAttributes> = {
    where: {
      accountId,
      videoId
    }
  }
  if (transaction) options.transaction = transaction

  return AccountVideoRate.findOne(options)
}