Blame view
imports/collections/students/serverCsvUpload.js
7.81 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 |
import { Students } from '/imports/collections/students/index' import { Parents } from '/imports/collections/parents/index'; import csv1 from 'csv2json-convertor' import json2csv from 'json2csv' |
5d043355e
|
16 |
import fs from 'fs' |
102b779c9
|
17 18 19 20 21 |
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 |
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, |
5d043355e
|
52 |
|
102b779c9
|
53 54 55 56 |
run(item){ data = item ; console.log("data"); console.log(data); |
5d043355e
|
57 |
return true; |
102b779c9
|
58 59 60 61 |
const user = Users.findOne({_id: this.userId}); orgId = user.orgId; newStudentId = Users.insert({ // emails: [{address:data.email, verified: false}], |
5d043355e
|
62 63 64 |
username: data["First Name*"], firstName: data["First Name*"], lastName: data['Last Name*'], |
102b779c9
|
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 |
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, |
5d043355e
|
97 98 99 100 |
admissionId: data['Student Admission ID*'], address: data['Student Admission ID*'], gender: data['Gender(male/female)*'], dob: data['Birthday(YYYY-MM-DD)*'], |
102b779c9
|
101 102 103 104 105 106 107 108 109 110 111 112 |
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 |
export const studentUploadCsv = new ValidatedMethod({ name: 'student.uploadCsv', |
5d043355e
|
115 |
validate: null, |
d0a93cc6d
|
116 |
|
102b779c9
|
117 118 119 120 121 122 123 |
run({data}) { let validation = new Validation(); let constants = new Constants(); //console.log("++++++++++++++++++++++++"+constants.csv_students_data()); temp = constants.csv_students_data(); // console.log(temp); |
5d043355e
|
124 |
// var data_1=csv1.csvtojson(("/Users/satheeshnagaraj/Downloads/11.csv")); //csvtojson is function that accepts csv filenames and returns JSON object |
102b779c9
|
125 126 |
//console.log(data);'' Stores = new Mongo.Collection('stores'); |
5d043355e
|
127 |
// data = data_1; |
102b779c9
|
128 129 130 131 132 |
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/'; |
5d043355e
|
133 134 135 136 |
for (let i = 0; i < data.length; i++) { |
102b779c9
|
137 138 139 140 141 142 143 144 |
//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) |
5d043355e
|
145 |
{ |
102b779c9
|
146 147 148 |
var value = item[key]; var n = key.indexOf("*"); if(n!=-1) { |
5d043355e
|
149 |
if(!(validation.notNull(value))) |
102b779c9
|
150 151 152 |
{ errors.push(key+"is Empty"); } |
5d043355e
|
153 |
is_not_null = is_not_null && validation.notNull(value); |
102b779c9
|
154 155 156 157 158 159 160 161 162 163 164 |
} } //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*"]))) { |
5d043355e
|
165 |
errors.push('Parent Email is invalid'); |
102b779c9
|
166 167 168 |
} if (!(validation.mobileNumber(item["Parent Mobile*"]))) { |
5d043355e
|
169 |
errors.push('Parent Mobile is invalid'); |
102b779c9
|
170 171 172 |
} var is_valid = formate_validation && is_not_null ; |
5d043355e
|
173 |
|
102b779c9
|
174 175 |
// console.log(formate_validation); if (is_valid) |
5d043355e
|
176 |
{ |
102b779c9
|
177 178 179 180 |
CSV_valid_buffer.push(item); console.log("----------------------------------------1"); Meteor.call('student.addCSV',item); console.log("----------------------------------------2"); |
5d043355e
|
181 |
|
102b779c9
|
182 |
// exists = Stores.findOne({ |
5d043355e
|
183 |
// "Student 'Admission' ID*": item["Student Admission ID*"] |
102b779c9
|
184 |
// }); |
5d043355e
|
185 |
// if (!exists) |
102b779c9
|
186 187 188 |
// { // Stores.insert(item); // } |
5d043355e
|
189 |
// else |
102b779c9
|
190 191 192 |
// { // console.warn('Rejected. This item already exists.'); // } |
5d043355e
|
193 194 |
} else |
102b779c9
|
195 196 197 198 199 200 201 202 203 204 205 206 |
{ 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
|
207 208 209 210 |
return {}; }, }); |