11import { supabase } from "@/integrations/supabase/client" ;
22import { generateSlug } from "@/lib/slug" ;
3+ import { fromZonedTime } from "date-fns-tz" ;
34
45export interface StageImportData {
56 name : string ;
@@ -14,6 +15,20 @@ export interface SetImportData {
1415 description ?: string ;
1516}
1617
18+ // Helper function to convert local time string to UTC for database storage
19+ function convertLocalTimeToUTC (
20+ timeString : string | undefined ,
21+ timezone : string ,
22+ ) : string | null {
23+ if ( ! timeString ) return null ;
24+
25+ // Parse the time string and interpret it as being in the specified timezone
26+ // First create a date object assuming the time is in the target timezone
27+ const utcDate = fromZonedTime ( timeString , timezone ) ;
28+
29+ return utcDate . toISOString ( ) ;
30+ }
31+
1732export interface ImportResult {
1833 success : boolean ;
1934 message : string ;
@@ -136,6 +151,7 @@ export async function importStages(
136151export async function importSets (
137152 sets : SetImportData [ ] ,
138153 editionId : string ,
154+ timezone : string = "UTC" ,
139155 onProgress ?: ( completed : number , total : number ) => void ,
140156) : Promise < ImportResult > {
141157 try {
@@ -226,6 +242,10 @@ export async function importSets(
226242 . eq ( "festival_edition_id" , editionId )
227243 . limit ( 1 ) ;
228244
245+ // Convert times to UTC for database storage
246+ const utcTimeStart = convertLocalTimeToUTC ( set . time_start , timezone ) ;
247+ const utcTimeEnd = convertLocalTimeToUTC ( set . time_end , timezone ) ;
248+
229249 let createdSetId = "" ;
230250 let setError ;
231251
@@ -235,8 +255,8 @@ export async function importSets(
235255 const { error } = await supabase
236256 . from ( "sets" )
237257 . update ( {
238- time_start : set . time_start || null ,
239- time_end : set . time_end || null ,
258+ time_start : utcTimeStart ,
259+ time_end : utcTimeEnd ,
240260 description : set . description || null ,
241261 archived : false ,
242262 } )
@@ -254,8 +274,8 @@ export async function importSets(
254274 slug : generateSlug ( setName ) ,
255275 stage_id : stage . id ,
256276 festival_edition_id : editionId ,
257- time_start : set . time_start || null ,
258- time_end : set . time_end || null ,
277+ time_start : utcTimeStart ,
278+ time_end : utcTimeEnd ,
259279 description : set . description || null ,
260280 archived : false ,
261281 created_by : userId ,
0 commit comments