mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 20:51:13 -05:00
Fix notification logs migration
This commit is contained in:
@@ -1368,3 +1368,127 @@ export async function fetchEditHistory(itemId: string) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Conflict detection interfaces and functions
|
||||
*/
|
||||
export interface ConflictCheckResult {
|
||||
hasConflict: boolean;
|
||||
serverVersion?: {
|
||||
last_modified_at: string;
|
||||
last_modified_by: string;
|
||||
modified_by_profile?: {
|
||||
username: string;
|
||||
display_name: string;
|
||||
avatar_url: string;
|
||||
};
|
||||
};
|
||||
clientVersion?: {
|
||||
last_modified_at: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a submission has been modified since the client last loaded it
|
||||
* Used for optimistic locking to prevent concurrent edit conflicts
|
||||
*/
|
||||
export async function checkSubmissionConflict(
|
||||
submissionId: string,
|
||||
clientLastModified: string
|
||||
): Promise<ConflictCheckResult> {
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from('content_submissions')
|
||||
.select(`
|
||||
last_modified_at,
|
||||
last_modified_by,
|
||||
profiles:last_modified_by (
|
||||
username,
|
||||
display_name,
|
||||
avatar_url
|
||||
)
|
||||
`)
|
||||
.eq('id', submissionId)
|
||||
.single();
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
if (!data.last_modified_at) {
|
||||
return {
|
||||
hasConflict: false,
|
||||
clientVersion: { last_modified_at: clientLastModified },
|
||||
};
|
||||
}
|
||||
|
||||
const serverTimestamp = new Date(data.last_modified_at).getTime();
|
||||
const clientTimestamp = new Date(clientLastModified).getTime();
|
||||
|
||||
return {
|
||||
hasConflict: serverTimestamp > clientTimestamp,
|
||||
serverVersion: {
|
||||
last_modified_at: data.last_modified_at,
|
||||
last_modified_by: data.last_modified_by,
|
||||
modified_by_profile: data.profiles as any,
|
||||
},
|
||||
clientVersion: {
|
||||
last_modified_at: clientLastModified,
|
||||
},
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
logger.error('Error checking submission conflict', {
|
||||
submissionId,
|
||||
error: getErrorMessage(error),
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch recent versions of submission items for conflict resolution
|
||||
*/
|
||||
export async function fetchSubmissionVersions(
|
||||
submissionId: string,
|
||||
limit: number = 10
|
||||
) {
|
||||
try {
|
||||
// Get all item IDs for this submission
|
||||
const { data: items, error: itemsError } = await supabase
|
||||
.from('submission_items')
|
||||
.select('id')
|
||||
.eq('submission_id', submissionId);
|
||||
|
||||
if (itemsError) throw itemsError;
|
||||
if (!items || items.length === 0) return [];
|
||||
|
||||
const itemIds = items.map(i => i.id);
|
||||
|
||||
// Fetch edit history for all items
|
||||
const { data, error } = await supabase
|
||||
.from('item_edit_history')
|
||||
.select(`
|
||||
id,
|
||||
item_id,
|
||||
changes,
|
||||
edited_at,
|
||||
editor:profiles!item_edit_history_editor_id_fkey (
|
||||
user_id,
|
||||
username,
|
||||
display_name,
|
||||
avatar_url
|
||||
)
|
||||
`)
|
||||
.in('item_id', itemIds)
|
||||
.order('edited_at', { ascending: false })
|
||||
.limit(limit);
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
return data || [];
|
||||
} catch (error: unknown) {
|
||||
logger.error('Error fetching submission versions', {
|
||||
submissionId,
|
||||
error: getErrorMessage(error),
|
||||
});
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user