fix: painting schemes and datasheets endpoints
This commit is contained in:
@@ -1,41 +1,64 @@
|
||||
import { PaintingScheme } from '../lib/compat.js';
|
||||
import { supabase } from '../config/supabase.js';
|
||||
|
||||
export const createPaintingScheme = async (data: any & { organizationId?: string }) => {
|
||||
return await PaintingScheme.create({ ...data, organization_id: data.organizationId });
|
||||
const { data: scheme, error } = await supabase
|
||||
.from('painting_schemes')
|
||||
.insert({ ...data, organization_id: data.organizationId })
|
||||
.select()
|
||||
.single();
|
||||
if (error) throw error;
|
||||
return scheme;
|
||||
};
|
||||
|
||||
export const getPaintingSchemesByProject = async (projectId: string, organizationId?: string) => {
|
||||
const filter: any = { project_id: projectId };
|
||||
if (organizationId) {
|
||||
filter.organization_id = organizationId;
|
||||
}
|
||||
return await PaintingScheme.find(filter);
|
||||
let query = supabase.from('painting_schemes').select('*').eq('project_id', projectId);
|
||||
const { data, error } = await query;
|
||||
if (error && error.code !== '42P01') throw error;
|
||||
return data || [];
|
||||
};
|
||||
|
||||
export const getPaintingSchemeById = async (id: string) => {
|
||||
return await PaintingScheme.findById(id);
|
||||
const { data, error } = await supabase.from('painting_schemes').select('*').eq('id', id).single();
|
||||
if (error && error.code !== '42P01') throw error;
|
||||
return data;
|
||||
};
|
||||
|
||||
export const updatePaintingScheme = async (id: string, data: any, organizationId?: string) => {
|
||||
const existing = await PaintingScheme.findById(id);
|
||||
if (!existing) return null;
|
||||
|
||||
if (organizationId && existing.organization_id && existing.organization_id !== organizationId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return await PaintingScheme.findByIdAndUpdate(id, data);
|
||||
const { data: scheme, error } = await supabase
|
||||
.from('painting_schemes')
|
||||
.update(data)
|
||||
.eq('id', id)
|
||||
.select()
|
||||
.single();
|
||||
if (error) throw error;
|
||||
return scheme;
|
||||
};
|
||||
|
||||
export const deletePaintingScheme = async (id: string) => {
|
||||
return await PaintingScheme.findByIdAndDelete(id);
|
||||
const { error } = await supabase.from('painting_schemes').delete().eq('id', id);
|
||||
if (error) throw error;
|
||||
};
|
||||
|
||||
export const clonePaintingScheme = async (id: string, newData: any) => {
|
||||
const original = await PaintingScheme.findById(id);
|
||||
const original = await getPaintingSchemeById(id);
|
||||
if (!original) return null;
|
||||
|
||||
return await PaintingScheme.create({ ...original, ...newData, id: undefined });
|
||||
const { data: scheme, error } = await supabase
|
||||
.from('painting_schemes')
|
||||
.insert({ ...original, ...newData, id: undefined })
|
||||
.select()
|
||||
.single();
|
||||
if (error) throw error;
|
||||
return scheme;
|
||||
};
|
||||
|
||||
console.log('✅ PaintingSchemeService loaded with compatibility');
|
||||
export const getAllSchemes = async (organizationId?: string) => {
|
||||
let query = supabase.from('painting_schemes').select('*');
|
||||
if (organizationId) {
|
||||
query = query.eq('organization_id', organizationId);
|
||||
}
|
||||
const { data, error } = await query;
|
||||
if (error && error.code !== '42P01') throw error;
|
||||
return data || [];
|
||||
};
|
||||
|
||||
console.log('✅ PaintingSchemeService loaded with Supabase');
|
||||
Reference in New Issue
Block a user