Commit 102b779c908d4681c5264d3c902c1052e8ff1bae
1 parent
eff76b8c3d
Exists in
master
[#0001]-Student_csv
Showing
3 changed files
with
292 additions
and
5 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' | ||
5 | import csv from 'csv2json-convertor' | ||
4 | import { ValidatedMethod } from 'meteor/mdg:validated-method'; | 6 | import { ValidatedMethod } from 'meteor/mdg:validated-method'; |
5 | import { SimpleSchema } from 'meteor/aldeed:simple-schema'; | 7 | import { SimpleSchema } from 'meteor/aldeed:simple-schema'; |
6 | import { DDPRateLimiter } from 'meteor/ddp-rate-limiter'; | 8 | import { DDPRateLimiter } from 'meteor/ddp-rate-limiter'; |
7 | import { Bert } from 'meteor/themeteorchef:bert'; | 9 | import { Bert } from 'meteor/themeteorchef:bert'; |
8 | import { Users } from '/imports/collections/users/index'; | 10 | import { Users } from '/imports/collections/users/index'; |
9 | import { Orgs } from '/imports/collections/orgs/index'; | 11 | import { Orgs } from '/imports/collections/orgs/index'; |
10 | import {Students } from '/imports/collections/students/index' | 12 | import { Students } from '/imports/collections/students/index' |
11 | export const orgMethod = new ValidatedMethod({ | 13 | import { Parents } from '/imports/collections/parents/index'; |
12 | name: 'org.method', | 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 | validate: new SimpleSchema({ | 23 | validate: new SimpleSchema({ |
15 | itemId: { type: String }, | 24 | itemId: { type: String }, |
16 | }).validator(), | 25 | }).validator(), |
17 | 26 | ||
18 | run({itemId}) { | 27 | run({itemId}) { |
19 | return {}; | 28 | return {}; |
20 | }, | 29 | }, |
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 | export const studentUploadCsv = new ValidatedMethod({ | 115 | export const studentUploadCsv = new ValidatedMethod({ |
25 | name: 'student.uploadCsv', | 116 | name: 'student.uploadCsv', |
26 | 117 | ||
27 | validate: new SimpleSchema({ | 118 | validate: new SimpleSchema({ |
28 | itemId: { type: String }, | 119 | data: { type: [Object] }, |
29 | }).validator(), | 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 | return {}; | 212 | return {}; |
33 | }, | 213 | }, |
34 | 214 | ||
35 | }); | 215 | }); |
36 | 216 |
imports/constants/constants.js
File was created | 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 | |||
86 |
imports/validation/validationMethods.js
1 | export default class Validation{ | 1 | export default class Validation{ |
2 | 2 | ||
3 | validateEmail (value) { | 3 | validateEmail (value) { |
4 | // regex from http://stackoverflow.com/questions/46155/validate-email-address-in-javascript | 4 | // regex from http://stackoverflow.com/questions/46155/validate-email-address-in-javascript |
5 | 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,}))$/; | 5 | 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,}))$/; |
6 | return re.test(value); | 6 | return re.test(value); |
7 | }; | 7 | }; |
8 | 8 | ||
9 | // containsNoSpecialCharacters(str){ | 9 | // containsNoSpecialCharacters(str){ |
10 | // return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str); | 10 | // return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str); |
11 | // } | 11 | // } |
12 | 12 | ||
13 | noSpecialChars(str){ | 13 | noSpecialChars(str){ |
14 | str = String(str); | 14 | str = String(str); |
15 | return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str); | 15 | return !/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str); |
16 | } | 16 | } |
17 | 17 | ||
18 | noQwertysAllowed (str){ | 18 | noQwertysAllowed (str){ |
19 | str = str.toLowerCase(); | 19 | str = str.toLowerCase(); |
20 | if(str.toLowerCase().indexOf("qwerty") >-1){ | 20 | if(str.toLowerCase().indexOf("qwerty") >-1){ |
21 | return false; | 21 | return false; |
22 | } else{ | 22 | } else{ |
23 | return true; | 23 | return true; |
24 | } | 24 | } |
25 | } | 25 | } |
26 | 26 | ||
27 | passwordValidation (str){ | 27 | passwordValidation (str){ |
28 | if(str.length <6){ | 28 | if(str.length <6){ |
29 | return false; | 29 | return false; |
30 | } else{ | 30 | } else{ |
31 | return true; | 31 | return true; |
32 | } | 32 | } |
33 | } | 33 | } |
34 | 34 | ||
35 | isNumberOnly(str){ | 35 | isNumberOnly(str){ |
36 | if(!/^\d+$/.test(str)){ | 36 | if(!/^\d+$/.test(str)){ |
37 | return false; | 37 | return false; |
38 | }else { | 38 | }else { |
39 | return true; | 39 | return true; |
40 | } | 40 | } |
41 | } | 41 | } |
42 | isPositiveNotZeroNumber(str){ | 42 | isPositiveNotZeroNumber(str){ |
43 | str = parseFloat(str); | 43 | str = parseFloat(str); |
44 | if(str <=0){ | 44 | if(str <=0){ |
45 | return false; | 45 | return false; |
46 | }else { | 46 | }else { |
47 | return true; | 47 | return true; |
48 | } | 48 | } |
49 | } | 49 | } |
50 | isNumeric(n) { | 50 | isNumeric(n) { |
51 | return !isNaN(parseFloat(n)) && isFinite(n); | 51 | return !isNaN(parseFloat(n)) && isFinite(n); |
52 | } | 52 | } |
53 | 53 | ||
54 | isValidACN(str){ | 54 | isValidACN(str){ |
55 | str = String(str); | 55 | str = String(str); |
56 | //Remove any whitespace. | 56 | //Remove any whitespace. |
57 | str = str.replace(/\s+/g, ''); | 57 | str = str.replace(/\s+/g, ''); |
58 | console.log(str); | 58 | console.log(str); |
59 | console.log(str.length); | 59 | console.log(str.length); |
60 | if(/^\d+$/.test(str) && str.length ==9){ | 60 | if(/^\d+$/.test(str) && str.length ==9){ |
61 | return true; | 61 | return true; |
62 | } else{ | 62 | } else{ |
63 | return false; | 63 | return false; |
64 | } | 64 | } |
65 | } | 65 | } |
66 | isValidShortCode(str){ | 66 | isValidShortCode(str){ |
67 | str = String(str); | 67 | str = String(str); |
68 | console.log(str); | 68 | console.log(str); |
69 | if(str.length < 5 && str.length >2){ | 69 | if(str.length < 5 && str.length >2){ |
70 | return true; | 70 | return true; |
71 | } else{ | 71 | } else{ |
72 | return false; | 72 | return false; |
73 | } | 73 | } |
74 | } | 74 | } |
75 | containsNumbers (str){ | 75 | containsNumbers (str){ |
76 | if(/\d/g.test(str)){ | 76 | if(/\d/g.test(str)){ |
77 | return true; | 77 | return true; |
78 | }else{ | 78 | }else{ |
79 | return false; | 79 | return false; |
80 | } | 80 | } |
81 | } | 81 | } |
82 | isInt(n){ | 82 | isInt(n){ |
83 | console.log(typeof Number(n)); | 83 | console.log(typeof Number(n)); |
84 | return Number(n) % 1 === 0; | 84 | return Number(n) % 1 === 0; |
85 | } | 85 | } |
86 | 86 | ||
87 | isFloat(n){ | 87 | isFloat(n){ |
88 | return Number(n) % 1 !== 0; | 88 | return Number(n) % 1 !== 0; |
89 | } | 89 | } |
90 | decimalPlaces(num) { | 90 | decimalPlaces(num) { |
91 | num = parseFloat(num); | 91 | num = parseFloat(num); |
92 | var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); | 92 | var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); |
93 | if (!match) { return 0; } | 93 | if (!match) { return 0; } |
94 | return Math.max( | 94 | return Math.max( |
95 | 0, | 95 | 0, |
96 | // Number of digits right of decimal point. | 96 | // Number of digits right of decimal point. |
97 | (match[1] ? match[1].length : 0) | 97 | (match[1] ? match[1].length : 0) |
98 | // Adjust for scientific notation. | 98 | // Adjust for scientific notation. |
99 | - (match[2] ? +match[2] : 0)); | 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 | }; |
102 | 124 |