Blame view

imports/collections/students/serverCsvUpload.js 9.07 KB
d0a93cc6d   Deepak   csv file
1
2
3
  // import { } from '/imports/collections/orgs/methods';
  import _                                  from 'lodash';
  import { Meteor }                         from 'meteor/meteor';
102b779c9   satheesh   [#0001]-Student_csv
4
5
  import Papa                               from 'meteor/harrison:papa-parse'
  import csv                                from 'csv2json-convertor'
d0a93cc6d   Deepak   csv file
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   satheesh   [#0001]-Student_csv
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   Deepak   fixed bug and add...
16
  import fs                                 from 'fs'
102b779c9   satheesh   [#0001]-Student_csv
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   Deepak   csv file
22
23
24
25
26
27
28
29
30
31
  
    validate: new SimpleSchema({
      itemId: { type: String },
    }).validator(),
  
    run({itemId}) {
      return {};
    },
  
  });
102b779c9   satheesh   [#0001]-Student_csv
32
33
34
35
36
37
  export const save_csv_data = new ValidatedMethod({
    name: 'save_csv_data',
  
    validate: null,
  
    run({item,filename,csv_fiels}) {
9db2d9cbe   Deepak   fixed error in cs...
38

167dd6325   satheesh   [#004]-Revised Sc...
39
40
41
      if ((item.length))
      { console.log("++++++++++++++++++"+item.length)
        var csv = json2csv({
102b779c9   satheesh   [#0001]-Student_csv
42
43
        data: item,
        fields: csv_fiels
167dd6325   satheesh   [#004]-Revised Sc...
44
                        });
102b779c9   satheesh   [#0001]-Student_csv
45
46
47
      fs.writeFile(filename, csv,function(err) {
        if (err) throw err;
      });
167dd6325   satheesh   [#004]-Revised Sc...
48
49
  
      }
102b779c9   satheesh   [#0001]-Student_csv
50
51
52
53
54
55
56
    },
  });
  
  export const addStudentCSV= new ValidatedMethod({
    name: 'student.addCSV',
  
    validate: null,
5d043355e   Deepak   fixed bug and add...
57

102b779c9   satheesh   [#0001]-Student_csv
58
59
    run(item){
      data = item ;
102b779c9   satheesh   [#0001]-Student_csv
60
61
62
63
      const user = Users.findOne({_id: this.userId});
      orgId = user.orgId;
      newStudentId = Users.insert({
       // emails:     [{address:data.email, verified: false}],
0c2ad31a0   satheesh   [#003]
64
        //username:   data["First Name*"],
5d043355e   Deepak   fixed bug and add...
65
66
        firstName:  data["First Name*"],
        lastName:   data['Last Name*'],
102b779c9   satheesh   [#0001]-Student_csv
67
68
69
70
71
        orgId:      orgId,
        role:       'STUDENT'
      });
      newParentUserId = Users.insert({
        //emails:     [{address:data.parentEmail, verified: false}],
167dd6325   satheesh   [#004]-Revised Sc...
72
        //3:   data['Parent Name*'],
102b779c9   satheesh   [#0001]-Student_csv
73
        orgId:      orgId,
dd5d357af   satheesh   [#005]-CSV duplic...
74
75
76
        firstName:  data['Parent Name*'],
        emails: [{ address: data['Parent Email*'],verified: false }],
        phones: [{ number:  data['Parent Mobile*'],verified: false }],
102b779c9   satheesh   [#0001]-Student_csv
77
        role:       'PARENT'
dd5d357af   satheesh   [#005]-CSV duplic...
78

102b779c9   satheesh   [#0001]-Student_csv
79
80
81
82
83
      });
      if(newParentUserId){
        newParentId = Parents.insert({
          userId: newParentUserId,
          orgId: orgId,
167dd6325   satheesh   [#004]-Revised Sc...
84
          profession: data['Parent Profession']
102b779c9   satheesh   [#0001]-Student_csv
85
86
87
88
89
90
91
92
93
94
        });
        console.log("newParentUserId");
        console.log(newParentUserId);
      }
      console.log("newUserId");
      console.log(newStudentId);
      if(newStudentId){
        Students.insert({
          userId: newStudentId,
          orgId: orgId,
0c2ad31a0   satheesh   [#003]
95
          admissionId:  data['Student Admission ID*'],
167dd6325   satheesh   [#004]-Revised Sc...
96
          enrollmentDate: data['Enrollment Date(YYYY-MM-DD)*'],
0c2ad31a0   satheesh   [#003]
97
          gender:       data['Gender(male/female)*'],
5d043355e   Deepak   fixed bug and add...
98
          dob:          data['Birthday(YYYY-MM-DD)*'],
167dd6325   satheesh   [#004]-Revised Sc...
99
100
101
          bloodGroup:   data['Blood Group*'],
          nationality:  data['Nationality*'],
          motherTongue: data['Mother Tongue*'],
9db2d9cbe   Deepak   fixed error in cs...
102
          religion:     data['Religion*'],
167dd6325   satheesh   [#004]-Revised Sc...
103
          community:    data['Community'],
0c2ad31a0   satheesh   [#003]
104
105
106
          rollNo:       data['Student Roll*'],
          class:        data['Student Class Name*'],
          section:      data['Student Section Name*'],
167dd6325   satheesh   [#004]-Revised Sc...
107
108
  
          prevInstitute: [{ name:     data['Previous Institution Name'],
9db2d9cbe   Deepak   fixed error in cs...
109
                           fromYear:  data['Class-From'],
167dd6325   satheesh   [#004]-Revised Sc...
110
111
                           toYear:    data['Class-To'],
                           fromClass: data['From-Year'],
9db2d9cbe   Deepak   fixed error in cs...
112
                           toClass:   data['To-Year']
167dd6325   satheesh   [#004]-Revised Sc...
113
114
                         }],
          permanentAddress: { street: data['Student Line Adress*'],
9db2d9cbe   Deepak   fixed error in cs...
115
                              city:   data['Student City*'],
167dd6325   satheesh   [#004]-Revised Sc...
116
                              state:  data['Student State*'],
9db2d9cbe   Deepak   fixed error in cs...
117
                              zip:    data['Student Zip Code*']
167dd6325   satheesh   [#004]-Revised Sc...
118
                            },
0c2ad31a0   satheesh   [#003]
119
          parent:       [{id: newParentUserId, relatinship: data['Parent Relation*']}]
102b779c9   satheesh   [#0001]-Student_csv
120
121
122
123
124
125
        });
      }
      return {newStudentId};
    },
  
  });
d0a93cc6d   Deepak   csv file
126
127
  export const studentUploadCsv = new ValidatedMethod({
    name: 'student.uploadCsv',
5d043355e   Deepak   fixed bug and add...
128
    validate: null,
d0a93cc6d   Deepak   csv file
129

102b779c9   satheesh   [#0001]-Student_csv
130
131
132
    run({data}) {
     let validation = new Validation();
     let constants = new Constants();
dd5d357af   satheesh   [#005]-CSV duplic...
133

5d043355e   Deepak   fixed bug and add...
134
    //  var data_1=csv1.csvtojson(("/Users/satheeshnagaraj/Downloads/11.csv")); //csvtojson is function that accepts csv filenames and returns JSON object
102b779c9   satheesh   [#0001]-Student_csv
135
     //console.log(data);''
0c2ad31a0   satheesh   [#003]
136
   //  Stores  = new Mongo.Collection('stores');
5d043355e   Deepak   fixed bug and add...
137
    //  data = data_1;
102b779c9   satheesh   [#0001]-Student_csv
138
139
140
  
              var CSV_valid_buffer   = [];
              var CSV_invalid_buffer = [];
167dd6325   satheesh   [#004]-Revised Sc...
141
              var filename           = "Student" + new Date().getTime().toString();
dd5d357af   satheesh   [#005]-CSV duplic...
142
143
              var csv_filepath ='/Users/satheeshnagaraj/Documents/Workspace/Meteor/production/ydapp/CSV_Files/';  
              //var csv_filepath = '/Users/deepak/dev/yd/csv';
9db2d9cbe   Deepak   fixed error in cs...
144

5d043355e   Deepak   fixed bug and add...
145
146
147
148
  
  
              for (let i = 0; i < data.length; i++)
                  {
102b779c9   satheesh   [#0001]-Student_csv
149
150
151
152
153
154
155
156
  
                    //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   Deepak   fixed bug and add...
157
                        {
102b779c9   satheesh   [#0001]-Student_csv
158
159
160
                            var value = item[key];
                            var n = key.indexOf("*");
                            if(n!=-1) {
5d043355e   Deepak   fixed bug and add...
161
                                         if(!(validation.notNull(value)))
102b779c9   satheesh   [#0001]-Student_csv
162
163
164
                                         {
                                           errors.push(key+"is Empty");
                                         }
5d043355e   Deepak   fixed bug and add...
165
                                         is_not_null = is_not_null && validation.notNull(value);
102b779c9   satheesh   [#0001]-Student_csv
166
167
168
169
170
171
172
173
174
175
176
                                      }
                        }
                    //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   Deepak   fixed bug and add...
177
                        errors.push('Parent Email is invalid');
102b779c9   satheesh   [#0001]-Student_csv
178
179
180
                    }
                    if (!(validation.mobileNumber(item["Parent Mobile*"])))
                    {
5d043355e   Deepak   fixed bug and add...
181
                          errors.push('Parent Mobile is invalid');
102b779c9   satheesh   [#0001]-Student_csv
182
183
184
                    }
  
                    var is_valid = formate_validation && is_not_null ;
5d043355e   Deepak   fixed bug and add...
185

102b779c9   satheesh   [#0001]-Student_csv
186
187
                   // console.log(formate_validation);
                      if (is_valid)
5d043355e   Deepak   fixed bug and add...
188
                         {
dd5d357af   satheesh   [#005]-CSV duplic...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
            
                            exists = Users.findOne({"firstName": item["First Name*"],"lastName": item["Last Name*"]}, {"_id": 1});
                            
                                 if (!exists)
                                    { 
                                      CSV_valid_buffer.push(item);
                                      Meteor.call('student.addCSV',item);
                                      console.warn('done');
                                    }
                                 else
                                    { 
                                      console.log(exists._id);
                                      student_data  = Students.findOne({"userId":exists._id});
                                      console.log("--------------"+student_data);
  
                                      parent_data   = Users.findOne({"_id":student_data.parent[0].id});
  
                                      console.log(parent_data.emails[0].address);
                                      if ( parent_data.emails[0].address != item["Parent Email*"])
                                       {
                                          CSV_valid_buffer.push(item);
                                          Meteor.call('student.addCSV',item);
                                          console.warn('done');
                                       }
                                      else
                                       { 
                                          console.warn('This data already exists.');
                                          errors.push('This data already exists');
                                          var str = errors.toString();
                                          item.Errors = str;
                                          CSV_invalid_buffer.push(item);   
                                       }   
                                    }
                           }
102b779c9   satheesh   [#0001]-Student_csv
223
                  }
102b779c9   satheesh   [#0001]-Student_csv
224
225
              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   Deepak   csv file
226
227
228
229
      return {};
    },
  
  });