mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 17:51:13 -05:00
feat: Implement company management plan
This commit is contained in:
86
src/lib/companyHelpers.ts
Normal file
86
src/lib/companyHelpers.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
|
||||
export interface CompanyFormData {
|
||||
name: string;
|
||||
slug: string;
|
||||
description?: string;
|
||||
person_type: 'company' | 'individual' | 'firm' | 'organization';
|
||||
website_url?: string;
|
||||
founded_year?: number;
|
||||
headquarters_location?: string;
|
||||
}
|
||||
|
||||
export async function submitCompanyCreation(
|
||||
data: CompanyFormData,
|
||||
companyType: 'manufacturer' | 'designer' | 'operator' | 'property_owner',
|
||||
userId: string,
|
||||
isModerator: boolean
|
||||
) {
|
||||
if (isModerator) {
|
||||
// Moderators can create directly
|
||||
const { data: newCompany, error } = await supabase
|
||||
.from('companies')
|
||||
.insert({
|
||||
...data,
|
||||
company_type: companyType
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
return { company: newCompany, submitted: false };
|
||||
} else {
|
||||
// Regular users submit for moderation
|
||||
const { error } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'company_create',
|
||||
content: {
|
||||
...data,
|
||||
company_type: companyType
|
||||
},
|
||||
status: 'pending'
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
return { company: null, submitted: true };
|
||||
}
|
||||
}
|
||||
|
||||
export async function submitCompanyUpdate(
|
||||
companyId: string,
|
||||
data: CompanyFormData,
|
||||
userId: string,
|
||||
isModerator: boolean
|
||||
) {
|
||||
if (isModerator) {
|
||||
// Moderators can update directly
|
||||
const { error } = await supabase
|
||||
.from('companies')
|
||||
.update({
|
||||
...data,
|
||||
updated_at: new Date().toISOString()
|
||||
})
|
||||
.eq('id', companyId);
|
||||
|
||||
if (error) throw error;
|
||||
return { submitted: false };
|
||||
} else {
|
||||
// Regular users submit for moderation
|
||||
const { error } = await supabase
|
||||
.from('content_submissions')
|
||||
.insert({
|
||||
user_id: userId,
|
||||
submission_type: 'company_edit',
|
||||
content: {
|
||||
company_id: companyId,
|
||||
...data
|
||||
},
|
||||
status: 'pending'
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
return { submitted: true };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user