mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 06:31:13 -05:00
Add action_type to submission items
This commit is contained in:
@@ -2399,6 +2399,7 @@ export type Database = {
|
|||||||
}
|
}
|
||||||
submission_items: {
|
submission_items: {
|
||||||
Row: {
|
Row: {
|
||||||
|
action_type: string | null
|
||||||
approved_entity_id: string | null
|
approved_entity_id: string | null
|
||||||
created_at: string
|
created_at: string
|
||||||
depends_on: string | null
|
depends_on: string | null
|
||||||
@@ -2413,6 +2414,7 @@ export type Database = {
|
|||||||
updated_at: string
|
updated_at: string
|
||||||
}
|
}
|
||||||
Insert: {
|
Insert: {
|
||||||
|
action_type?: string | null
|
||||||
approved_entity_id?: string | null
|
approved_entity_id?: string | null
|
||||||
created_at?: string
|
created_at?: string
|
||||||
depends_on?: string | null
|
depends_on?: string | null
|
||||||
@@ -2427,6 +2429,7 @@ export type Database = {
|
|||||||
updated_at?: string
|
updated_at?: string
|
||||||
}
|
}
|
||||||
Update: {
|
Update: {
|
||||||
|
action_type?: string | null
|
||||||
approved_entity_id?: string | null
|
approved_entity_id?: string | null
|
||||||
created_at?: string
|
created_at?: string
|
||||||
depends_on?: string | null
|
depends_on?: string | null
|
||||||
|
|||||||
@@ -133,17 +133,22 @@ async function detectPhotoChanges(submissionId: string): Promise<PhotoChange[]>
|
|||||||
* Detects what changed between original_data and item_data
|
* Detects what changed between original_data and item_data
|
||||||
*/
|
*/
|
||||||
export async function detectChanges(
|
export async function detectChanges(
|
||||||
item: { item_data?: any; original_data?: any; item_type: string },
|
item: { item_data?: any; original_data?: any; item_type: string; action_type?: string },
|
||||||
submissionId?: string
|
submissionId?: string
|
||||||
): Promise<ChangesSummary> {
|
): Promise<ChangesSummary> {
|
||||||
const itemData = item.item_data || {};
|
const itemData = item.item_data || {};
|
||||||
const originalData = item.original_data || {};
|
const originalData = item.original_data || {};
|
||||||
|
|
||||||
// Determine action type - special handling for photo_delete
|
// Determine action type - prioritize explicit action_type field to preserve submission intent
|
||||||
let action: 'create' | 'edit' | 'delete' = 'edit';
|
let action: 'create' | 'edit' | 'delete' = 'edit';
|
||||||
|
|
||||||
if (item.item_type === 'photo_delete' || itemData.action === 'delete' || itemData.deleted) {
|
if (item.item_type === 'photo_delete' || itemData.action === 'delete' || itemData.deleted) {
|
||||||
action = 'delete';
|
action = 'delete';
|
||||||
|
} else if (item.action_type) {
|
||||||
|
// Use explicit action_type if set (preserves original submission intent even after moderator edits)
|
||||||
|
action = item.action_type as 'create' | 'edit' | 'delete';
|
||||||
} else if (!originalData || Object.keys(originalData).length === 0) {
|
} else if (!originalData || Object.keys(originalData).length === 0) {
|
||||||
|
// Fall back to inference for backwards compatibility
|
||||||
action = 'create';
|
action = 'create';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ export interface SubmissionItemWithDeps {
|
|||||||
item_type: string;
|
item_type: string;
|
||||||
item_data: any;
|
item_data: any;
|
||||||
original_data: any;
|
original_data: any;
|
||||||
|
action_type?: 'create' | 'edit' | 'delete';
|
||||||
status: 'pending' | 'approved' | 'rejected';
|
status: 'pending' | 'approved' | 'rejected';
|
||||||
depends_on: string | null;
|
depends_on: string | null;
|
||||||
order_index: number;
|
order_index: number;
|
||||||
@@ -1050,6 +1051,10 @@ export async function editSubmissionItem(
|
|||||||
// Preserve original_data if not already set
|
// Preserve original_data if not already set
|
||||||
const originalData = currentItem.original_data || currentItem.item_data;
|
const originalData = currentItem.original_data || currentItem.item_data;
|
||||||
|
|
||||||
|
// Determine original action type - preserve submission intent
|
||||||
|
const originalAction = currentItem.action_type ||
|
||||||
|
(currentItem.original_data && Object.keys(currentItem.original_data).length > 0) ? 'edit' : 'create';
|
||||||
|
|
||||||
if (isModerator) {
|
if (isModerator) {
|
||||||
// Moderators can edit directly
|
// Moderators can edit directly
|
||||||
const { error: updateError } = await supabase
|
const { error: updateError } = await supabase
|
||||||
@@ -1057,6 +1062,7 @@ export async function editSubmissionItem(
|
|||||||
.update({
|
.update({
|
||||||
item_data: newData,
|
item_data: newData,
|
||||||
original_data: originalData,
|
original_data: originalData,
|
||||||
|
action_type: originalAction, // Preserve original submission intent
|
||||||
updated_at: new Date().toISOString(),
|
updated_at: new Date().toISOString(),
|
||||||
})
|
})
|
||||||
.eq('id', itemId);
|
.eq('id', itemId);
|
||||||
@@ -1083,6 +1089,7 @@ export async function editSubmissionItem(
|
|||||||
.update({
|
.update({
|
||||||
item_data: newData,
|
item_data: newData,
|
||||||
original_data: originalData,
|
original_data: originalData,
|
||||||
|
action_type: originalAction, // Preserve original submission intent
|
||||||
updated_at: new Date().toISOString(),
|
updated_at: new Date().toISOString(),
|
||||||
})
|
})
|
||||||
.eq('id', itemId);
|
.eq('id', itemId);
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
-- Add action_type column to submission_items to preserve original submission intent
|
||||||
|
ALTER TABLE submission_items
|
||||||
|
ADD COLUMN IF NOT EXISTS action_type text DEFAULT 'create';
|
||||||
|
|
||||||
|
-- Add check constraint to ensure valid values
|
||||||
|
ALTER TABLE submission_items
|
||||||
|
ADD CONSTRAINT submission_items_action_type_check
|
||||||
|
CHECK (action_type IN ('create', 'edit', 'delete'));
|
||||||
|
|
||||||
|
-- Create index for better query performance
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_submission_items_action_type
|
||||||
|
ON submission_items(action_type);
|
||||||
|
|
||||||
|
-- Backfill existing records based on original_data presence
|
||||||
|
UPDATE submission_items
|
||||||
|
SET action_type = CASE
|
||||||
|
WHEN item_type = 'photo_delete' THEN 'delete'
|
||||||
|
WHEN original_data IS NULL OR original_data::text = '{}' THEN 'create'
|
||||||
|
ELSE 'edit'
|
||||||
|
END
|
||||||
|
WHERE action_type IS NULL;
|
||||||
Reference in New Issue
Block a user