You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

172 lines
4.7 KiB

import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import IHeaderProps from 'src/app/models/header';
import { logOutButton, login, home } from 'src/app/constants/constants';
import { RouterService } from 'src/app/shared/services/routerService';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { LeadscoreService } from 'src/app/shared/services/leadscore.service';
import { first, finalize } from 'rxjs/operators';
import { IGoogleFormData } from 'src/app/models/googleFormData';
import { AuthenticationService } from 'src/app/shared/services/authentication.service';
import { Subscription } from 'rxjs';
import * as XLSX from 'xlsx';
import { HomeService } from './home.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
})
export class HomeComponent {
@ViewChild('fileUploader') fileUploader: ElementRef;
headerProps: IHeaderProps = {
rightIcon: logOutButton,
rightIconRoute: 'logout',
title: 'NPS EXPORTS VIEW',
};
fileUploadForm: FormGroup;
isLoading = false;
submitButton = false;
error = '';
isSalesForm = true;
submitted = false;
file: any;
arrayBuffer: any;
filelist: any = [];
customerPhonenumbers: string[] = [];
constructor(
private routerService: RouterService,
private formBuilder: FormBuilder,
private homeService: HomeService
) {
this.fileUploadForm = this.formBuilder.group({
fileUpload: [
'',
[
Validators.required,
Validators.minLength(10),
Validators.pattern('^[0-9]*'),
],
],
});
}
get f() {
return this.fileUploadForm.controls;
}
switchSalesServiceTab() {
this.submitButton = false;
this.removeSelectedFile();
this.isSalesForm = !this.isSalesForm;
}
triggerWhatsappMessages(messages: object[]) {
let feedbackURL = 'https://be18n8dnwum.typeform.com/to/OBJbe7fB';
if (!this.isSalesForm) {
feedbackURL = 'https://r3ng0yuxlm2.typeform.com/to/UieAnCVh';
}
const omniBody = {
scenarioKey: '0A9B6D3A044E5EF5D38E688900202DD8',
destinations: messages,
sms: {
text: 'Hi, Thank you. Bajaj Auto Test Message.',
regional: {
indiaDlt: {
contentTemplateId: '1107161477011893589',
principalEntityId: '1101635620000025441',
},
},
},
whatsApp: {
templateName: 'infobip_kz_test_hsm_2',
templateData: ['feedback URL: ' + feedbackURL],
language: 'en_GB',
},
};
this.homeService.sendOmni(omniBody).subscribe(
(res) => {
console.log('success', res);
this.isLoading = false;
// this.submitButton = true;
this.submitted = true;
this.removeSelectedFile();
},
(err) => {
console.log('failed', err);
this.isLoading = false;
// this.submitButton = true;
}
);
}
readExcelFile() {
this.isLoading = true;
this.submitButton = false;
let fileReader = new FileReader();
fileReader.readAsArrayBuffer(this.file);
fileReader.onload = (e) => {
this.arrayBuffer = fileReader.result;
let data = new Uint8Array(this.arrayBuffer);
let arr = new Array();
let phoneNumber: object[] = [];
for (let i = 0; i != data.length; ++i) {
arr[i] = String.fromCharCode(data[i]);
}
let bstr = arr.join('');
let workbook = XLSX.read(bstr, { type: 'binary' });
let first_sheet_name = workbook.SheetNames[0];
let worksheet = workbook.Sheets[first_sheet_name];
this.filelist = XLSX.utils.sheet_to_json(worksheet, { raw: true });
// pull data according to upload file type
if (this.isSalesForm) {
for (let i = 0; i < this.filelist.length; i++) {
let data = {
to: {
phoneNumber: '91' + this.filelist[i]['PHONE NUMBER'],
},
};
phoneNumber.push(data);
}
} else {
for (let i = 0; i < this.filelist.length; i++) {
let data = {
to: {
phoneNumber: '91' + this.filelist[i]['Mobile No'],
},
};
phoneNumber.push(data);
}
phoneNumber.shift();
}
// after reading file and storing all phonenumbers in an array
// call whatsapp sending api function here
this.triggerWhatsappMessages(phoneNumber);
};
}
addfile(event: any) {
this.submitButton = true;
this.file = event.target.files[0];
}
removeSelectedFile() {
this.fileUploader.nativeElement.value = null;
}
feedback() {
this.routerService.navigate('feedbacks');
}
}