Built motion from commit 450cc5d.|0.0.139
[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                 closeUrl: {
57                         type: DataTypes.STRING
58                 },
59                 closeMethod: {
60                         type: DataTypes.ENUM('GET', 'POST')
61                 },
62                 signature: {
63                         type: DataTypes.BLOB,
64                         get: function() {
65                                 if (this.getDataValue('signature')) {
66                                         return _.toString(this.getDataValue('signature'));
67                                 }
68                         }
69                 },
70                 custom: {
71                         type: DataTypes.BOOLEAN,
72                         defaultValue: false
73                 },
74                 service: {
75                         type: DataTypes.BOOLEAN,
76                         defaultValue: false
77                 }
78         }, {
79                 tableName: 'mail_accounts',
80                 associate: function(models) {
81                         // RELATIONS
82                         MailAccount.belongsTo(models.List);
83                         MailAccount.belongsTo(models.Template);
84                         MailAccount.hasMany(models.MailRoom);
85                         MailAccount.hasMany(models.MailDisposition);
86                         MailAccount.hasMany(models.MailApplication);
87                         MailAccount.hasOne(models.MailServerIn, {
88                                 onDelete: 'cascade',
89                                 hooks: true
90                         });
91                         MailAccount.hasOne(models.MailServerOut, {
92                                 onDelete: 'cascade',
93                                 hooks: true
94                         });
95
96                         // SCOPES
97                         MailAccount.addScope('default', {
98                                 where: {
99                                         service: false,
100                                 },
101                                 include: [{
102                                         model: models.MailServerIn,
103                                         attributes: ['id', 'host', 'port', 'username', 'password', 'ssl', 'delete', 'filter', 'state', 'mailbox', 'connTimeout', 'authTimeout', 'keepalive', 'source']
104                                 }, {
105                                         model: models.MailServerOut,
106                                         attributes: ['id', 'host', 'port', 'username', 'password', 'ssl', 'state', 'source', 'description']
107                                 }, {
108                                         model: models.MailApplication,
109                                         include: [{
110                                                 model: models.User,
111                                                 attributes: ['id',
112                                                         'name',
113                                                         'email',
114                                                         'internal',
115                                                         'fullname'
116                                                 ]
117                                         }, {
118                                                 model: models.MailQueue
119                                         }]
120                                 }]
121                         });
122                 }
123         });
124
125         return MailAccount;
126 };