Created
September 20, 2021 07:57
-
-
Save hoangsetup/7c079ec8513af2ae84a8d6da61b03360 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/call-state.controller.ts | |
import { Request, Response } from 'express'; | |
import CustomerModel from '../models/customer.model'; | |
import CallStateModel from '../models/call.state.model'; | |
import { emitCallStateChange } from '../socket'; | |
import moment from 'moment'; | |
const callStateController = { | |
handleNewCallState: async (req: Request, res: Response) => { | |
try { | |
const { phone_number: phoneNumber, state, timestamp } = req.body; | |
let customer = await CustomerModel.findOne({ phoneNumber }).exec(); | |
if (!customer) { | |
customer = await CustomerModel.create({ | |
phoneNumber, | |
name: 'New customer', | |
}); | |
} | |
await CallStateModel.create({ | |
customerId: customer._id, | |
timestamp, | |
state, | |
}); | |
emitCallStateChange({ | |
customerId: customer._id, | |
customerName: customer.name, | |
state, | |
phone: phoneNumber, | |
date: moment(timestamp).format('YYYY/MM/DD hh:mma') | |
}); | |
res.json({ success: true }); | |
} catch (error) { | |
res.status(500); | |
if (error instanceof Error) { | |
res.json({ message: error.message }); | |
} else { | |
res.json({ message: error }); | |
} | |
} | |
}, | |
}; | |
export default callStateController; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment