Skip to content

Commit

Permalink
Added:Control Certer Backend APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyvishal committed Mar 29, 2024
1 parent d211b55 commit 6bc91a9
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 6 deletions.
73 changes: 73 additions & 0 deletions controllers/ControlCenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import axios from 'axios'
import Actions from '../services/Actions.js'
import logger from '../utils/logger.js'
import {
ITEM_ID,
ITEM_NAME,
CAT_ATTR_TAG_RELATIONS,
STRAPI_TOURISM_TOKEN,
} from '../utils/constants.js'
const action = new Actions()
const TOURISM_STRAPI_URL = process.env.TOURISM_STRAPI_URL || ''
export const cancelBookingController = async (req, res) => {
try {
const { recipientNumber, messageBody, orderId } = req.body
console.log(orderId)
const getOrderFulfillmentDetails = await axios.get(
`${TOURISM_STRAPI_URL}/order-fulfillments?order_id=${orderId}`,
{
headers: {
Authorization: `Bearer ${STRAPI_TOURISM_TOKEN}`,
},
}
)
if (getOrderFulfillmentDetails.data.data.length) {
await axios.put(
`${TOURISM_STRAPI_URL}/order-fulfillments/${getOrderFulfillmentDetails.data.data[0].id}`,
{
data: {
state_code: 'CANCELLED',
state_value: 'CANCELLED BY HOTEL',
},
},
{
headers: {
Authorization: `Bearer ${STRAPI_TOURISM_TOKEN}`,
},
}
)

await action.send_message(recipientNumber, messageBody)
return res.send({ message: 'Notified' })
}

return res.send({ message: 'Cancel Booking Failed' })
} catch (error) {
logger.error(error.message)
return res.send({ message: error.message })
}
}

export const updateCatalog = async (req, res) => {
try {
await axios.put(
`${TOURISM_STRAPI_URL}/items/${ITEM_ID}`,
{
data: {
name: ITEM_NAME,
cat_attr_tag_relations: CAT_ATTR_TAG_RELATIONS,
},
},
{
headers: {
Authorization: `Bearer ${STRAPI_TOURISM_TOKEN}`,
},
}
)

return res.send({ message: 'Catalog Updated' })
} catch (error) {
logger.error(error.message)
return res.send({ message: error.message })
}
}
16 changes: 16 additions & 0 deletions controllers/Notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Actions from '../services/Actions.js'
import logger from '../utils/logger.js'
const action = new Actions()
export const notificationController = async (req, res) => {
try {
const { recipientNumber, messageBody } = req.body
const sendWhatsappNotificationResponse = await action.send_message(
recipientNumber,
messageBody
)
return res.send(sendWhatsappNotificationResponse)
} catch (error) {
logger.error(error.message)
return res.send({ message: error.message })
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"body-parser": "^1.20.2",
"chai": "^5.0.0",
"config": "^3.3.11",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"js-yaml": "^4.1.0",
Expand Down
17 changes: 12 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import dotenv from 'dotenv'
import cors from 'cors'
dotenv.config()
import express from 'express'
import bodyParser from 'body-parser'
import logger from './utils/logger.js'
import messageController from './controllers/Bot.js'
import DBService from './services/DBService.js'

import { notificationController } from './controllers/Notification.js'
import {
cancelBookingController,
updateCatalog,
} from './controllers/ControlCenter.js'
const app = express()

app.use(cors())
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

Expand All @@ -17,10 +22,12 @@ app.use(bodyParser.json())
// Define endpoints here
// app.post('/act', actions.act)
app.post('/webhook', messageController.process_wa_webhook)

app.post('/notify', notificationController)
app.post('/cancel-booking', cancelBookingController)
app.post('/update-catalog', updateCatalog)
// Reset all sessions
const db = new DBService();
await db.clear_all_sessions();
const db = new DBService()
await db.clear_all_sessions()

// Start the Express server
app.listen(process.env.SERVER_PORT, () => {
Expand Down
5 changes: 4 additions & 1 deletion services/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ class Actions {

async send_message(recipient, message) {
try {
await client.messages.create({
const data = await client.messages.create({
body: message,
from: `whatsapp:${twilioNumber}`,
to: `whatsapp:${recipient}`,
})
const status = await client.messages(data.sid).fetch()

return { data, status }
} catch (error) {
logger.error(`Error sending message: ${error.message}`)
throw new Error(`Failed to send message: ${error.message}`)
Expand Down
30 changes: 30 additions & 0 deletions services/notificationService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import logger from '../utils/logger.js'
import Twilio from 'twilio'
const TWILIO_NUMBER = process.env.TWILIO_NUMBER
const TWILIO_AUTH_TOKEN = process.env.TWILIO_AUTH_TOKEN
const TWILIO_ACCOUNT_SID = process.env.TWILIO_ACCOUNT_SID

const client = Twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)

export const sendWhatsappNotification = async (
recipientNumber,
messageBody
) => {
try {
const data = await client.messages.create({
body: messageBody,
from: `whatsapp:${TWILIO_NUMBER}`,
to: `whatsapp:${recipientNumber}`,
})

const status = await client.messages(data.sid).fetch()
console.log('data===>', data)
console.log('status===>', status)
if(status?.status!=="failed"){
return {message:'Notification Sent'}
}return {message:'Failed to Send Notification'}
} catch (error) {
logger.error(error.message)
throw new Error(error.message)
}
}
7 changes: 7 additions & 0 deletions utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const STRAPI_TOURISM_TOKEN =
'82ed7f48afbd0c0605ef36bf4b81259f90c7e6bc357db032a011b1c0ab2bc7db2a753fa84c959f403b256ee66b44174dcc453bd0a40797ce8c22c1b6dab7f416cf3b8a247c19144bc3d019f229612f36b12612223b35f28a1a7fec6ff779228730b45fd93a793399d8f462ba0bada15077725a843d3023cf133838876da3547e'

export const ITEM_ID = '2'
export const ITEM_NAME = 'MUSEUM'

export const CAT_ATTR_TAG_RELATIONS = [2, 3, 4, 5]

0 comments on commit 6bc91a9

Please sign in to comment.