Built motion from commit 5ae821c.|0.0.127
[motion.git] / server / models / mail_account.js
1 /**
2  * Chat Website Model
3  */
4
5 var _ = require('lodash');
6
7 module.exports = function(sequelize, DataTypes) {
8
9         var MailAccount = sequelize.define('MailAccount', {
10                 description: DataTypes.STRING,
11                 name: {
12                         type: DataTypes.STRING
13                 },
14                 address: {
15                         type: DataTypes.STRING,
16                         unique: true,
17                         isEmail: true,
18                         set: function(address) {
19                                 if (address) {
20                                         this.setDataValue('address', address.toLowerCase());
21                                 }
22                         }
23                 },
24                 fidelity: {
25                         type: DataTypes.BOOLEAN,
26                         defaultValue: false
27                 },
28                 timeout: {
29                         type: DataTypes.INTEGER,
30                         defaultValue: 0
31                 },
32                 whiteLabel: {
33                         type: DataTypes.STRING
34                 },
35                 acceptUrl: {
36                         type: DataTypes.STRING
37                 },
38                 rejectUrl: {
39                         type: DataTypes.STRING
40                 },
41                 acceptMethod: {
42                         type: DataTypes.ENUM('GET', 'POST')
43                 },
44                 rejectMethod: {
45                         type: DataTypes.ENUM('GET', 'POST')
46                 },
47                 actions: {
48                         type: DataTypes.STRING,
49                         get: function() {
50                                 return this.getDataValue('actions') ? JSON.parse(this.getDataValue('actions')) : [];
51                         },
52                         set: function(val) {
53                                 return this.setDataValue('actions', JSON.stringify(val));
54                         }
55                 },
56                 signature: {
57                         type: DataTypes.BLOB,
58                         get: function() {
59                                 if (this.getDataValue('signature')) {
60                                         return _.toString(this.getDataValue('signature'));
61                                 }
62                         }
63                 }
64         }, {
65                 tableName: 'mail_accounts',
66                 associate: function(models) {
67                         // RELATIONS
68                         MailAccount.belongsTo(models.List);
69                         MailAccount.belongsTo(models.Template);
70                         MailAccount.hasMany(models.MailRoom);
71                         MailAccount.hasMany(models.MailDisposition);
72                         MailAccount.hasMany(models.MailApplication);
73                         MailAccount.hasOne(models.MailServerIn, {
74                                 onDelete: 'cascade',
75                                 hooks: true
76                         });
77                         MailAccount.hasOne(models.MailServerOut, {
78                                 onDelete: 'cascade',
79                                 hooks: true
80                         });
81
82                         // SCOPES
83                         MailAccount.addScope('default', {
84                                 include: [{
85                                         model: models.MailServerIn,
86                                         attributes: ['id', 'host', 'port', 'username', 'password', 'ssl', 'delete', 'filter', 'state', 'mailbox', 'connTimeout', 'authTimeout', 'keepalive', 'source']
87                                 }, {
88                                         model: models.MailServerOut,
89                                         attributes: ['id', 'host', 'port', 'username', 'password', 'ssl', 'service', 'state', 'source']
90                                 }, {
91                                         model: models.MailApplication,
92                                         include: [{
93                                                 model: models.User,
94                                                 attributes: ['id',
95                                                         'name',
96                                                         'email',
97                                                         'internal',
98                                                         'fullname'
99                                                 ]
100                                         }, {
101                                                 model: models.MailQueue
102                                         }]
103                                 }]
104                         });
105                 }
106         });
107
108         return MailAccount;
109 };