Skip to content

Commit 3756c3c

Browse files
authored
Merge pull request #18 from emanuellcs/improve-ui
Improve ui
2 parents fba36cf + 03968c4 commit 3756c3c

5 files changed

Lines changed: 675 additions & 397 deletions

File tree

actions/appointments.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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

actions/finance.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type NewSale = Omit<
1414
"id" | "payments" | "created_at" | "updatedAt" | "clientName" | "totalAmount"
1515
> & {
1616
totalAmount?: number;
17+
createdAt?: string;
1718
};
1819

1920
/**
@@ -23,6 +24,41 @@ export async function createSaleAction(sale: NewSale) {
2324
try {
2425
const supabase = getSupabaseAdmin();
2526

27+
// Fetch appointment start time if linked
28+
let inheritedCreatedAt = sale.createdAt || new Date().toISOString();
29+
if (sale.appointmentId) {
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+
);
59+
}
60+
}
61+
2662
// Get default commission
2763
const { data: settingData } = await supabase
2864
.from("app_settings")
@@ -48,6 +84,7 @@ export async function createSaleAction(sale: NewSale) {
4884
total_amount: computedTotal,
4985
status: sale.status || "pending",
5086
notes: sale.notes || null,
87+
created_at: inheritedCreatedAt,
5188
},
5289
])
5390
.select("*")

app/agenda/page.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)