Skip to content

Commit

Permalink
add list sessions route
Browse files Browse the repository at this point in the history
  • Loading branch information
ookamiiixd committed Feb 20, 2023
1 parent cf78fd3 commit c3b4afe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/controllers/session.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import type { RequestHandler } from 'express';
import type { WebSocket } from 'ws';
import { createSession, deleteSession, getSession, sessionExists } from '../wa';
import {
createSession,
deleteSession,
getSession,
getSessionStatus,
listSessions,
sessionExists,
} from '../wa';

export const list: RequestHandler = (req, res) => {
res.status(200).json(listSessions());
};

export const find: RequestHandler = (req, res) =>
res.status(200).json({ message: 'Session found' });

export const status: RequestHandler = (req, res) => {
const state = ['CONNECTING', 'CONNECTED', 'DISCONNECTING', 'DISCONNECTED'];
const session = getSession(req.params.sessionId)!;

let status = state[(session.ws as WebSocket).readyState];
status = session.user ? 'AUTHENTICATED' : status;
res.status(200).json({ status });
res.status(200).json({ status: getSessionStatus(session) });
};

export const add: RequestHandler = async (req, res) => {
const { sessionId, ...options } = req.body;
const { sessionId, readIncomingMessages, ...socketConfig } = req.body;

if (sessionExists(sessionId)) return res.status(400).json({ error: 'Session already exists' });
createSession({ sessionId, res, socketConfig: options });
createSession({ sessionId, res, readIncomingMessages, socketConfig });
};

export const addSSE: RequestHandler = async (req, res) => {
Expand Down
1 change: 1 addition & 0 deletions src/routes/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import requestValidator from '../middlewares/request-validator';
import sessionValidator from '../middlewares/session-validator';

const router = Router();
router.get('/', controller.list);
router.get('/:sessionId', sessionValidator, controller.find);
router.get('/:sessionId/status', sessionValidator, controller.status);
router.post('/add', body('sessionId').isString().notEmpty(), requestValidator, controller.add);
Expand Down

0 comments on commit c3b4afe

Please sign in to comment.