Newer
Older
import { Component, OnInit, Input, EventEmitter, Output, Inject, ViewChildren, QueryList, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { LANG } from '../../translate.component';
import { catchError, tap, finalize, exhaustMap, filter } from 'rxjs/operators';
import { of, forkJoin, Subject } from 'rxjs';
import { NotificationService } from '../../notification.service';
import { MatDialog, MAT_DIALOG_DATA, MatDialogRef, MatTabGroup } from '@angular/material';
import { AppService } from '../../../service/app.service';
import { DocumentViewerComponent } from '../../viewer/document-viewer.component';
import { SortPipe } from '../../../plugins/sorting.pipe';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ConfirmComponent } from '../../../plugins/modal/confirm.component';
import { FunctionsService } from '../../../service/functions.service';
import { ContactService } from '../../../service/contact.service';
import { ContactAutocompleteComponent } from '../../contact/autocomplete/contact-autocomplete.component';
@Component({
templateUrl: "attachment-create.component.html",
styleUrls: [
'attachment-create.component.scss',
'../../indexation/indexing-form/indexing-form.component.scss'
],
providers: [AppService, SortPipe, ContactService],
})
export class AttachmentCreateComponent implements OnInit {
lang: any = LANG;
loading: boolean = true;
sendMassMode: boolean = false;
attachmentsTypes: any[] = [];
creationMode: boolean = true;
attachFormGroup: FormGroup[] = [];
attachments: any[] = [];
now: Date = new Date();
resourceContacts: any[] = [];
selectedContact = new FormControl();
loadingContact: boolean = false;
@Input('resId') resId: number = null;
@ViewChildren('appDocumentViewer') appDocumentViewer: QueryList<DocumentViewerComponent>;
@ViewChildren('contactAutocomplete') contactAutocomplete: ContactAutocompleteComponent;
constructor(
public http: HttpClient,
@Inject(MAT_DIALOG_DATA) public data: any,
public dialogRef: MatDialogRef<AttachmentCreateComponent>,
public appService: AppService,
private notify: NotificationService,
private sortPipe: SortPipe,
public dialog: MatDialog,
public functions: FunctionsService,
private contactService: ContactService, ) {
async ngOnInit(): Promise<void> {
await this.loadAttachmentTypes();
await this.loadResource();
this.loading = false;
}
loadAttachmentTypes() {
return new Promise((resolve, reject) => {
this.http.get('../../rest/attachmentsTypes').pipe(
tap((data: any) => {
Object.keys(data.attachmentsTypes).forEach(templateType => {
if (data.attachmentsTypes[templateType].show) {
this.attachmentsTypes.push({
id: templateType,
...data.attachmentsTypes[templateType]
});
}
});
this.attachmentsTypes = this.sortPipe.transform(this.attachmentsTypes, 'label');
resolve(true)
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
this.dialogRef.close('');
return of(false);
})
).subscribe();
});
}
loadResource() {
return new Promise((resolve, reject) => {
this.http.get(`../../rest/resources/${this.data.resIdMaster}?light=true`).pipe(
tap(async (data: any) => {

Alex ORLUC
committed
let contact: any = '';
if (data.categoryId === 'outgoing') {
if (!this.functions.empty(data.recipients) && data.recipients.length > 0) {
await this.getContacts(data.recipients);
contact = !this.functions.empty(data.recipients) ? [{ id: this.resourceContacts[0].id, type: this.resourceContacts[0].type }] : '';

Alex ORLUC
committed
}
} else {
if (!this.functions.empty(data.senders) && data.senders.length > 0) {
await this.getContacts(data.senders);
contact = !this.functions.empty(data.senders) ? [{ id: this.resourceContacts[0].id, type: this.resourceContacts[0].type }] : '';

Alex ORLUC
committed
}
this.attachments.push({
title: new FormControl({ value: data.subject, disabled: false }, [Validators.required]),

Alex ORLUC
committed
recipient: new FormControl({ value: contact, disabled: false }),
type: new FormControl({ value: '', disabled: false }, [Validators.required]),
validationDate: new FormControl({ value: '', disabled: false }),
format: new FormControl({ value: '', disabled: false }, [Validators.required]),
encodedFile: new FormControl({ value: '', disabled: false }, [Validators.required])
});
this.attachFormGroup.push(new FormGroup(this.attachments[0]));

Alex ORLUC
committed
if (data.categoryId === 'outgoing') {
if (!this.functions.empty(data.recipients) && data.recipients.length > 1) {
this.toggleSendMass();
}
} else {
if (!this.functions.empty(data.senders) && data.senders.length > 1) {
this.toggleSendMass();
}
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
}
resolve(true);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
this.dialogRef.close('');
return of(false);
})
).subscribe();
});
}
async getContacts(contacts: any) {
this.resourceContacts = [];
await Promise.all(contacts.map(async (elem: any) => {
await this.getContact(elem.id, elem.type);
}));
this.resourceContacts = this.sortPipe.transform(this.resourceContacts, 'label');
}
selectContact(contact: any) {
this.loadingContact = true;
const contactChosen = JSON.parse(JSON.stringify(this.resourceContacts.filter(resContact => resContact.id === contact.id && resContact.type === contact.type)[0]));
this.attachments[this.indexTab].recipient.setValue([contactChosen]);
setTimeout(() => {
this.loadingContact = false;
}, 0);
this.selectedContact.reset();
}
getContact(contactId: number, type: string) {
return new Promise((resolve, reject) => {
if (type === 'contact') {
this.http.get('../../rest/contacts/' + contactId).pipe(
tap((data: any) => {
this.resourceContacts.push({
id: data.id,
type: 'contact',
label: this.contactService.formatContact(data)
});
resolve(true);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
resolve(false);
return of(false);
})
).subscribe();
} else if (type === 'user') {
this.http.get('../../rest/users/' + contactId).pipe(
tap((data: any) => {
this.resourceContacts.push({
id: data.id,
type: 'user',
label: `${data.firstname} ${data.lastname}`
});
resolve(true);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
resolve(false);
return of(false);
})
).subscribe();
} else if (type === 'entity') {
this.http.get('../../rest/entities/' + contactId).pipe(
tap((data: any) => {
this.resourceContacts.push({
id: data.id,
type: 'entity',
label: data.entity_label
});
resolve(true);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
resolve(false);
return of(false);
})
).subscribe();
}
});
}
selectAttachType(attachment: any, type: any) {
attachment.type = type.id;
}
formatAttachments() {
let formattedAttachments: any[] = [];
this.attachments.forEach((element, index: number) => {
formattedAttachments.push({
resIdMaster: this.data.resIdMaster,
type: element.type.value,
title: element.title.value,
recipientId: element.recipient.value.length > 0 ? element.recipient.value[0].id : null,
recipientType: element.recipient.value.length > 0 ? element.recipient.value[0].type : null,
validationDate: element.validationDate.value !== '' ? element.validationDate.value : null,
encodedFile: element.encodedFile.value,
format: element.format.value
});
});
return formattedAttachments;
}
onSubmit(mode: string = 'default') {
this.appDocumentViewer.toArray()[this.indexTab].getFile().pipe(
tap((data) => {
this.attachments[this.indexTab].encodedFile.setValue(data.content);
this.attachments[this.indexTab].format.setValue(data.format);
}),
if (this.isValid()) {
this.sendingData = true;
const attach = this.formatAttachments();
await Promise.all(this.attachments.map(async (element: any, index: number) => {
resId = await this.saveAttachment(attach[index]);
}));
if (this.sendMassMode && resId !== null && mode === 'mailing') {
await this.generateMailling(resId);
}
this.sendingData = false;
this.notify.success(this.lang.attachmentAdded);
this.dialogRef.close('success');
} else {
this.sendingData = false;
this.notify.error(this.lang.mustCompleteAllAttachments);
}
})
).subscribe();
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
saveAttachment(attachment: any) {
attachment.status = this.sendMassMode ? 'SEND_MASS' : 'A_TRA';
return new Promise((resolve, reject) => {
this.http.post(`../../rest/attachments`, attachment).pipe(
tap((data: any) => {
resolve(data.id);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
this.dialogRef.close('');
return of(false);
})
).subscribe();
});
}
generateMailling(resId: number) {
return new Promise((resolve, reject) => {
this.http.post(`../../rest/attachments/${resId}/mailing`, {}).pipe(
tap(() => {
resolve(true);
}),
catchError((err: any) => {
this.notify.handleSoftErrors(err);
this.dialogRef.close('');
return of(false);
})
).subscribe();
});
}
isValid() {
let state = true;
this.attachFormGroup.forEach(formgroup => {
if (formgroup.status === 'INVALID') {
state = false;
}
Object.keys(formgroup.controls).forEach(key => {
formgroup.controls[key].markAsTouched();
});
});
return state;
}
isPjValid(index: number) {
let state = true;
if (this.attachFormGroup[index].status === 'INVALID') {
state = false;
}
return state;
}
isDocLoading() {
let state = false;
this.appDocumentViewer.toArray().forEach((app, index: number) => {
if (app.isEditingTemplate() && app.editor.async) {
state = true;
}
});
return state;
}
setDatasViewer(i: number) {
let datas: any = {};
Object.keys(this.attachments[i]).forEach(element => {
if (['title', 'validationDate', 'recipient'].indexOf(element) > -1) {

Alex ORLUC
committed
if (element === 'recipient' && this.attachments[i][element].value.length > 0) {
datas['recipientId'] = this.attachments[i][element].value[0].id
datas['recipientType'] = this.attachments[i][element].value[0].type
} else {
datas['attachment_' + element] = this.attachments[i][element].value;
}
datas['resId'] = this.data.resIdMaster;
if (this.sendMassMode) {
datas['inMailing'] = true;
}
this.appDocumentViewer.toArray()[i].setDatas(datas);
}
newPj() {
this.appDocumentViewer.toArray()[this.indexTab].getFile().pipe(
tap((data) => {
this.attachments[this.indexTab].encodedFile.setValue(data.content);
this.attachments[this.indexTab].format.setValue(data.format);
this.attachments.push({
title: new FormControl({ value: '', disabled: false }, [Validators.required]),
recipient: new FormControl({ value: !this.functions.empty(this.resourceContacts[this.attachments.length]) ? [{ id: this.resourceContacts[this.attachments.length].id, type: this.resourceContacts[this.attachments.length].type }] : null, disabled: false }),
type: new FormControl({ value: '', disabled: false }, [Validators.required]),
validationDate: new FormControl({ value: null, disabled: false }),
encodedFile: new FormControl({ value: '', disabled: false }, [Validators.required]),
format: new FormControl({ value: '', disabled: false }, [Validators.required])
});
this.attachFormGroup.push(new FormGroup(this.attachments[this.attachments.length - 1]));
this.indexTab = this.attachments.length - 1;
}),
).subscribe();
}
removePj(i: number) {
const dialogRef = this.dialog.open(ConfirmComponent, { autoFocus: false, disableClose: true, data: { title: this.lang.delete + ' : PJ n°' + (i + 1), msg: this.lang.confirmAction } });
dialogRef.afterClosed().pipe(
filter((data: string) => data === 'ok'),
tap(() => {
this.indexTab = 0;
this.attachments.splice(i, 1);
this.attachFormGroup.splice(i, 1);
}),
catchError((err: any) => {
this.notify.handleErrors(err);
return of(false);
})
).subscribe();
getAttachType(attachType: any, i: number) {
this.appDocumentViewer.toArray()[i].loadTemplatesByResId(this.data.resIdMaster, attachType);
}
isEmptyField(field: any) {
if (field.value === null) {
return true;
} else if (Array.isArray(field.value)) {
if (field.value.length > 0) {
return false;
} else {
return true;
}
} else if (String(field.value) !== '') {
return false;
} else {
return true;
}
}
toggleSendMass() {
if (this.sendMassMode) {
this.sendMassMode = !this.sendMassMode;
this.selectedContact.enable();
} else {
if (this.attachments.length === 1) {
this.sendMassMode = !this.sendMassMode;
this.selectedContact.disable();
} else {
this.notify.error('Veuillez supprimer les <b>autres onglets PJ</b> avant de passer en <b>publipostage</b>.');
}
}
}