Skip to content

Commit

Permalink
Added: Update Status API
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyvishal committed Apr 9, 2024
1 parent f312d50 commit 07edeea
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 14 deletions.
5 changes: 4 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ TEST_RECEPIENT_NUMBER=
STRAPI_TOURISM_TOKEN=
GOOGLE_MAPS_API_KEY=your_api_key_here
SERVER_URL=http://13.201.62.138:3001
DEVELOPER_MODE_ON=0
DEVELOPER_MODE_ON=0
STRAPI_RETAIL_TOKEN=
STRAPI_ENERGY_TOKEN=
STRAPI_HOTEL_TOKEN=
3 changes: 3 additions & 0 deletions .github/workflows/api_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
echo "GOOGLE_MAPS_API_KEY=${{secrets.GOOGLE_MAPS_API_KEY}}" >> .env
echo "SERVER_URL=${{secrets.SERVER_URL}}" >> .env
echo "DEVELOPER_MODE_ON=${{secrets.DEVELOPER_MODE_ON}}" >> .env
echo "STRAPI_RETAIL_TOKEN=${{secrets.STRAPI_RETAIL_TOKEN}}" >> .env
echo "STRAPI_ENERGY_TOKEN=${{secrets.STRAPI_ENERGY_TOKEN}}" >> .env
echo "STRAPI_HOTEL_TOKEN=${{secrets.STRAPI_HOTEL_TOKEN}}" >> .env
- name: Set up Node.js
uses: actions/setup-node@v2
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
echo "GOOGLE_MAPS_API_KEY=${{secrets.GOOGLE_MAPS_API_KEY}}" >> .env
echo "SERVER_URL=${{secrets.SERVER_URL}}" >> .env
echo "DEVELOPER_MODE_ON=${{secrets.DEVELOPER_MODE_ON}}" >> .env
echo "STRAPI_RETAIL_TOKEN=${{secrets.STRAPI_RETAIL_TOKEN}}" >> .env
echo "STRAPI_ENERGY_TOKEN=${{secrets.STRAPI_ENERGY_TOKEN}}" >> .env
echo "STRAPI_HOTEL_TOKEN=${{secrets.STRAPI_HOTEL_TOKEN}}" >> .env
- name: Create SSH key file
run: echo -e "${{ secrets.EC2_SSH_KEY }}" > ~/ec2_key
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/lint_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ jobs:
echo "GOOGLE_MAPS_API_KEY=${{secrets.GOOGLE_MAPS_API_KEY}}" >> .env
echo "SERVER_URL=${{secrets.SERVER_URL}}" >> .env
echo "DEVELOPER_MODE_ON=${{secrets.DEVELOPER_MODE_ON}}" >> .env
echo "STRAPI_RETAIL_TOKEN=${{secrets.STRAPI_RETAIL_TOKEN}}" >> .env
echo "STRAPI_ENERGY_TOKEN=${{secrets.STRAPI_ENERGY_TOKEN}}" >> .env
echo "STRAPI_HOTEL_TOKEN=${{secrets.STRAPI_HOTEL_TOKEN}}" >> .env
- name: Set up Node.js
uses: actions/setup-node@v2
with:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ jobs:
echo "GOOGLE_MAPS_API_KEY=${{secrets.GOOGLE_MAPS_API_KEY}}" >> .env
echo "SERVER_URL=${{secrets.SERVER_URL}}" >> .env
echo "DEVELOPER_MODE_ON=${{secrets.DEVELOPER_MODE_ON}}" >> .env
echo "STRAPI_RETAIL_TOKEN=${{secrets.STRAPI_RETAIL_TOKEN}}" >> .env
echo "STRAPI_ENERGY_TOKEN=${{secrets.STRAPI_ENERGY_TOKEN}}" >> .env
echo "STRAPI_HOTEL_TOKEN=${{secrets.STRAPI_HOTEL_TOKEN}}" >> .env
- name: Set up Node.js
uses: actions/setup-node@v2
with:
Expand Down
4 changes: 3 additions & 1 deletion controllers/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import MapsService from '../services/MapService.js'
import get_text_by_key from '../utils/language.js'
const mapService = new MapsService()

import {ORDER_DETAILS} from '../utils/constants.js'
import CronService from '../services/CronService.js'
const actionsService = new ActionsService()
const db = new DBService();

Expand Down Expand Up @@ -385,5 +387,5 @@ async function process_action(action, text, session, sender=null, format='applic
}
export default {
process_wa_webhook,
process_text
process_text,
}
77 changes: 75 additions & 2 deletions controllers/ControlCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
NEW_CATALOG_AVAILABLE,
TRIGGER_BLIZZARD_MESSAGE,
CANCEL_BOOKING_MESSAGE,
TOURISM_STRAPI_URL
TOURISM_STRAPI_URL,
HOTEL_STRAPI_URL,
RETAIL_STRAPI_URL,
ENERGY_STRAPI_URL,
DOMAINS,
UPDATE_STATUS_MESSAGE
} from '../utils/constants.js'
import DBService from '../services/DBService.js'
import MapsService from '../services/MapService.js'
Expand Down Expand Up @@ -138,4 +143,72 @@ export const notify = async (req, res) => {
res.send("Triggered!")
}
else res.status(400).send('Point and message are required in the body.')
}
}


export const updateStatus = async (req, res) => {
try {
const { orderId, domain="" } = req.body
if(!orderId){
return res.status(400).json({message:"Order Id is Required", status:false})
}
let DOMAIN_DETAILS = {
url:"",
token:"",
message:""
}
switch(domain){
case DOMAINS.ENERGY:
DOMAIN_DETAILS = {
url:ENERGY_STRAPI_URL,
token:process.env.STRAPI_ENERGY_TOKEN,
message:UPDATE_STATUS_MESSAGE.ENERGY

}
break;
case DOMAINS.RETAIL:
DOMAIN_DETAILS = {
url:RETAIL_STRAPI_URL,
token:process.env.STRAPI_RETAIL_TOKEN,
message:UPDATE_STATUS_MESSAGE.RETAIL
}
break;
case DOMAINS.HOTEL:
DOMAIN_DETAILS = {
url:HOTEL_STRAPI_URL,
token:process.env.STRAPI_HOTEL_TOKEN,
message:UPDATE_STATUS_MESSAGE.HOTEL
}
break;
case DOMAINS.TOURISM:
DOMAIN_DETAILS = {
url:TOURISM_STRAPI_URL,
token:process.env.STRAPI_TOURISM_TOKEN,
message:UPDATE_STATUS_MESSAGE.TOURISM
}
break;
}
const validOrderId = await action.call_api(`${DOMAIN_DETAILS.url}/orders/${orderId}`,'GET',{},{ Authorization: `Bearer ${DOMAIN_DETAILS.token}`})
logger.info(`OrderDetails: ${JSON.stringify(validOrderId)}`)
if(!validOrderId.status){
return res.status(400).send({ message: `Invalid Order Id`, status:false })
}

const getOrderFulfillmentDetails = await action.call_api(`${DOMAIN_DETAILS.url}/order-fulfillments?order_id=${orderId}`,'GET',{},{ Authorization: `Bearer ${DOMAIN_DETAILS.token}`})
logger.info(`Order Fulfillment Details: ${JSON.stringify(getOrderFulfillmentDetails)}`)
if (getOrderFulfillmentDetails.data.data.length) {
const updateStatusResponse = await action.call_api(`${DOMAIN_DETAILS.url}/order-fulfillments/${getOrderFulfillmentDetails.data.data[0].id}`,'PUT',{
data: {
state_code: DOMAIN_DETAILS.message,
state_value: DOMAIN_DETAILS.message,
},
},{ Authorization: `Bearer ${DOMAIN_DETAILS.token}`})
return res.status(200).send({ message: `Status Updated to: ${updateStatusResponse.data.data.attributes.state_value}`, status:true })
}

return res.status(400).send({ message: 'Order Status Update Failed', status:false })
} catch (error) {
logger.error(error.message)
return res.status(400).send({ message: error.message, status:false })
}
}
5 changes: 4 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import bodyParser from 'body-parser'
import logger from './utils/logger.js'
import messageController from './controllers/Bot.js'
import DBService from './services/DBService.js'

import {
cancelBooking,
updateCatalog,
notify,
triggerExceptionOnLocation
triggerExceptionOnLocation,
updateStatus
} from './controllers/ControlCenter.js'
import path from 'path'
import { fileURLToPath } from 'url';
Expand All @@ -30,6 +32,7 @@ app.post('/notify', notify)
app.post('/cancel-booking', cancelBooking)
app.post('/update-catalog', updateCatalog)
app.post('/trigger-exception', triggerExceptionOnLocation)
app.post('/update-status', updateStatus)

// Reset all sessions
const db = new DBService()
Expand Down
8 changes: 4 additions & 4 deletions services/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Actions {
}

async call_api(endpoint, method, data, headers = {}) {
logger.info(`Calling ${method} on ${endpoint}...`);
logger.info(`Data: ${JSON.stringify(data, null, 2)}`);
// logger.info(`Calling ${method} on ${endpoint}...`);
// logger.info(`Data: ${JSON.stringify(data, null, 2)}`);
let responseObject = {
status: false,
retry: false,
Expand All @@ -50,8 +50,8 @@ class Actions {
data: response.data,
cookies: response.headers['set-cookie'],
}
logger.info(`API call was successful: , response.status`)
logger.info(JSON.stringify(response.data, null, 2))
// logger.info(`API call was successful: , response.status`)
// logger.info(JSON.stringify(response.data, null, 2))
} catch (error) {
logger.error(error)

Expand Down
76 changes: 76 additions & 0 deletions services/CronService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
TOURISM_STRAPI_URL,
HOTEL_STRAPI_URL,
RETAIL_STRAPI_URL,
ENERGY_STRAPI_URL,
DOMAINS
} from '../utils/constants.js'
import ActionsService from '../services/Actions.js'
const action = new ActionsService()
class CronService {
constructor() {

}
async sendStatusUpdatedNotification(db, recipient){
try{
const data = await db.get_data('orderDetails')
if(data.data.domain){
const statusNotificationPromises = [];
for(let i=0;i<data.data.domain.length;i++){
const domainName = Object.keys(data.data.domain[i])[0];
let DOMAIN_DETAILS = {
url:"",
token:"",
message:""
}
switch(domainName){
case DOMAINS.ENERGY:
DOMAIN_DETAILS = {
url:ENERGY_STRAPI_URL,
token:process.env.STRAPI_ENERGY_TOKEN
}
break;
case DOMAINS.RETAIL:
DOMAIN_DETAILS = {
url:RETAIL_STRAPI_URL,
token:process.env.STRAPI_RETAIL_TOKEN
}
break;
case DOMAINS.HOTEL:
DOMAIN_DETAILS = {
url:HOTEL_STRAPI_URL,
token:process.env.STRAPI_HOTEL_TOKEN
}
break;
case DOMAINS.TOURISM:
DOMAIN_DETAILS = {
url:TOURISM_STRAPI_URL,
token:process.env.STRAPI_TOURISM_TOKEN
}
break;
}
for(let j=0;j<data.data.domain[i][domainName].orders.length;j++){
const order = data.data.domain[i][domainName].orders[j]
const getStatus = await action.call_api(`${DOMAIN_DETAILS.url}/order-fulfillments?order_id=${order.orderId}`,'GET',{},{ Authorization: `Bearer ${DOMAIN_DETAILS.token}`})
const validOrderFulfillment = getOrderFulfillmentDetails.data.data.filter((strapiOrders)=>strapiOrders.id == order.orderId)
console.log("here-->",validOrderFulfillment.length, validOrderFulfillment[0].attributes.state_code !== null, order.orderFulfillmentStatus, validOrderFulfillment[0].attributes.state_code ,validOrderFulfillment[0].attributes.state_code !== order.orderFulfillmentStatus);
if(validOrderFulfillment.length && validOrderFulfillment[0].attributes.state_code && validOrderFulfillment[0].attributes.state_code !== order.orderFulfillmentStatus){
console.log("here");
await action.send_message(
process.env.TEST_RECEPIENT_NUMBER,
`Hey,\nStatus for order id: ${order.orderId} for Domain: ${domainName} is changed.\nNew Status Is ${validOrderFulfillment[0].attributes.state_code}`
)
}
}
}
return {}
}
}catch(error){
console.log(error)
throw new Error(error.message)
}

}
}

export default CronService
32 changes: 32 additions & 0 deletions services/DBService.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,38 @@ class DBService {

return sessions;
}
async set_data(key, data) {
let response = {
status: false,
}
try {
await this.redisClient.set(key, JSON.stringify(data))
response.status = true
response.message = 'Data set successfully!'
} catch (err) {
logger.error(err)
response.error = err
}

logger.info(response)
return response
}
async get_data(key) {
let response = {
status: false,
}
try {
const data = await this.redisClient.get(key)
response.status = true
response.message = 'Data set successfully!';
response.data = JSON.parse(data)
} catch (err) {
logger.error(err)
response.error = err
}

logger.info(response)
return response}
}

export default DBService;
Loading

0 comments on commit 07edeea

Please sign in to comment.