saveTransaction method
Implementation
Future<String> saveTransaction(
String type,
Map<String, dynamic> payload,
) async {
await ensureReady();
final now = DateTime.now().toUtc();
final request =
(payload['request'] as Map?)?.cast<String, dynamic>() ??
const <String, dynamic>{};
final requestBody = (request['body'] ?? '').toString();
String atSignHint = 'unknown';
try {
if (requestBody.isNotEmpty) {
final decoded = jsonDecode(requestBody);
if (decoded is Map && decoded['atSign'] != null) {
atSignHint = decoded['atSign'].toString();
}
}
} catch (_) {
// Keep unknown if request body is not valid JSON.
}
final sanitizedAtSign = atSignHint.replaceAll(
RegExp(r'[^A-Za-z0-9_-]'),
'_',
);
final timestamp = now.toIso8601String().replaceAll(':', '-');
final sanitizedType = type.replaceAll(RegExp(r'[^A-Za-z0-9_-]'), '_');
final filename = '${timestamp}_${sanitizedType}_${sanitizedAtSign}.json';
final file = File(path.join(_directory!.path, filename));
final contents = <String, dynamic>{
'createdAtUtc': now.toIso8601String(),
'type': type,
...payload,
};
await file.writeAsString(
const JsonEncoder.withIndent(' ').convert(contents),
);
return file.path;
}