6989a98d75
- Arkitektur: docs/auth/passwordless-architecture.md - Backend: iom/quixzoom-auth-service/ (FastAPI + Redis) - Webb: quixzoom-market-pages/se/login/ (QR-kod + polling) - App: iom/quixzoom-app/src/features/auth/ (push + deep links) Flöde: QR-kod → app-godkännande → webb-inloggad
145 lines
3.9 KiB
JavaScript
145 lines
3.9 KiB
JavaScript
const { Order } = require('../models');
|
|
const reportQueue = require('../queues/reportQueue');
|
|
const logger = require('../utils/logger');
|
|
|
|
class OrderController {
|
|
async create(req, res) {
|
|
try {
|
|
const order = await Order.create({
|
|
productName: req.body.productName,
|
|
productType: req.body.productType,
|
|
ingredients: req.body.ingredients,
|
|
usage: req.body.usage,
|
|
format: req.body.format,
|
|
priceLevel: req.body.priceLevel,
|
|
certifications: req.body.certifications,
|
|
targetGroups: req.body.targetGroups || [],
|
|
targetMarkets: req.body.targetMarkets || [],
|
|
currentMarkets: req.body.currentMarkets || [],
|
|
email: req.body.email,
|
|
priority: req.body.priority || 'normal',
|
|
estimatedCompletion: new Date(Date.now() + 72 * 60 * 60 * 1000) // 72 hours
|
|
});
|
|
|
|
// Add to queue for processing
|
|
await reportQueue.add({
|
|
orderId: order.id
|
|
}, {
|
|
delay: 1000, // Start after 1 second
|
|
priority: req.body.priority === 'urgent' ? 1 :
|
|
req.body.priority === 'high' ? 2 : 3
|
|
});
|
|
|
|
logger.info(`Order created: ${order.id}`);
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
data: {
|
|
id: order.id,
|
|
productName: order.productName,
|
|
status: order.status,
|
|
progress: order.progress,
|
|
estimatedCompletion: order.estimatedCompletion,
|
|
message: 'Din beställning har mottagits och bearbetas.'
|
|
}
|
|
});
|
|
} catch (error) {
|
|
logger.error('Order creation failed:', error);
|
|
res.status(500).json({
|
|
error: 'Kunde inte skapa beställning',
|
|
message: error.message
|
|
});
|
|
}
|
|
}
|
|
|
|
async getById(req, res) {
|
|
try {
|
|
const order = await Order.findByPk(req.params.id);
|
|
|
|
if (!order) {
|
|
return res.status(404).json({ error: 'Beställning hittades inte' });
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
data: order
|
|
});
|
|
} catch (error) {
|
|
logger.error('Get order failed:', error);
|
|
res.status(500).json({ error: 'Kunde inte hämta beställning' });
|
|
}
|
|
}
|
|
|
|
async getByEmail(req, res) {
|
|
try {
|
|
const orders = await Order.findAll({
|
|
where: { email: req.query.email },
|
|
order: [['createdAt', 'DESC']]
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: orders
|
|
});
|
|
} catch (error) {
|
|
logger.error('Get orders by email failed:', error);
|
|
res.status(500).json({ error: 'Kunde inte hämta beställningar' });
|
|
}
|
|
}
|
|
|
|
async getAll(req, res) {
|
|
try {
|
|
const { status, page = 1, limit = 20 } = req.query;
|
|
const where = {};
|
|
|
|
if (status) where.status = status;
|
|
|
|
const orders = await Order.findAndCountAll({
|
|
where,
|
|
order: [['createdAt', 'DESC']],
|
|
limit: parseInt(limit),
|
|
offset: (parseInt(page) - 1) * parseInt(limit)
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: orders.rows,
|
|
pagination: {
|
|
total: orders.count,
|
|
page: parseInt(page),
|
|
pages: Math.ceil(orders.count / parseInt(limit))
|
|
}
|
|
});
|
|
} catch (error) {
|
|
logger.error('Get all orders failed:', error);
|
|
res.status(500).json({ error: 'Kunde inte hämta beställningar' });
|
|
}
|
|
}
|
|
|
|
async updateStatus(req, res) {
|
|
try {
|
|
const order = await Order.findByPk(req.params.id);
|
|
|
|
if (!order) {
|
|
return res.status(404).json({ error: 'Beställning hittades inte' });
|
|
}
|
|
|
|
await order.update({
|
|
status: req.body.status,
|
|
progress: req.body.progress,
|
|
progressMessage: req.body.progressMessage
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: order
|
|
});
|
|
} catch (error) {
|
|
logger.error('Update order status failed:', error);
|
|
res.status(500).json({ error: 'Kunde inte uppdatera status' });
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = new OrderController();
|