mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 13:11:12 -05:00
Implement rejection workflow
This commit is contained in:
@@ -281,6 +281,113 @@ async function approvePhotos(data: any): Promise<string> {
|
||||
return data.photos?.[0]?.url || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Reject multiple items with optional cascade to dependents
|
||||
*/
|
||||
export async function rejectSubmissionItems(
|
||||
items: SubmissionItemWithDeps[],
|
||||
reason: string,
|
||||
userId: string,
|
||||
cascade: boolean = true
|
||||
): Promise<void> {
|
||||
if (!userId) {
|
||||
throw new Error('User authentication required to reject items');
|
||||
}
|
||||
|
||||
if (!reason || !reason.trim()) {
|
||||
throw new Error('Rejection reason is required');
|
||||
}
|
||||
|
||||
const itemsToReject = new Set<string>(items.map(i => i.id));
|
||||
|
||||
// If cascading, collect all dependent items
|
||||
if (cascade) {
|
||||
for (const item of items) {
|
||||
await collectDependents(item, itemsToReject);
|
||||
}
|
||||
}
|
||||
|
||||
// Update all items to rejected status
|
||||
const updates = Array.from(itemsToReject).map(async (itemId) => {
|
||||
const { error } = await supabase
|
||||
.from('submission_items')
|
||||
.update({
|
||||
status: 'rejected',
|
||||
rejection_reason: reason,
|
||||
updated_at: new Date().toISOString(),
|
||||
})
|
||||
.eq('id', itemId);
|
||||
|
||||
if (error) {
|
||||
console.error(`Error rejecting item ${itemId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(updates);
|
||||
|
||||
// Update parent submission status
|
||||
const submissionId = items[0]?.submission_id;
|
||||
if (submissionId) {
|
||||
await updateSubmissionStatusAfterRejection(submissionId);
|
||||
}
|
||||
}
|
||||
|
||||
async function collectDependents(
|
||||
item: SubmissionItemWithDeps,
|
||||
rejectedSet: Set<string>
|
||||
): Promise<void> {
|
||||
if (item.dependents && item.dependents.length > 0) {
|
||||
for (const dependent of item.dependents) {
|
||||
rejectedSet.add(dependent.id);
|
||||
await collectDependents(dependent, rejectedSet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function updateSubmissionStatusAfterRejection(submissionId: string): Promise<void> {
|
||||
// Get all items for this submission
|
||||
const { data: allItems, error: fetchError } = await supabase
|
||||
.from('submission_items')
|
||||
.select('status')
|
||||
.eq('submission_id', submissionId);
|
||||
|
||||
if (fetchError) {
|
||||
console.error('Error fetching submission items:', fetchError);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!allItems || allItems.length === 0) return;
|
||||
|
||||
const statuses = allItems.map(i => i.status);
|
||||
const allRejected = statuses.every(s => s === 'rejected');
|
||||
const allApproved = statuses.every(s => s === 'approved');
|
||||
const anyPending = statuses.some(s => s === 'pending');
|
||||
|
||||
let newStatus: string;
|
||||
if (allRejected) {
|
||||
newStatus = 'rejected';
|
||||
} else if (allApproved) {
|
||||
newStatus = 'approved';
|
||||
} else if (anyPending) {
|
||||
newStatus = 'pending';
|
||||
} else {
|
||||
newStatus = 'partially_approved';
|
||||
}
|
||||
|
||||
const { error: updateError } = await supabase
|
||||
.from('content_submissions')
|
||||
.update({
|
||||
status: newStatus,
|
||||
updated_at: new Date().toISOString(),
|
||||
})
|
||||
.eq('id', submissionId);
|
||||
|
||||
if (updateError) {
|
||||
console.error('Error updating submission status:', updateError);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Escalate submission for admin review
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user