Skip to content

Commit 73e37cb

Browse files
DayifourricashtransYaishaa
authored
Merge the branch main on the branch develop (#14)
* Update docker-build.yml (#10) * Update docker-build.yml * README.md explicitement bien détaillé --------- Co-authored-by: Manager Dayif <sekoudayifourouk@gmail.com> * Notification-Service Fonctionnel ✅ (#13) * Refactor OTP service to publish notifications with updated message structure and routing key * Refactor RabbitMQ configuration and health route for improved readability and consistency * Refactor notification controller and server initialization for improved structure and readability * Add user contact service and update notification handling for email and phone * Refactor code structure for improved readability in Otp, NotificationService, and UserContactService * Update Docker Compose configuration, refactor Notification entity imports, enhance OTP service with user contact retrieval * Update Docker Compose to use external network and enhance external notification consumer logging * Update dependencies: body-parser to 2.2.1 and nodemailer to 6.10.1 * Enhance OTP generation to validate email and phone inputs, and retrieve user contacts if not provided * Refactor InterServices and NotificationService to include optional email and phone fields, enhancing contact retrieval logic for notifications * Add Zod validation library and enhance notification service with multi-channel support for email and SMS * Enhance notification and OTP generation with Zod validation for request bodies * feat: Implement notification and OTP services with RabbitMQ integration - Added RabbitMQ configuration and channel management in `rabbitmq.js`. - Created notification controller to handle sending notifications and retrieving them. - Developed OTP controller for generating and verifying OTPs. - Introduced data source configuration for PostgreSQL using TypeORM. - Defined Notification and Otp entities with appropriate fields and relationships. - Implemented notification service for sending notifications via SMS and email. - Created OTP service for generating and verifying OTPs, including publishing events to RabbitMQ. - Added user contact service for retrieving user contact information. - Implemented mail service for sending emails using Nodemailer. - Developed message templates for generating notification messages based on type. - Created health check route for service status. - Set up consumers for processing notifications from RabbitMQ. - Added external consumer for handling inter-service notifications. * feat: notifications inter-service et robustesse consumer Co-authored-by: Aissata Traore <traoreaissata423@gmail.com> * fix: messages de transfert sender/receiver Co-authored-by: Aissata Traore <traoreaissata423@gmail.com> --------- Co-authored-by: Aissata Traore <traoreaissata423@gmail.com> --------- Co-authored-by: ricashtrans <ricashtrans@gmail.com> Co-authored-by: Aissata Traore <traoreaissata423@gmail.com>
1 parent fb8961f commit 73e37cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1756
-259
lines changed

.github/workflows/docker-build.yml

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
name: Build & Push Notification Service Docker Image
1+
name: Ricash CI - Build & Push Docker Image
22

33
on:
4+
pull_request:
5+
branches: [ develop ]
46
push:
5-
branches: ["main"]
7+
branches: [ develop, main ]
68

79
permissions:
810
contents: read
911
packages: write
1012

13+
env:
14+
REGISTRY: ghcr.io
15+
IMAGE_NAME: ricash-org/notification-service
16+
1117
jobs:
1218
build:
1319
runs-on: ubuntu-latest
1420

1521
steps:
1622
- name: Checkout code
17-
uses: actions/checkout@v3
18-
19-
- name: Set up QEMU
20-
uses: docker/setup-qemu-action@v3
23+
uses: actions/checkout@v4
2124

2225
- name: Set up Docker Buildx
2326
uses: docker/setup-buildx-action@v3
@@ -29,11 +32,22 @@ jobs:
2932
username: ${{ github.actor }}
3033
password: ${{ secrets.GITHUB_TOKEN }}
3134

32-
- name: Build & Push
33-
uses: docker/build-push-action@v5
34-
with:
35-
context: .
36-
push: true
37-
platforms: linux/amd64,linux/arm64
38-
file: ./Dockerfile
39-
tags: ghcr.io/ricash-org/notification-service:latest
35+
# ---------- PR BUILD (NO PUSH) ----------
36+
- name: Build Docker image (PR)
37+
if: github.event_name == 'pull_request'
38+
run: |
39+
docker build -t $REGISTRY/$IMAGE_NAME:pr-${{ github.event.pull_request.number }} .
40+
41+
# ---------- DEVELOP ----------
42+
- name: Build & Push (develop)
43+
if: github.ref == 'refs/heads/develop'
44+
run: |
45+
docker build -t $REGISTRY/$IMAGE_NAME:dev .
46+
docker push $REGISTRY/$IMAGE_NAME:dev
47+
48+
# ---------- MAIN / PROD ----------
49+
- name: Build & Push (prod)
50+
if: github.ref == 'refs/heads/main'
51+
run: |
52+
docker build -t $REGISTRY/$IMAGE_NAME:prod .
53+
docker push $REGISTRY/$IMAGE_NAME:prod

Docker-compose.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ services:
66
container_name: notification-service
77
restart: always
88
ports:
9-
- "3000:3000"
9+
- "8005:8005"
1010
env_file: .env
1111
depends_on:
1212
- postgres
1313
- rabbitmq
1414
networks:
15-
- notification-net
15+
- ricash-net
1616

1717
postgres:
1818
image: postgres:16
@@ -27,8 +27,8 @@ services:
2727
volumes:
2828
- pgdata:/var/lib/postgresql/data
2929
networks:
30-
- notification-net
31-
30+
- ricash-net
31+
3232
rabbitmq:
3333
image: rabbitmq:3-management
3434
container_name: rabbitmq
@@ -38,11 +38,12 @@ services:
3838
environment:
3939
RABBITMQ_DEFAULT_USER: ricash
4040
RABBITMQ_DEFAULT_PASS: ricash123
41-
42-
41+
networks:
42+
- ricash-net
4343

4444
networks:
45-
notification-net:
45+
ricash-net:
46+
external: true
4647

4748
volumes:
4849
pgdata:

README.md

Lines changed: 165 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3,125 +3,190 @@
33
Ce projet implémente un **service de notifications** en **Node.js**, **Express** et **TypeScript**.
44
Il gère deux fonctionnalités principales :
55

6-
- La génération et la vérification d’OTP (codes à usage unique).
7-
- L’envoi de notifications (par e-mail,SMS ou autres canaux).
8-
6+
- La génération et la vérification d’OTP (codes à usage unique).
7+
- L’envoi de notifications (par e-mail,SMS ou autres canaux).
98

109
---
1110

12-
## Fonctionnalités principales
11+
## Fonctionnalités principales
1312

14-
- Génération et validation d’OTP avec expiration automatique.
15-
- Envoi de notifications personnalisées via des templates.
13+
- Génération et validation d’OTP avec expiration automatique.
14+
- Envoi de notifications personnalisées via des templates.
1615
- Architecture modulaire : contrôleurs, services, entités, utilitaires.
1716

1817
---
19-
# Endpoints
18+
19+
# Endpoints
2020

2121
Tous les endpoints sont accessibles sous :<br>
2222
/api/notifications
23-
24-
**Envoi d’une notification**
25-
26-
Post /api/notifications/envoyer
27-
28-
**Body json** <br>
29-
{<br>
30-
"utilisateurId": "+22350087965", <br>
31-
"typeNotification": "CONFIRMATION_TRANSFERT",<br>
32-
"canal": "SMS",<br>
33-
"context": {<br>
34-
"montant": 10000,<br>
35-
"destinataire": "Aisha"<br>
36-
}<br>
37-
}<br>
38-
39-
**Réponse json**<br>
40-
41-
{<br>
42-
"id": 42,<br>
43-
"utilisateurId": "+22350087965",<br>
44-
"typeNotification": "CONFIRMATION_TRANSFERT",<br>
45-
"canal": "SMS",<br>
46-
"message": "Votre transfert de 10000 F CFA à Aisha a été confirmé.",<br>
47-
"statut": "ENVOYEE",<br>
48-
"createdAt": "2025-12-02T20:10:00.000Z"<br>
49-
}<br>
50-
51-
52-
**Génération d'otp**<br>
53-
54-
POST /api/notifications/otp/generate <br>
55-
56-
**Body json**<br>
57-
58-
-Envoi par numéro de téléphone<br>
59-
{<br>
60-
"utilisateurId": "+22350087965",<br>
61-
"canalNotification": "SMS"<br>
62-
}<br>
63-
-Envoi par email<br>
64-
{<br>
65-
"utilisateurId": "youremail@gmail.com",<br>
66-
"canalNotification": "EMAIL"<br>
67-
}<br>
68-
69-
**Vérification d'un otp**<br>
70-
71-
POST /api/notifications/otp/verify<br>
72-
**BODY JSON**<br>
7323

24+
## Fonctionnalités principales
25+
26+
- Génération et validation d’OTP avec expiration automatique.
27+
- Envoi de notifications personnalisées via des templates.
28+
- Intégration RabbitMQ : consommation d’événements de `wallet-service` (dépôt, retrait, transfert, OTP…) et transformation en notifications.
29+
- Validation stricte des payloads HTTP avec **Zod** (emails et téléphones obligatoires, structure `transfer` dédiée, etc.).
30+
31+
---
32+
33+
## Endpoints HTTP
34+
35+
Tous les endpoints HTTP exposés par ce service sont préfixés par :
36+
37+
- `/api/notifications`
38+
39+
### 1. Envoi d’une notification (HTTP direct)
40+
41+
`POST /api/notifications/envoyer`
42+
43+
Depuis la refonte, le service est **strictement dépendant des coordonnées fournies dans le JSON**. Deux formes sont possibles :
44+
45+
#### a) Notification de transfert
46+
47+
```json
7448
{
75-
"utilisateurId": "+22350087965",<br>
76-
"code": "1234"<br>
49+
"type": "transfer",
50+
"sender": {
51+
"email": "expediteur@mail.com",
52+
"phone": "+22300000000"
53+
},
54+
"receiver": {
55+
"email": "destinataire@mail.com",
56+
"phone": "+22311111111"
57+
},
58+
"amount": 5000,
59+
"content": "Transfert de 5000 FCFA réussi."
7760
}
78-
**Réponse**<br>
79-
{<br>
80-
"success": true,<br>
81-
"message": "OTP validé"<br>
61+
```
62+
63+
- Le schéma Zod impose :
64+
- `type` = `"transfer"`.
65+
- `sender.email` / `sender.phone` obligatoires.
66+
- `receiver.email` / `receiver.phone` obligatoires.
67+
- `amount > 0`.
68+
- `content` non vide.
69+
- Le service crée **deux paires de notifications** (SMS + EMAIL) :
70+
- Pour l’expéditeur (role = `SENDER`).
71+
- Pour le destinataire (role = `RECEIVER`).
72+
- Les messages sont envoyés :
73+
- par SMS via Twilio sur `phone`.
74+
- par email via `mailService.sendEmail` sur `email`.
75+
- Le `context` des entités `Notification` contient notamment `montant` et `role`.
76+
77+
#### b) Notification simple (autres types)
78+
79+
```json
80+
{
81+
"type": "ALERT_SECURITE",
82+
"user": {
83+
"email": "client@mail.com",
84+
"phone": "+22322222222"
85+
},
86+
"content": "Connexion suspecte détectée."
8287
}
88+
```
8389

84-
**Autres réponses possibles**<br>
90+
- `type` peut être l’une des valeurs de `TypeNotification` (sauf `"transfer"` qui utilise le schéma dédié).
91+
- `user.email` et `user.phone` sont obligatoires.
92+
- Le service envoie systématiquement la notification **à la fois par SMS et par email**.
8593

86-
{ "success": false, "message": "Code invalide" }<br>
87-
{ "success": false, "message": "Code expiré" }<br>
88-
{ "success": false, "message": "Ce code a déjà été utilisé" }<br>
94+
En cas de JSON invalide (champ manquant / mauvais type), le contrôleur renvoie :
8995

90-
---
91-
## Structure du projet
96+
```json
97+
{
98+
"success": false,
99+
"message": "Corps de requête invalide",
100+
"errors": { ...détail Zod... }
101+
}
102+
```
92103

104+
### 2. Génération d’OTP
93105

94-
```bash
95-
notification-service/
96-
97-
├── src/
98-
│ ├── controllers/
99-
│ │ ├── notificationController.ts # Gère les requêtes liées à l’envoi de notifications
100-
│ │ ├── otpController.ts # Gère la génération et la vérification des OTP
101-
│ │
102-
│ ├── entities/
103-
│ │ ├── Notification.ts # Modèle de données pour les notifications
104-
│ │ ├── Otp.ts # Modèle de données pour les OTP (code, expiration, utilisateur)
105-
│ │
106-
│ ├── routes/
107-
│ │ ├── notificationRoutes.ts # Définition des routes Express pour les notifications et OTP
108-
│ │
109-
│ ├── services/
110-
│ │ ├── notificationService.ts # Logique métier liée aux notifications
111-
│ │ ├── otpService.ts # Logique métier liée aux OTP
112-
│ │
113-
│ ├── utils/
114-
│ │ ├── mailService.ts # Gère l’envoi des e-mails (transporteur, configuration…)
115-
│ │ ├── messageTemplates.ts # Contient les templates des messages
116-
│ │
117-
│ ├── app.ts # Configuration principale de l’application Express
118-
│ ├── data-source.ts # Configuration et connexion à la base de données
119-
│ ├── index.ts # Point d’entrée pour la déclaration des routes
120-
│ ├── server.ts # Lancement du serveur Express
121-
122-
├── .env # Variables d’environnement (PORT, DB_URL, etc.)
123-
├── package.json # Dépendances et scripts du projet
124-
├── tsconfig.json # Configuration TypeScript
106+
`POST /api/notifications/otp/generate`
107+
108+
Le service génère un code OTP (4 chiffres), l’enregistre en base avec une expiration (5 minutes) puis publie un événement `otp.verification` sur RabbitMQ. Désormais, il dépend **strictement** des coordonnées envoyées dans le JSON.
125109

110+
```json
111+
{
112+
"utilisateurId": "user-otp-1",
113+
"canalNotification": "SMS",
114+
"email": "userotp@mail.com",
115+
"phone": "+22300000000"
116+
}
117+
```
118+
119+
- `utilisateurId`: identifiant métier (user id).
120+
- `canalNotification`: `"SMS"` ou `"EMAIL"`.
121+
- `email`: email du destinataire (obligatoire).
122+
- `phone`: numéro du destinataire (obligatoire).
123+
124+
│ │ ├── Notification.ts # Modèle de données pour les notifications
125+
126+
L’événement publié (contrat inter-services) contient :
127+
128+
```json
129+
{
130+
"utilisateurId": "user-otp-1",
131+
"typeNotification": "VERIFICATION_TELEPHONE",
132+
"canal": "SMS",
133+
"context": { "code": "1234" },
134+
"email": "userotp@mail.com",
135+
"phone": "+22300000000",
136+
"metadata": {
137+
"service": "notification-service:otp",
138+
"correlationId": "otp-<id>"
139+
}
140+
}
141+
```
142+
143+
Les templates de message utilisent ce `context` pour produire des textes explicites, par exemple :
144+
145+
- `VERIFICATION_TELEPHONE` :
146+
> « Votre code OTP de vérification téléphone est : {code}. Ce code est valable 5 minutes. Ne le partagez jamais avec un tiers. »
126147
148+
### 3. Vérification d’un OTP
127149

150+
`POST /api/notifications/otp/verify`
151+
152+
Body JSON :
153+
154+
```json
155+
{
156+
"utilisateurId": "user-otp-1",
157+
"code": "1234"
158+
}
159+
```
160+
161+
Réponses possibles :
162+
163+
```json
164+
{ "success": true, "message": "OTP validé" }
165+
{ "success": false, "message": "Code invalide" }
166+
{ "success": false, "message": "Code expiré" }
167+
{ "success": false, "message": "Ce code a déjà été utilisé" }
168+
```
169+
170+
---
171+
172+
│ │ ├── Otp.ts # Modèle de données pour les OTP (code, expiration, utilisateur)
173+
│ │
174+
│ ├── routes/
175+
│ │ ├── notificationRoutes.ts # Définition des routes Express pour les notifications et OTP
176+
│ │
177+
│ ├── services/
178+
│ │ ├── notificationService.ts # Logique métier liée aux notifications
179+
│ │ ├── otpService.ts # Logique métier liée aux OTP
180+
│ │
181+
│ ├── utils/
182+
│ │ ├── mailService.ts # Gère l’envoi des e-mails (transporteur, configuration…)
183+
│ │ ├── messageTemplates.ts # Contient les templates des messages
184+
│ │
185+
│ ├── app.ts # Configuration principale de l’application Express
186+
│ ├── data-source.ts # Configuration et connexion à la base de données
187+
│ ├── index.ts # Point d’entrée pour la déclaration des routes
188+
│ ├── server.ts # Lancement du serveur Express
189+
190+
├── .env # Variables d’environnement (PORT, DB_URL, etc.)
191+
├── package.json # Dépendances et scripts du projet
192+
├── tsconfig.json # Configuration TypeScript

0 commit comments

Comments
 (0)