mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 18:31:13 -05:00
Fix: Implement company submission and validation plan
This commit is contained in:
@@ -117,6 +117,23 @@ export interface RideFormData {
|
||||
card_image_id?: string;
|
||||
}
|
||||
|
||||
export interface CompanyFormData {
|
||||
name: string;
|
||||
slug: string;
|
||||
description?: string;
|
||||
person_type: 'company' | 'individual' | 'firm' | 'organization';
|
||||
founded_year?: number;
|
||||
founded_date?: string;
|
||||
founded_date_precision?: string;
|
||||
headquarters_location?: string;
|
||||
website_url?: string;
|
||||
images?: ImageAssignments;
|
||||
banner_image_url?: string;
|
||||
banner_image_id?: string;
|
||||
card_image_url?: string;
|
||||
card_image_id?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* ⚠️ CRITICAL SECURITY PATTERN ⚠️
|
||||
*
|
||||
@@ -438,3 +455,432 @@ export async function submitRideUpdate(
|
||||
|
||||
return { submitted: true, submissionId: submissionData.id };
|
||||
}
|
||||
|
||||
/**
|
||||
* ⚠️ CRITICAL SECURITY PATTERN ⚠️
|
||||
*
|
||||
* Submits a new manufacturer for creation through the moderation queue.
|
||||
*/
|
||||
export async function submitManufacturerCreation(
|
||||
data: CompanyFormData,
|
||||
userId: string
|
||||
): Promise<{ submitted: boolean; submissionId: string }> {
|
||||
let processedImages = data.images;
|
||||
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
||||
try {
|
||||
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
||||
processedImages = { ...data.images, uploaded: uploadedImages };
|
||||
} catch (error) {
|
||||
console.error('Failed to upload images:', error);
|
||||
throw new Error('Failed to upload images. Please check your connection and try again.');
|
||||
}
|
||||
}
|
||||
|
||||
const { data: submissionData, error: submissionError } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'manufacturer',
|
||||
content: { action: 'create' },
|
||||
status: 'pending'
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (submissionError) throw submissionError;
|
||||
|
||||
const { error: itemError } = await supabase
|
||||
.from('submission_items')
|
||||
.insert({
|
||||
submission_id: submissionData.id,
|
||||
item_type: 'manufacturer',
|
||||
item_data: {
|
||||
...data,
|
||||
company_type: 'manufacturer',
|
||||
images: processedImages as unknown as Json
|
||||
},
|
||||
status: 'pending',
|
||||
order_index: 0
|
||||
});
|
||||
|
||||
if (itemError) throw itemError;
|
||||
|
||||
return { submitted: true, submissionId: submissionData.id };
|
||||
}
|
||||
|
||||
export async function submitManufacturerUpdate(
|
||||
companyId: string,
|
||||
data: CompanyFormData,
|
||||
userId: string
|
||||
): Promise<{ submitted: boolean; submissionId: string }> {
|
||||
const { data: existingCompany, error: fetchError } = await supabase
|
||||
.from('companies')
|
||||
.select('*')
|
||||
.eq('id', companyId)
|
||||
.single();
|
||||
|
||||
if (fetchError) throw new Error(`Failed to fetch manufacturer: ${fetchError.message}`);
|
||||
if (!existingCompany) throw new Error('Manufacturer not found');
|
||||
|
||||
let processedImages = data.images;
|
||||
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
||||
try {
|
||||
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
||||
processedImages = { ...data.images, uploaded: uploadedImages };
|
||||
} catch (error) {
|
||||
console.error('Failed to upload images:', error);
|
||||
throw new Error('Failed to upload images. Please check your connection and try again.');
|
||||
}
|
||||
}
|
||||
|
||||
const { data: submissionData, error: submissionError } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'manufacturer',
|
||||
content: { action: 'edit', company_id: companyId },
|
||||
status: 'pending'
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (submissionError) throw submissionError;
|
||||
|
||||
const { error: itemError } = await supabase
|
||||
.from('submission_items')
|
||||
.insert({
|
||||
submission_id: submissionData.id,
|
||||
item_type: 'manufacturer',
|
||||
item_data: {
|
||||
...data,
|
||||
company_id: companyId,
|
||||
company_type: 'manufacturer',
|
||||
images: processedImages as any
|
||||
},
|
||||
original_data: JSON.parse(JSON.stringify(existingCompany)),
|
||||
status: 'pending',
|
||||
order_index: 0
|
||||
});
|
||||
|
||||
if (itemError) throw itemError;
|
||||
|
||||
return { submitted: true, submissionId: submissionData.id };
|
||||
}
|
||||
|
||||
export async function submitDesignerCreation(
|
||||
data: CompanyFormData,
|
||||
userId: string
|
||||
): Promise<{ submitted: boolean; submissionId: string }> {
|
||||
let processedImages = data.images;
|
||||
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
||||
try {
|
||||
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
||||
processedImages = { ...data.images, uploaded: uploadedImages };
|
||||
} catch (error) {
|
||||
console.error('Failed to upload images:', error);
|
||||
throw new Error('Failed to upload images. Please check your connection and try again.');
|
||||
}
|
||||
}
|
||||
|
||||
const { data: submissionData, error: submissionError } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'designer',
|
||||
content: { action: 'create' },
|
||||
status: 'pending'
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (submissionError) throw submissionError;
|
||||
|
||||
const { error: itemError } = await supabase
|
||||
.from('submission_items')
|
||||
.insert({
|
||||
submission_id: submissionData.id,
|
||||
item_type: 'designer',
|
||||
item_data: {
|
||||
...data,
|
||||
company_type: 'designer',
|
||||
images: processedImages as unknown as Json
|
||||
},
|
||||
status: 'pending',
|
||||
order_index: 0
|
||||
});
|
||||
|
||||
if (itemError) throw itemError;
|
||||
|
||||
return { submitted: true, submissionId: submissionData.id };
|
||||
}
|
||||
|
||||
export async function submitDesignerUpdate(
|
||||
companyId: string,
|
||||
data: CompanyFormData,
|
||||
userId: string
|
||||
): Promise<{ submitted: boolean; submissionId: string }> {
|
||||
const { data: existingCompany, error: fetchError } = await supabase
|
||||
.from('companies')
|
||||
.select('*')
|
||||
.eq('id', companyId)
|
||||
.single();
|
||||
|
||||
if (fetchError) throw new Error(`Failed to fetch designer: ${fetchError.message}`);
|
||||
if (!existingCompany) throw new Error('Designer not found');
|
||||
|
||||
let processedImages = data.images;
|
||||
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
||||
try {
|
||||
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
||||
processedImages = { ...data.images, uploaded: uploadedImages };
|
||||
} catch (error) {
|
||||
console.error('Failed to upload images:', error);
|
||||
throw new Error('Failed to upload images. Please check your connection and try again.');
|
||||
}
|
||||
}
|
||||
|
||||
const { data: submissionData, error: submissionError } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'designer',
|
||||
content: { action: 'edit', company_id: companyId },
|
||||
status: 'pending'
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (submissionError) throw submissionError;
|
||||
|
||||
const { error: itemError } = await supabase
|
||||
.from('submission_items')
|
||||
.insert({
|
||||
submission_id: submissionData.id,
|
||||
item_type: 'designer',
|
||||
item_data: {
|
||||
...data,
|
||||
company_id: companyId,
|
||||
company_type: 'designer',
|
||||
images: processedImages as any
|
||||
},
|
||||
original_data: JSON.parse(JSON.stringify(existingCompany)),
|
||||
status: 'pending',
|
||||
order_index: 0
|
||||
});
|
||||
|
||||
if (itemError) throw itemError;
|
||||
|
||||
return { submitted: true, submissionId: submissionData.id };
|
||||
}
|
||||
|
||||
export async function submitOperatorCreation(
|
||||
data: CompanyFormData,
|
||||
userId: string
|
||||
): Promise<{ submitted: boolean; submissionId: string }> {
|
||||
let processedImages = data.images;
|
||||
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
||||
try {
|
||||
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
||||
processedImages = { ...data.images, uploaded: uploadedImages };
|
||||
} catch (error) {
|
||||
console.error('Failed to upload images:', error);
|
||||
throw new Error('Failed to upload images. Please check your connection and try again.');
|
||||
}
|
||||
}
|
||||
|
||||
const { data: submissionData, error: submissionError } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'operator',
|
||||
content: { action: 'create' },
|
||||
status: 'pending'
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (submissionError) throw submissionError;
|
||||
|
||||
const { error: itemError } = await supabase
|
||||
.from('submission_items')
|
||||
.insert({
|
||||
submission_id: submissionData.id,
|
||||
item_type: 'operator',
|
||||
item_data: {
|
||||
...data,
|
||||
company_type: 'operator',
|
||||
images: processedImages as unknown as Json
|
||||
},
|
||||
status: 'pending',
|
||||
order_index: 0
|
||||
});
|
||||
|
||||
if (itemError) throw itemError;
|
||||
|
||||
return { submitted: true, submissionId: submissionData.id };
|
||||
}
|
||||
|
||||
export async function submitOperatorUpdate(
|
||||
companyId: string,
|
||||
data: CompanyFormData,
|
||||
userId: string
|
||||
): Promise<{ submitted: boolean; submissionId: string }> {
|
||||
const { data: existingCompany, error: fetchError } = await supabase
|
||||
.from('companies')
|
||||
.select('*')
|
||||
.eq('id', companyId)
|
||||
.single();
|
||||
|
||||
if (fetchError) throw new Error(`Failed to fetch operator: ${fetchError.message}`);
|
||||
if (!existingCompany) throw new Error('Operator not found');
|
||||
|
||||
let processedImages = data.images;
|
||||
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
||||
try {
|
||||
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
||||
processedImages = { ...data.images, uploaded: uploadedImages };
|
||||
} catch (error) {
|
||||
console.error('Failed to upload images:', error);
|
||||
throw new Error('Failed to upload images. Please check your connection and try again.');
|
||||
}
|
||||
}
|
||||
|
||||
const { data: submissionData, error: submissionError } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'operator',
|
||||
content: { action: 'edit', company_id: companyId },
|
||||
status: 'pending'
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (submissionError) throw submissionError;
|
||||
|
||||
const { error: itemError } = await supabase
|
||||
.from('submission_items')
|
||||
.insert({
|
||||
submission_id: submissionData.id,
|
||||
item_type: 'operator',
|
||||
item_data: {
|
||||
...data,
|
||||
company_id: companyId,
|
||||
company_type: 'operator',
|
||||
images: processedImages as any
|
||||
},
|
||||
original_data: JSON.parse(JSON.stringify(existingCompany)),
|
||||
status: 'pending',
|
||||
order_index: 0
|
||||
});
|
||||
|
||||
if (itemError) throw itemError;
|
||||
|
||||
return { submitted: true, submissionId: submissionData.id };
|
||||
}
|
||||
|
||||
export async function submitPropertyOwnerCreation(
|
||||
data: CompanyFormData,
|
||||
userId: string
|
||||
): Promise<{ submitted: boolean; submissionId: string }> {
|
||||
let processedImages = data.images;
|
||||
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
||||
try {
|
||||
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
||||
processedImages = { ...data.images, uploaded: uploadedImages };
|
||||
} catch (error) {
|
||||
console.error('Failed to upload images:', error);
|
||||
throw new Error('Failed to upload images. Please check your connection and try again.');
|
||||
}
|
||||
}
|
||||
|
||||
const { data: submissionData, error: submissionError } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'property_owner',
|
||||
content: { action: 'create' },
|
||||
status: 'pending'
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (submissionError) throw submissionError;
|
||||
|
||||
const { error: itemError } = await supabase
|
||||
.from('submission_items')
|
||||
.insert({
|
||||
submission_id: submissionData.id,
|
||||
item_type: 'property_owner',
|
||||
item_data: {
|
||||
...data,
|
||||
company_type: 'property_owner',
|
||||
images: processedImages as unknown as Json
|
||||
},
|
||||
status: 'pending',
|
||||
order_index: 0
|
||||
});
|
||||
|
||||
if (itemError) throw itemError;
|
||||
|
||||
return { submitted: true, submissionId: submissionData.id };
|
||||
}
|
||||
|
||||
export async function submitPropertyOwnerUpdate(
|
||||
companyId: string,
|
||||
data: CompanyFormData,
|
||||
userId: string
|
||||
): Promise<{ submitted: boolean; submissionId: string }> {
|
||||
const { data: existingCompany, error: fetchError } = await supabase
|
||||
.from('companies')
|
||||
.select('*')
|
||||
.eq('id', companyId)
|
||||
.single();
|
||||
|
||||
if (fetchError) throw new Error(`Failed to fetch property owner: ${fetchError.message}`);
|
||||
if (!existingCompany) throw new Error('Property owner not found');
|
||||
|
||||
let processedImages = data.images;
|
||||
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
||||
try {
|
||||
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
||||
processedImages = { ...data.images, uploaded: uploadedImages };
|
||||
} catch (error) {
|
||||
console.error('Failed to upload images:', error);
|
||||
throw new Error('Failed to upload images. Please check your connection and try again.');
|
||||
}
|
||||
}
|
||||
|
||||
const { data: submissionData, error: submissionError } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'property_owner',
|
||||
content: { action: 'edit', company_id: companyId },
|
||||
status: 'pending'
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (submissionError) throw submissionError;
|
||||
|
||||
const { error: itemError } = await supabase
|
||||
.from('submission_items')
|
||||
.insert({
|
||||
submission_id: submissionData.id,
|
||||
item_type: 'property_owner',
|
||||
item_data: {
|
||||
...data,
|
||||
company_id: companyId,
|
||||
company_type: 'property_owner',
|
||||
images: processedImages as any
|
||||
},
|
||||
original_data: JSON.parse(JSON.stringify(existingCompany)),
|
||||
status: 'pending',
|
||||
order_index: 0
|
||||
});
|
||||
|
||||
if (itemError) throw itemError;
|
||||
|
||||
return { submitted: true, submissionId: submissionData.id };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user