From d2e4b4b792719b6243d0f5549abf2b3bb1ba9254 Mon Sep 17 00:00:00 2001
From: Antonin Coquet <ext.sopra.acoquet@grandlyon.com>
Date: Wed, 7 Apr 2021 15:52:45 +0200
Subject: [PATCH] Fix after MR review

---
 .../mail-templates/structureErrorReport.json  |  2 +-
 src/mailer/mailer.service.ts                  | 44 ++-----------------
 src/structures/services/structures.service.ts |  6 +--
 src/structures/structures.controller.ts       |  1 -
 4 files changed, 7 insertions(+), 46 deletions(-)

diff --git a/src/mailer/mail-templates/structureErrorReport.json b/src/mailer/mail-templates/structureErrorReport.json
index f828eb7b3..3faeacf33 100644
--- a/src/mailer/mail-templates/structureErrorReport.json
+++ b/src/mailer/mail-templates/structureErrorReport.json
@@ -1,3 +1,3 @@
 {
-  "subject": "Une demande de modification a été faite sur votre structure, Réseau des Acteurs de la Médiation Numérique de la Métropole de Lyon"
+  "subject": "Une erreur a été remontée sur votre structure, Réseau des Acteurs de la Médiation Numérique de la Métropole de Lyon"
 }
diff --git a/src/mailer/mailer.service.ts b/src/mailer/mailer.service.ts
index c990b850c..ff76d5fe2 100644
--- a/src/mailer/mailer.service.ts
+++ b/src/mailer/mailer.service.ts
@@ -21,53 +21,15 @@ export class MailerService {
    * @param {string} html
    * @param {string} text
    */
-  public async send(to: string, subject: string, html: string): Promise<AxiosResponse<any>> {
+  public async send(to: string | {}[], subject: string, html: string): Promise<AxiosResponse<any>> {
+    let emailsToSend = typeof to === 'string' ? [{ email: to }] : to;
     const formData = new FormData();
-
-    const data = JSON.stringify({
-      // eslint-disable-next-line camelcase
-      from_email: this.config.from,
-      // eslint-disable-next-line camelcase
-      from_name: this.config.from_name,
-      to: [{ email: to }],
-      reply_to: 'inclusionnumerique@grandlyon.com',
-      subject: subject,
-      content: this.addSignature(html),
-    });
-    formData.append('metadata', data);
-    const contentLength = formData.getLengthSync();
-    Logger.log(`Send mail : ${subject}`, 'Mailer');
-    return new Promise((resolve, reject) => {
-      this.httpService
-        .post(process.env.MAIL_URL, formData, {
-          headers: {
-            'Content-Length': contentLength,
-            Authorization: 'Bearer ' + process.env.MAIL_TOKEN,
-            ...formData.getHeaders(),
-          },
-        })
-        .subscribe(
-          (body) => {
-            Logger.log(`Send mail - success : ${subject}`, 'Mailer');
-            return resolve(body);
-          },
-          (err) => {
-            Logger.error(err, 'Mailer');
-            return reject(err);
-          }
-        );
-    });
-  }
-
-  public async sendMultiple(to: {}[], subject: string, html: string): Promise<AxiosResponse<any>> {
-    const formData = new FormData();
-
     const data = JSON.stringify({
       // eslint-disable-next-line camelcase
       from_email: this.config.from,
       // eslint-disable-next-line camelcase
       from_name: this.config.from_name,
-      to: to,
+      to: emailsToSend,
       reply_to: 'inclusionnumerique@grandlyon.com',
       subject: subject,
       content: this.addSignature(html),
diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index a0b0b81e2..14a34afa7 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -355,9 +355,9 @@ export class StructuresService {
           return admin.email;
         })
       );
-    let uniqueEmails = [...new Set(emails)];
+    const uniqueEmails = [...new Set(emails)];
 
-    const test = uniqueEmails.map((item) => {
+    const emailsObject = uniqueEmails.map((item) => {
       const container = {};
       container['email'] = item;
       return container;
@@ -373,6 +373,6 @@ export class StructuresService {
       id: structure._id,
       structureName: structure.structureName,
     });
-    this.mailerService.sendMultiple(test, jsonConfig.subject, html);
+    this.mailerService.send(emailsObject, jsonConfig.subject, html);
   }
 }
diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts
index 52582b6bb..a7586a4cf 100644
--- a/src/structures/structures.controller.ts
+++ b/src/structures/structures.controller.ts
@@ -238,7 +238,6 @@ export class StructuresController {
 
   @Post('reportStructureError')
   public async reportStructureError(@Body() data: { structureId: string; content: string }): Promise<void> {
-    console.log('report structure error contrller');
     return await this.structureService.reportStructureError(data.structureId, data.content);
   }
 }
-- 
GitLab