From 388ece2ee70108897d03bd20767d6ad89228eeb8 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Mon, 11 May 2020 14:59:41 +0200
Subject: [PATCH] FEAT #13339 TIME 0:25 front manage duplicates contacts

---
 .../manage-duplicate.component.html           |  2 +-
 .../manage-duplicate.component.ts             | 41 ++++++++++---------
 src/frontend/lang/lang-en.ts                  |  1 +
 src/frontend/lang/lang-fr.ts                  |  1 +
 src/frontend/lang/lang-nl.ts                  |  1 +
 5 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/src/frontend/app/administration/contact/contact-duplicate/manage-duplicate/manage-duplicate.component.html b/src/frontend/app/administration/contact/contact-duplicate/manage-duplicate/manage-duplicate.component.html
index ee381f54571..f415af9d3dc 100644
--- a/src/frontend/app/administration/contact/contact-duplicate/manage-duplicate/manage-duplicate.component.html
+++ b/src/frontend/app/administration/contact/contact-duplicate/manage-duplicate/manage-duplicate.component.html
@@ -7,7 +7,7 @@
     <span class="divider-modal"></span>
     <div mat-dialog-actions class="actions">
         <button mat-raised-button mat-button color="primary" [disabled]="contactSelected===null"
-            (click)="onSubmit()">Fusionner</button>
+            (click)="onSubmit()">{{lang.merge}}</button>
         <button mat-raised-button mat-button [mat-dialog-close]="">{{lang.cancel}}</button>
     </div>
 </div>
diff --git a/src/frontend/app/administration/contact/contact-duplicate/manage-duplicate/manage-duplicate.component.ts b/src/frontend/app/administration/contact/contact-duplicate/manage-duplicate/manage-duplicate.component.ts
index 11bbf8aa042..0c68d7cf0a1 100644
--- a/src/frontend/app/administration/contact/contact-duplicate/manage-duplicate/manage-duplicate.component.ts
+++ b/src/frontend/app/administration/contact/contact-duplicate/manage-duplicate/manage-duplicate.component.ts
@@ -5,6 +5,9 @@ import { HttpClient } from '@angular/common/http';
 import { FunctionsService } from '../../../../../service/functions.service';
 import { ContactDetailComponent } from '../../../../contact/contact-detail/contact-detail.component';
 import { LANG } from '../../../../translate.component';
+import { tap, catchError } from 'rxjs/operators';
+import { of } from 'rxjs/internal/observable/of';
+import { NotificationService } from '../../../../notification.service';
 
 @Component({
     selector: 'app-manage-duplicate',
@@ -21,36 +24,20 @@ export class ManageDuplicateComponent implements OnInit {
 
     constructor(
         public http: HttpClient,
+        private notify: NotificationService,
         @Inject(MAT_DIALOG_DATA) public data: any,
         public dialogRef: MatDialogRef<ManageDuplicateComponent>,
         public headerService: HeaderService,
         private functionsService: FunctionsService) {
     }
 
-    ngOnInit(): void {
-        console.log(this.data);
-
-        /*this.data.duplicate = [
-            {
-                id: 15,
-                type: 'contact'
-            },
-            {
-                id: 16,
-                type: 'contact'
-            },
-            {
-                id: 17,
-                type: 'contact'
-            }
-        ];*/
-    }
+    ngOnInit(): void { }
 
     mergeContact(contact: any, index: number) {
 
         this.contactSelected = index;
 
-        this.data.duplicate.forEach((contact: any, indexContact: number) => {
+        this.data.duplicate.forEach((contactItem: any, indexContact: number) => {
             Object.keys(this.appContactDetail.toArray()[indexContact].getContactInfo()).forEach(element => {
                 if (this.functionsService.empty(this.appContactDetail.toArray()[index].getContactInfo()[element]) && this.appContactDetail.toArray()[index].getContactInfo()[element] !== this.appContactDetail.toArray()[indexContact].getContactInfo()[element]) {
                     this.appContactDetail.toArray()[index].setContactInfo(element, this.appContactDetail.toArray()[indexContact].getContactInfo()[element]);
@@ -68,6 +55,20 @@ export class ManageDuplicateComponent implements OnInit {
     }
 
     onSubmit() {
-        this.dialogRef.close('success');
+        const masterContact: number = this.data.duplicate.filter((contact: any, index: number) => index === this.contactSelected).map((contact: any) => contact.id)[0];
+        const slaveContacts: number[] = this.data.duplicate.filter((contact: any, index: number) => index !== this.contactSelected).map((contact: any) => contact.id);
+
+        console.log(masterContact);
+        console.log(slaveContacts);
+
+        this.http.post('../rest/contacts/merged', { master : masterContact, saves : slaveContacts}).pipe(
+            tap(() => {
+                this.dialogRef.close('success');
+            }),
+            catchError((err: any) => {
+                this.notify.handleSoftErrors(err);
+                return of(false);
+            })
+        ).subscribe();
     }
 }
diff --git a/src/frontend/lang/lang-en.ts b/src/frontend/lang/lang-en.ts
index dc1d64b7eb2..d5aaaeb0527 100755
--- a/src/frontend/lang/lang-en.ts
+++ b/src/frontend/lang/lang-en.ts
@@ -1699,4 +1699,5 @@ export const LANG_EN = {
     "launchSearch": "Launch search",
     "duplicatesContactDisplayed": "{0} duplicates ({1} displayed)",
     "duplicatesContactsAdmin": "Manage contacts duplicates",
+    "merge": "Merge",
 };
diff --git a/src/frontend/lang/lang-fr.ts b/src/frontend/lang/lang-fr.ts
index aff4b4058c0..30ae01edccc 100755
--- a/src/frontend/lang/lang-fr.ts
+++ b/src/frontend/lang/lang-fr.ts
@@ -1701,4 +1701,5 @@ export const LANG_FR = {
     "launchSearch": "Lancer la recherche",
     "duplicatesContactDisplayed": "{0} doulons ({1} affichés)",
     "duplicatesContactsAdmin": "Gérer les doublons de contacts",
+    "merge": "Fusionner",
 };
diff --git a/src/frontend/lang/lang-nl.ts b/src/frontend/lang/lang-nl.ts
index 92b50f081d4..bf5abee197e 100755
--- a/src/frontend/lang/lang-nl.ts
+++ b/src/frontend/lang/lang-nl.ts
@@ -1683,4 +1683,5 @@ export const LANG_NL = {
     "launchSearch": "Launch search", //_TO_TRANSLATE
     "duplicatesContactDisplayed": "{0} duplicates ({1} displayed)", //_TO_TRANSLATE
     "duplicatesContactsAdmin": "Manage contacts duplicates", //_TO_TRANSLATE
+    "merge": "Merge", //_TO_TRANSLATE
 };
-- 
GitLab