mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 13:11:12 -05:00
feat: Enhance notification payloads
This commit is contained in:
@@ -12,7 +12,77 @@ interface RequestBody {
|
||||
user_id: string;
|
||||
submission_type: string;
|
||||
status: 'approved' | 'rejected';
|
||||
rejection_reason?: string;
|
||||
reviewer_notes?: string;
|
||||
}
|
||||
|
||||
async function constructEntityURL(
|
||||
supabase: any,
|
||||
submissionType: string,
|
||||
itemData: any
|
||||
): Promise<string> {
|
||||
const baseURL = 'https://www.thrillwiki.com';
|
||||
|
||||
if (submissionType === 'park') {
|
||||
const parkSlug = itemData.slug;
|
||||
return `${baseURL}/parks/${parkSlug}`;
|
||||
}
|
||||
|
||||
if (submissionType === 'ride') {
|
||||
const rideSlug = itemData.slug;
|
||||
const parkId = itemData.park_id;
|
||||
|
||||
if (!parkId) {
|
||||
return `${baseURL}/rides/${rideSlug}`;
|
||||
}
|
||||
|
||||
// Fetch park slug
|
||||
const { data: park } = await supabase
|
||||
.from('parks')
|
||||
.select('slug')
|
||||
.eq('id', parkId)
|
||||
.maybeSingle();
|
||||
|
||||
const parkSlug = park?.slug || 'unknown';
|
||||
return `${baseURL}/parks/${parkSlug}/rides/${rideSlug}`;
|
||||
}
|
||||
|
||||
if (submissionType === 'company') {
|
||||
const companySlug = itemData.slug;
|
||||
const companyType = itemData.company_type;
|
||||
|
||||
if (companyType === 'manufacturer') {
|
||||
return `${baseURL}/manufacturers/${companySlug}`;
|
||||
} else if (companyType === 'operator') {
|
||||
return `${baseURL}/operators/${companySlug}`;
|
||||
} else if (companyType === 'property_owner') {
|
||||
return `${baseURL}/owners/${companySlug}`;
|
||||
} else if (companyType === 'designer') {
|
||||
return `${baseURL}/designers/${companySlug}`;
|
||||
}
|
||||
return `${baseURL}/companies/${companySlug}`;
|
||||
}
|
||||
|
||||
if (submissionType === 'ride_model') {
|
||||
const modelSlug = itemData.slug;
|
||||
const manufacturerId = itemData.manufacturer_id;
|
||||
|
||||
if (!manufacturerId) {
|
||||
return `${baseURL}/models/${modelSlug}`;
|
||||
}
|
||||
|
||||
// Fetch manufacturer slug
|
||||
const { data: manufacturer } = await supabase
|
||||
.from('companies')
|
||||
.select('slug')
|
||||
.eq('id', manufacturerId)
|
||||
.eq('company_type', 'manufacturer')
|
||||
.maybeSingle();
|
||||
|
||||
const manufacturerSlug = manufacturer?.slug || 'unknown';
|
||||
return `${baseURL}/manufacturers/${manufacturerSlug}/models/${modelSlug}`;
|
||||
}
|
||||
|
||||
return `${baseURL}`;
|
||||
}
|
||||
|
||||
serve(async (req) => {
|
||||
@@ -28,9 +98,9 @@ serve(async (req) => {
|
||||
|
||||
const supabase = createClient(supabaseUrl, supabaseServiceKey);
|
||||
|
||||
const { submission_id, user_id, submission_type, status, rejection_reason } = await req.json() as RequestBody;
|
||||
const { submission_id, user_id, submission_type, status, reviewer_notes } = await req.json() as RequestBody;
|
||||
|
||||
// Fetch submission items to get entity name
|
||||
// Fetch submission items to get entity data
|
||||
const { data: items, error: itemsError } = await supabase
|
||||
.from('submission_items')
|
||||
.select('item_data')
|
||||
@@ -43,21 +113,40 @@ serve(async (req) => {
|
||||
throw new Error(`Failed to fetch submission items: ${itemsError.message}`);
|
||||
}
|
||||
|
||||
// Extract entity name from item_data
|
||||
const entityName = items?.item_data?.name || 'your submission';
|
||||
const entityType = submission_type.replace('_', ' ');
|
||||
if (!items || !items.item_data) {
|
||||
throw new Error('No submission items found');
|
||||
}
|
||||
|
||||
// Determine workflow and build payload
|
||||
// Extract entity data
|
||||
const entityName = items.item_data.name || 'your submission';
|
||||
const entityType = submission_type.replace('_', ' ');
|
||||
|
||||
// Construct entity URL
|
||||
const entityURL = await constructEntityURL(supabase, submission_type, items.item_data);
|
||||
|
||||
// Determine workflow and build payload based on status
|
||||
const workflowId = status === 'approved' ? 'submission-approved' : 'submission-rejected';
|
||||
|
||||
const payload: Record<string, string> = {
|
||||
entityName,
|
||||
entityType,
|
||||
submissionId: submission_id,
|
||||
};
|
||||
|
||||
if (status === 'rejected' && rejection_reason) {
|
||||
payload.rejectionReason = rejection_reason;
|
||||
let payload: Record<string, string>;
|
||||
|
||||
if (status === 'approved') {
|
||||
// Approval payload
|
||||
payload = {
|
||||
entityType,
|
||||
entityName,
|
||||
submissionId: submission_id,
|
||||
entityURL,
|
||||
moderationNotes: reviewer_notes || '',
|
||||
};
|
||||
} else {
|
||||
// Rejection payload
|
||||
payload = {
|
||||
rejectionReason: reviewer_notes || 'No reason provided',
|
||||
entityType,
|
||||
entityName,
|
||||
entityURL,
|
||||
actualStatus: 'rejected',
|
||||
};
|
||||
}
|
||||
|
||||
console.log('Sending notification to user:', {
|
||||
|
||||
Reference in New Issue
Block a user