Spaces:
Sleeping
Sleeping
File size: 2,930 Bytes
f4656a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 'use strict';
const SessionModel = require('../models/session.model');
const ProductModel = require('../models/product.model');
const logger = require('../utils/logger');
const SessionController = {
/**
* GET /api/session/:id
* Returns full session state including responses and recommendations.
*/
async getSession(req, res) {
try {
const session = await SessionModel.findById(req.params.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
return res.status(200).json({
id: session.id,
channel: session.channel,
userId: session.user_id,
currentQuestionIndex: session.current_question_index,
responses: session.responses,
completed: session.completed,
recommendations: session.recommendations,
metadata: session.metadata,
createdAt: session.created_at,
updatedAt: session.updated_at,
expiresAt: session.expires_at,
});
} catch (err) {
logger.error('getSession error:', err);
return res.status(500).json({ error: 'Internal server error' });
}
},
/**
* POST /api/session/reset/:id
* Deletes the session so the user can start fresh.
*/
async resetSession(req, res) {
try {
const session = await SessionModel.findById(req.params.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
await SessionModel.delete(req.params.id);
logger.info(`Session manually reset: ${req.params.id}`);
return res.status(200).json({ success: true, message: 'Session reset successfully' });
} catch (err) {
logger.error('resetSession error:', err);
return res.status(500).json({ error: 'Internal server error' });
}
},
/**
* GET /api/products
* Returns product catalog with optional category filter.
*/
async getProducts(req, res) {
try {
const { category, limit = 50, offset = 0 } = req.query;
const products = await ProductModel.findAll({ category, limit: parseInt(limit), offset: parseInt(offset) });
return res.status(200).json({ products, count: products.length });
} catch (err) {
logger.error('getProducts error:', err);
return res.status(500).json({ error: 'Internal server error' });
}
},
/**
* GET /api/health
* Health check — verifies app and DB are running.
*/
async healthCheck(req, res) {
try {
// Quick DB probe
await ProductModel.findAll({ limit: 1 });
return res.status(200).json({
status: 'ok',
timestamp: new Date().toISOString(),
uptime: Math.floor(process.uptime()),
environment: process.env.NODE_ENV || 'development',
});
} catch (err) {
logger.error('Health check failed:', err);
return res.status(503).json({ status: 'error', message: err.message });
}
},
};
module.exports = SessionController;
|