Blame view

imports/collections/staff/serverCsvUpload.js 7.53 KB
7ca753f37   Deepak   added staff uploa...
1
2
3
4
5
6
7
8
9
10
11
12
13
  // import { } from '/imports/collections/staff/methods';
  import _                                  from 'lodash';
  import { Meteor }                         from 'meteor/meteor';
  import Papa                               from 'meteor/harrison:papa-parse'
  import csv                                from 'csv2json-convertor'
  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';
  import { Students }                       from '/imports/collections/students/index'
  import { Parents }                        from '/imports/collections/parents/index';
ec5f044a9   Deepak   Finished add teac...
14
  import { Staffs }                         from '/imports/collections/staff/index';
7ca753f37   Deepak   added staff uploa...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  import csv1                               from 'csv2json-convertor'
  import json2csv                           from 'json2csv'
  import fs                                 from 'fs'
  import Validation                         from '/imports/validation/validationMethods';
  import Constants                          from '/imports/constants/constants'
  
  export const staffCsvtMethod = new ValidatedMethod({
    name: 'staff.csvMethod',
  
    validate: new SimpleSchema({
      itemId: { type: String },
    }).validator(),
  
    run({itemId}) {
      return {};
    },
  
  });
  
  export const staffSaveCsvData = new ValidatedMethod({
    name: 'staff.saveCsvData',
  
    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 addStaffCSV= new ValidatedMethod({
    name: 'staff.addCSV',
  
    validate: null,
  
    run(item){
      data = item ;
      console.log("data");
      console.log(data);
     // return true;
      const user = Users.findOne({_id: this.userId});
      orgId = user.orgId;
      newStaffId = Users.insert({
       // emails:     [{address:data.email, verified: false}],
        //username:   data["First Name*"],
        firstName:  data["First Name*"],
        lastName:   data['Last Name*'],
        orgId:      orgId,
        role:       'STAFF'
      });
      console.log("newStaffId");
      console.log(newStaffId);
167dd6325   satheesh   [#004]-Revised Sc...
72

7ca753f37   Deepak   added staff uploa...
73
      if(newStaffId){
167dd6325   satheesh   [#004]-Revised Sc...
74
        newStaff = Staffs.insert({
7ca753f37   Deepak   added staff uploa...
75
76
          userId: newStaffId,
          orgId: orgId,
167dd6325   satheesh   [#004]-Revised Sc...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  
          employeeId:          data['Employee ID*'],
          martialStatus:       data['Marital Status(married/unmarried)*'],
          dob:                 data['Staff Birthday(YYYY-MM-DD)*'],
          gender:              data['Gender(male/female)*'],
          teaching:            data['Teaching Staff(yes/no)*'],
          type:                data['Job Type(permanent/contract)*'],
          doj:                 data['Date of joining(YYYY-MM-DD)*'],
  
          bloodGroup:          data['Blood group*'],
          nationality:         data['Nationality*'],
          motherTongue:        data['Mother Tongue*'],
          religion:            data['Religion*'],
  
          PANNumber:           data['PAN Number'],
          ESINumber:           data['ESI Number'],
          aadharNumber:        data['Aadhar Number'],
          PFNumber:            data['PF Number'],
  
          permanentAddress: { street: data['Staff Line Address*'],
                              city:   data['City*'], 
                              state:  data['State*'],
                              zip:    data['Zip*'] 
                            },
  
  
          bankdetails: {     bankAccountNo:     data['Bank Account No'],
                              bankIFSC:          data['Bank IFSC'], 
                              bankBranchDetails: data['Bank Branch Details']
                       },
  
          workingExperience: [{   previousJobRole:      data['Previous job Role'],
                                  previousJobType:      data['Previous Job Type'], 
                                  previousOrganization: data['Previous Organization'],
                                  from:                 data['From'],
                                  to:                   data['To']
                             }],   
  
          educationDetails:  [{   qualifaication:      data['Qualification*'],
                                  specialization:      data['Specialization*'], 
                                  university:          data['University*'],
                                  from:                data['From*'],
                                  to:                  data['To*']
                             }]
7ca753f37   Deepak   added staff uploa...
121
        });
167dd6325   satheesh   [#004]-Revised Sc...
122
123
        console.log("newStaff");
        console.log(newStaff);
7ca753f37   Deepak   added staff uploa...
124
      }
167dd6325   satheesh   [#004]-Revised Sc...
125
      return {newStaffId};
7ca753f37   Deepak   added staff uploa...
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    },
  
  });
  
  export const staffUploadCsv = new ValidatedMethod({
    name: 'staff.uploadCsv',
  
    validate: null,
  
    run({data}) {
       let validation = new Validation();
       let constants = new Constants();
       //console.log("++++++++++++++++++++++++"+constants.csv_students_data());
       temp = constants.csv_students_data();
       console.log(temp);
       var CSV_valid_buffer   = [];
       var CSV_invalid_buffer = [];
167dd6325   satheesh   [#004]-Revised Sc...
143
144
       var filename           = "Staff-" + new Date().getTime().toString();
       var csv_filepath = '/Users/satheeshnagaraj/Documents/Workspace/Meteor/Youngdesk/ydapp/CSV_Files/';
7ca753f37   Deepak   added staff uploa...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
       for (let i = 0; i < data.length; i++)
          {
              let item = data[i];
              delete item['Errors'];
              var formate_validation = 1;
              var is_not_null = 1;
              var errors = [];
              for (var key in item){
                var value = item[key];
                var n = key.indexOf("*");
                if(n!=-1) {
                    if(!(validation.notNull(value)))
                    {
                      errors.push(key+"is Empty");
                    }
                    is_not_null = is_not_null && validation.notNull(value);
                  }
              }
167dd6325   satheesh   [#004]-Revised Sc...
163
              var formate_validation = validation.validateEmail(item["Staff Email*"]) && validation.mobileNumber(item["Staff Phone*"]) ; //&& validation.validateEmail(item["Parent Mobile"]) );
7ca753f37   Deepak   added staff uploa...
164

167dd6325   satheesh   [#004]-Revised Sc...
165
166
                    if (!(validation.validateEmail(item["Staff Email*"]))){
                        errors.push('Staff Email is invalid');
7ca753f37   Deepak   added staff uploa...
167
                    }
167dd6325   satheesh   [#004]-Revised Sc...
168
                    if (!(validation.mobileNumber(item["Staff Phone*"])))
7ca753f37   Deepak   added staff uploa...
169
                    {
167dd6325   satheesh   [#004]-Revised Sc...
170
                          errors.push('Staffs Mobile is invalid');
7ca753f37   Deepak   added staff uploa...
171
172
173
174
175
176
177
                    }
  
                    var is_valid = formate_validation && is_not_null ;
                      if (is_valid)
                         {
                            CSV_valid_buffer.push(item);
                            console.log("----------------------------------------1");
167dd6325   satheesh   [#004]-Revised Sc...
178
179
                            
                            Meteor.call('staff.addCSV',item);
7ca753f37   Deepak   added staff uploa...
180
181
182
183
184
185
186
187
188
189
190
                            console.log("----------------------------------------2");
  
                          }
                      else
                         {
                           var str = errors.toString();
                           item.Errors = str;
                           CSV_invalid_buffer.push(item);
                         }
                  }
      console.log(csv_filepath+filename+"_CSV_invalid_data"+".csv");
167dd6325   satheesh   [#004]-Revised Sc...
191
192
      Meteor.call('staff.saveCsvData',{ item : CSV_invalid_buffer, filename: csv_filepath+filename+"_CSV_invalid_data"+".csv",csv_fiels :constants.csv_staff_data()});
      Meteor.call('staff.saveCsvData',{ item : CSV_valid_buffer,filename: csv_filepath+filename+"_CSV_valid_data"+".csv",csv_fiels :constants.csv_staff_data()});
7ca753f37   Deepak   added staff uploa...
193
194
195
196
      return {};
    },
  
  });