2024-02-11 23:48:16 +01:00
|
|
|
const { DataTypes, Model } = require('sequelize')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef ClientCustomMetadataProvider
|
|
|
|
* @property {UUIDV4} id
|
|
|
|
* @property {string} name
|
|
|
|
* @property {string} url
|
|
|
|
* @property {string} slug
|
|
|
|
*/
|
2024-01-03 01:36:56 +01:00
|
|
|
|
|
|
|
class CustomMetadataProvider extends Model {
|
|
|
|
constructor(values, options) {
|
|
|
|
super(values, options)
|
|
|
|
|
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.id
|
|
|
|
/** @type {string} */
|
2024-02-11 23:48:16 +01:00
|
|
|
this.mediaType
|
|
|
|
/** @type {string} */
|
2024-01-03 01:36:56 +01:00
|
|
|
this.name
|
|
|
|
/** @type {string} */
|
|
|
|
this.url
|
|
|
|
/** @type {string} */
|
2024-02-11 23:48:16 +01:00
|
|
|
this.authHeaderValue
|
|
|
|
/** @type {Object} */
|
|
|
|
this.extraData
|
|
|
|
/** @type {Date} */
|
|
|
|
this.createdAt
|
|
|
|
/** @type {Date} */
|
|
|
|
this.updatedAt
|
2024-01-03 01:36:56 +01:00
|
|
|
}
|
|
|
|
|
2024-02-11 23:48:16 +01:00
|
|
|
/**
|
|
|
|
* Get providers for client by media type
|
|
|
|
* Currently only available for "book" media type
|
2024-09-12 23:36:39 +02:00
|
|
|
*
|
|
|
|
* @param {string} mediaType
|
2024-02-11 23:48:16 +01:00
|
|
|
* @returns {Promise<ClientCustomMetadataProvider[]>}
|
|
|
|
*/
|
|
|
|
static async getForClientByMediaType(mediaType) {
|
|
|
|
if (mediaType !== 'book') return []
|
|
|
|
const customMetadataProviders = await this.findAll({
|
|
|
|
where: {
|
|
|
|
mediaType
|
|
|
|
}
|
|
|
|
})
|
2024-09-12 23:36:39 +02:00
|
|
|
return customMetadataProviders.map((cmp) => cmp.toClientJson())
|
2024-02-11 23:48:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if provider exists by slug
|
2024-09-12 23:36:39 +02:00
|
|
|
*
|
|
|
|
* @param {string} providerSlug
|
2024-02-11 23:48:16 +01:00
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
static async checkExistsBySlug(providerSlug) {
|
|
|
|
const providerId = providerSlug?.split?.('custom-')[1]
|
|
|
|
if (!providerId) return false
|
2024-01-12 21:45:03 +01:00
|
|
|
|
2024-02-11 23:48:16 +01:00
|
|
|
return (await this.count({ where: { id: providerId } })) > 0
|
|
|
|
}
|
2024-01-03 01:36:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize model
|
2024-09-12 23:36:39 +02:00
|
|
|
* @param {import('../Database').sequelize} sequelize
|
2024-01-03 01:36:56 +01:00
|
|
|
*/
|
|
|
|
static init(sequelize) {
|
2024-09-12 23:36:39 +02:00
|
|
|
super.init(
|
|
|
|
{
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true
|
|
|
|
},
|
|
|
|
name: DataTypes.STRING,
|
|
|
|
mediaType: DataTypes.STRING,
|
|
|
|
url: DataTypes.STRING,
|
|
|
|
authHeaderValue: DataTypes.STRING,
|
|
|
|
extraData: DataTypes.JSON
|
2024-01-03 01:36:56 +01:00
|
|
|
},
|
2024-09-12 23:36:39 +02:00
|
|
|
{
|
|
|
|
sequelize,
|
|
|
|
modelName: 'customMetadataProvider'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
getSlug() {
|
|
|
|
return `custom-${this.id}`
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Safe for clients
|
|
|
|
* @returns {ClientCustomMetadataProvider}
|
|
|
|
*/
|
|
|
|
toClientJson() {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
name: this.name,
|
|
|
|
mediaType: this.mediaType,
|
|
|
|
slug: this.getSlug()
|
|
|
|
}
|
2024-01-03 01:36:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 23:36:39 +02:00
|
|
|
module.exports = CustomMetadataProvider
|