2021-05-12 07:09:04 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2018-08-29 09:26:25 -05:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Model, Table } from 'sequelize-typescript'
|
2021-05-12 07:09:04 -05:00
|
|
|
import { AttributesOnly } from '@shared/core-utils'
|
2018-08-29 09:26:25 -05:00
|
|
|
import { VideoModel } from './video'
|
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'videoView',
|
2019-04-26 02:16:43 -05:00
|
|
|
updatedAt: false,
|
2018-08-29 09:26:25 -05:00
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
2018-09-04 08:25:02 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'startDate' ]
|
2018-08-29 09:26:25 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2021-05-12 07:09:04 -05:00
|
|
|
export class VideoViewModel extends Model<Partial<AttributesOnly<VideoViewModel>>> {
|
2018-08-29 09:26:25 -05:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column(Sequelize.DATE)
|
|
|
|
startDate: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column(Sequelize.DATE)
|
|
|
|
endDate: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
views: number
|
|
|
|
|
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
Video: VideoModel
|
|
|
|
|
2019-04-11 10:33:36 -05:00
|
|
|
static removeOldRemoteViewsHistory (beforeDate: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
startDate: {
|
|
|
|
[Sequelize.Op.lt]: beforeDate
|
|
|
|
},
|
|
|
|
videoId: {
|
|
|
|
[Sequelize.Op.in]: Sequelize.literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoViewModel.destroy(query)
|
|
|
|
}
|
2018-08-29 09:26:25 -05:00
|
|
|
}
|