Files
prosappweb/lib/models/schedules_entity.dart
T
Lizandro GuarnizoandClaude Sonnet 4.6 0ecca498ad fix: align Flutter endpoints and model parsing with actual NestJS backend
- Auth: phone login → POST /auth/phone (direct, no OTP); link phone → POST /auth/verify-phone; add email → POST /auth/link-email
- Services: replace query-param paths with dedicated role endpoints (/services/me, /services/professional/requests, /services/professional, etc.)
- Services: PATCH /services/:id → PATCH /services/:id/status
- Calendar: → GET /services/professional/calendar
- Professionals: /users/professionals → /professionals (handles {data:[...]} response)
- Professional info: /professional-info/:id → /professionals/:id; PATCH → /professionals/me
- Comments: /comments?... → /comments/user/:id and /comments/professional/:id
- Chat: /chats → /chat; start chat → POST /chat/start/:professionalId; poll GET /chat/:chatId/messages; send → POST /chat/:chatId/message
- Cities: /cities → GET /locations/countries (parse nested countries→regions→cities)
- Profesional.fromDocument: null-safe fields; convert schedules array→Schedules, specializations array→name/picture lists, payment_methods object/array
- Usuario.fromDocument: null-safe professional_state and id
- UsuarioProfesional.fromDocument: handle backend format (users nested, professional at top level)
- ScheduleEntity.parseTime: handle ISO8601 time strings from backend
- MessageEntity.fromDocument: accept sender_id (backend) or owner_id (legacy)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-18 16:07:44 -05:00

113 lines
3.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class ScheduleEntity {
final bool enabled;
final bool continuousDay;
final TimeOfDay? range1Hour1;
final TimeOfDay? range1Hour2;
final TimeOfDay? range2Hour1;
final TimeOfDay? range2Hour2;
const ScheduleEntity({
required this.enabled,
required this.continuousDay,
required this.range1Hour1,
required this.range1Hour2,
required this.range2Hour1,
required this.range2Hour2,
});
static const empty = ScheduleEntity(
enabled: false,
continuousDay: false,
range1Hour1: null,
range1Hour2: null,
range2Hour1: null,
range2Hour2: null,
);
ScheduleEntity copyWith({
bool? enabled,
bool? continuousDay,
TimeOfDay? range1Hour1,
TimeOfDay? range1Hour2,
TimeOfDay? range2Hour1,
TimeOfDay? range2Hour2,
}) {
return ScheduleEntity(
enabled: enabled ?? this.enabled,
continuousDay: continuousDay ?? this.continuousDay,
range1Hour1: range1Hour1 ?? this.range1Hour1,
range1Hour2: range1Hour2 ?? this.range1Hour2,
range2Hour1: range2Hour1 ?? this.range2Hour1,
range2Hour2: range2Hour2 ?? this.range2Hour2,
);
}
static ScheduleEntity fromDocument(Map<String, dynamic> doc) {
return ScheduleEntity(
enabled: doc['habilitado'] as bool,
continuousDay: doc['continuous_day'] as bool,
range1Hour1: parseTime(doc['range1Hour1']),
range1Hour2: parseTime(doc['range1Hour2']),
range2Hour1: parseTime(doc['range2Hour1']),
range2Hour2: parseTime(doc['range2Hour2']),
);
}
static TimeOfDay? parseTime(String? time) {
try {
if (time == null) return null;
// Handle ISO8601 from backend (e.g. "1970-01-01T08:30:00.000Z")
final t = time.contains('T') ? time.split('T')[1] : time;
final components = t.split(':');
if (components.length < 2) return null;
return TimeOfDay(hour: int.parse(components[0]), minute: int.parse(components[1]));
} catch (e) {
return null;
}
}
Map<String, dynamic> toJson() {
return {
'habilitado': enabled,
'continuous_day': continuousDay,
'range1Hour1': formatTimeOfDay(range1Hour1),
'range1Hour2': formatTimeOfDay(range1Hour2),
'range2Hour1': formatTimeOfDay(range2Hour1),
'range2Hour2': formatTimeOfDay(range2Hour2),
};
}
String? formatTimeOfDay(TimeOfDay? time) {
if (time != null) {
return "${time.hour.toString()}:${time.minute.toString()}";
}
return null;
}
static String? getFormatTime(TimeOfDay? time) {
if (time == null) {
return null;
}
final now = DateTime.now();
final dateTime =
DateTime(now.year, now.month, now.day, time.hour, time.minute);
final format = DateFormat.jm();
return format.format(dateTime);
}
@override
String toString() {
return '''ScheduleEntity {
enabled: $enabled,
continuousDay: $continuousDay,
range1Hour1: $range1Hour1,
range1Hour2: $range1Hour2,
range2Hour1: $range2Hour1,
range2Hour2: $range2Hour2
}''';
}
}