import { Component, OnInit } 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 { headerProps: IHeaderProps = { rightIcon: logOutButton, rightIconRoute: 'logout', title: 'NPS EXPORTS VIEW', }; fileUploadForm: FormGroup; isLoading = false; error = ''; isSalesForm = true; file: any; arrayBuffer: any; filelist: any = []; arrayToBeSend: 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.arrayToBeSend = []; this.isSalesForm = !this.isSalesForm; } triggerWhatsappMessages(phoneNumber: string[]) { const httpBody = { messages: [ { from: '447491163530', // TODO: need check if we can send array here to: phoneNumber[1], messageId: 'a28dd97c-1ffb-4fcf-99f1-0b557ed381da', content: { templateName: 'infobip_kz_test_hsm_2', templateData: { body: { placeholders: ['BFL Credit Card'], }, }, language: 'en_GB', }, callbackData: 'Callback data', }, ], }; this.homeService.sendWhatsApp(httpBody).subscribe( (res) => { console.log('success', res); this.isLoading = false; }, (err) => { console.log('failed',err); this.isLoading = false; } ) } readExcelFile(){ this.isLoading = true; 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: string[] = []; 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++) { var details = { 'CUSTOMER NAME': this.filelist[i]['CUSTOMER NAME'], 'PHONE NUMBER': this.filelist[i]['PHONE NUMBER'], 'LEAD ID': this.filelist[i]['LEAD ID'], 'DATE OF ENQUIRY': this.filelist[i]['DATE OF ENQUIRY'], }; this.arrayToBeSend.push(details); phoneNumber.push(this.filelist[i]['PHONE NUMBER']); } } else { for (let i = 0; i < this.filelist.length; i++) { var details = { 'CUSTOMER NAME': this.filelist[i]['Customer'], 'PHONE NUMBER': this.filelist[i]['Mobile No'], 'LEAD ID': this.filelist[i]['__EMPTY'], 'DATE OF ENQUIRY': this.filelist[i]['Ready For Invoice Date Time'], }; this.arrayToBeSend.push(details); phoneNumber.push(this.filelist[i]['PHONE NUMBER']); } } // after reading file and storing all phonenumbers in an array // call whatsapp sending api function here this.triggerWhatsappMessages(phoneNumber); }; } addfile(event: any) { this.file = event.target.files[0]; } }