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 ;
@@ -137,6 +152,7 @@ export async function importStages(
137152export async function importSets (
138153 sets : SetImportData [ ] ,
139154 editionId : string ,
155+ timezone : string = "UTC" ,
140156 onProgress ?: ( completed : number , total : number ) => void ,
141157) : Promise < ImportResult > {
142158 try {
@@ -227,6 +243,10 @@ export async function importSets(
227243 . eq ( "festival_edition_id" , editionId )
228244 . limit ( 1 ) ;
229245
246+ // Convert times to UTC for database storage
247+ const utcTimeStart = convertLocalTimeToUTC ( set . time_start , timezone ) ;
248+ const utcTimeEnd = convertLocalTimeToUTC ( set . time_end , timezone ) ;
249+
230250 let createdSetId = "" ;
231251 let setError ;
232252
@@ -236,8 +256,8 @@ export async function importSets(
236256 const { error } = await supabase
237257 . from ( "sets" )
238258 . update ( {
239- time_start : set . time_start || null ,
240- time_end : set . time_end || null ,
259+ time_start : utcTimeStart ,
260+ time_end : utcTimeEnd ,
241261 description : set . description || null ,
242262 archived : false ,
243263 } )
@@ -255,8 +275,8 @@ export async function importSets(
255275 slug : generateSlug ( setName ) ,
256276 stage_id : stage . id ,
257277 festival_edition_id : editionId ,
258- time_start : set . time_start || null ,
259- time_end : set . time_end || null ,
278+ time_start : utcTimeStart ,
279+ time_end : utcTimeEnd ,
260280 description : set . description || null ,
261281 archived : false ,
262282 created_by : userId ,
0 commit comments