stabilized app

This commit is contained in:
Torsten Schulz
2025-07-21 14:59:43 +02:00
committed by Torsten (PC)
parent 51fd9fcd13
commit 1451225978
25 changed files with 3590 additions and 228 deletions

View File

@@ -1,14 +1,12 @@
#include "produce_worker.h"
#include "connection_guard.h" // Include for ConnectionGuard
#include "connection_guard.h"
#include <iostream>
#include <algorithm>
#include <thread>
#include <nlohmann/json.hpp>
ProduceWorker::ProduceWorker(ConnectionPool &pool, MessageBroker &broker)
: Worker(pool, broker, "ProduceWorker")
{
}
: Worker(pool, broker, "ProduceWorker") {}
ProduceWorker::~ProduceWorker() {
}
@@ -43,10 +41,8 @@ void ProduceWorker::processProductions() {
setCurrentStep("Get Database Connection");
ConnectionGuard connGuard(pool);
auto &db = connGuard.get();
setCurrentStep("Fetch Finished Productions");
auto finishedProductions = getFinishedProductions(db);
setCurrentStep("Process Finished Productions");
for (const auto &production : finishedProductions) {
if (production.find("branch_id") == production.end() ||
@@ -56,16 +52,20 @@ void ProduceWorker::processProductions() {
production.find("user_id") == production.end()) {
continue;
}
int branchId = std::stoi(production.at("branch_id"));
int productId = std::stoi(production.at("product_id"));
int quantity = std::stoi(production.at("quantity"));
int quality = std::stoi(production.at("quality"));
int userId = std::stoi(production.at("user_id"));
if (addToInventory(db, branchId, productId, quantity, quality, userId)) {
}
int regionId = std::stoi(production.at("region_id"));
addToInventory(db, branchId, productId, quantity, quality, userId);
deleteProduction(db, production.at("production_id"));
addProductionToLog(regionId, userId, productId, quantity);
const nlohmann::json message = {
{"event", "production_ready"},
{"branch_id", std::to_string(branchId) }
};
sendMessageToFalukantUsers(userId, message);
}
} catch (const std::exception &e) {
std::cerr << "[ProduceWorker] Fehler in processProductions: " << e.what() << std::endl;
@@ -83,13 +83,12 @@ std::vector<std::unordered_map<std::string, std::string>> ProduceWorker::getFini
return {};
}
bool ProduceWorker::addToInventory(Database &db,
int branchId,
int productId,
int quantity,
int quality,
int userId)
{
bool ProduceWorker::addToInventory(Database &db,
int branchId,
int productId,
int quantity,
int quality,
int userId) {
try {
db.prepare("get_stocks", QUERY_GET_AVAILABLE_STOCKS);
auto stocks = db.execute("get_stocks", {std::to_string(branchId)});
@@ -99,22 +98,29 @@ bool ProduceWorker::addToInventory(Database &db,
int totalCapacity = std::stoi(stock.at("total_capacity"));
int filledCapacity = std::stoi(stock.at("filled"));
int freeCapacity = totalCapacity - filledCapacity;
if (freeCapacity <= 0) {
continue;
}
int toStore = std::min(remainingQuantity, freeCapacity);
if (!storeInStock(db, stockId, productId, toStore, quality)) {
return false;
}
remainingQuantity -= toStore;
sendProductionReadyEvent(userId, productId, quantity, quality, branchId);
if (remainingQuantity <= 0) {
break;
}
}
return (remainingQuantity == 0);
if (remainingQuantity == 0) {
sendProductionReadyEvent(userId, productId, quantity, quality, branchId);
return true;
}
db.prepare("QUERY_ADD_OVERPRODUCTION_NOTIFICATION", QUERY_ADD_OVERPRODUCTION_NOTIFICATION);
nlohmann::json notification = {
{"tr", "production.overproduction"},
{"value", remainingQuantity}
};
db.execute("QUERY_ADD_OVERPRODUCTION_NOTIFICATION", {std::to_string(userId), notification.dump()});
return true;
} catch (const std::exception &e) {
std::cerr << "[ProduceWorker] Fehler in addToInventory: " << e.what() << std::endl;
}
@@ -125,8 +131,7 @@ bool ProduceWorker::storeInStock(Database &db,
int stockId,
int productId,
int quantity,
int quality)
{
int quality) {
try {
db.prepare("insert_inventory", QUERY_INSERT_INVENTORY);
db.execute("insert_inventory", {std::to_string(stockId),
@@ -170,3 +175,15 @@ void ProduceWorker::sendProductionReadyEvent(int userId,
<< e.what() << std::endl;
}
}
void ProduceWorker::addProductionToLog(int regionId, int userId, int productId, int quantity) {
try {
ConnectionGuard connGuard(pool);
auto &db = connGuard.get();
db.prepare("QUERY_INSERT_UPDATE_PRODUCTION_LOG", QUERY_INSERT_UPDATE_PRODUCTION_LOG);
db.execute("QUERY_INSERT_UPDATE_PRODUCTION_LOG", { std::to_string(regionId), std::to_string(productId),
std::to_string(productId), std::to_string(userId) });
} catch (const std::exception &e) {
}
}