index.js 4.05 KB
// import {Students } from '/imports/collections/students/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';
import { Users }                          from '/imports/collections/users/index';

class Student {
  constructor(doc) {
    _.assign(this, doc);
  };

  getUserIds() {
    return _.filter(_.map(this.users, 'userId'));
  };
};
export { Student };

class StudentsCollection extends Mongo.Collection {
  insert(item, callback) {
    _.assign(item, {
      createdAt:        new Date().getTime(),
    });
    return super.insert(item, callback);
  };
};

export const Students = new StudentsCollection('Students', {
  transform: (item) => {
    return new Student(item);
  },
});

_.assign(Students, {
  allStudents: () => {
    const user = Users.current();
    if(!user) return null;
    return Orgs.find({'users.userId': user._id});
  },
  current: () => {
    const user = Users.current();
    if(!user) return null;
    return Orgs.findOne({_id: user.orgId});
  },
  currentOrgUsers: () => {
    const OrgsArr = Orgs.current();
    if(!OrgsArr) return null;
    return OrgsArr.users;
  },

});

Students.deny({
  insert() { return true; },
  update() { return true; },
  remove() { return true; },
});


Students.schema = new SimpleSchema({
  userId:               { type: String },
  orgId:                { type: String },
  admissionId:          { type: String, optional: true },
  enrollmentDate:       { type: String, optional: true },
  address:              { type: String, optional: true },
  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 },
  rollNo:               { type: String, optional: true },
  class:                { type: String, optional: true },
  section:              { type: String, optional: true },
  bloodGroup:           { type: String, optional: true },
  community:            { type: String, optional: true },
  nationality:          { type: String, optional: true },
  motherTongue:         { type: String, optional: true },
  religion:             { type: String, optional: true },
  permanentAddress: {
    type: new SimpleSchema({
      home:             { type: String, optional: true },
      street:           { type: String, optional: true },
      town:             { type: String, optional: true },
      city:             { type: String, optional: true },
      state:            { type: String, optional: true },
      zip:              { type: String, optional: true },
    }),
    optional: true
  },
  parent: {
    type: [new SimpleSchema({
      id:               { type: String, },
      relationship:     { type: String, },
    })],
    optional: true
  },
  prevInstitute: {
    type: [new SimpleSchema({
      name:             { type: String, },
      fromYear:         { type: String, },
      toYear:           { type: String, },
      fromClass:        { type: String, },
      toClass:          { type: String, },
    })],
    optional: true
  },

  services: {
    type: Object,
    optional: true,
    blackbox: true,
  },

  isMetaUser:           { type: Boolean, optional: true },

  createdAt:            { type: Date, autoValue: function(){return new Date();}}

});

Students.attachSchema(Students.schema);

Students.privateFields = {
  orgId:              1,
  address:            1,

  firstName:          1,
  lastName:           1,
  emails:             1,
  phones:             1,

  isMetaUser:         1,
  createdAt:          1,
};

Students.publicFields = {
  firstName:          1,
  lastName:           1,
  emails:             1,

  createdAt:          1,
};