Skip to content

Commit 5ad4d72

Browse files
committed
fix(api): handle DateTime deserialization from database for User model
Anonymous user creation was failing with a type cast error because the `postgres` driver returns native `DateTime` objects, while the `User.fromJson` factory expects ISO 8601 strings for date fields. This change introduces a custom deserialization function for the `userRepository`. This function intercepts the data map from the database, converts `DateTime` objects to the expected string format, and then passes the corrected map to the standard `User.fromJson` factory. This resolves the runtime error by aligning the data format from the data source with the expectations of the data model.
1 parent fc53d02 commit 5ad4d72

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lib/src/config/app_dependencies.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,18 @@ class AppDependencies {
124124
userRepository = _createRepository(
125125
connection,
126126
'users',
127-
User.fromJson,
127+
(json) {
128+
// The postgres driver returns DateTime objects, but the model's
129+
// fromJson expects ISO 8601 strings. We must convert them first.
130+
if (json['created_at'] is DateTime) {
131+
json['created_at'] = (json['created_at'] as DateTime).toIso8601String();
132+
}
133+
if (json['last_engagement_shown_at'] is DateTime) {
134+
json['last_engagement_shown_at'] =
135+
(json['last_engagement_shown_at'] as DateTime).toIso8601String();
136+
}
137+
return User.fromJson(json);
138+
},
128139
(user) {
129140
// The `roles` field is a List<String>, but the database expects a
130141
// JSONB array. We must explicitly encode it.

0 commit comments

Comments
 (0)