Commit 0c2ad31a0d48c7be6be5c9afcac18c4fc590834b

Authored by satheesh
1 parent 5d043355e3
Exists in master

[#003]

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