Skip to content
Snippets Groups Projects
sent-resource-list.component.ts 15.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • import {Component, OnInit, ViewChild, EventEmitter, Input, Output} from '@angular/core';
    
    import { HttpClient } from '@angular/common/http';
    
    import { TranslateService } from '@ngx-translate/core';
    
    import { NotificationService } from '@service/notification/notification.service';
    import { of } from 'rxjs';
    
    import { MatDialog } from '@angular/material/dialog';
    import { MatSort } from '@angular/material/sort';
    import { MatTableDataSource } from '@angular/material/table';
    
    import { map, catchError, filter, tap } from 'rxjs/operators';
    
    import { FunctionsService } from '@service/functions.service';
    import { PrivilegeService } from '@service/privileges.service';
    
    import { SentResourcePageComponent } from './sent-resource-page/sent-resource-page.component';
    
    import { SentNumericPackagePageComponent } from './sent-numeric-package-page/sent-numeric-package-page.component';
    
        selector: 'app-sent-resource-list',
    
        templateUrl: 'sent-resource-list.component.html',
    
        styleUrls: ['sent-resource-list.component.scss'],
    
    export class SentResourceListComponent implements OnInit {
    
    
        loading: boolean = true;
    
        dataSource: any;
        displayedColumns: string[] = ['creationDate'];
    
    
        sentResources: any[] = [];
    
    
        resultsLength = 0;
    
        currentFilter: string = '';
        filterTypes: any[] = [];
    
    
        @Input('resId') resId: number = null;
    
        @Input() currentUserId: number = null;
        @Input() currentGroupId: number = null;
        @Input() currentBasketId: number = null;
    
        @Output() reloadBadgeSentResource = new EventEmitter<string>();
    
        @ViewChild(MatSort, { static: false }) sort: MatSort;
    
        constructor(
    
            public translate: TranslateService,
    
            public http: HttpClient,
            private notify: NotificationService,
            public dialog: MatDialog,
            public functions: FunctionsService,
            public privilegeService: PrivilegeService) { }
    
        async ngOnInit(): Promise<void> {
    
            this.loadList();
        }
    
        async loadList() {
    
            this.sentResources = [];
    
            this.loading = true;
    
            await this.initEmailList();
    
            await this.initMessageExchange();
    
            await this.initShippings();
    
            this.reloadBadgeSentResource.emit(`${this.sentResources.length}`);
    
            this.initFilter();
    
            setTimeout(() => {
    
                this.dataSource = new MatTableDataSource(this.sentResources);
    
                this.dataSource.sort = this.sort;
            }, 0);
            this.loading = false;
        }
    
    
        initAcknowledgementReceiptList() {
            return new Promise((resolve) => {
    
                this.http.get(`../rest/resources/${this.resId}/acknowledgementReceipts?type=ar`).pipe(
    
                    map((data: any) => {
                        data = data.map((item: any) => {
    
                            let email;
                            if (!this.functions.empty(item.contact.email)) {
                                email = item.contact.email;
                            } else {
    
                                email = this.translate.instant('lang.withoutEmail');
    
                            if (!this.functions.empty(item.contact.firstname) || !this.functions.empty(item.contact.lastname)) {
                                name = item.contact.firstname + ' ' + item.contact.lastname;
                            } else if (!this.functions.empty(item.contact.company)) {
                                name = item.contact.company;
    
                                name = this.translate.instant('lang.contactDeleted');
    
                                recipients: item.format === 'html' ? email : name,
    
                                creationDate: item.creationDate,
                                sendDate: item.sendDate,
    
                                type: 'acknowledgementReceipt',
                                typeColor: '#7d5ba6',
    
                                desc: item.format === 'html' ? this.translate.instant('lang.ARelectronic') : this.translate.instant('lang.ARPaper'),
    
                                status: item.format === 'html' && item.sendDate === null ? 'ERROR' : 'SENT',
                                hasAttach: false,
                                hasNote: false,
    
                                hasMainDoc: false,
    
                                canManage: true
    
                        return data;
                    }),
                    tap((data: any) => {
    
                        this.sentResources = this.sentResources.concat(data);
    
                        resolve(true);
                    }),
                    catchError((err: any) => {
                        this.notify.handleSoftErrors(err);
                        resolve(false);
                        return of(false);
                    })
                ).subscribe();
    
                this.http.get(`../rest/resources/${this.resId}/emails?type=email`).pipe(
    
                    map((data: any) => {
                        data.emails = data.emails.map((item: any) => {
                            return {
                                id: item.id,
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                                sender: item.sender.email,
    
                                recipients: item.recipients,
                                creationDate: item.creation_date,
                                sendDate: item.send_date,
    
                                type: 'email',
                                typeColor: '#5bc0de',
    
                                desc: !this.functions.empty(item.object) ? item.object : `<i>${this.translate.instant('lang.emptySubject')}<i>`,
    
                                status: item.status,
                                hasAttach: !this.functions.empty(item.document.attachments),
                                hasNote: !this.functions.empty(item.document.notes),
    
                                hasMainDoc: item.document.isLinked,
                                canManage: true
    
                        return data.emails;
                    }),
                    tap((data: any) => {
    
                        this.sentResources = this.sentResources.concat(data);
    
    
                        resolve(true);
                    }),
                    catchError((err: any) => {
                        this.notify.handleSoftErrors(err);
                        resolve(false);
                        return of(false);
                    })
                ).subscribe();
            });
        }
    
        initMessageExchange() {
    
                this.http.get(`../rest/resources/${this.resId}/messageExchanges`).pipe(
    
                    map((data: any) => {
                        data.messageExchanges = data.messageExchanges.map((item: any) => {
                            return {
                                id: item.messageId,
                                sender: item.sender,
                                recipients: item.recipient,
                                creationDate: item.creationDate,
                                sendDate: item.receptionDate,
                                operationDate: item.operationDate,
                                type: 'm2m_ARCHIVETRANSFER',
                                typeColor: '#F99830',
    
                                desc: this.translate.instant('lang.m2m_ARCHIVETRANSFER'),
    
                                status: item.status.toUpperCase(),
    
                                hasAttach: false,
                                hasNote: false,
    
                                hasMainDoc: false,
    
                        return data.messageExchanges;
                    }),
                    tap((data: any) => {
    
                        this.sentResources = this.sentResources.concat(data);
    
                        resolve(true);
                    }),
                    catchError((err: any) => {
                        this.notify.handleSoftErrors(err);
                        resolve(false);
                        return of(false);
                    })
                ).subscribe();
    
        initShippings() {
    
                this.http.get(`../rest/resources/${this.resId}/shippings`).pipe(
    
                    map((data: any) => {
                        data = data.map((item: any) => {
                            return {
                                id: item.id,
                                sender: item.userLabel,
    
                                recipients: item.recipients.map((item: any) => item.contactLabel),
    
                                creationDate: item.creationDate,
                                sendDate: item.creationDate,
                                type: 'shipping',
                                typeColor: '#9440D5',
    
                                desc: this.translate.instant('lang.shipping'),
    
                                status: 'SENT',
                                hasAttach: item.creationDate === 'attachment',
                                hasNote: false,
                                hasMainDoc: item.creationDate === 'resource',
                                canManage: false
                            }
    
                        return data;
                    }),
                    tap((data: any) => {
    
                        this.sentResources = this.sentResources.concat(data);
    
    
                        resolve(true);
                    }),
                    catchError((err: any) => {
                        this.notify.handleSoftErrors(err);
                        resolve(false);
                        return of(false);
                    })
                ).subscribe();
            });
        }
    
    
            this.sentResources.forEach((element: any) => {
    
                if (this.filterTypes.filter(type => type.id === element.type).length === 0) {
    
                    this.filterTypes.push({
    
                        label: this.translate.instant('lang.' + element.type)
    
                    });
                }
            });
        }
    
        filterType(ev: any) {
            this.currentFilter = ev.value;
            this.dataSource.filter = ev.value;
        }
    
        open(row: any = {id: null, type: null}) {
    
            if (row.type === 'm2m_ARCHIVETRANSFER') {
                this.openPromptNumericPackage(row);
            } else {
                this.openPromptMail(row);
            }
        }
    
    
        openPromptMail(row: any = {id: null, type: null}) {
    
            let title = this.translate.instant('lang.sendElement');
    
    
            if (row.id !== null) {
    
                title = this.translate.instant('lang.' + row.type);
    
            }
    
            if (row.canManage || row.id === null) {
    
                const dialogRef = this.dialog.open(SentResourcePageComponent, {
                    panelClass: 'maarch-modal', width: '60vw', disableClose: true, data: {
                        title: title, resId: this.resId, emailId: row.id, emailType: row.type, currentUserId: this.currentUserId, currentGroupId: this.currentGroupId, currentBasketId: this.currentBasketId
                    }
                });
    
    
                dialogRef.afterClosed().pipe(
                    filter((data: any) => data.state === 'success' || data === 'success'),
                    tap(() => {
                        this.refreshEmailList();
                        setTimeout(() => {
                            this.refreshWaitingElements();
                        }, 5000);
                    }),
                    catchError((err: any) => {
                        this.notify.handleSoftErrors(err);
                        return of(false);
                    })
                ).subscribe();
            }
        }
    
        openPromptNumericPackage(row: any = {id: null, type: null}) {
    
    
            let title = this.translate.instant('lang.sendElement');
    
                title = this.translate.instant('lang.' + row.type);
    
            }
    
            if (row.canManage || row.id === null) {
                const dialogRef = this.dialog.open(SentNumericPackagePageComponent, { panelClass: 'maarch-modal', width:'60vw', disableClose: true, data: { title: title, resId: this.resId, emailId: row.id } });
    
                dialogRef.afterClosed().pipe(
    
                    filter((data: any) => data.state === 'success' || data === 'success'),
    
                    }),
                    catchError((err: any) => {
                        this.notify.handleSoftErrors(err);
                        return of(false);
                    })
                ).subscribe();
            }
    
            this.sentResources.forEach((draftElement: any) => {
    
                if (draftElement.status == 'WAITING' && draftElement.type == 'email') {
    
                    this.http.get(`../rest/emails/${draftElement.id}`).pipe(
    
                        tap((data: any) => {
                            if (data.status == 'SENT' || data.status == 'ERROR') {
                                if (data.status == 'SENT') {
    
                                    this.notify.success(this.translate.instant('lang.emailSent'));
    
                                    this.notify.error(this.translate.instant('lang.emailCannotSent'));
    
                                this.sentResources.forEach((element: any, key: number) => {
    
                                    if (element.id == draftElement.id && element.type == 'email') {
                                        this.sentResources[key].status = data.status;
                                        this.sentResources[key].sendDate = data.sendDate;
                                    }
                                });
                            }
                        })
                    ).subscribe();
                }
            });
            setTimeout(() => {
                this.dataSource = new MatTableDataSource(this.sentResources);
                this.dataSource.sort = this.sort;
            }, 0);
        }
    
                this.http.get(`../rest/resources/${this.resId}/emails?type=email`).pipe(
    
                    map((data: any) => {
                        data.emails = data.emails.map((item: any) => {
                            return {
                                id: item.id,
                                sender: item.sender.email,
                                recipients: item.recipients,
                                creationDate: item.creation_date,
                                sendDate: item.send_date,
                                type: 'email',
                                typeColor: '#5bc0de',
    
                                desc: !this.functions.empty(item.object) ? item.object : `<i>${this.translate.instant('lang.emptySubject')}<i>`,
    
                                status: item.status,
                                hasAttach: !this.functions.empty(item.document.attachments),
                                hasNote: !this.functions.empty(item.document.notes),
                                hasMainDoc: item.document.isLinked,
                                canManage: true
                            }
                        });
                        return data.emails;
                    }),
                    tap((data: any) => {
                        const sentResourcesNoEmails = this.sentResources.filter(elem => elem.type !== 'email');
                        this.sentResources = sentResourcesNoEmails.concat(data);
                        setTimeout(() => {
                            this.dataSource = new MatTableDataSource(this.sentResources);
                            this.dataSource.sort = this.sort;
                        }, 0);
    
                        resolve(true);
                    }),
                    catchError((err: any) => {
                        this.notify.handleSoftErrors(err);
                        resolve(false);
                        return of(false);
                    })
                ).subscribe();
            });
        }