+
+ static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
+ return AccountVideoRateModel.sequelize.transaction(async t => {
+ const query = {
+ where: {
+ updatedAt: {
+ [Op.lt]: beforeUpdatedAt
+ },
+ videoId,
+ type
+ },
+ transaction: t
+ }
+
+ const deleted = await AccountVideoRateModel.destroy(query)
+
+ const options = {
+ transaction: t,
+ where: {
+ id: videoId
+ }
+ }
+
+ if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
+ else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
+ })
+ }
+
+ toFormattedJSON (): AccountVideoRate {
+ return {
+ video: this.Video.toFormattedJSON(),
+ rating: this.type
+ }
+ }