Newer
Older
import { DatePipe } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Component, ElementRef, OnInit, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core';
import { AlertController, LoadingController, MenuController, ModalController } from '@ionic/angular';
import { TranslateService } from '@ngx-translate/core';
import { of } from 'rxjs';
import { catchError, finalize, tap } from 'rxjs/operators';
import { VisaWorkflowComponent } from '../document/visa-workflow/visa-workflow.component';
import { AuthService } from '../service/auth.service';
import { NotificationService } from '../service/notification.service';
import { SignaturesContentService } from '../service/signatures.service';
import { SignaturePositionComponent } from './signature-position/signature-position.component';
import { ActionsService } from '../service/actions.service';
import { FunctionsService } from '../service/functions.service';
@Component({
templateUrl: 'indexation.component.html',
styleUrls: ['indexation.component.scss'],
})
export class IndexationComponent implements OnInit {
@ViewChild('appVisaWorkflow', { static: false }) appVisaWorkflow: VisaWorkflowComponent;
@ViewChild('rightContent', { static: true }) rightContent: TemplateRef<any>;
@ViewChild('docToUpload') fileImport: ElementRef;
loading: boolean = false;
fromDocument: number = null;
constructor(
private translate: TranslateService,
private menu: MenuController,
public signaturesService: SignaturesContentService,
public viewContainerRef: ViewContainerRef,
public notificationService: NotificationService,
public loadingController: LoadingController,
public alertController: AlertController,
public datePipe: DatePipe,
public modalController: ModalController,
public actionsService: ActionsService,
private functionsService: FunctionsService

Alex ORLUC
committed
ngOnInit(): void { }
ionViewWillEnter() {
this.menu.enable(true, 'left-menu');
this.menu.enable(true, 'right-menu');
this.signaturesService.initTemplate(this.rightContent, this.viewContainerRef, 'rightContent');
if (window.history.state.documentId !== undefined) {
this.fromDocument = window.history.state.documentId;
this.getDocumentData(this.fromDocument);
}
}
getDocumentData(resId: number) {
return new Promise((resolve) => {
this.http.get(`../rest/documents/${resId}`).pipe(
tap((data: any) => {
if (!this.functionsService.empty(data.document.reference)) {
let arrRef = data.document.reference.split('/');
arrRef = arrRef.slice(3, arrRef.length);
if (arrRef.length > 0) {
ref = arrRef.join('/');
}
this.filesToUpload.push({
title: data.document.title,
mainDocument: true,
content: '',
linkId: data.document.linkId,
metadata: data.document.metadata,
});
this.getDocumentContent(resId);
this.appVisaWorkflow.loadWorkflow(data.document.workflow.map((item: any) => {
item.userSignatureModes.unshift('visa');

Hamza HRAMCHI
committed
item.signatureMode = item.userId === null && item.signatureMode === 'stamp' ? 'sign' : item.signatureMode;
this.appVisaWorkflow.formatRole(item);
return {
...item,

Hamza HRAMCHI
committed
processDate: null,
current: false,
role: item.mode === 'visa' ? 'visa' : item.signatureMode,
modes: item.userId === null ? item.modes : item.userSignatureModes
};
}));
for (let index = 0; index < data.document.attachments.length; index++) {
this.getAttachment(data.document.attachments[index].id);
}

Hamza HRAMCHI
committed
const externalUsers: any[] = this.appVisaWorkflow.getCurrentWorkflow().filter((user: any) => user.userId === null);
if (externalUsers.length > 0) {
externalUsers.forEach(async (element: any) => {
const obj: any = await this.appVisaWorkflow.formatData(element);
element = obj;
});
resolve(true);
}),
catchError((err: any) => {
this.notificationService.handleErrors(err);
return of(false);
})
).subscribe();
});
}
getDocumentContent(resId: number) {
return new Promise((resolve) => {
this.http.get(`../rest/documents/${resId}/content?type=original`).pipe(
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
tap((data: any) => {
this.filesToUpload[0].content = data.encodedDocument;
resolve(true);
}),
catchError((err: any) => {
this.notificationService.handleErrors(err);
return of(false);
})
).subscribe();
});
}
getAttachment(attachId: number) {
return new Promise((resolve) => {
this.http.get(`../rest/attachments/${attachId}`).pipe(
tap((data: any) => {
this.filesToUpload.push({
title: data.attachment.title,
mainDocument: false,
content: data.attachment.encodedDocument
});
resolve(true);
}),
catchError((err: any) => {
this.notificationService.handleErrors(err);
return of(false);
})
).subscribe();
});
}
ionViewWillLeave() {
this.signaturesService.detachTemplate('rightContent');
}
async promptSaveDoc() {
const alert = await this.alertController.create({

Alex ORLUC
committed
cssClass: 'alert-info-no-msg',
header: this.translate.instant('lang.areYouSure'),
inputs: [
{
name: 'note',
id: 'note',
type: 'textarea',
placeholder: this.translate.instant('lang.addNote')
},
],
buttons: [
{
text: this.translate.instant('lang.cancel'),
role: 'cancel',
cssClass: 'secondary',
handler: () => { }
},
{
text: this.translate.instant('lang.validate'),
handler: (data: any) => {
this.loadingController.create({
message: this.translate.instant('lang.processing'),
spinner: 'dots'
}).then(async (load: HTMLIonLoadingElement) => {
load.present();
const objTosend = this.formatData(data.note);
for (let index = 0; index < objTosend.length; index++) {

Hamza HRAMCHI
committed
await this.saveDocument(objTosend, index);
if (objTosend[0].workflow[0].userId === this.authService.user.id) {
this.actionsService.setEvent('refreshList');
}
}
load.dismiss();
if (this.errors.length === 0) {
this.notificationService.success('lang.documentsImported');
this.router.navigate(['/home']);
}
});
}
}
]
});
await alert.present();
}
saveDocument(data: any, index: number) {
return new Promise((resolve) => {

Hamza HRAMCHI
committed
this.http.post('../rest/documents', data[index]).pipe(
this.errors = [];
}),
finalize(() => resolve(true)),
catchError((err: any) => {

Hamza HRAMCHI
committed
this.errors.push(data[index].title);
this.notificationService.handleErrors(err);
return of(false);
})
).subscribe();
});
}
formatData(note: string) {

Alex ORLUC
committed
const today: Date = new Date();
let noteObj: any = null;

Alex ORLUC
committed
let linkId: string = null;
if (note !== '') {
noteObj = {
value: note,
creator: `${this.authService.user.firstname} ${this.authService.user.lastname}`,
creationDate: this.datePipe.transform(today, 'dd-MM-y')

Alex ORLUC
committed
const formattedObj: any[] = [];
const signedFiles = this.filesToUpload.filter((item: any) => item.mainDocument);
const attachFiles = this.filesToUpload.filter((item: any) => !item.mainDocument);

Alex ORLUC
committed
if (signedFiles.length > 1) {
linkId = this.datePipe.transform(today, 'ddMMYhmmss') + '_' + Math.random().toString(36).substr(2, 9);
}
signedFiles.forEach((file: any) => {
const metadata = {};
if (this.fromDocument !== null) {
file.metadata.forEach((element: any) => {
metadata[element.label] = element.value;
});
}

Florian Azizian
committed
let formattedReference = '';
if (file.reference !== '') {
formattedReference = this.datePipe.transform(today, 'y/MM/dd') + '/' + file.reference;
}
docIndex: file.docIndex,

Florian Azizian
committed
reference: formattedReference,
linkId: this.fromDocument !== null ? file.linkId : linkId,
sender: `${this.authService.user.firstname} ${this.authService.user.lastname}`,
attachments: attachFiles.map((item: any) => ({
title: item.title,
encodedDocument: item.content
})),
workflow: this.appVisaWorkflow.getCurrentWorkflow().map((item: any, index: number) => ({

Hamza HRAMCHI
committed
externalInformations: item.otp === undefined ? item.externalInformations : item.otp,
userId: item.userId,
mode: this.authService.getWorkflowMode(item.role),
signatureMode: this.authService.getSignatureMode(item.role),
signaturePositions: item.signaturePositions !== undefined ? this.formatPositions(item.signaturePositions.filter((pos: any) => pos.mainDocument === file.mainDocument && file.signPos !== undefined && pos.docIndex === file.docIndex)).map((itemFile: any) => ({
page: itemFile.page,

Hamza HRAMCHI
committed
docIndex: itemFile.docIndex,
positionX: itemFile.position.positionX,
positionY: itemFile.position.positionY,
})) : []
})),
metadata: metadata
});
});
return formattedObj;
}
formatPositions(position: any) {
delete position.mainDocument;
return position;
}
dndUploadFile(event: any) {
const fileInput = {
target: {
}
};
this.uploadTrigger(fileInput);
}
uploadTrigger(fileInput: any) {
this.loadingController.create({
message: this.translate.instant('lang.loadingDocument'),
spinner: 'dots'
}).then(async (load: HTMLIonLoadingElement) => {
load.present();
if (fileInput.target.files && fileInput.target.files[0] && this.isExtensionAllowed(fileInput.target.files)) {
for (let index = 0; index < fileInput.target.files.length; index++) {
const filename = fileInput.target.files[index].name;
const file = {
title: filename.substr(0, filename.lastIndexOf('.')),
reference: filename.substr(0, filename.lastIndexOf('.')).substr(0, 53),
mainDocument: true,
content: '',
docIndex: index,
};
const reader = new FileReader();
reader.readAsArrayBuffer(fileInput.target.files[index]);
reader.onload = (value: any) => {
file.mainDocument = this.filesToUpload.length === 0;
file.reference = this.filesToUpload.length === 0 ? file.reference : '';
file.content = this.getBase64Document(value.target.result);
this.filesToUpload.push(file);
this.setFilePosition();
if (this.filesToUpload.length === 1) {
setTimeout(() => {
this.menu.open('right-menu');
}, 500);
}
};

Hamza HRAMCHI
committed
if (index === fileInput.target.files.length - 1) {
load.dismiss();
}
}
this.fileImport.nativeElement.value = '';
} else {
this.loading = false;
load.dismiss();
});
setFilePosition() {
this.filesToUpload.forEach((file: any, position: number) => {
file['docIndex'] = position;
});
}
isExtensionAllowed(files: any[]) {
for (let index = 0; index < files.length; index++) {
if (files[index].name.toLowerCase().split('.').pop() !== 'pdf') {
this.notificationService.error('lang.onlyPdfAuthorized');
return false;
}
}
return true;
}
getBase64Document(buffer: ArrayBuffer) {
const TYPED_ARRAY = new Uint8Array(buffer);
const STRING_CHAR = TYPED_ARRAY.reduce((data, byte) => data + String.fromCharCode(byte), '');
return btoa(STRING_CHAR);
}
deleteFile(index: number) {
this.filesToUpload.splice(index, 1);

Hamza HRAMCHI
committed
if (this.filesToUpload.length === 0) {
this.appVisaWorkflow.getCurrentWorkflow().forEach((element: any) => {
element.signaturePositions = [];
});
}
this.setFilePosition();

Hamza HRAMCHI
committed
const allUsers: any[] = this.appVisaWorkflow.getCurrentWorkflow();
const usersWithNoteRole: any[] = this.appVisaWorkflow.getCurrentWorkflow().filter((user: any) => user.role === 'note');
if ((allUsers.length === 1 && this.appVisaWorkflow.getCurrentWorkflow()[0].role === 'note') || (allUsers.length === usersWithNoteRole.length)) {

Hamza HRAMCHI
committed
this.notificationService.error('lang.cannotSetSignature');
} else if (this.appVisaWorkflow.getCurrentWorkflow().length > 0) {
this.appVisaWorkflow.getCurrentWorkflow().forEach((user: any, indexUser: number) => {
if (user.signaturePositions === undefined) {
this.appVisaWorkflow.visaWorkflow[indexUser].signaturePositions = [];
}
});
const modal = await this.modalController.create({
component: SignaturePositionComponent,
cssClass: 'custom-alert-fullscreen',
componentProps: {

Hamza HRAMCHI
committed
workflow: this.appVisaWorkflow.getCurrentWorkflow(),
resource: this.filesToUpload[index],
pdfContent: 'data:application/pdf;base64,' + this.filesToUpload[index].content,
docIndex: this.filesToUpload.indexOf(this.filesToUpload[index])
}
});
await modal.present();
const { data } = await modal.onWillDismiss();
if (data !== undefined) {
this.filesToUpload[index].signPos = data;

Hamza HRAMCHI
committed
this.appVisaWorkflow.setPositionsWorkfow(this.filesToUpload, index, data);
this.notificationService.error('lang.mustSetWorkflowBeforeSignPositions');
if (this.filesToUpload.filter((item: any) => item.title === '').length > 0) {
this.notificationService.error('lang.subjectMandatory');
return false;
} else if (this.filesToUpload.filter((item: any) => item.mainDocument).length === 0) {
this.notificationService.error('lang.mainDocumentMandatory');
return false;
} else if (this.appVisaWorkflow.getCurrentWorkflow().length === 0) {
this.notificationService.error('lang.workflowUserstMandatory');
this.menu.open('right-menu');
return false;
} else if (this.appVisaWorkflow.getCurrentWorkflow().filter((user: any) => user.userId === null && user.noConnector !== undefined).length > 0) {

Hamza HRAMCHI
committed
this.notificationService.error('lang.noConnector');
return false;
} else if (this.hasEmptyOtpSignaturePosition()) {
this.notificationService.error('lang.otpSignaturePositionMandatory');
return false;
hasEmptyOtpSignaturePosition() {
const workflow = this.formatData(null)[0].workflow;
const isSign = /_sign_/g;
const hasEmptyPosition = workflow.filter((item: any) => item.signaturePositions.length === 0 && !this.functionsService.empty(item.externalInformations) && !item.mode.includes('fast') && item.mode.match(isSign) !== null).length > 0;
return hasEmptyPosition;
}