Phase 3.5: Mitra Chat Request History (backend route + mitra app screens)

Replaces the home-screen pending-requests banner with a "Riwayat
Permintaan" CTA that opens a list of the mitra's last 20 chat requests
(any status). Pending rows pin to the top; non-pending rows open a
read-only detail screen with a "Lihat percakapan" CTA on accepted rows.

Backend:
- New service `getRecentRequestsForMitra(mitraId, { limit })` capped at
  20, pending pinned via `(response IS NULL AND status='pending_acceptance')
  DESC`. Customer call_name returned verbatim, with `'Anonim'` only as
  null-safety fallback (no anonymity-flag masking — see project memory).
- New route `GET /api/mitra/chat-requests/recent`. Strictly per-mitra
  scoped via the existing `resolveMitra` preHandler.

Mitra app:
- New `RequestResponse` enum in core/constants.dart.
- New Riverpod notifier `requestHistoryProvider` (AsyncValue<List<...>>,
  keepAlive) — pull-to-refresh + screen-mount fetch only, no WS.
- Two new screens (history list + detail) and two new GoRoutes.
- Home screen: `_PendingRequestsBanner` removed → `_RequestHistoryButton`
  Card with red count badge. Live count comes from the existing
  chatRequestProvider so nothing changes about the WS-driven badge math.

Plan + acceptance criteria in requirement/phase3.5-plan.md. flutter
analyze clean (zero new issues). Backend smoke-tested against real DB.
Real-device E2E pending.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-27 18:59:17 +08:00
parent e54bdf2c6c
commit 89afd01899
14 changed files with 866 additions and 49 deletions

View File

@@ -0,0 +1,42 @@
import '../../../core/constants.dart';
class RequestHistoryEntry {
final String notificationId;
final String sessionId;
final DateTime notifiedAt;
final DateTime? respondedAt;
final RequestResponse? response;
final String sessionStatus;
final String customerCallName;
final TopicSensitivity topicSensitivity;
const RequestHistoryEntry({
required this.notificationId,
required this.sessionId,
required this.notifiedAt,
required this.respondedAt,
required this.response,
required this.sessionStatus,
required this.customerCallName,
required this.topicSensitivity,
});
bool get isPending =>
response == null && sessionStatus == SessionStatus.pendingAcceptance;
factory RequestHistoryEntry.fromJson(Map<String, dynamic> json) {
return RequestHistoryEntry(
notificationId: json['notification_id'] as String,
sessionId: json['session_id'] as String,
notifiedAt: DateTime.parse(json['notified_at'] as String).toLocal(),
respondedAt: json['responded_at'] != null
? DateTime.parse(json['responded_at'] as String).toLocal()
: null,
response: RequestResponse.fromString(json['response'] as String?),
sessionStatus: json['session_status'] as String? ?? '',
customerCallName: json['customer_call_name'] as String? ?? 'Anonim',
topicSensitivity:
TopicSensitivity.fromString(json['topic_sensitivity'] as String?),
);
}
}

View File

@@ -0,0 +1,37 @@
import 'dart:async';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../../core/api/api_client_provider.dart';
import '../models/request_history_entry.dart';
part 'request_history_notifier.g.dart';
@Riverpod(keepAlive: true)
class RequestHistory extends _$RequestHistory {
@override
FutureOr<List<RequestHistoryEntry>> build() async {
return _fetch();
}
Future<void> refresh() async {
state = const AsyncLoading();
state = await AsyncValue.guard(_fetch);
}
Future<List<RequestHistoryEntry>> _fetch() async {
final api = ref.read(apiClientProvider);
final response = await api.get('/api/mitra/chat-requests/recent');
final items = (response['data'] as List<dynamic>).cast<Map<String, dynamic>>();
return items.map(RequestHistoryEntry.fromJson).toList();
}
RequestHistoryEntry? findById(String notificationId) {
final list = state.valueOrNull;
if (list == null) return null;
for (final e in list) {
if (e.notificationId == notificationId) return e;
}
return null;
}
}

View File

@@ -0,0 +1,26 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'request_history_notifier.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$requestHistoryHash() => r'f97e75e7fa5088293c606fd3cec1ad7bd4f96480';
/// See also [RequestHistory].
@ProviderFor(RequestHistory)
final requestHistoryProvider =
AsyncNotifierProvider<RequestHistory, List<RequestHistoryEntry>>.internal(
RequestHistory.new,
name: r'requestHistoryProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$requestHistoryHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$RequestHistory = AsyncNotifier<List<RequestHistoryEntry>>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package

View File

@@ -0,0 +1,159 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../core/chat/widgets/sensitivity_badge.dart';
import '../../../core/constants.dart';
import '../models/request_history_entry.dart';
import '../notifiers/request_history_notifier.dart';
import '../utils/request_status_label.dart';
class RequestHistoryDetailScreen extends ConsumerWidget {
final String notificationId;
const RequestHistoryDetailScreen({super.key, required this.notificationId});
@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(requestHistoryProvider);
final entry = state.valueOrNull == null
? null
: ref.read(requestHistoryProvider.notifier).findById(notificationId);
return Scaffold(
appBar: AppBar(title: const Text('Detail Permintaan')),
body: state.isLoading && entry == null
? const Center(child: CircularProgressIndicator())
: entry == null
? _NotFound(
onRetry: () =>
ref.read(requestHistoryProvider.notifier).refresh(),
)
: _Detail(entry: entry),
);
}
}
class _NotFound extends StatelessWidget {
final VoidCallback onRetry;
const _NotFound({required this.onRetry});
@override
Widget build(BuildContext context) {
return Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.search_off, size: 48, color: Colors.grey),
const SizedBox(height: 12),
const Text('Permintaan tidak ditemukan'),
const SizedBox(height: 12),
TextButton(onPressed: onRetry, child: const Text('Muat ulang')),
],
),
),
);
}
}
class _Detail extends StatelessWidget {
final RequestHistoryEntry entry;
const _Detail({required this.entry});
@override
Widget build(BuildContext context) {
final status = labelFor(entry);
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Flexible(
child: Text(
entry.customerCallName,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
overflow: TextOverflow.ellipsis,
),
),
if (entry.topicSensitivity == TopicSensitivity.sensitive) ...[
const SizedBox(width: 8),
const SensitivityBadge(
sensitivity: TopicSensitivity.sensitive,
fontSize: 11,
),
],
],
),
const SizedBox(height: 16),
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: status.background,
borderRadius: BorderRadius.circular(999),
),
child: Text(
status.label,
style: TextStyle(
fontSize: 12,
color: status.foreground,
fontWeight: FontWeight.w600,
),
),
),
const Divider(height: 32),
_LabeledRow('Diterima notifikasi', formatAbsolute(entry.notifiedAt)),
if (entry.respondedAt != null) ...[
const SizedBox(height: 8),
_LabeledRow(
'Direspon pada',
formatAbsolute(entry.respondedAt!),
),
],
if (entry.response == RequestResponse.accepted) ...[
const SizedBox(height: 24),
SizedBox(
width: double.infinity,
child: FilledButton.icon(
icon: const Icon(Icons.chat_bubble_outline),
label: const Text('Lihat percakapan'),
onPressed: () =>
context.push('/chat/history/${entry.sessionId}'),
),
),
],
],
),
),
),
);
}
}
class _LabeledRow extends StatelessWidget {
final String label;
final String value;
const _LabeledRow(this.label, this.value);
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 140,
child: Text(label, style: TextStyle(color: Colors.grey.shade700)),
),
Expanded(child: Text(value)),
],
);
}
}

View File

@@ -0,0 +1,153 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../core/chat/chat_request_notifier.dart';
import '../../../core/chat/widgets/sensitivity_badge.dart';
import '../../../core/constants.dart';
import '../models/request_history_entry.dart';
import '../notifiers/request_history_notifier.dart';
import '../utils/request_status_label.dart';
class RequestHistoryScreen extends ConsumerWidget {
const RequestHistoryScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(requestHistoryProvider);
return Scaffold(
appBar: AppBar(title: const Text('Riwayat Permintaan')),
body: RefreshIndicator(
onRefresh: () => ref.read(requestHistoryProvider.notifier).refresh(),
child: state.when(
loading: () => const _CenterScroll(child: CircularProgressIndicator()),
error: (err, _) => _CenterScroll(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.error_outline, size: 48, color: Colors.grey),
const SizedBox(height: 12),
const Text('Gagal memuat riwayat'),
const SizedBox(height: 12),
TextButton(
onPressed: () =>
ref.read(requestHistoryProvider.notifier).refresh(),
child: const Text('Coba lagi'),
),
],
),
),
),
data: (entries) {
if (entries.isEmpty) {
return const _CenterScroll(
child: Padding(
padding: EdgeInsets.all(24),
child: Text('Belum ada permintaan chat'),
),
);
}
return ListView.separated(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.symmetric(vertical: 8),
itemCount: entries.length,
separatorBuilder: (_, __) => const Divider(height: 1),
itemBuilder: (context, index) => _RequestRow(entries[index]),
);
},
),
),
);
}
}
class _CenterScroll extends StatelessWidget {
final Widget child;
const _CenterScroll({required this.child});
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, c) => SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: SizedBox(
height: c.maxHeight,
child: Center(child: child),
),
),
);
}
}
class _RequestRow extends ConsumerWidget {
final RequestHistoryEntry entry;
const _RequestRow(this.entry);
@override
Widget build(BuildContext context, WidgetRef ref) {
final status = labelFor(entry);
return ListTile(
title: Row(
children: [
Flexible(
child: Text(
entry.customerCallName,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontWeight: FontWeight.w600),
),
),
if (entry.topicSensitivity == TopicSensitivity.sensitive) ...[
const SizedBox(width: 8),
const SensitivityBadge(
sensitivity: TopicSensitivity.sensitive,
fontSize: 10,
),
],
],
),
subtitle: Padding(
padding: const EdgeInsets.only(top: 4),
child: Row(
children: [
Container(
padding:
const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: status.background,
borderRadius: BorderRadius.circular(999),
),
child: Text(
status.label,
style: TextStyle(
fontSize: 11,
color: status.foreground,
fontWeight: FontWeight.w600,
),
),
),
const SizedBox(width: 8),
Text(
formatRelative(entry.notifiedAt),
style: TextStyle(fontSize: 12, color: Colors.grey.shade600),
),
],
),
),
trailing: const Icon(Icons.chevron_right),
onTap: () => _onTap(context, ref),
);
}
Future<void> _onTap(BuildContext context, WidgetRef ref) async {
if (entry.isPending) {
await ref
.read(chatRequestProvider.notifier)
.setIncomingFromNotification(entry.sessionId);
} else {
context.push('/chat/requests/history/${entry.notificationId}');
}
}
}

View File

@@ -0,0 +1,77 @@
import 'package:flutter/material.dart';
import '../../../core/constants.dart';
import '../models/request_history_entry.dart';
class RequestStatusLabel {
final String label;
final Color background;
final Color foreground;
const RequestStatusLabel(this.label, this.background, this.foreground);
}
RequestStatusLabel labelFor(RequestHistoryEntry entry) {
if (entry.isPending) {
return RequestStatusLabel(
'Menunggu respon',
Colors.amber.shade100,
Colors.amber.shade900,
);
}
switch (entry.response) {
case RequestResponse.accepted:
return RequestStatusLabel(
'Diterima',
Colors.green.shade100,
Colors.green.shade900,
);
case RequestResponse.declined:
return RequestStatusLabel(
'Ditolak',
Colors.grey.shade300,
Colors.grey.shade800,
);
case RequestResponse.missed:
return RequestStatusLabel(
'Terlewat',
Colors.grey.shade300,
Colors.grey.shade800,
);
case RequestResponse.ignored:
if (entry.sessionStatus == SessionStatus.cancelled) {
return RequestStatusLabel(
'Dibatalkan',
Colors.grey.shade300,
Colors.grey.shade800,
);
}
return RequestStatusLabel(
'Kedaluwarsa',
Colors.grey.shade300,
Colors.grey.shade800,
);
case null:
// Non-pending null (session moved past pending_acceptance without a logged response)
return RequestStatusLabel(
'Kedaluwarsa',
Colors.grey.shade300,
Colors.grey.shade800,
);
}
}
String formatRelative(DateTime when) {
final now = DateTime.now();
final diff = now.difference(when);
if (diff.inSeconds < 60) return 'Baru saja';
if (diff.inMinutes < 60) return '${diff.inMinutes} menit lalu';
if (diff.inHours < 24) return '${diff.inHours} jam lalu';
if (diff.inDays < 2) return 'Kemarin';
return '${diff.inDays} hari lalu';
}
String _two(int n) => n.toString().padLeft(2, '0');
String formatAbsolute(DateTime when) {
return '${when.day}/${when.month}/${when.year} ${_two(when.hour)}:${_two(when.minute)}';
}