Skip to content

Commit

Permalink
Initial session API implentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcodes78 committed Oct 25, 2021
1 parent a958727 commit d4a12b0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ whatsapp = require('./whatsapp'),
{ listen } = require('./socket'),
chats = require('./routes/chatsRouter'),
groups = require('./routes/groupsRouter')
sessions = require('./routes/sessionRouter')

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

app.use('/chats', chats)
app.use('/groups', groups)

app.get('/', (req, res) => {
res.sendFile('./index.html', {root: __dirname})
})
app.use('/sessions', sessions)

app.all('*', (req, res) => {
response(res, 404, {success: false, message: 'The requested url cannot be found.'})
Expand Down
42 changes: 42 additions & 0 deletions src/controllers/sessionController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { response } = require('../response'),
{ validationResult } = require('express-validator'),
whatsapp = require('../whatsapp')

const createSession = (req, res) => {
const errors = validationResult(req)
if(!errors.isEmpty()) return response(res, 400, {success: false, message: 'Please fill out all required inputs.'})

const session = req.body.session;

if(whatsapp.checkSession(session)) return response(res, 422, {
error: 'Session already exists.'
})

whatsapp.createSession(null, session)
.then(() => {
response(res, 200, {
success: true,
message: "New session created."
})
})
.catch(err => {
response(res, 422, {
error: 'Error creating session.',
message: err
})
})
}

const getActiveSessions = (req, res) => {

response(res, 200, {
success: true,
data: whatsapp.getActiveSessions()
})

}

module.exports = {
createSession: createSession,
getActiveSessions: getActiveSessions
}
16 changes: 16 additions & 0 deletions src/routes/sessionRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const router = require('express').Router(),
{ query, body } = require('express-validator'),
controller = require('./../controllers/sessionController')

router.get(
'/',
controller.getActiveSessions
)

router.post(
'/',
body('session').notEmpty(),
controller.createSession
)

module.exports = router
11 changes: 3 additions & 8 deletions src/whatsapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const createExistedSession = async session => {
.catch(err => console.log('Unexpected error: ' + err))
}

const createSession = async (socket, session) => {
const createSession = async (session) => {
let wa = new WAConnection()

wa.browserDescription = ['Windows', 'Chrome', '10']
Expand All @@ -51,9 +51,8 @@ const createSession = async (socket, session) => {

wa.on('qr', qr => {
qrcode.toDataURL(qr, (err, url) => {
if(err) socket.emit('message', 'An error occured during creating QR image.')

socket.emit('qr', {id: session, qr: url})
//save session
console.log(qr)
})
})

Expand All @@ -65,14 +64,10 @@ const createSession = async (socket, session) => {
connections[session] = wa

clearTimeout(timeout)

socket.emit('added', session)
})

wa.on('close', () => {
deleteSession(session)

socket.emit('deleted', session)
})

return await wa.connect()
Expand Down

0 comments on commit d4a12b0

Please sign in to comment.