Built motion from commit 2438401.|1.0.5
[motion.git] / server / models / cm_contact.js
1 /**
2  * Contact Model
3  */
4
5 module.exports = function(sequelize, DataTypes) {
6
7   var Contact = sequelize.define('Contact', {
8     firstName: {
9       type: DataTypes.STRING,
10       validate: {
11         notEmpty: true
12       }
13     },
14     lastName: DataTypes.STRING,
15     tags: {
16       type: DataTypes.STRING,
17       get: function() {
18         var tags;
19         if (this.getDataValue('tags')) {
20           tags = this.getDataValue('tags').split(';');
21           if (tags.length > 1) {
22             tags.pop();
23           } else {
24             tags = []; //if the length is 1 the value is still in the format of the old tags, with comma separator. So I return an empty array since the old format is obsolete and invalid
25           }
26         } else {
27           tags = [];
28         }
29         return tags;
30       },
31       set: function(val) {
32         this.setDataValue('tags', val && val.length ? val.join(';') + ';' : null);
33       }
34     },
35     street: DataTypes.STRING,
36     postalCode: DataTypes.STRING,
37     city: DataTypes.STRING,
38     country: DataTypes.STRING,
39     dateOfBirth: DataTypes.STRING,
40     description: DataTypes.TEXT,
41     phone: DataTypes.STRING,
42     mobile: DataTypes.STRING,
43     fax: DataTypes.STRING,
44     email: DataTypes.STRING,
45     url: DataTypes.STRING,
46     planningtime: DataTypes.DATE,
47     variables: DataTypes.STRING
48   }, {
49     tableName: 'cm_contacts',
50     associate: function(models) {
51       Contact.belongsTo(models.Company);
52       Contact.belongsTo(models.User);
53       Contact.belongsTo(models.List);
54       Contact.addScope('list', {
55         include: [models.List]
56       });
57       Contact.addScope('company', {
58         include: [models.Company]
59       });
60       Contact.addScope('user', {
61         include: [models.User]
62       });
63       Contact.addScope('company_list_filter', function(query) {
64         var scope = {
65           where: {}
66         };
67         if (query.CompanyId) {
68           scope.where.CompanyId = query.CompanyId;
69         }
70         if (query.ListId) {
71           scope.where.ListId = query.ListId;
72         }
73         return scope;
74       });
75     }
76   });
77
78   return Contact;
79 };