Blame view

imports/collections/students/serverCsvUpload.js 7.57 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
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   Deepak   fixed bug and add...
52

102b779c9   satheesh   [#0001]-Student_csv
53
54
55
56
    run(item){
      data = item ;
      console.log("data");
      console.log(data);
0c2ad31a0   satheesh   [#003]
57
     // return true;
102b779c9   satheesh   [#0001]-Student_csv
58
59
60
61
      const user = Users.findOne({_id: this.userId});
      orgId = user.orgId;
      newStudentId = Users.insert({
       // emails:     [{address:data.email, verified: false}],
0c2ad31a0   satheesh   [#003]
62
        //username:   data["First Name*"],
5d043355e   Deepak   fixed bug and add...
63
64
        firstName:  data["First Name*"],
        lastName:   data['Last Name*'],
102b779c9   satheesh   [#0001]-Student_csv
65
66
67
68
69
        orgId:      orgId,
        role:       'STUDENT'
      });
      newParentUserId = Users.insert({
        //emails:     [{address:data.parentEmail, verified: false}],
bea06ff0b   Deepak   Fixed teachers view
70
        //username:   data['Parent Name*'],
0c2ad31a0   satheesh   [#003]
71
        firstName:  data['Parent Name*'],
102b779c9   satheesh   [#0001]-Student_csv
72
73
74
75
76
77
78
        orgId:      orgId,
        role:       'PARENT'
      });
      if(newParentUserId){
        newParentId = Parents.insert({
          userId: newParentUserId,
          orgId: orgId,
102b779c9   satheesh   [#0001]-Student_csv
79
80
81
82
83
84
85
86
87
88
        });
        console.log("newParentUserId");
        console.log(newParentUserId);
      }
      console.log("newUserId");
      console.log(newStudentId);
      if(newStudentId){
        Students.insert({
          userId: newStudentId,
          orgId: orgId,
0c2ad31a0   satheesh   [#003]
89
90
91
          admissionId:  data['Student Admission ID*'],
          address:      data['Student Admission ID*'],
          gender:       data['Gender(male/female)*'],
5d043355e   Deepak   fixed bug and add...
92
          dob:          data['Birthday(YYYY-MM-DD)*'],
0c2ad31a0   satheesh   [#003]
93
94
95
96
97
98
          rollNo:       data['Student Roll*'],
          class:        data['Student Class Name*'],
          section:      data['Student Section Name*'],
          bloodGroup:   data['Blood Group*'],
          community:    data['Community'],
          parent:       [{id: newParentUserId, relatinship: data['Parent Relation*']}]
102b779c9   satheesh   [#0001]-Student_csv
99
100
101
102
103
104
        });
      }
      return {newStudentId};
    },
  
  });
d0a93cc6d   Deepak   csv file
105
106
  export const studentUploadCsv = new ValidatedMethod({
    name: 'student.uploadCsv',
5d043355e   Deepak   fixed bug and add...
107
    validate: null,
d0a93cc6d   Deepak   csv file
108

102b779c9   satheesh   [#0001]-Student_csv
109
110
111
112
113
114
115
    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   Deepak   fixed bug and add...
116
    //  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
117
     //console.log(data);''
0c2ad31a0   satheesh   [#003]
118
   //  Stores  = new Mongo.Collection('stores');
5d043355e   Deepak   fixed bug and add...
119
    //  data = data_1;
102b779c9   satheesh   [#0001]-Student_csv
120
121
122
123
  
              var CSV_valid_buffer   = [];
              var CSV_invalid_buffer = [];
              var filename           = new Date().getTime().toString();
bea06ff0b   Deepak   Fixed teachers view
124
              var csv_filepath = './CSV_Files/';
5d043355e   Deepak   fixed bug and add...
125
126
127
128
  
  
              for (let i = 0; i < data.length; i++)
                  {
102b779c9   satheesh   [#0001]-Student_csv
129
130
131
132
133
134
135
136
  
                    //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...
137
                        {
102b779c9   satheesh   [#0001]-Student_csv
138
139
140
                            var value = item[key];
                            var n = key.indexOf("*");
                            if(n!=-1) {
5d043355e   Deepak   fixed bug and add...
141
                                         if(!(validation.notNull(value)))
102b779c9   satheesh   [#0001]-Student_csv
142
143
144
                                         {
                                           errors.push(key+"is Empty");
                                         }
5d043355e   Deepak   fixed bug and add...
145
                                         is_not_null = is_not_null && validation.notNull(value);
102b779c9   satheesh   [#0001]-Student_csv
146
147
148
149
150
151
152
153
154
155
156
                                      }
                        }
                    //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...
157
                        errors.push('Parent Email is invalid');
102b779c9   satheesh   [#0001]-Student_csv
158
159
160
                    }
                    if (!(validation.mobileNumber(item["Parent Mobile*"])))
                    {
5d043355e   Deepak   fixed bug and add...
161
                          errors.push('Parent Mobile is invalid');
102b779c9   satheesh   [#0001]-Student_csv
162
163
164
                    }
  
                    var is_valid = formate_validation && is_not_null ;
5d043355e   Deepak   fixed bug and add...
165

102b779c9   satheesh   [#0001]-Student_csv
166
167
                   // console.log(formate_validation);
                      if (is_valid)
5d043355e   Deepak   fixed bug and add...
168
                         {
102b779c9   satheesh   [#0001]-Student_csv
169
170
171
172
                              CSV_valid_buffer.push(item);
                              console.log("----------------------------------------1");
                              Meteor.call('student.addCSV',item);
                              console.log("----------------------------------------2");
5d043355e   Deepak   fixed bug and add...
173

102b779c9   satheesh   [#0001]-Student_csv
174
                           //   exists = Stores.findOne({
5d043355e   Deepak   fixed bug and add...
175
                           //                               "Student 'Admission' ID*": item["Student Admission ID*"]
102b779c9   satheesh   [#0001]-Student_csv
176
                            //                          });
5d043355e   Deepak   fixed bug and add...
177
                           //   if (!exists)
102b779c9   satheesh   [#0001]-Student_csv
178
179
180
                            //     {
                           //        Stores.insert(item);
                            //     }
5d043355e   Deepak   fixed bug and add...
181
                            //  else
102b779c9   satheesh   [#0001]-Student_csv
182
183
184
                             //    {
                            //       console.warn('Rejected. This item already exists.');
                             //    }
5d043355e   Deepak   fixed bug and add...
185
186
                          }
                      else
102b779c9   satheesh   [#0001]-Student_csv
187
188
189
190
191
192
193
194
195
196
197
198
                         {
                         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   Deepak   csv file
199
200
201
202
      return {};
    },
  
  });