module.exports = function(sequelize, DataTypes) {
var FaxRoom = sequelize.define('FaxRoom', {
- roomId: DataTypes.STRING,
- // subject: DataTypes.STRING,
from: DataTypes.STRING,
- status: DataTypes.ENUM('NEW', 'OPEN', 'PENDING', 'SOLVED', 'CLOSED')
+ account: DataTypes.STRING,
+ status: {
+ type: DataTypes.ENUM('NEW', 'OPEN', 'CLOSED', 'UNMANAGED'),
+ defaultValue: 'NEW'
+ },
+ lastEvent: {
+ type: DataTypes.ENUM('SENT', 'SENDING', 'RECEIVED', 'FAILED', 'NOTE')
+ },
+ tags: {
+ type: DataTypes.TEXT,
+ get: function() {
+ var tags;
+ if (this.getDataValue('tags')) {
+ tags = this.getDataValue('tags').split(';');
+ tags.pop();
+ } else {
+ tags = [];
+ }
+ return tags;
+ },
+ set: function(val) {
+ this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
+ }
+ },
}, {
tableName: 'fax_rooms',
+ paranoid: true,
associate: function(models) {
// hasMany relations
FaxRoom.hasMany(models.FaxMessage);
FaxRoom.belongsTo(models.FaxAccount);
- FaxRoom.belongsTo(models.User);
+ FaxRoom.belongsToMany(models.User, {
+ through: 'user_has_fax_rooms'
+ });
+ // SCOPES
+ FaxRoom.addScope('default', {
+ order: [
+ ['createdAt', 'DESC']
+ ],
+ include: [{
+ model: models.FaxAccount
+ }, {
+ model: models.FaxMessage,
+ include: [{
+ model: models.FaxAttachment
+ }, {
+ model: models.User,
+ attributes: ['id', 'name', 'fullname', 'email']
+ }]
+ }, {
+ model: models.User,
+ attributes: ['id', 'name', 'fullname', 'email']
+ }]
+ });
}
});