2023-03-12 20:51:45 +01:00
|
|
|
const { DataTypes, Model } = require('sequelize')
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Polymorphic association: https://sequelize.org/docs/v6/advanced-association-concepts/polymorphic-associations/
|
|
|
|
* Book has many AudioBookmark. PodcastEpisode has many AudioBookmark.
|
|
|
|
*/
|
|
|
|
module.exports = (sequelize) => {
|
|
|
|
class AudioBookmark extends Model {
|
|
|
|
getMediaItem(options) {
|
|
|
|
if (!this.mediaItemType) return Promise.resolve(null)
|
2023-03-19 21:19:22 +01:00
|
|
|
const mixinMethodName = `get${sequelize.uppercaseFirst(this.mediaItemType)}`
|
2023-03-12 20:51:45 +01:00
|
|
|
return this[mixinMethodName](options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioBookmark.init({
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true
|
|
|
|
},
|
2023-03-19 21:19:22 +01:00
|
|
|
mediaItemId: DataTypes.UUIDV4,
|
2023-03-12 20:51:45 +01:00
|
|
|
mediaItemType: DataTypes.STRING,
|
|
|
|
title: DataTypes.STRING,
|
|
|
|
time: DataTypes.INTEGER
|
|
|
|
}, {
|
|
|
|
sequelize,
|
2023-03-19 21:19:22 +01:00
|
|
|
modelName: 'audioBookmark'
|
2023-03-12 20:51:45 +01:00
|
|
|
})
|
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
const { user, book, podcastEpisode } = sequelize.models
|
|
|
|
book.hasMany(AudioBookmark, {
|
|
|
|
foreignKey: 'mediaItemId',
|
2023-03-12 20:51:45 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-19 21:19:22 +01:00
|
|
|
mediaItemType: 'book'
|
2023-03-12 20:51:45 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-19 21:19:22 +01:00
|
|
|
AudioBookmark.belongsTo(book, { foreignKey: 'mediaItemId', constraints: false })
|
2023-03-12 20:51:45 +01:00
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
podcastEpisode.hasMany(AudioBookmark, {
|
|
|
|
foreignKey: 'mediaItemId',
|
2023-03-12 20:51:45 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-19 21:19:22 +01:00
|
|
|
mediaItemType: 'podcastEpisode'
|
2023-03-12 20:51:45 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-19 21:19:22 +01:00
|
|
|
AudioBookmark.belongsTo(podcastEpisode, { foreignKey: 'mediaItemId', constraints: false })
|
2023-03-12 20:51:45 +01:00
|
|
|
|
|
|
|
AudioBookmark.addHook('afterFind', findResult => {
|
2023-03-19 21:19:22 +01:00
|
|
|
if (!findResult) return
|
|
|
|
|
2023-03-12 20:51:45 +01:00
|
|
|
if (!Array.isArray(findResult)) findResult = [findResult]
|
2023-03-19 21:19:22 +01:00
|
|
|
|
2023-03-12 20:51:45 +01:00
|
|
|
for (const instance of findResult) {
|
2023-03-19 21:19:22 +01:00
|
|
|
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
|
2023-03-12 20:51:45 +01:00
|
|
|
}
|
|
|
|
// To prevent mistakes:
|
2023-03-19 21:19:22 +01:00
|
|
|
delete instance.book
|
|
|
|
delete instance.dataValues.book
|
|
|
|
delete instance.podcastEpisode
|
|
|
|
delete instance.dataValues.podcastEpisode
|
2023-03-12 20:51:45 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
user.hasMany(AudioBookmark)
|
|
|
|
AudioBookmark.belongsTo(user)
|
2023-03-12 20:51:45 +01:00
|
|
|
|
|
|
|
return AudioBookmark
|
|
|
|
}
|