fix: safe parsing of Prisma Decimal fields to prevent data loss on save

Prisma serializes Decimal fields (rate, latitude, longitude) as JSON
strings, causing 'as num?' casts to throw in fromDocument(). The catch
block in getProfessional() then returned an empty Profesional, so saving
overwrote all existing data with empty strings.

Also added try-catch in all patch API calls to surface errors to user
instead of silently failing with a stuck loading button.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-06 22:00:29 -05:00
co-authored by Claude Sonnet 4.6
parent 7451b7c57b
commit 706b78b737
2 changed files with 24 additions and 8 deletions
+6 -4
View File
@@ -134,14 +134,16 @@ class Profesional {
address: (doc['address'] as String?) ?? '',
aditionalAddress: (doc['additional_address'] as String?) ?? '',
profession: (doc['profession'] as String?) ?? '',
ratePreferences: (doc['rate_preferences'] as bool?) ?? false,
rate: (doc['rate'] as num?)?.toString() ?? '',
ratePreferences: doc['rate_preferences'] is bool
? doc['rate_preferences'] as bool
: doc['rate_preferences'] == 'true',
rate: doc['rate']?.toString() ?? '',
locationPreferences: locationPrefsFromValue(doc['location_preferences']),
bannerPicture: (doc['banner_picture'] as String?) ?? '',
identificationPicture: (doc['identification_picture'] as String?) ?? '',
certificatePicture: (doc['certificate_picture'] as String?) ?? '',
latitude: (doc['latitude'] as num?)?.toDouble() ?? 0.0,
longitude: (doc['longitude'] as num?)?.toDouble() ?? 0.0,
latitude: double.tryParse(doc['latitude']?.toString() ?? '') ?? 0.0,
longitude: double.tryParse(doc['longitude']?.toString() ?? '') ?? 0.0,
specializations: specs,
specializationsPictures: specPics,
schedules: parsedSchedules,
+18 -4
View File
@@ -72,7 +72,12 @@ class ProfessionalFormProvider with ChangeNotifier {
Future<bool> updateProfesionalInfo(String userId) async {
if (!_validForm()) return false;
await _api.patch('/professionals/me', profesional!.toDocument());
try {
await _api.patch('/professionals/me', profesional!.toDocument());
} catch (e) {
NotificationsService.showSnackbar('Error al guardar: $e');
return false;
}
NotificationsService.showSnackbar('Información actualizada');
return true;
}
@@ -84,8 +89,12 @@ class ProfessionalFormProvider with ChangeNotifier {
Future<bool> updateProfesionalProfileInfo(String userId) async {
if (!_validProfileForm()) return false;
await _api.patch('/professionals/me', profesional!.toDocument());
// Re-fetch from server to confirm saved data is reflected in the form
try {
await _api.patch('/professionals/me', profesional!.toDocument());
} catch (e) {
NotificationsService.showSnackbar('Error al guardar: $e');
return false;
}
try {
final data = await _api.get('/professionals/me');
profesional = Profesional.fromDocument(data as Map<String, dynamic>);
@@ -96,7 +105,12 @@ class ProfessionalFormProvider with ChangeNotifier {
}
Future<bool> updateProfesionalProfileScheduleInfo(String userId) async {
await _api.patch('/professionals/me', profesional!.toDocument());
try {
await _api.patch('/professionals/me', profesional!.toDocument());
} catch (e) {
NotificationsService.showSnackbar('Error al guardar: $e');
return false;
}
NotificationsService.showSnackbar('Información actualizada');
return true;
}