Built motion from commit 57474b6.|0.0.99
[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: DataTypes.STRING,
16     street: DataTypes.STRING,
17     postalCode: DataTypes.STRING,
18     city: DataTypes.STRING,
19     country: DataTypes.STRING,
20     dateOfBirth: DataTypes.STRING,
21     description: DataTypes.STRING,
22     phone: DataTypes.STRING,
23     mobile: DataTypes.STRING,
24     fax: DataTypes.STRING,
25     email: DataTypes.STRING,
26     url: DataTypes.STRING
27   }, {
28     tableName: 'cm_contacts',
29     associate: function(models) {
30       Contact.belongsTo(models.Company);
31       Contact.belongsTo(models.User);
32       Contact.belongsTo(models.List);
33       Contact.addScope('list', {
34         include: [models.List]
35       });
36       Contact.addScope('company', {
37         include: [models.Company]
38       });
39       Contact.addScope('user', {
40         include: [models.User]
41       });
42       Contact.addScope('company_list_filter', function(query) {
43         var scope = {
44           where: {}
45         };
46         if (query.CompanyId) {
47           scope.where.CompanyId = query.CompanyId;
48           delete query.CompanyId;
49         }
50         if (query.ListId) {
51           scope.where.ListId = query.ListId;
52           delete query.ListId;
53         }
54         return scope;
55       });
56     }
57   });
58
59   return Contact;
60 };