From afdae30a9c657c7fbfc17fbd2a229daa82d8d543 Mon Sep 17 00:00:00 2001
From: Augustin LECONTE <["ext.sopra.aleconte@grandlyon.com"]>
Date: Thu, 6 Jan 2022 10:15:08 +0000
Subject: [PATCH] feat(aptic-structures): send mail when structure is created
 via aptic

---
 src/configuration/config.ts                   |  4 ++++
 .../mail-templates/newApticStructure.ejs      |  6 ++++++
 .../mail-templates/newApticStructure.json     |  3 +++
 .../services/aptic-structures.service.ts      |  1 +
 src/users/users.service.ts                    | 21 +++++++++++++++++++
 5 files changed, 35 insertions(+)
 create mode 100644 src/mailer/mail-templates/newApticStructure.ejs
 create mode 100644 src/mailer/mail-templates/newApticStructure.json

diff --git a/src/configuration/config.ts b/src/configuration/config.ts
index 97ab675e8..de4534cc6 100644
--- a/src/configuration/config.ts
+++ b/src/configuration/config.ts
@@ -61,5 +61,9 @@ export const config = {
       ejs: 'structureDeletionNotification.ejs',
       json: 'structureDeletionNotification.json',
     },
+    newApticStructure: {
+      ejs: 'newApticStructure.ejs',
+      json: 'newApticStructure.json',
+    },
   },
 };
diff --git a/src/mailer/mail-templates/newApticStructure.ejs b/src/mailer/mail-templates/newApticStructure.ejs
new file mode 100644
index 000000000..1f5c39011
--- /dev/null
+++ b/src/mailer/mail-templates/newApticStructure.ejs
@@ -0,0 +1,6 @@
+Bonjour,<br />
+<br />
+Une nouvelle structure a été crée via Aptic. Ses caractéristiques sont : <br />
+- Nom: <%= name %><br />
+- Adresse: <%= address %><br />
+- Description: <%= description %><br />
diff --git a/src/mailer/mail-templates/newApticStructure.json b/src/mailer/mail-templates/newApticStructure.json
new file mode 100644
index 000000000..60799afda
--- /dev/null
+++ b/src/mailer/mail-templates/newApticStructure.json
@@ -0,0 +1,3 @@
+{
+  "subject": "Nouvelle structure Aptic"
+}
diff --git a/src/structures/services/aptic-structures.service.ts b/src/structures/services/aptic-structures.service.ts
index 1833e57fb..46098d0e8 100644
--- a/src/structures/services/aptic-structures.service.ts
+++ b/src/structures/services/aptic-structures.service.ts
@@ -97,6 +97,7 @@ export class ApticStructuresService {
         createdStructure.save();
         this.structuresSearchService.indexStructure(createdStructure);
         // Send admin weird structure mail
+        this.userService.sendAdminApticNewStructureMail(createdStructure);
         this.verifyDuplication(createdStructure);
       }
     });
diff --git a/src/users/users.service.ts b/src/users/users.service.ts
index 3782bd1d8..cb49fc0f8 100644
--- a/src/users/users.service.ts
+++ b/src/users/users.service.ts
@@ -12,6 +12,7 @@ import { IUser } from './interfaces/user.interface';
 import { EmailChangeDto } from './dto/change-email.dto';
 import { PendingStructureDto } from '../admin/dto/pending-structure.dto';
 import { OwnerDto } from './dto/owner.dto';
+import { StructureDocument } from '../structures/schemas/structure.schema';
 
 @Injectable()
 export class UsersService {
@@ -157,6 +158,26 @@ export class UsersService {
     });
   }
 
+  /**
+   * Send to all admins mail for aptic duplicated data
+   */
+  public async sendAdminApticNewStructureMail(structure: StructureDocument): Promise<any> {
+    const config = this.mailerService.config;
+    const ejsPath = this.mailerService.getTemplateLocation(config.templates.newApticStructure.ejs);
+    const jsonConfig = this.mailerService.loadJsonConfig(config.templates.newApticStructure.json);
+
+    const html = await ejs.renderFile(ejsPath, {
+      config,
+      name: structure.structureName,
+      address: `${structure.address.numero} ${structure.address.street} ${structure.address.commune}`,
+      description: structure.otherDescription,
+    });
+    const admins = await this.getAdmins();
+    admins.forEach((admin) => {
+      this.mailerService.send(admin.email, jsonConfig.subject, html);
+    });
+  }
+
   /**
    * Send approval email for user
    * a new account.
-- 
GitLab