Blame view
imports/collections/students/serverCsvUpload.js
7.88 KB
d0a93cc6d
|
1 2 3 |
// import { } from '/imports/collections/orgs/methods'; import _ from 'lodash'; import { Meteor } from 'meteor/meteor'; |
102b779c9
|
4 5 |
import Papa from 'meteor/harrison:papa-parse' import csv from 'csv2json-convertor' |
d0a93cc6d
|
6 7 8 9 10 11 |
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'; |
102b779c9
|
12 13 14 15 16 17 18 19 20 21 |
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', |
d0a93cc6d
|
22 23 24 25 26 27 28 29 30 31 |
validate: new SimpleSchema({ itemId: { type: String }, }).validator(), run({itemId}) { return {}; }, }); |
102b779c9
|
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 111 112 |
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}; }, }); |
d0a93cc6d
|
113 114 115 116 |
export const studentUploadCsv = new ValidatedMethod({ name: 'student.uploadCsv', validate: new SimpleSchema({ |
102b779c9
|
117 |
data: { type: [Object] }, |
d0a93cc6d
|
118 |
}).validator(), |
102b779c9
|
119 120 121 122 123 124 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
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()}); |
d0a93cc6d
|
209 210 211 212 |
return {}; }, }); |