correcao na organiz

This commit is contained in:
2026-04-02 11:53:45 +00:00
parent 3132bb73a2
commit 44aac9ac2d
4 changed files with 49 additions and 33 deletions

View File

@@ -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<AuthProviderProps> = ({ children }) => {
const [appUser, setAppUser] = useState<AppUser | null>(null);
@@ -27,6 +31,8 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
} else {
setAppUser(defaultUser);
}
setApiOrganizationId(DEFAULT_ORGANIZATION_ID, DEFAULT_ORGANIZATION_NAME);
}, []);
const isDeveloper = useCallback(() => false, []);

View File

@@ -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);

View File

@@ -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();

View File

@@ -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<INotification> & { 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);