schedule-video-update.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Sequelize, Table, UpdatedAt } from 'sequelize-typescript'
  2. import { ScopeNames as VideoScopeNames, VideoModel } from './video'
  3. import { VideoPrivacy } from '../../../shared/models/videos'
  4. import { Transaction } from 'sequelize'
  5. @Table({
  6. tableName: 'scheduleVideoUpdate',
  7. indexes: [
  8. {
  9. fields: [ 'videoId' ],
  10. unique: true
  11. },
  12. {
  13. fields: [ 'updateAt' ]
  14. }
  15. ]
  16. })
  17. export class ScheduleVideoUpdateModel extends Model<ScheduleVideoUpdateModel> {
  18. @AllowNull(false)
  19. @Default(null)
  20. @Column
  21. updateAt: Date
  22. @AllowNull(true)
  23. @Default(null)
  24. @Column
  25. privacy: VideoPrivacy.PUBLIC | VideoPrivacy.UNLISTED
  26. @CreatedAt
  27. createdAt: Date
  28. @UpdatedAt
  29. updatedAt: Date
  30. @ForeignKey(() => VideoModel)
  31. @Column
  32. videoId: number
  33. @BelongsTo(() => VideoModel, {
  34. foreignKey: {
  35. allowNull: false
  36. },
  37. onDelete: 'cascade'
  38. })
  39. Video: VideoModel
  40. static areVideosToUpdate () {
  41. const query = {
  42. logging: false,
  43. attributes: [ 'id' ],
  44. where: {
  45. updateAt: {
  46. [Sequelize.Op.lte]: new Date()
  47. }
  48. }
  49. }
  50. return ScheduleVideoUpdateModel.findOne(query)
  51. .then(res => !!res)
  52. }
  53. static listVideosToUpdate (t: Transaction) {
  54. const query = {
  55. where: {
  56. updateAt: {
  57. [Sequelize.Op.lte]: new Date()
  58. }
  59. },
  60. include: [
  61. {
  62. model: VideoModel.scope(
  63. [
  64. VideoScopeNames.WITH_FILES,
  65. VideoScopeNames.WITH_ACCOUNT_DETAILS
  66. ]
  67. )
  68. }
  69. ],
  70. transaction: t
  71. }
  72. return ScheduleVideoUpdateModel.findAll(query)
  73. }
  74. static deleteByVideoId (videoId: number, t: Transaction) {
  75. const query = {
  76. where: {
  77. videoId
  78. },
  79. transaction: t
  80. }
  81. return ScheduleVideoUpdateModel.destroy(query)
  82. }
  83. toFormattedJSON () {
  84. return {
  85. updateAt: this.updateAt,
  86. privacy: this.privacy || undefined
  87. }
  88. }
  89. }