correcao na organiz
This commit is contained in:
@@ -2,6 +2,7 @@ import React, { useState, useEffect, useCallback, useMemo } from 'react';
|
|||||||
import type { AppUser, UserRole } from '../types';
|
import type { AppUser, UserRole } from '../types';
|
||||||
import { AuthContext } from './AuthContextType';
|
import { AuthContext } from './AuthContextType';
|
||||||
import { getUser } from '../main';
|
import { getUser } from '../main';
|
||||||
|
import { setApiOrganizationId } from '../services/api';
|
||||||
|
|
||||||
interface AuthProviderProps {
|
interface AuthProviderProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
@@ -17,6 +18,9 @@ const defaultUser: AppUser = {
|
|||||||
updatedAt: new Date().toISOString()
|
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 }) => {
|
export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
||||||
const [appUser, setAppUser] = useState<AppUser | null>(null);
|
const [appUser, setAppUser] = useState<AppUser | null>(null);
|
||||||
|
|
||||||
@@ -27,6 +31,8 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
|||||||
} else {
|
} else {
|
||||||
setAppUser(defaultUser);
|
setAppUser(defaultUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setApiOrganizationId(DEFAULT_ORGANIZATION_ID, DEFAULT_ORGANIZATION_NAME);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const isDeveloper = useCallback(() => false, []);
|
const isDeveloper = useCallback(() => false, []);
|
||||||
|
|||||||
@@ -1,25 +1,23 @@
|
|||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import { notificationService } from '../services/notificationService.js';
|
import { supabase } from '../config/supabase.js';
|
||||||
|
|
||||||
export const notificationController = {
|
export const notificationController = {
|
||||||
getUserNotifications: async (req: Request, res: Response) => {
|
getUserNotifications: async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const organizationId = req.headers['x-organization-id'] as string;
|
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) {
|
if (!organizationId) {
|
||||||
return res.status(400).json({ error: 'Organization ID is required' });
|
return res.status(400).json({ error: 'Organization ID is required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifications = await notificationService.getUserNotifications(
|
const { data: notifications, error } = await supabase
|
||||||
userId,
|
.from('notifications')
|
||||||
organizationId,
|
.select('*')
|
||||||
req.query.includeArchived === 'true'
|
.eq('organization_id', organizationId)
|
||||||
);
|
.order('created_at', { ascending: false });
|
||||||
res.json(notifications);
|
|
||||||
|
if (error) throw error;
|
||||||
|
res.json(notifications || []);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
res.status(500).json({ error: 'Error fetching notifications' });
|
res.status(500).json({ error: 'Error fetching notifications' });
|
||||||
@@ -29,8 +27,13 @@ export const notificationController = {
|
|||||||
markAsRead: async (req: Request, res: Response) => {
|
markAsRead: async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const notification = await notificationService.markAsRead(id as string);
|
const { error } = await supabase
|
||||||
res.json(notification);
|
.from('notifications')
|
||||||
|
.update({ is_read: true })
|
||||||
|
.eq('id', id);
|
||||||
|
|
||||||
|
if (error) throw error;
|
||||||
|
res.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
res.status(500).json({ error: 'Error marking notification as read' });
|
res.status(500).json({ error: 'Error marking notification as read' });
|
||||||
@@ -40,13 +43,17 @@ export const notificationController = {
|
|||||||
markAllAsRead: async (req: Request, res: Response) => {
|
markAllAsRead: async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const organizationId = req.headers['x-organization-id'] as string;
|
const organizationId = req.headers['x-organization-id'] as string;
|
||||||
const userId = req.headers['x-user-id'] as string;
|
|
||||||
|
|
||||||
if (!organizationId) {
|
if (!organizationId) {
|
||||||
return res.status(400).json({ error: 'Organization ID is required' });
|
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 });
|
res.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@@ -57,13 +64,17 @@ export const notificationController = {
|
|||||||
clearAll: async (req: Request, res: Response) => {
|
clearAll: async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const organizationId = req.headers['x-organization-id'] as string;
|
const organizationId = req.headers['x-organization-id'] as string;
|
||||||
const userId = req.headers['x-user-id'] as string;
|
|
||||||
|
|
||||||
if (!organizationId) {
|
if (!organizationId) {
|
||||||
return res.status(400).json({ error: 'Organization ID is required' });
|
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 });
|
res.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@@ -74,9 +85,13 @@ export const notificationController = {
|
|||||||
archive: async (req: Request, res: Response) => {
|
archive: async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const userId = req.headers['x-user-id'] as string;
|
const { error } = await supabase
|
||||||
const notification = await notificationService.archive(id as string, userId);
|
.from('notifications')
|
||||||
res.json(notification);
|
.update({ is_archived: true })
|
||||||
|
.eq('id', id);
|
||||||
|
|
||||||
|
if (error) throw error;
|
||||||
|
res.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
res.status(500).json({ error: 'Error archiving notification' });
|
res.status(500).json({ error: 'Error archiving notification' });
|
||||||
@@ -86,8 +101,12 @@ export const notificationController = {
|
|||||||
delete: async (req: Request, res: Response) => {
|
delete: async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const userId = req.headers['x-user-id'] as string;
|
const { error } = await supabase
|
||||||
await notificationService.softDelete(id as string, userId);
|
.from('notifications')
|
||||||
|
.delete()
|
||||||
|
.eq('id', id);
|
||||||
|
|
||||||
|
if (error) throw error;
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export const extractUser = async (req: Request, res: Response, next: NextFunctio
|
|||||||
email: 'guest@gpi.app',
|
email: 'guest@gpi.app',
|
||||||
name: 'Guest User',
|
name: 'Guest User',
|
||||||
role: 'user',
|
role: 'user',
|
||||||
organizationId: 'default-org',
|
organizationId: req.headers['x-organization-id'] as string || 'default-org',
|
||||||
organizationRole: 'user'
|
organizationRole: 'user'
|
||||||
};
|
};
|
||||||
next();
|
next();
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { StockItem, Instrument, Notification, TechnicalDataSheet } from '../lib/compat.js';
|
import { StockItem, Instrument, Notification, TechnicalDataSheet } from '../lib/compat.js';
|
||||||
import { INotification } from '../models/Notification.js';
|
|
||||||
import { addMonths, isBefore } from 'date-fns';
|
import { addMonths, isBefore } from 'date-fns';
|
||||||
|
|
||||||
export const notificationService = {
|
export const notificationService = {
|
||||||
async create(data: Partial<INotification> & { organizationId: string }) {
|
async create(data: any & { organizationId: string }) {
|
||||||
return await Notification.create(data);
|
return await Notification.create(data);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -11,14 +10,6 @@ export const notificationService = {
|
|||||||
return await Notification.find({ organizationId });
|
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) {
|
async checkLowStock(stockItemId: string) {
|
||||||
try {
|
try {
|
||||||
const item = await StockItem.findById(stockItemId);
|
const item = await StockItem.findById(stockItemId);
|
||||||
|
|||||||
Reference in New Issue
Block a user