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