import { dwStorage } from '@/lib/dw/storage';
// DONT REALLY NEED THESE ANYMORE, BUT KEEPING AS AN OPTION
[key: string]: string | null;
interface PhotoFieldsConfig {
[key: string]: string | null;
* Handles deletion of old photo files when they are being replaced or removed
* @param currentData Object containing the current photo URLs
* @param newData Object containing the new photo URLs
* @param photoFields Array of field names that contain photo URLs
* @returns Array of any errors that occurred during deletion
async function handlePhotoUpdates(
currentData: PhotoFieldsConfig,
newData: PhotoFieldsConfig,
const deletionErrors: string[] = [];
for (const field of photoFields) {
const currentUrl = currentData[field];
const newUrl = newData[field];
if (currentUrl && currentUrl !== newUrl) {
const deleteSuccess = await dwStorage.deleteFile(currentUrl);
deletionErrors.push(`Failed to delete old photo for ${field}: ${currentUrl}`);
// Handle photos if specified
if (options.photoFields?.length) {
const photoData: PhotoData = {};
options.photoFields.forEach(field => {
photoData[field] = formData.get(field)?.toString() || null;
const { data: currentRecord, error: fetchError } = await supabase
.select(options.photoFields.join(', '))
if (fetchError) throw fetchError;
// Convert the record to PhotoData type
const currentPhotoData: PhotoData = {};
options.photoFields.forEach(field => {
currentPhotoData[field] = (currentRecord as GenericRecord)[field] || null;
await handlePhotoUpdates(currentPhotoData, photoData, options.photoFields);
Object.assign(options.recordData, photoData);