File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,6 +42,28 @@ export async function createAppointmentAction(
4242 } ;
4343 }
4444
45+ // Sync sale's created_at to appointment's start_time
46+ if ( data ?. id ) {
47+ try {
48+ const appointmentId = parseInt ( data . id , 10 ) ;
49+ const appointmentStartTime = appointment . startTime ;
50+
51+ await supabase
52+ . from ( "sales" )
53+ . update ( { created_at : appointmentStartTime } )
54+ . eq ( "appointment_id" , appointmentId ) ;
55+
56+ console . log (
57+ `Sale created_at synced to appointment start_time: ${ appointmentStartTime } `
58+ ) ;
59+ } catch ( syncError ) {
60+ console . warn (
61+ "Failed to sync sale created_at to appointment start_time:" ,
62+ syncError
63+ ) ;
64+ }
65+ }
66+
4567 revalidatePath ( "/agenda" ) ;
4668 revalidatePath ( "/financeiro" ) ;
4769
Original file line number Diff line number Diff line change @@ -27,14 +27,35 @@ export async function createSaleAction(sale: NewSale) {
2727 // Fetch appointment start time if linked
2828 let inheritedCreatedAt = sale . createdAt || new Date ( ) . toISOString ( ) ;
2929 if ( sale . appointmentId ) {
30- const { data : appt } = await supabase
31- . from ( "appointments" )
32- . select ( "start_time" )
33- . eq ( "id" , parseInt ( sale . appointmentId ) )
34- . single ( ) ;
35-
36- if ( appt ?. start_time ) {
37- inheritedCreatedAt = appt . start_time ;
30+ try {
31+ const appointmentId = parseInt ( sale . appointmentId , 10 ) ;
32+ if ( isNaN ( appointmentId ) ) {
33+ console . warn (
34+ `Invalid appointmentId provided to createSaleAction: ${ sale . appointmentId } `
35+ ) ;
36+ } else {
37+ const { data : appt , error : apptErr } = await supabase
38+ . from ( "appointments" )
39+ . select ( "start_time" )
40+ . eq ( "id" , appointmentId )
41+ . single ( ) ;
42+
43+ if ( apptErr ) {
44+ console . warn (
45+ `Failed to fetch appointment ${ appointmentId } : ${ apptErr . message } `
46+ ) ;
47+ } else if ( appt ?. start_time ) {
48+ inheritedCreatedAt = appt . start_time ;
49+ console . log (
50+ `Sale linked to appointment ${ appointmentId } : using start_time ${ appt . start_time } `
51+ ) ;
52+ }
53+ }
54+ } catch ( appointmentFetchError ) {
55+ console . error (
56+ "Error fetching appointment for sale creation:" ,
57+ appointmentFetchError
58+ ) ;
3859 }
3960 }
4061
Original file line number Diff line number Diff line change @@ -248,20 +248,29 @@ export default function AgendaPage() {
248248
249249 const supabaseRes = await addAppointment ( supabasePayload ) ;
250250 if ( supabaseRes ) {
251+ // Close modal immediately after successful DB save (independent operation)
252+ setIsModalOpen ( false ) ;
253+ toast . success ( "Agendamento criado!" ) ;
254+ // Attempt Google Calendar sync (non-blocking)
251255 res = await createCalendarEvent ( googlePayload ) ;
256+ if ( ! res ?. success ) {
257+ toast . error ( res ?. error || "Erro ao sincronizar com Google Agenda" ) ;
258+ }
252259 } else {
253260 throw new Error ( "Falha ao salvar no banco de dados." ) ;
254261 }
255262 }
256263
257264 if ( res ?. success ) {
258- toast . success (
259- selectedEvent ? "Agendamento atualizado!" : "Agendamento criado!" ,
260- ) ;
261- setIsModalOpen ( false ) ;
265+ if ( selectedEvent ) {
266+ // Edit mode: only show success toast for Google Calendar result
267+ toast . success ( "Agendamento atualizado!" ) ;
268+ setIsModalOpen ( false ) ;
269+ }
262270 fetchEvents ( ) ;
263- } else {
264- toast . error ( res ?. error || "Erro ao salvar no Google Agenda" ) ;
271+ } else if ( selectedEvent ) {
272+ // Edit mode: Google Calendar sync failed
273+ toast . error ( res ?. error || "Erro ao atualizar no Google Agenda" ) ;
265274 }
266275 } catch ( e ) {
267276 console . error ( e ) ;
Original file line number Diff line number Diff line change @@ -381,14 +381,14 @@ export default function FinanceiroPage() {
381381 </ Button >
382382
383383 { showFilters && (
384- < div className = "flex items-center gap-2 animate-in fade-in slide-in-from-right-1" >
384+ < div className = "flex flex-wrap items-center gap-1 sm: gap-2 animate-in fade-in slide-in-from-right-1" >
385385 < div className = "flex items-center gap-1.5" >
386386 < Label className = "text-[10px] font-black uppercase tracking-widest text-muted-foreground whitespace-nowrap" >
387387 De
388388 </ Label >
389389 < Input
390390 type = "date"
391- className = "h-9 w-32 text-xs"
391+ className = "h-9 w-24 sm:w-28 md:w- 32 text-xs"
392392 value = { dateRange . start }
393393 onChange = { ( e ) => {
394394 setDateRange ( ( p ) => ( { ...p , start : e . target . value } ) ) ;
@@ -402,7 +402,7 @@ export default function FinanceiroPage() {
402402 </ Label >
403403 < Input
404404 type = "date"
405- className = "h-9 w-32 text-xs"
405+ className = "h-9 w-24 sm:w-28 md:w- 32 text-xs"
406406 value = { dateRange . end }
407407 onChange = { ( e ) => {
408408 setDateRange ( ( p ) => ( { ...p , end : e . target . value } ) ) ;
@@ -418,7 +418,7 @@ export default function FinanceiroPage() {
418418 setDateRange ( { start : "" , end : "" } ) ;
419419 setPage ( 1 ) ;
420420 } }
421- className = "h-9 px-2 text-[10px] font-bold uppercase tracking-widest text-destructive hover:bg-destructive/5"
421+ className = "h-9 px-2 text-[10px] font-bold uppercase tracking-widest text-destructive hover:bg-destructive/5 min-h-9 "
422422 >
423423 Limpar
424424 </ Button >
You can’t perform that action at this time.
0 commit comments