index.js 4.34 KB
// 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() {
    return (this._id);
    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',
  'STAFF':    'STAFF',
  'ADMIN':    'ADMIN',
  'PARENT':   'PARENT'
};


Users.schema = new SimpleSchema({
  role:                 { type: String },
  orgId:                { type: String },
  username:             { type: String, optional: true },
  prefix:               { type: String, optional: true },
  firstName:            { type: String, optional: true },
  middlename:           { type: String, optional: true },
  lastName:             { type: String, optional: true },
  address:              { type: String, optional: true, },
  gender:               { type: String, optional: true },
  dob:                  { type: String, optional: true },
  emails: {
    type: [new SimpleSchema({
      address:          { type: String, },
      verified:         { type: Boolean, },
    })],
    optional: true
  },
  phones: {
    type: [new SimpleSchema({
      country:          { type: String, optional: true },
      prefix:           { type: String, optional: true },
      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,
};