Blame view

imports/collections/staff/serverCsvUpload.js 7.99 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
  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*'],
dd5d357af   satheesh   [#005]-CSV duplic...
67
68
        emails: [{ address: data['Staff Email*'],verified: false }],
        phones: [{ number:  data['Staff Phone*'],verified: false }],
7ca753f37   Deepak   added staff uploa...
69
70
71
72
73
        orgId:      orgId,
        role:       'STAFF'
      });
      console.log("newStaffId");
      console.log(newStaffId);
167dd6325   satheesh   [#004]-Revised Sc...
74

7ca753f37   Deepak   added staff uploa...
75
      if(newStaffId){
167dd6325   satheesh   [#004]-Revised Sc...
76
        newStaff = Staffs.insert({
7ca753f37   Deepak   added staff uploa...
77
78
          userId: newStaffId,
          orgId: orgId,
167dd6325   satheesh   [#004]-Revised Sc...
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
121
122
  
          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...
123
        });
167dd6325   satheesh   [#004]-Revised Sc...
124
125
        console.log("newStaff");
        console.log(newStaff);
7ca753f37   Deepak   added staff uploa...
126
      }
167dd6325   satheesh   [#004]-Revised Sc...
127
      return {newStaffId};
7ca753f37   Deepak   added staff uploa...
128
129
130
131
132
133
134
135
136
137
138
139
    },
  
  });
  
  export const staffUploadCsv = new ValidatedMethod({
    name: 'staff.uploadCsv',
  
    validate: null,
  
    run({data}) {
       let validation = new Validation();
       let constants = new Constants();
7ca753f37   Deepak   added staff uploa...
140
141
       var CSV_valid_buffer   = [];
       var CSV_invalid_buffer = [];
167dd6325   satheesh   [#004]-Revised Sc...
142
143
       var filename           = "Staff-" + new Date().getTime().toString();
       var csv_filepath = '/Users/satheeshnagaraj/Documents/Workspace/Meteor/Youngdesk/ydapp/CSV_Files/';
7ca753f37   Deepak   added staff uploa...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
       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...
162
              var formate_validation = validation.validateEmail(item["Staff Email*"]) && validation.mobileNumber(item["Staff Phone*"]) ; //&& validation.validateEmail(item["Parent Mobile"]) );
7ca753f37   Deepak   added staff uploa...
163

167dd6325   satheesh   [#004]-Revised Sc...
164
165
                    if (!(validation.validateEmail(item["Staff Email*"]))){
                        errors.push('Staff Email is invalid');
7ca753f37   Deepak   added staff uploa...
166
                    }
167dd6325   satheesh   [#004]-Revised Sc...
167
                    if (!(validation.mobileNumber(item["Staff Phone*"])))
7ca753f37   Deepak   added staff uploa...
168
                    {
167dd6325   satheesh   [#004]-Revised Sc...
169
                          errors.push('Staffs Mobile is invalid');
7ca753f37   Deepak   added staff uploa...
170
171
172
                    }
  
                    var is_valid = formate_validation && is_not_null ;
dd5d357af   satheesh   [#005]-CSV duplic...
173

7ca753f37   Deepak   added staff uploa...
174
175
                      if (is_valid)
                         {
dd5d357af   satheesh   [#005]-CSV duplic...
176
                           exists = Users.findOne({"firstName": item["First Name*"],"lastName": item["Last Name*"],'emails.$.address' : item['Staff Email*'],'phones.$.number' : item['Staff Phone*']}, {"_id": 1});
167dd6325   satheesh   [#004]-Revised Sc...
177
                            
dd5d357af   satheesh   [#005]-CSV duplic...
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
                                 if (!exists)
                                    { 
                                      CSV_valid_buffer.push(item);
                                      Meteor.call('staff.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);
                                    }
                   
7ca753f37   Deepak   added staff uploa...
193
                          }
dd5d357af   satheesh   [#005]-CSV duplic...
194

7ca753f37   Deepak   added staff uploa...
195
                  }
dd5d357af   satheesh   [#005]-CSV duplic...
196

7ca753f37   Deepak   added staff uploa...
197
      console.log(csv_filepath+filename+"_CSV_invalid_data"+".csv");
167dd6325   satheesh   [#004]-Revised Sc...
198
199
      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...
200
201
202
203
      return {};
    },
  
  });