2023-07-05 01:14:44 +02:00
|
|
|
const { DataTypes, Model } = require('sequelize')
|
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
class PlaylistMediaItem extends Model {
|
|
|
|
constructor(values, options) {
|
|
|
|
super(values, options)
|
|
|
|
|
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.id
|
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.mediaItemId
|
|
|
|
/** @type {string} */
|
|
|
|
this.mediaItemType
|
|
|
|
/** @type {number} */
|
|
|
|
this.order
|
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.playlistId
|
|
|
|
/** @type {Date} */
|
|
|
|
this.createdAt
|
|
|
|
}
|
|
|
|
|
|
|
|
static removeByIds(playlistId, mediaItemId) {
|
|
|
|
return this.destroy({
|
|
|
|
where: {
|
|
|
|
playlistId,
|
|
|
|
mediaItemId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
getMediaItem(options) {
|
|
|
|
if (!this.mediaItemType) return Promise.resolve(null)
|
|
|
|
const mixinMethodName = `get${this.sequelize.uppercaseFirst(this.mediaItemType)}`
|
|
|
|
return this[mixinMethodName](options)
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
/**
|
|
|
|
* Initialize model
|
2024-05-29 00:24:02 +02:00
|
|
|
* @param {import('../Database').sequelize} sequelize
|
2023-08-16 23:38:48 +02:00
|
|
|
*/
|
|
|
|
static init(sequelize) {
|
2024-05-29 00:24:02 +02:00
|
|
|
super.init(
|
|
|
|
{
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true
|
|
|
|
},
|
2024-11-17 22:45:21 +01:00
|
|
|
mediaItemId: DataTypes.UUID,
|
2024-05-29 00:24:02 +02:00
|
|
|
mediaItemType: DataTypes.STRING,
|
|
|
|
order: DataTypes.INTEGER
|
2023-08-16 23:38:48 +02:00
|
|
|
},
|
2024-05-29 00:24:02 +02:00
|
|
|
{
|
|
|
|
sequelize,
|
|
|
|
timestamps: true,
|
|
|
|
updatedAt: false,
|
|
|
|
modelName: 'playlistMediaItem'
|
|
|
|
}
|
|
|
|
)
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
const { book, podcastEpisode, playlist } = sequelize.models
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
book.hasMany(PlaylistMediaItem, {
|
|
|
|
foreignKey: 'mediaItemId',
|
|
|
|
constraints: false,
|
|
|
|
scope: {
|
|
|
|
mediaItemType: 'book'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
PlaylistMediaItem.belongsTo(book, { foreignKey: 'mediaItemId', constraints: false })
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
podcastEpisode.hasOne(PlaylistMediaItem, {
|
|
|
|
foreignKey: 'mediaItemId',
|
|
|
|
constraints: false,
|
|
|
|
scope: {
|
|
|
|
mediaItemType: 'podcastEpisode'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
PlaylistMediaItem.belongsTo(podcastEpisode, { foreignKey: 'mediaItemId', constraints: false })
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2024-05-29 00:24:02 +02:00
|
|
|
PlaylistMediaItem.addHook('afterFind', (findResult) => {
|
2023-08-16 23:38:48 +02:00
|
|
|
if (!findResult) return
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
if (!Array.isArray(findResult)) findResult = [findResult]
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
for (const instance of findResult) {
|
|
|
|
if (instance.mediaItemType === 'book' && instance.book !== undefined) {
|
|
|
|
instance.mediaItem = instance.book
|
|
|
|
instance.dataValues.mediaItem = instance.dataValues.book
|
|
|
|
} else if (instance.mediaItemType === 'podcastEpisode' && instance.podcastEpisode !== undefined) {
|
|
|
|
instance.mediaItem = instance.podcastEpisode
|
|
|
|
instance.dataValues.mediaItem = instance.dataValues.podcastEpisode
|
|
|
|
}
|
|
|
|
// To prevent mistakes:
|
|
|
|
delete instance.book
|
|
|
|
delete instance.dataValues.book
|
|
|
|
delete instance.podcastEpisode
|
|
|
|
delete instance.dataValues.podcastEpisode
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
2023-08-16 23:38:48 +02:00
|
|
|
})
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
playlist.hasMany(PlaylistMediaItem, {
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
PlaylistMediaItem.belongsTo(playlist)
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 23:38:48 +02:00
|
|
|
module.exports = PlaylistMediaItem
|