Blame view

imports/collections/users/index.js 4.16 KB
ff976df49   Deepak   added orgs public...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  // import { Users }                          from '/imports/collections/users/index';
  // import { Users }                          from '/imports/collections/users/index';
  
  import _                                  from 'lodash';
  import { Meteor }                         from 'meteor/meteor';
  import { Mongo }                          from 'meteor/mongo';
  import { SimpleSchema }                   from 'meteor/aldeed:simple-schema';
  
  import { Orgs }                           from '/imports/collections/orgs/index';
  
  class User {
  
    constructor(doc) {
      _.assign(this, doc);
    };
  
    isEmailVerified() {
      return !! _.find(this.emails, x => x.verified);
    };
  
    isPhoneVerified() {
      return !! _.find(this.phones, x => x.verified);
    };
  
    isIdentityVerified() {
      return !! _.find(this.identities, x => x.verified);
    };
  
    getRole() {
      const org = Orgs.findOne({_id: this.orgId});
      if(!org) return null;
      const connection = _.find(org.users, {userId: this._id});
      if(!connection) return null;
      return connection.role;
    };
  
    getFullName() {
      return `${this.firstName} ${this.lastName}`;
    };
    getFirstName() {
      return `${this.firstName}`;
    };
    getLastName() {
      return `${this.lastName}`;
    };
    getEmail() {
      return `${this.emails[0].address}`;
    };
    getOrganization(){
        return `${this.orgId}`;
    };
  
    getNameByUserId(userId){
      var user = Users.findOne({"_id":userId});
      return user.firstName + " " + user.lastName;
    }
    getAvatarUrl() {
      let random = parseInt(this._id.substr(0, 4), 36);
      random = '' + (random % 32);
      while(random.length < 3) random = '0' + random;
      return `/files/random/random${ random }.png`;
    };
  };
  
  export { User };
  
  
  const transform = function(doc) {
    return new User(doc);
  };
  
  export const Users = {
  
    current: function() {
      return Meteor.users.findOne({_id: Meteor.userId()}, _.extend({transform: transform}));
    },
  
    find: function(selector, options) {
      return Meteor.users.find(selector || {}, _.extend({transform: transform}, options));
    },
  
    findOne: function(selector, options) {
      return Meteor.users.findOne(selector || {}, _.extend({transform: transform}, options));
    },
  
    insert:         _.bind(Meteor.users.insert, Meteor.users),
    update:         _.bind(Meteor.users.update, Meteor.users),
    remove:         _.bind(Meteor.users.remove, Meteor.users),
    allow:          _.bind(Meteor.users.allow, Meteor.users),
    deny:           _.bind(Meteor.users.deny, Meteor.users),
    attachSchema:   _.bind(Meteor.users.attachSchema, Meteor.users),
  
  };
  
  
  Users.deny({
    insert() { return true; },
    update() { return true; },
    remove() { return true; },
  });
  
  Users.roles = {
    'STUDENT':  'STUDENT',
    'TEACHER':  'TEACHER',
    'ADMIN':    'ADMIN',
    'PARENT':   'PARENT'
  };
  
  
  Users.schema = new SimpleSchema({
39d8f536d   Deepak   some more cleamup
111
    role:                { type: String },
ff976df49   Deepak   added orgs public...
112
    orgId:                { type: String },
ff976df49   Deepak   added orgs public...
113
114
115
116
117
118
    prefix:               { type: String, optional: true },
    firstName:            { type: String, optional: true },
    middlename:           { type: String, optional: true },
    lastName:             { type: String, optional: true },
    gender:               { type: String, optional: true },
    dob:                  { type: String, optional: true },
ff976df49   Deepak   added orgs public...
119
120
121
122
123
124
    emails: {
      type: [new SimpleSchema({
        address:          { type: String, },
        verified:         { type: Boolean, },
      })],
    },
ff976df49   Deepak   added orgs public...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    phones: {
      type: [new SimpleSchema({
        country:          { type: String, },
        prefix:           { type: String, },
        number:           { type: String, },
        verified:         { type: Boolean, },
      })],
      optional: true
    },
  
    services: {
      type: Object,
      optional: true,
      blackbox: true,
    },
  
    isMetaUser:           { type: Boolean, optional: true },
  
    createdAt:            { type: Date, autoValue: function(){return new Date();}}
  
  });
  
  Users.attachSchema(Users.schema);
  
  Users.privateFields = {
    orgId:              1,
    address:            1,
  
    firstName:          1,
    lastName:           1,
    emails:             1,
    phones:             1,
  
    isMetaUser:         1,
    createdAt:          1,
  };
  
  Users.publicFields = {
    firstName:          1,
    lastName:           1,
    emails:             1,
  
    createdAt:          1,
  };