From 44aac9ac2db7609f41309c42e4914270ef87dd3d Mon Sep 17 00:00:00 2001 From: admtracksteel Date: Thu, 2 Apr 2026 11:53:45 +0000 Subject: [PATCH] correcao na organiz --- src/client/context/AuthContext.tsx | 6 ++ .../controllers/notificationController.ts | 63 ++++++++++++------- src/server/middleware/authMiddleware.ts | 2 +- src/server/services/notificationService.ts | 11 +--- 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/client/context/AuthContext.tsx b/src/client/context/AuthContext.tsx index a70793c..8dca2d0 100644 --- a/src/client/context/AuthContext.tsx +++ b/src/client/context/AuthContext.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect, useCallback, useMemo } from 'react'; import type { AppUser, UserRole } from '../types'; import { AuthContext } from './AuthContextType'; import { getUser } from '../main'; +import { setApiOrganizationId } from '../services/api'; interface AuthProviderProps { children: React.ReactNode; @@ -17,6 +18,9 @@ const defaultUser: AppUser = { updatedAt: new Date().toISOString() }; +const DEFAULT_ORGANIZATION_ID = 'default-org'; +const DEFAULT_ORGANIZATION_NAME = 'Organização Padrão'; + export const AuthProvider: React.FC = ({ children }) => { const [appUser, setAppUser] = useState(null); @@ -27,6 +31,8 @@ export const AuthProvider: React.FC = ({ children }) => { } else { setAppUser(defaultUser); } + + setApiOrganizationId(DEFAULT_ORGANIZATION_ID, DEFAULT_ORGANIZATION_NAME); }, []); const isDeveloper = useCallback(() => false, []); diff --git a/src/server/controllers/notificationController.ts b/src/server/controllers/notificationController.ts index 4c066df..22645fa 100644 --- a/src/server/controllers/notificationController.ts +++ b/src/server/controllers/notificationController.ts @@ -1,25 +1,23 @@ import { Request, Response } from 'express'; -import { notificationService } from '../services/notificationService.js'; +import { supabase } from '../config/supabase.js'; export const notificationController = { getUserNotifications: async (req: Request, res: Response) => { try { const organizationId = req.headers['x-organization-id'] as string; - const userId = req.headers['x-user-id'] as string; // Assumindo que temos o ID do usuário (clerkId ou email) - - // Se não tiver userId no header (ainda não implementado auth full), tentar pegar do query ou usar um fallback - // Nota: Idealmente o middleware de auth popula req.user. Vamos assumir que passamos x-user-id no frontend por enquanto. if (!organizationId) { return res.status(400).json({ error: 'Organization ID is required' }); } - const notifications = await notificationService.getUserNotifications( - userId, - organizationId, - req.query.includeArchived === 'true' - ); - res.json(notifications); + const { data: notifications, error } = await supabase + .from('notifications') + .select('*') + .eq('organization_id', organizationId) + .order('created_at', { ascending: false }); + + if (error) throw error; + res.json(notifications || []); } catch (error) { console.error(error); res.status(500).json({ error: 'Error fetching notifications' }); @@ -29,8 +27,13 @@ export const notificationController = { markAsRead: async (req: Request, res: Response) => { try { const { id } = req.params; - const notification = await notificationService.markAsRead(id as string); - res.json(notification); + const { error } = await supabase + .from('notifications') + .update({ is_read: true }) + .eq('id', id); + + if (error) throw error; + res.json({ success: true }); } catch (error) { console.error(error); res.status(500).json({ error: 'Error marking notification as read' }); @@ -40,13 +43,17 @@ export const notificationController = { markAllAsRead: async (req: Request, res: Response) => { try { const organizationId = req.headers['x-organization-id'] as string; - const userId = req.headers['x-user-id'] as string; if (!organizationId) { return res.status(400).json({ error: 'Organization ID is required' }); } - await notificationService.markAllAsRead(userId, organizationId); + const { error } = await supabase + .from('notifications') + .update({ is_read: true }) + .eq('organization_id', organizationId); + + if (error) throw error; res.json({ success: true }); } catch (error) { console.error(error); @@ -57,13 +64,17 @@ export const notificationController = { clearAll: async (req: Request, res: Response) => { try { const organizationId = req.headers['x-organization-id'] as string; - const userId = req.headers['x-user-id'] as string; if (!organizationId) { return res.status(400).json({ error: 'Organization ID is required' }); } - await notificationService.clearAll(userId, organizationId); + const { error } = await supabase + .from('notifications') + .delete() + .eq('organization_id', organizationId); + + if (error) throw error; res.json({ success: true }); } catch (error) { console.error(error); @@ -74,9 +85,13 @@ export const notificationController = { archive: async (req: Request, res: Response) => { try { const { id } = req.params; - const userId = req.headers['x-user-id'] as string; - const notification = await notificationService.archive(id as string, userId); - res.json(notification); + const { error } = await supabase + .from('notifications') + .update({ is_archived: true }) + .eq('id', id); + + if (error) throw error; + res.json({ success: true }); } catch (error) { console.error(error); res.status(500).json({ error: 'Error archiving notification' }); @@ -86,8 +101,12 @@ export const notificationController = { delete: async (req: Request, res: Response) => { try { const { id } = req.params; - const userId = req.headers['x-user-id'] as string; - await notificationService.softDelete(id as string, userId); + const { error } = await supabase + .from('notifications') + .delete() + .eq('id', id); + + if (error) throw error; res.json({ success: true }); } catch (error) { console.error(error); diff --git a/src/server/middleware/authMiddleware.ts b/src/server/middleware/authMiddleware.ts index f08eafb..9be414a 100644 --- a/src/server/middleware/authMiddleware.ts +++ b/src/server/middleware/authMiddleware.ts @@ -23,7 +23,7 @@ export const extractUser = async (req: Request, res: Response, next: NextFunctio email: 'guest@gpi.app', name: 'Guest User', role: 'user', - organizationId: 'default-org', + organizationId: req.headers['x-organization-id'] as string || 'default-org', organizationRole: 'user' }; next(); diff --git a/src/server/services/notificationService.ts b/src/server/services/notificationService.ts index 0fb234a..a82bf31 100644 --- a/src/server/services/notificationService.ts +++ b/src/server/services/notificationService.ts @@ -1,9 +1,8 @@ import { StockItem, Instrument, Notification, TechnicalDataSheet } from '../lib/compat.js'; -import { INotification } from '../models/Notification.js'; import { addMonths, isBefore } from 'date-fns'; export const notificationService = { - async create(data: Partial & { organizationId: string }) { + async create(data: any & { organizationId: string }) { return await Notification.create(data); }, @@ -11,14 +10,6 @@ export const notificationService = { return await Notification.find({ organizationId }); }, - async markAsRead(id: string) { - return await Notification.findOneAndUpdate({ id }, { isRead: true }); - }, - - async delete(id: string) { - return await Notification.findByIdAndDelete(id); - }, - async checkLowStock(stockItemId: string) { try { const item = await StockItem.findById(stockItemId);