diff --git a/imports/collections/students/serverCsvUpload.js b/imports/collections/students/serverCsvUpload.js index e88128d..9659db4 100644 --- a/imports/collections/students/serverCsvUpload.js +++ b/imports/collections/students/serverCsvUpload.js @@ -1,15 +1,24 @@ // import { } from '/imports/collections/orgs/methods'; import _ from 'lodash'; import { Meteor } from 'meteor/meteor'; +import Papa from 'meteor/harrison:papa-parse' +import csv from 'csv2json-convertor' import { ValidatedMethod } from 'meteor/mdg:validated-method'; import { SimpleSchema } from 'meteor/aldeed:simple-schema'; import { DDPRateLimiter } from 'meteor/ddp-rate-limiter'; import { Bert } from 'meteor/themeteorchef:bert'; import { Users } from '/imports/collections/users/index'; import { Orgs } from '/imports/collections/orgs/index'; -import {Students } from '/imports/collections/students/index' -export const orgMethod = new ValidatedMethod({ - name: 'org.method', +import { Students } from '/imports/collections/students/index' +import { Parents } from '/imports/collections/parents/index'; +import csv1 from 'csv2json-convertor' +import json2csv from 'json2csv' +import fs from 'fs' +import Validation from '/imports/validation/validationMethods'; +import Constants from '/imports/constants/constants' + +export const studenCsvtMethod = new ValidatedMethod({ + name: 'student.csvMethod', validate: new SimpleSchema({ itemId: { type: String }, @@ -21,14 +30,185 @@ export const orgMethod = new ValidatedMethod({ }); +export const save_csv_data = new ValidatedMethod({ + name: 'save_csv_data', + + validate: null, + + run({item,filename,csv_fiels}) { + var csv = json2csv({ + data: item, + fields: csv_fiels + }); + fs.writeFile(filename, csv,function(err) { + if (err) throw err; + }); + }, +}); + +export const addStudentCSV= new ValidatedMethod({ + name: 'student.addCSV', + + validate: null, + + run(item){ + data = item ; + console.log("data"); + console.log(data); + const user = Users.findOne({_id: this.userId}); + orgId = user.orgId; + newStudentId = Users.insert({ + // emails: [{address:data.email, verified: false}], + username: data["first Name*"], + firstName: data.firstName, + middleName: data.middleName, + lastName: data.lastName, + orgId: orgId, + role: 'STUDENT' + }); + newParentUserId = Users.insert({ + //emails: [{address:data.parentEmail, verified: false}], + username: data.parentName, + firstName: data.parentName, + orgId: orgId, + role: 'PARENT' + }); + if(newParentUserId){ + newParentId = Parents.insert({ + userId: newParentUserId, + orgId: orgId, + address: data.address, + gender: data.gender, + dob: data.dob, + rollNo: data.rollNo, + class: data.studentclass, + section: data.section, + bloodGroup: data.bloodGroup, + community: data.community, + }); + console.log("newParentUserId"); + console.log(newParentUserId); + } + console.log("newUserId"); + console.log(newStudentId); + if(newStudentId){ + Students.insert({ + userId: newStudentId, + orgId: orgId, + admissionId: data.admissionId, + address: data.address, + gender: data.gender, + dob: data.dob, + rollNo: data.rollNo, + class: data.studentclass, + section: data.section, + bloodGroup: data.bloodGroup, + community: data.community, + parent: [{id: newParentUserId, relatinship: data.relation}] + }); + } + return {newStudentId}; + }, + +}); + export const studentUploadCsv = new ValidatedMethod({ name: 'student.uploadCsv', validate: new SimpleSchema({ - itemId: { type: String }, + data: { type: [Object] }, }).validator(), - run({itemId}) { + run({data}) { + let validation = new Validation(); + let constants = new Constants(); + //console.log("++++++++++++++++++++++++"+constants.csv_students_data()); + temp = constants.csv_students_data(); + // + console.log(temp); + var data_1=csv1.csvtojson(("/Users/satheeshnagaraj/Downloads/11.csv")); //csvtojson is function that accepts csv filenames and returns JSON object + //console.log(data);'' + Stores = new Mongo.Collection('stores'); + data = data_1; + + var CSV_valid_buffer = []; + var CSV_invalid_buffer = []; + var filename = new Date().getTime().toString(); + var csv_filepath = '/Users/satheeshnagaraj/Documents/Workspace/Meteor/ydapp/CSV_Files/'; + + + for (let i = 0; i < data.length; i++) + { + + //let item= {Errors:""}; + let item = data[i]; + delete item['Errors']; + var formate_validation = 1; + var is_not_null = 1; + var errors = []; + for (var key in item) + { + var value = item[key]; + var n = key.indexOf("*"); + if(n!=-1) { + if(!(validation.notNull(value))) + { + errors.push(key+"is Empty"); + } + is_not_null = is_not_null && validation.notNull(value); + } + } + //console.log(validation.mobileNumber(item["Parent Mobile*"])); + + + + + var formate_validation = validation.validateEmail(item["Parent Email*"]) && validation.mobileNumber(item["Parent Mobile*"]) ; //&& validation.validateEmail(item["Parent Mobile"]) ); + + if (!(validation.validateEmail(item["Parent Email*"]))) + { + errors.push('Parent Email is invalid'); + } + if (!(validation.mobileNumber(item["Parent Mobile*"]))) + { + errors.push('Parent Mobile is invalid'); + } + + var is_valid = formate_validation && is_not_null ; + + // console.log(formate_validation); + if (is_valid) + { + CSV_valid_buffer.push(item); + console.log("----------------------------------------1"); + Meteor.call('student.addCSV',item); + console.log("----------------------------------------2"); + + // exists = Stores.findOne({ + // "Student 'Admission' ID*": item["Student Admission ID*"] + // }); + // if (!exists) + // { + // Stores.insert(item); + // } + // else + // { + // console.warn('Rejected. This item already exists.'); + // } + } + else + { + var str = errors.toString(); + item.Errors = str; + CSV_invalid_buffer.push(item); + //console.log(str); + // console.log(CSV_invalid_buffer); + } + } + console.log(csv_filepath+filename+"_CSV_invalid_data"+".csv"); + + Meteor.call('save_csv_data',{ item : CSV_invalid_buffer, filename: csv_filepath+filename+"_CSV_invalid_data"+".csv",csv_fiels :constants.csv_students_data()}); + Meteor.call('save_csv_data',{ item : CSV_valid_buffer,filename: csv_filepath+filename+"_CSV_valid_data"+".csv",csv_fiels :constants.csv_students_data()}); return {}; }, diff --git a/imports/constants/constants.js b/imports/constants/constants.js new file mode 100644 index 0000000..0459b29 --- /dev/null +++ b/imports/constants/constants.js @@ -0,0 +1,85 @@ +export default class Constants{ + +csv_students_data() { + // Constants Fields required to Export Valid CSV + var csv_students_fields =['Errors', + 'Student Admission ID*', + 'Enrollment Date(YYYY-MM-DD)*', + 'First Name*', + 'Last Name*', + 'Gender(male/female)*', + 'Birthday(YYYY-MM-DD)*', + 'Blood Group*', + 'Nationality*', + 'Mother Tongue*', + 'Religion*', + 'Community', + 'Student Roll*', + 'Student Class Name*', + 'Student Section Name*', + 'Previous Institution Name', + 'Class-From', + 'Class-To', + 'From-Year', + 'To-Year', + 'Student Line Adress*', + 'Student City*', + 'Student State*', + 'Student Zip Code*', + 'Parent Name*', + 'Parent Email*', + 'Parent Relation*', + 'Parent Profession', + 'Parent Mobile*', + ]; + // 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,}))$/; + return csv_students_fields; + }; + + csv_staff_data() { + // Constants Fields required to Export Valid CSV + var csv_staffs_fields = [ 'Employee ID*', + 'First Name*', + 'Last Name*', + 'Gender(male/female)*', + 'Marital Status(married/unmarried)*', + 'Staff Email*','Staff Phone*', + 'Staff Birthday(YYYY-MM-DD)*', + 'Blood group*', + 'Nationality*', + 'Mother Tongue*', + 'Religion*', + 'Staff Line Address*', + 'City*', + 'State*', + 'Zip*', + 'Job Role*', + 'Teaching Staff(yes/no)*', + 'Job Type(permanent/contract)*', + 'Date of joining(YYYY-MM-DD)*', + 'Qualification*', + 'University*', + 'Specialization*', + 'From*', + 'To*', + 'Bank Account No*', + 'Bank IFSC*', + 'Bank Branch Details*', + 'Previous Job Details*', + 'Previous job Role*', + 'Previous Job Type*', + 'Previous Organization*', + 'From*', + 'To*', + 'PAN Number', + 'ESI Number', + 'Aadhar Number', + 'PF Number' + ] + // 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,}))$/; + return csv_staffs_fields; + }; + +} + + diff --git a/imports/validation/validationMethods.js b/imports/validation/validationMethods.js index d5b0ba5..6ec219c 100644 --- a/imports/validation/validationMethods.js +++ b/imports/validation/validationMethods.js @@ -98,4 +98,26 @@ export default class Validation{ // Adjust for scientific notation. - (match[2] ? +match[2] : 0)); } + notNull(input) + { + if (input.length == 0) + { + return false; + } + return true; + } + mobileNumber(txtMobId) + { + + var IndNum = /^[0]?[789]\d{9}$/; + + if(IndNum.test(txtMobId)) + { + return true ; + } + else + { + return false; + } + } };