const storeModel = require("../models/store.model"); // Get all available stores exports.getAllStores = async (req, res) => { try { const stores = await storeModel.getAllStores(); res.json(stores); } catch (error) { console.error("Get all stores error:", error); res.status(500).json({ error: "Failed to fetch stores" }); } }; // Get stores for household exports.getHouseholdStores = async (req, res) => { try { const stores = await storeModel.getHouseholdStores(req.params.householdId); res.json(stores); } catch (error) { console.error("Get household stores error:", error); res.status(500).json({ error: "Failed to fetch household stores" }); } }; // Add store to household exports.addStoreToHousehold = async (req, res) => { try { const { storeId, isDefault } = req.body; if (!storeId) { return res.status(400).json({ error: "Store ID is required" }); } // Check if store exists const store = await storeModel.getStoreById(storeId); if (!store) { return res.status(404).json({ error: "Store not found" }); } await storeModel.addStoreToHousehold( req.params.householdId, storeId, isDefault || false ); res.status(201).json({ message: "Store added to household successfully", store }); } catch (error) { console.error("Add store to household error:", error); res.status(500).json({ error: "Failed to add store to household" }); } }; // Remove store from household exports.removeStoreFromHousehold = async (req, res) => { try { await storeModel.removeStoreFromHousehold( req.params.householdId, req.params.storeId ); res.json({ message: "Store removed from household successfully" }); } catch (error) { console.error("Remove store from household error:", error); res.status(500).json({ error: "Failed to remove store from household" }); } }; // Set default store exports.setDefaultStore = async (req, res) => { try { await storeModel.setDefaultStore( req.params.householdId, req.params.storeId ); res.json({ message: "Default store updated successfully" }); } catch (error) { console.error("Set default store error:", error); res.status(500).json({ error: "Failed to set default store" }); } }; // Create store (system admin only) exports.createStore = async (req, res) => { try { const { name, default_zones } = req.body; if (!name || name.trim().length === 0) { return res.status(400).json({ error: "Store name is required" }); } const store = await storeModel.createStore(name.trim(), default_zones || null); res.status(201).json({ message: "Store created successfully", store }); } catch (error) { console.error("Create store error:", error); if (error.code === '23505') { // Unique violation return res.status(400).json({ error: "Store with this name already exists" }); } res.status(500).json({ error: "Failed to create store" }); } }; // Update store (system admin only) exports.updateStore = async (req, res) => { try { const { name, default_zones } = req.body; const store = await storeModel.updateStore(req.params.storeId, { name: name?.trim(), default_zones }); if (!store) { return res.status(404).json({ error: "Store not found" }); } res.json({ message: "Store updated successfully", store }); } catch (error) { console.error("Update store error:", error); res.status(500).json({ error: "Failed to update store" }); } }; // Delete store (system admin only) exports.deleteStore = async (req, res) => { try { await storeModel.deleteStore(req.params.storeId); res.json({ message: "Store deleted successfully" }); } catch (error) { console.error("Delete store error:", error); if (error.message.includes('in use')) { return res.status(400).json({ error: error.message }); } res.status(500).json({ error: "Failed to delete store" }); } };