From 2bb08349784250ac267cb8a1356060905568c990 Mon Sep 17 00:00:00 2001 From: SHEDRACK ALITA Date: Wed, 24 Jul 2024 13:31:34 +0100 Subject: [PATCH 1/4] Update SmsController.ts Made few changes in the sms controller file --- src/controllers/SmsController.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/controllers/SmsController.ts b/src/controllers/SmsController.ts index 47bb2bc1..a71b28f2 100644 --- a/src/controllers/SmsController.ts +++ b/src/controllers/SmsController.ts @@ -7,20 +7,21 @@ export const sendSms = async (req: Request, res: Response): Promise => { const { phone_number, message } = req.body; const sender_id = req.user.id; - if (!phone_number || !message || !sender_id) { - res.status(400).json({ - status: "unsuccessful", - status_code: 400, - message: - "Valid phone number, message content, and sender ID must be provided.", - }); - return; - } - try { + //This statement checks if a phone number verified, message content and send ID is provided, and if not throw an error with status code 400 + if (!phone_number || !message || !sender_id) { + res.status(400).json({ + status: "Unsuccessful", + status_code: 400, + message: + "Valid phone number, message content, and sender ID must be provided.", + }); + return; + } + + //Checks if the sender id exist in the database. This implies that the send must be a registered user. const userRepository = AppDataSource.getRepository(User); const sender = await userRepository.findOneBy({ id: sender_id }); - if (!sender) { res.status(404).json({ status: "unsuccessful", @@ -30,6 +31,7 @@ export const sendSms = async (req: Request, res: Response): Promise => { return; } + //After all coditions above are met, a success response is returned. await SmsService.sendSms(sender, phone_number, message); res.status(200).json({ status: "success", @@ -37,6 +39,7 @@ export const sendSms = async (req: Request, res: Response): Promise => { message: "SMS sent successfully.", }); } catch (error) { + //An error response is returned here if something went wrong with the Twilio API server or there is a bad network! res.status(500).json({ status: "unsuccessful", status_code: 500, From 5a22e3504446f40452aa8925d84abcd758be51e6 Mon Sep 17 00:00:00 2001 From: SHEDRACK ALITA Date: Wed, 24 Jul 2024 13:35:31 +0100 Subject: [PATCH 2/4] Update sms.services.ts Made few changes --- src/services/sms.services.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/services/sms.services.ts b/src/services/sms.services.ts index 5e8cd52a..b4d71e4e 100644 --- a/src/services/sms.services.ts +++ b/src/services/sms.services.ts @@ -19,12 +19,12 @@ class SmsService { await this.twilioClient.messages.create({ body: message, from: config.TWILIO_PHONE_NUMBER, - to: phoneNumber, + to: phone_number, }); const sms = new Sms(); sms.sender = sender; - sms.phone_number = phoneNumber; + sms.phone_number = phone_number; sms.message = message; const smsRepository = AppDataSource.getRepository(Sms); From 9af45e14db372156277d6b74b4454ffe55591b73 Mon Sep 17 00:00:00 2001 From: SHEDRACK ALITA Date: Wed, 24 Jul 2024 13:36:55 +0100 Subject: [PATCH 3/4] Update .env.example Added trilio phone number --- .env.example | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index d65fef0b..31e4b13d 100644 --- a/.env.example +++ b/.env.example @@ -11,4 +11,5 @@ SMTP_PASSWORD= SMTP_HOST= SMTP_SERVICE= TWILIO_ACCOUNT_SID= -TWILIO_AUTH_TOKEN= \ No newline at end of file +TWILIO_AUTH_TOKEN= +TWILIO_PHONE_NUMBER= From e4ddda8b783e0318f8aa6102ec144dc56b762999 Mon Sep 17 00:00:00 2001 From: SHEDRACK ALITA Date: Wed, 24 Jul 2024 13:40:07 +0100 Subject: [PATCH 4/4] Update index.ts Modified the twilio account SID dotenv variable name --- src/config/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/index.ts b/src/config/index.ts index 853e7f73..7ead2904 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -15,7 +15,7 @@ const config = { SMTP_HOST: process.env.SMTP_HOST, SMTP_SERVICE: process.env.SMTP_SERVICE, NODE_ENV: process.env.NODE_ENV, - TWILIO_SID: process.env.TWILIO_SID, + TWILIO_SID: process.env.TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN: process.env.TWILIO_AUTH_TOKEN, TWILIO_PHONE_NUMBER: process.env.TWILIO_PHONE_NUMBER,