mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 05:31:13 -05:00
Refactor submission item handling
This commit is contained in:
@@ -76,9 +76,18 @@ export const EntityEditPreview = ({ submissionId, entityType, entityName }: Enti
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
// Fetch items with relational data
|
||||
const { data: items, error } = await supabase
|
||||
.from('submission_items')
|
||||
.select('*')
|
||||
.select(`
|
||||
*,
|
||||
park_submission:park_submissions!item_data_id(*),
|
||||
ride_submission:ride_submissions!item_data_id(*),
|
||||
photo_submission:photo_submissions!item_data_id(
|
||||
*,
|
||||
photo_items:photo_submission_items(*)
|
||||
)
|
||||
`)
|
||||
.eq('submission_id', submissionId)
|
||||
.order('order_index', { ascending: true });
|
||||
|
||||
@@ -86,8 +95,28 @@ export const EntityEditPreview = ({ submissionId, entityType, entityName }: Enti
|
||||
|
||||
if (items && items.length > 0) {
|
||||
const firstItem = items[0];
|
||||
setItemData(firstItem.item_data as Record<string, unknown>);
|
||||
setOriginalData(firstItem.original_data as Record<string, unknown> | null);
|
||||
|
||||
// Transform relational data to item_data format
|
||||
let itemDataObj: Record<string, unknown> = {};
|
||||
switch (firstItem.item_type) {
|
||||
case 'park':
|
||||
itemDataObj = (firstItem as any).park_submission || {};
|
||||
break;
|
||||
case 'ride':
|
||||
itemDataObj = (firstItem as any).ride_submission || {};
|
||||
break;
|
||||
case 'photo':
|
||||
itemDataObj = {
|
||||
...(firstItem as any).photo_submission,
|
||||
photos: (firstItem as any).photo_submission?.photo_items || []
|
||||
};
|
||||
break;
|
||||
default:
|
||||
itemDataObj = {};
|
||||
}
|
||||
|
||||
setItemData(itemDataObj);
|
||||
setOriginalData(null); // Original data not used in new relational model
|
||||
|
||||
// Check for photo edit/delete operations
|
||||
if (firstItem.item_type === 'photo_edit' || firstItem.item_type === 'photo_delete') {
|
||||
@@ -144,16 +173,13 @@ export const EntityEditPreview = ({ submissionId, entityType, entityName }: Enti
|
||||
}
|
||||
|
||||
// Check for other field changes by comparing with original_data
|
||||
if (firstItem.original_data) {
|
||||
const originalData = firstItem.original_data as Record<string, unknown>;
|
||||
const excludeFields = ['images', 'updated_at', 'created_at'];
|
||||
Object.keys(data).forEach(key => {
|
||||
if (!excludeFields.includes(key)) {
|
||||
// Use deep equality check for objects and arrays
|
||||
const isEqual = deepEqual(data[key], originalData[key]);
|
||||
if (!isEqual) {
|
||||
changed.push(key);
|
||||
}
|
||||
// Note: In new relational model, we don't track original_data at item level
|
||||
// Field changes are determined by comparing current vs approved entity data
|
||||
if (itemDataObj) {
|
||||
const excludeFields = ['images', 'updated_at', 'created_at', 'id'];
|
||||
Object.keys(itemDataObj).forEach(key => {
|
||||
if (!excludeFields.includes(key) && itemDataObj[key] !== null && itemDataObj[key] !== undefined) {
|
||||
changed.push(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4146,11 +4146,9 @@ export type Database = {
|
||||
depends_on: string | null
|
||||
id: string
|
||||
is_test_data: boolean | null
|
||||
item_data: Json
|
||||
item_data_id: string | null
|
||||
item_type: string
|
||||
order_index: number | null
|
||||
original_data: Json | null
|
||||
rejection_reason: string | null
|
||||
status: string
|
||||
submission_id: string
|
||||
@@ -4163,11 +4161,9 @@ export type Database = {
|
||||
depends_on?: string | null
|
||||
id?: string
|
||||
is_test_data?: boolean | null
|
||||
item_data: Json
|
||||
item_data_id?: string | null
|
||||
item_type: string
|
||||
order_index?: number | null
|
||||
original_data?: Json | null
|
||||
rejection_reason?: string | null
|
||||
status?: string
|
||||
submission_id: string
|
||||
@@ -4180,11 +4176,9 @@ export type Database = {
|
||||
depends_on?: string | null
|
||||
id?: string
|
||||
is_test_data?: boolean | null
|
||||
item_data?: Json
|
||||
item_data_id?: string | null
|
||||
item_type?: string
|
||||
order_index?: number | null
|
||||
original_data?: Json | null
|
||||
rejection_reason?: string | null
|
||||
status?: string
|
||||
submission_id?: string
|
||||
|
||||
@@ -359,16 +359,35 @@ export async function fetchSystemActivities(
|
||||
const submissionIds = submissions.map(s => s.id);
|
||||
const { data: submissionItems } = await supabase
|
||||
.from('submission_items')
|
||||
.select('submission_id, item_type, item_data')
|
||||
.select(`
|
||||
submission_id,
|
||||
item_type,
|
||||
photo_submission:photo_submissions!item_data_id(
|
||||
*,
|
||||
photo_items:photo_submission_items(*)
|
||||
)
|
||||
`)
|
||||
.in('submission_id', submissionIds)
|
||||
.in('item_type', ['photo', 'photo_delete', 'photo_edit']);
|
||||
|
||||
const itemsMap = new Map(submissionItems?.map(item => [item.submission_id, item]) || []);
|
||||
const itemsMap = new Map(
|
||||
submissionItems?.map(item => {
|
||||
// Transform photo data
|
||||
const itemData = item.item_type === 'photo'
|
||||
? {
|
||||
...(item as any).photo_submission,
|
||||
photos: (item as any).photo_submission?.photo_items || []
|
||||
}
|
||||
: (item as any).photo_submission;
|
||||
|
||||
return [item.submission_id, { ...item, item_data: itemData }];
|
||||
}) || []
|
||||
);
|
||||
|
||||
for (const submission of submissions) {
|
||||
const contentData = submission.content as SubmissionContent;
|
||||
const submissionItem = itemsMap.get(submission.id);
|
||||
const itemData = submissionItem?.item_data as SubmissionItemData;
|
||||
const itemData = submissionItem?.item_data as any;
|
||||
|
||||
// Build base details
|
||||
const details: SubmissionReviewDetails = {
|
||||
|
||||
Reference in New Issue
Block a user