Migracao Logto + Supabase - backend e frontend atualizados para nova autenticação

This commit is contained in:
2026-03-30 20:50:10 +00:00
parent 9d3958b82b
commit f89d5571f4
22 changed files with 1266 additions and 1047 deletions

View File

@@ -1,81 +1,45 @@
import Inspection from '../models/Inspection.js';
import { Inspection, findOneGpi, queryGpi } from '../lib/compat.js';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const createInspection = async (data: any & { organizationId?: string, createdBy?: string }) => {
const newInspection = new Inspection({
return await Inspection.create({
...data,
date: data.date ? new Date(data.date) : null,
organizationId: data.organizationId,
createdBy: data.createdBy
date: data.date ? new Date(data.date).toISOString() : null,
organization_id: data.organizationId,
created_by: data.createdBy
});
const saved = await newInspection.save();
return { ...saved.toObject(), id: saved._id.toString() };
};
export const getInspectionsByProject = async (projectId: string, organizationId?: string) => {
const query = { projectId, ...(organizationId ? { organizationId } : {}) };
const inspections = await Inspection.find(query).sort({ date: -1 }).lean();
return inspections.map(i => ({ ...i, id: i._id.toString() }));
const filter: any = { project_id: projectId };
if (organizationId) {
filter.organization_id = organizationId;
}
return await Inspection.find(filter);
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const updateInspection = async (id: string, data: any, organizationId?: string, userId?: string, userRole?: string, isDeveloper: boolean = false) => {
const existing = await Inspection.findById(id);
if (!existing) return null;
export const getInspectionById = async (id: string) => {
return await Inspection.findById(id);
};
// Organization Check
if (organizationId && existing.organizationId && existing.organizationId !== organizationId) {
return null;
}
export const updateInspection = async (id: string, data: any) => {
return await Inspection.findByIdAndUpdate(id, data);
};
// Role/Ownership check
const isPowerUser = userRole === 'admin' || isDeveloper;
if (!isPowerUser && existing.createdBy && existing.createdBy !== userId) {
console.warn(`Permission Denied: User ${userId} tried to update inspection ${id} created by ${existing.createdBy}`);
return null;
}
export const deleteInspection = async (id: string) => {
return await Inspection.findByIdAndDelete(id);
};
const updateData = {
...data,
date: data.date ? new Date(data.date) : undefined
export const getInspectionsByOrganization = async (organizationId: string) => {
return await Inspection.find({ organization_id: organizationId });
};
export const getInspectionStats = async (organizationId?: string) => {
const filter = organizationId ? { organization_id: organizationId } : {};
const inspections = await Inspection.find(filter);
return {
total: inspections.length,
inspections
};
if (organizationId && !existing.organizationId) {
updateData.organizationId = organizationId;
}
const updated = await Inspection.findOneAndUpdate({ _id: id }, updateData, { new: true }).lean();
if (updated) {
return { ...updated, id: updated._id.toString() };
}
return null;
};
export const deleteInspection = async (id: string, organizationId?: string, userId?: string, userRole?: string, isDeveloper: boolean = false) => {
const existing = await Inspection.findById(id);
if (!existing) return false;
// Organization Check
if (organizationId && existing.organizationId && existing.organizationId !== organizationId) {
return false;
}
// Role/Ownership check
const isPowerUser = userRole === 'admin' || isDeveloper;
if (!isPowerUser && existing.createdBy && existing.createdBy !== userId) {
return false;
}
await Inspection.deleteOne({ _id: id });
return true;
};
export const getAllInspections = async (organizationId?: string) => {
const query = organizationId ? { organizationId } : {};
const inspections = await Inspection.find(query).lean();
return inspections.map(i => ({ ...i, id: i._id.toString() }));
};
console.log('✅ InspectionService loaded with compatibility');