Commit 102b779c908d4681c5264d3c902c1052e8ff1bae

Authored by satheesh
1 parent eff76b8c3d
Exists in master

[#0001]-Student_csv

imports/collections/students/serverCsvUpload.js
1 1 // import { } from '/imports/collections/orgs/methods';
2 2 import _ from 'lodash';
3 3 import { Meteor } from 'meteor/meteor';
  4 +import Papa from 'meteor/harrison:papa-parse'
  5 +import csv from 'csv2json-convertor'
4 6 import { ValidatedMethod } from 'meteor/mdg:validated-method';
5 7 import { SimpleSchema } from 'meteor/aldeed:simple-schema';
6 8 import { DDPRateLimiter } from 'meteor/ddp-rate-limiter';
7 9 import { Bert } from 'meteor/themeteorchef:bert';
8 10 import { Users } from '/imports/collections/users/index';
9 11 import { Orgs } from '/imports/collections/orgs/index';
10   -import {Students } from '/imports/collections/students/index'
11   -export const orgMethod = new ValidatedMethod({
12   - name: 'org.method',
  12 +import { Students } from '/imports/collections/students/index'
  13 +import { Parents } from '/imports/collections/parents/index';
  14 +import csv1 from 'csv2json-convertor'
  15 +import json2csv from 'json2csv'
  16 +import fs from 'fs'
  17 +import Validation from '/imports/validation/validationMethods';
  18 +import Constants from '/imports/constants/constants'
  19 +
  20 +export const studenCsvtMethod = new ValidatedMethod({
  21 + name: 'student.csvMethod',
13 22  
14 23 validate: new SimpleSchema({
15 24 itemId: { type: String },
... ... @@ -21,14 +30,185 @@ export const orgMethod = new ValidatedMethod({
21 30  
22 31 });
23 32  
  33 +export const save_csv_data = new ValidatedMethod({
  34 + name: 'save_csv_data',
  35 +
  36 + validate: null,
  37 +
  38 + run({item,filename,csv_fiels}) {
  39 + var csv = json2csv({
  40 + data: item,
  41 + fields: csv_fiels
  42 + });
  43 + fs.writeFile(filename, csv,function(err) {
  44 + if (err) throw err;
  45 + });
  46 + },
  47 +});
  48 +
  49 +export const addStudentCSV= new ValidatedMethod({
  50 + name: 'student.addCSV',
  51 +
  52 + validate: null,
  53 +
  54 + run(item){
  55 + data = item ;
  56 + console.log("data");
  57 + console.log(data);
  58 + const user = Users.findOne({_id: this.userId});
  59 + orgId = user.orgId;
  60 + newStudentId = Users.insert({
  61 + // emails: [{address:data.email, verified: false}],
  62 + username: data["first Name*"],
  63 + firstName: data.firstName,
  64 + middleName: data.middleName,
  65 + lastName: data.lastName,
  66 + orgId: orgId,
  67 + role: 'STUDENT'
  68 + });
  69 + newParentUserId = Users.insert({
  70 + //emails: [{address:data.parentEmail, verified: false}],
  71 + username: data.parentName,
  72 + firstName: data.parentName,
  73 + orgId: orgId,
  74 + role: 'PARENT'
  75 + });
  76 + if(newParentUserId){
  77 + newParentId = Parents.insert({
  78 + userId: newParentUserId,
  79 + orgId: orgId,
  80 + address: data.address,
  81 + gender: data.gender,
  82 + dob: data.dob,
  83 + rollNo: data.rollNo,
  84 + class: data.studentclass,
  85 + section: data.section,
  86 + bloodGroup: data.bloodGroup,
  87 + community: data.community,
  88 + });
  89 + console.log("newParentUserId");
  90 + console.log(newParentUserId);
  91 + }
  92 + console.log("newUserId");
  93 + console.log(newStudentId);
  94 + if(newStudentId){
  95 + Students.insert({
  96 + userId: newStudentId,
  97 + orgId: orgId,
  98 + admissionId: data.admissionId,
  99 + address: data.address,
  100 + gender: data.gender,
  101 + dob: data.dob,
  102 + rollNo: data.rollNo,
  103 + class: data.studentclass,
  104 + section: data.section,
  105 + bloodGroup: data.bloodGroup,
  106 + community: data.community,
  107 + parent: [{id: newParentUserId, relatinship: data.relation}]
  108 + });
  109 + }
  110 + return {newStudentId};
  111 + },
  112 +
  113 +});
  114 +
24 115 export const studentUploadCsv = new ValidatedMethod({
25 116 name: 'student.uploadCsv',
26 117  
27 118 validate: new SimpleSchema({
28   - itemId: { type: String },
  119 + data: { type: [Object] },
29 120 }).validator(),
30 121  
31   - run({itemId}) {
  122 + run({data}) {
  123 + let validation = new Validation();
  124 + let constants = new Constants();
  125 + //console.log("++++++++++++++++++++++++"+constants.csv_students_data());
  126 + temp = constants.csv_students_data();
  127 + //
  128 + console.log(temp);
  129 + var data_1=csv1.csvtojson(("/Users/satheeshnagaraj/Downloads/11.csv")); //csvtojson is function that accepts csv filenames and returns JSON object
  130 + //console.log(data);''
  131 + Stores = new Mongo.Collection('stores');
  132 + data = data_1;
  133 +
  134 + var CSV_valid_buffer = [];
  135 + var CSV_invalid_buffer = [];
  136 + var filename = new Date().getTime().toString();
  137 + var csv_filepath = '/Users/satheeshnagaraj/Documents/Workspace/Meteor/ydapp/CSV_Files/';
  138 +
  139 +
  140 + for (let i = 0; i < data.length; i++)
  141 + {
  142 +
  143 + //let item= {Errors:""};
  144 + let item = data[i];
  145 + delete item['Errors'];
  146 + var formate_validation = 1;
  147 + var is_not_null = 1;
  148 + var errors = [];
  149 + for (var key in item)
  150 + {
  151 + var value = item[key];
  152 + var n = key.indexOf("*");
  153 + if(n!=-1) {
  154 + if(!(validation.notNull(value)))
  155 + {
  156 + errors.push(key+"is Empty");
  157 + }
  158 + is_not_null = is_not_null && validation.notNull(value);
  159 + }
  160 + }
  161 + //console.log(validation.mobileNumber(item["Parent Mobile*"]));
  162 +
  163 +
  164 +
  165 +
  166 + var formate_validation = validation.validateEmail(item["Parent Email*"]) && validation.mobileNumber(item["Parent Mobile*"]) ; //&& validation.validateEmail(item["Parent Mobile"]) );
  167 +
  168 + if (!(validation.validateEmail(item["Parent Email*"])))
  169 + {
  170 + errors.push('Parent Email is invalid');
  171 + }
  172 + if (!(validation.mobileNumber(item["Parent Mobile*"])))
  173 + {
  174 + errors.push('Parent Mobile is invalid');
  175 + }
  176 +
  177 + var is_valid = formate_validation && is_not_null ;
  178 +
  179 + // console.log(formate_validation);
  180 + if (is_valid)
  181 + {
  182 + CSV_valid_buffer.push(item);
  183 + console.log("----------------------------------------1");
  184 + Meteor.call('student.addCSV',item);
  185 + console.log("----------------------------------------2");
  186 +
  187 + // exists = Stores.findOne({
  188 + // "Student 'Admission' ID*": item["Student Admission ID*"]
  189 + // });
  190 + // if (!exists)
  191 + // {
  192 + // Stores.insert(item);
  193 + // }
  194 + // else
  195 + // {
  196 + // console.warn('Rejected. This item already exists.');
  197 + // }
  198 + }
  199 + else
  200 + {
  201 + var str = errors.toString();
  202 + item.Errors = str;
  203 + CSV_invalid_buffer.push(item);
  204 + //console.log(str);
  205 + // console.log(CSV_invalid_buffer);
  206 + }
  207 + }
  208 + console.log(csv_filepath+filename+"_CSV_invalid_data"+".csv");
  209 +
  210 + Meteor.call('save_csv_data',{ item : CSV_invalid_buffer, filename: csv_filepath+filename+"_CSV_invalid_data"+".csv",csv_fiels :constants.csv_students_data()});
  211 + Meteor.call('save_csv_data',{ item : CSV_valid_buffer,filename: csv_filepath+filename+"_CSV_valid_data"+".csv",csv_fiels :constants.csv_students_data()});
32 212 return {};
33 213 },
34 214  
... ...
imports/constants/constants.js
... ... @@ -0,0 +1,85 @@
  1 +export default class Constants{
  2 +
  3 +csv_students_data() {
  4 + // Constants Fields required to Export Valid CSV
  5 + var csv_students_fields =['Errors',
  6 + 'Student Admission ID*',
  7 + 'Enrollment Date(YYYY-MM-DD)*',
  8 + 'First Name*',
  9 + 'Last Name*',
  10 + 'Gender(male/female)*',
  11 + 'Birthday(YYYY-MM-DD)*',
  12 + 'Blood Group*',
  13 + 'Nationality*',
  14 + 'Mother Tongue*',
  15 + 'Religion*',
  16 + 'Community',
  17 + 'Student Roll*',
  18 + 'Student Class Name*',
  19 + 'Student Section Name*',
  20 + 'Previous Institution Name',
  21 + 'Class-From',
  22 + 'Class-To',
  23 + 'From-Year',
  24 + 'To-Year',
  25 + 'Student Line Adress*',
  26 + 'Student City*',
  27 + 'Student State*',
  28 + 'Student Zip Code*',
  29 + 'Parent Name*',
  30 + 'Parent Email*',
  31 + 'Parent Relation*',
  32 + 'Parent Profession',
  33 + 'Parent Mobile*',
  34 + ];
  35 + // var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  36 + return csv_students_fields;
  37 + };
  38 +
  39 + csv_staff_data() {
  40 + // Constants Fields required to Export Valid CSV
  41 + var csv_staffs_fields = [ 'Employee ID*',
  42 + 'First Name*',
  43 + 'Last Name*',
  44 + 'Gender(male/female)*',
  45 + 'Marital Status(married/unmarried)*',
  46 + 'Staff Email*','Staff Phone*',
  47 + 'Staff Birthday(YYYY-MM-DD)*',
  48 + 'Blood group*',
  49 + 'Nationality*',
  50 + 'Mother Tongue*',
  51 + 'Religion*',
  52 + 'Staff Line Address*',
  53 + 'City*',
  54 + 'State*',
  55 + 'Zip*',
  56 + 'Job Role*',
  57 + 'Teaching Staff(yes/no)*',
  58 + 'Job Type(permanent/contract)*',
  59 + 'Date of joining(YYYY-MM-DD)*',
  60 + 'Qualification*',
  61 + 'University*',
  62 + 'Specialization*',
  63 + 'From*',
  64 + 'To*',
  65 + 'Bank Account No*',
  66 + 'Bank IFSC*',
  67 + 'Bank Branch Details*',
  68 + 'Previous Job Details*',
  69 + 'Previous job Role*',
  70 + 'Previous Job Type*',
  71 + 'Previous Organization*',
  72 + 'From*',
  73 + 'To*',
  74 + 'PAN Number',
  75 + 'ESI Number',
  76 + 'Aadhar Number',
  77 + 'PF Number'
  78 + ]
  79 + // var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  80 + return csv_staffs_fields;
  81 + };
  82 +
  83 +}
  84 +
  85 +
... ...
imports/validation/validationMethods.js
... ... @@ -98,4 +98,26 @@ export default class Validation{
98 98 // Adjust for scientific notation.
99 99 - (match[2] ? +match[2] : 0));
100 100 }
  101 + notNull(input)
  102 + {
  103 + if (input.length == 0)
  104 + {
  105 + return false;
  106 + }
  107 + return true;
  108 + }
  109 + mobileNumber(txtMobId)
  110 + {
  111 +
  112 + var IndNum = /^[0]?[789]\d{9}$/;
  113 +
  114 + if(IndNum.test(txtMobId))
  115 + {
  116 + return true ;
  117 + }
  118 + else
  119 + {
  120 + return false;
  121 + }
  122 + }
101 123 };
... ...