CoreID/app/models/Message.model.js
2020-05-03 20:16:54 -05:00

40 lines
996 B
JavaScript

const { Model } = require('flitter-orm')
class MessageModel extends Model {
static get schema() {
return {
dismissed: {type: Boolean, default: false},
expires: {type: Date, default: () => {
const date = new Date
date.setYear(date.getUTCFullYear() + 1)
return date
}},
display_type: {type: String, default: 'info'},
message: String,
user_id: String,
}
}
static async for_user(user) {
return this.find({ user_id: user.id, dismissed: false, expires: { $gt: new Date } })
}
static async create(user, message, display_type = 'info') {
const msg = new this({
message,
display_type,
user_id: user.id,
})
await msg.save()
return msg
}
async dismiss() {
this.dismissed = true
return this.save()
}
}
module.exports = exports = MessageModel