103 lines
2.9 KiB
Dart
103 lines
2.9 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:brick_offline_first_with_supabase/brick_offline_first_with_supabase.dart';
|
|
import 'package:brick_supabase/brick_supabase.dart';
|
|
|
|
import '../utils/app_time.dart';
|
|
|
|
// meta: Map<String, dynamic>? is stored as JSON text in SQLite by Brick.
|
|
@ConnectOfflineFirstWithSupabase(
|
|
supabaseConfig: SupabaseSerializable(tableName: 'task_activity_logs'),
|
|
)
|
|
class TaskActivityLog extends OfflineFirstWithSupabaseModel {
|
|
final String id;
|
|
final String taskId;
|
|
final String? actorId;
|
|
final String actionType;
|
|
final Map<String, dynamic>? meta;
|
|
final DateTime createdAt;
|
|
|
|
TaskActivityLog({
|
|
required this.id,
|
|
required this.taskId,
|
|
this.actorId,
|
|
required this.actionType,
|
|
this.meta,
|
|
required this.createdAt,
|
|
});
|
|
|
|
factory TaskActivityLog.fromMap(Map<String, dynamic> map) {
|
|
final rawId = map['id'];
|
|
final rawTaskId = map['task_id'];
|
|
|
|
String id = rawId == null ? '' : rawId.toString();
|
|
String taskId = rawTaskId == null ? '' : rawTaskId.toString();
|
|
final actorId = map['actor_id']?.toString();
|
|
final actionType = (map['action_type'] as String?) ?? 'unknown';
|
|
|
|
Map<String, dynamic>? meta;
|
|
final rawMeta = map['meta'];
|
|
if (rawMeta is Map<String, dynamic>) {
|
|
meta = rawMeta;
|
|
} else if (rawMeta is Map) {
|
|
try {
|
|
meta = rawMeta.map((k, v) => MapEntry(k.toString(), v));
|
|
} catch (_) {
|
|
meta = null;
|
|
}
|
|
} else if (rawMeta is String && rawMeta.isNotEmpty) {
|
|
try {
|
|
final decoded = jsonDecode(rawMeta);
|
|
if (decoded is Map<String, dynamic>) {
|
|
meta = decoded;
|
|
} else if (decoded is Map) {
|
|
meta = decoded.map((k, v) => MapEntry(k.toString(), v));
|
|
}
|
|
} catch (_) {
|
|
meta = null;
|
|
}
|
|
}
|
|
|
|
final rawCreated = map['created_at'];
|
|
DateTime createdAt;
|
|
if (rawCreated is DateTime) {
|
|
createdAt = AppTime.toAppTime(rawCreated);
|
|
} else if (rawCreated is String) {
|
|
try {
|
|
createdAt = AppTime.parse(rawCreated);
|
|
} catch (_) {
|
|
createdAt = AppTime.now();
|
|
}
|
|
} else if (rawCreated is int) {
|
|
if (rawCreated > 1e12) {
|
|
createdAt = AppTime.toAppTime(
|
|
DateTime.fromMillisecondsSinceEpoch(rawCreated),
|
|
);
|
|
} else if (rawCreated > 1e10) {
|
|
createdAt = AppTime.toAppTime(
|
|
DateTime.fromMillisecondsSinceEpoch(rawCreated),
|
|
);
|
|
} else {
|
|
createdAt = AppTime.toAppTime(
|
|
DateTime.fromMillisecondsSinceEpoch(rawCreated * 1000),
|
|
);
|
|
}
|
|
} else if (rawCreated is double) {
|
|
createdAt = AppTime.toAppTime(
|
|
DateTime.fromMillisecondsSinceEpoch(rawCreated.toInt()),
|
|
);
|
|
} else {
|
|
createdAt = AppTime.now();
|
|
}
|
|
|
|
return TaskActivityLog(
|
|
id: id,
|
|
taskId: taskId,
|
|
actorId: actorId,
|
|
actionType: actionType,
|
|
meta: meta,
|
|
createdAt: createdAt,
|
|
);
|
|
}
|
|
}
|