new endpoints
This commit is contained in:
@@ -1,45 +1,96 @@
|
||||
import { Inspection, findOneGpi, queryGpi } from '../lib/compat.js';
|
||||
import { supabase } from '../config/supabase.js';
|
||||
|
||||
export const createInspection = async (data: any & { organizationId?: string, createdBy?: string }) => {
|
||||
return await Inspection.create({
|
||||
...data,
|
||||
date: data.date ? new Date(data.date).toISOString() : null,
|
||||
organization_id: data.organizationId,
|
||||
created_by: data.createdBy
|
||||
});
|
||||
const { data: inspection, error } = await supabase
|
||||
.from('inspections')
|
||||
.insert({
|
||||
...data,
|
||||
date: data.date ? new Date(data.date).toISOString() : null,
|
||||
organization_id: data.organizationId,
|
||||
created_by: data.createdBy
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return inspection;
|
||||
};
|
||||
|
||||
export const getInspectionsByProject = async (projectId: string, organizationId?: string) => {
|
||||
const filter: any = { project_id: projectId };
|
||||
let query = supabase
|
||||
.from('inspections')
|
||||
.select('*')
|
||||
.eq('project_id', projectId);
|
||||
|
||||
if (organizationId) {
|
||||
filter.organization_id = organizationId;
|
||||
query = query.eq('organization_id', organizationId);
|
||||
}
|
||||
return await Inspection.find(filter);
|
||||
|
||||
const { data, error } = await query;
|
||||
|
||||
if (error && error.code !== '42P01') throw error;
|
||||
return data || [];
|
||||
};
|
||||
|
||||
export const getInspectionById = async (id: string) => {
|
||||
return await Inspection.findById(id);
|
||||
const { data, error } = await supabase
|
||||
.from('inspections')
|
||||
.select('*')
|
||||
.eq('id', id)
|
||||
.single();
|
||||
|
||||
if (error && error.code !== '42P01') throw error;
|
||||
return data;
|
||||
};
|
||||
|
||||
export const updateInspection = async (id: string, data: any) => {
|
||||
return await Inspection.findByIdAndUpdate(id, data);
|
||||
const { data: inspection, error } = await supabase
|
||||
.from('inspections')
|
||||
.update(data)
|
||||
.eq('id', id)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return inspection;
|
||||
};
|
||||
|
||||
export const deleteInspection = async (id: string) => {
|
||||
return await Inspection.findByIdAndDelete(id);
|
||||
const { error } = await supabase
|
||||
.from('inspections')
|
||||
.delete()
|
||||
.eq('id', id);
|
||||
|
||||
if (error) throw error;
|
||||
};
|
||||
|
||||
export const getInspectionsByOrganization = async (organizationId: string) => {
|
||||
return await Inspection.find({ organization_id: organizationId });
|
||||
const { data, error } = await supabase
|
||||
.from('inspections')
|
||||
.select('*')
|
||||
.eq('organization_id', organizationId);
|
||||
|
||||
if (error && error.code !== '42P01') throw error;
|
||||
return data || [];
|
||||
};
|
||||
|
||||
export const getInspectionStats = async (organizationId?: string) => {
|
||||
const filter = organizationId ? { organization_id: organizationId } : {};
|
||||
const inspections = await Inspection.find(filter);
|
||||
let query = supabase.from('inspections').select('*');
|
||||
|
||||
if (organizationId) {
|
||||
query = query.eq('organization_id', organizationId);
|
||||
}
|
||||
|
||||
const { data, error } = await query;
|
||||
|
||||
if (error && error.code !== '42P01') {
|
||||
return { total: 0, inspections: [] };
|
||||
}
|
||||
|
||||
return {
|
||||
total: inspections.length,
|
||||
inspections
|
||||
total: data?.length || 0,
|
||||
inspections: data || []
|
||||
};
|
||||
};
|
||||
|
||||
console.log('✅ InspectionService loaded with compatibility');
|
||||
console.log('✅ InspectionService loaded with Supabase');
|
||||
Reference in New Issue
Block a user