Enable store selection for admins and associate sales with specific stores

Implements store selection and association functionality using useStoreContext hook and updates sale creation and retrieval APIs.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 91318273-c764-4fd4-be04-bdc12c38af32
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/0715008c-7244-40f4-befc-26c014852236/d304bad8-e4eb-4558-b9d6-38704ba052e8.jpg
This commit is contained in:
michaelschal committed 2025-07-09 19:04:37 +00:00
1 parent a5336a5919
commit 4384818f63
5 files changed
+216 -7

No files matched your search

+50 -3
View File
@@ -117,6 +117,26 @@ export async function registerRoutes(app: Express): Promise<Server> {
}
});
// Store routes for users
app.get('/api/stores', requireAuth, async (req, res) => {
try {
const user = await storage.getUser(req.session.userId!);
if (!user || !user.storeId) {
return res.status(404).json({ error: "User store not found" });
}
const store = await storage.getStore(user.storeId);
if (!store) {
return res.status(404).json({ error: "Store not found" });
}
res.json([store]); // Return as array to match admin endpoint format
} catch (error) {
console.error('Get user stores error:', error);
res.status(500).json({ error: "Failed to get stores" });
}
});
// Sales routes
app.get('/api/sales', requireAuth, async (req, res) => {
try {
@@ -125,9 +145,23 @@ export async function registerRoutes(app: Express): Promise<Server> {
return res.status(404).json({ error: "User not found" });
}
const { startDate, endDate } = req.query;
const { startDate, endDate, storeId } = req.query;
// Determine which store to query
let targetStoreId: number;
if (user.role === 'admin' && storeId) {
// Admin can query any store
targetStoreId = parseInt(storeId as string);
} else {
// Non-admin users can only query their own store
if (!user.storeId) {
return res.status(400).json({ error: "User has no assigned store" });
}
targetStoreId = user.storeId;
}
const sales = await storage.getSalesByStore(
user.storeId!,
targetStoreId,
startDate as string,
endDate as string
);
@@ -145,9 +179,22 @@ export async function registerRoutes(app: Express): Promise<Server> {
return res.status(404).json({ error: "User not found" });
}
// Determine which store to use for the sale
let targetStoreId: number;
if (user.role === 'admin' && req.body.storeId) {
// Admin can create sales for any store
targetStoreId = req.body.storeId;
} else {
// Non-admin users can only create sales for their assigned store
if (!user.storeId) {
return res.status(400).json({ error: "User has no assigned store" });
}
targetStoreId = user.storeId;
}
const saleData = insertSaleSchema.parse({
...req.body,
storeId: user.storeId,
storeId: targetStoreId,
userId: user.id
});