Extension request: WS→FCM fallback + chat-recovery on connect

Today the customer's "Perpanjang" only reaches the mitra via session-
scoped WS. If the mitra is on Home/Undangan, in a different session, or
backgrounded, the WS send no-ops and the 10s safeguard timeout fires
auto-reject (or auto-approve if the mitra happens to also have an
active general WS, depending on config) — either way the mitra never
saw the request.

Backend:
- extension.service.js::requestExtension now falls back to FCM via
  notification.service when the mitra isn't on the session WS. Mirrors
  the pairing notifyMitra pattern (Curhat Baru). Customer display name
  is pulled into the session lookup for the FCM body.
- shared.chat.routes.js: /chat/:sessionId/info now returns
  pending_extension (extension_id, duration_minutes, price,
  requested_at, expires_at, timeout_seconds) so the chat screen can
  rehydrate the accept/reject UI after a cold-start FCM tap. expires_at
  is derived from requested_at + extension_timeout_seconds config.

Mitra app:
- mitra_chat_notifier.dart::connect parses pending_extension from /info
  and seeds MitraChatConnectedData.extensionRequest — the existing
  _buildExtensionView renders unchanged.
- notification_service.dart::_navigateFromMessage handles
  type=extension_request → pushes /chat/session/<id>. Composes with
  the new /info pending_extension to bring the mitra straight into the
  accept/reject view.

Verified end-to-end on dev backend (FCM call returned sent=true; /info
returns pending_extension when within timeout window). Visual delivery
on emulator-5556 deferred — API 24 AVD queues FCM 5-30 min per
feedback-emulator-avd-versions.

Out of scope (follow-ups):
- Customer-side FCM for EXTENSION_RESPONSE (accepted/rejected/timeout)
- Perpanjang tab list endpoint + Flutter provider + UI

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-21 13:24:40 +08:00
parent 368d18a0bf
commit e4bffe1a71
4 changed files with 76 additions and 7 deletions

View File

@@ -179,6 +179,13 @@ class MitraChat extends _$MitraChat {
? rawTopics.whereType<String>().toList(growable: false)
: const <String>[];
// If the customer requested an extension while we were off-WS (e.g.
// mitra on Home, app backgrounded, FCM tap cold-started us here),
// `/info` carries the pending row so we can paint `_buildExtensionView`
// immediately. Without this, the in-chat extension flow would only
// recover if the customer happened to request again after we connect.
final pendingExt = sessionData?['pending_extension'] as Map<String, dynamic>?;
final response = await _apiClient.get('/api/shared/chat/$sessionId/messages');
final messagesData = response['data'] as List<dynamic>;
final messages = messagesData.map((m) => MitraChatMessage(
@@ -222,6 +229,7 @@ class MitraChat extends _$MitraChat {
topicSensitivity: sessionTopic,
topics: espTopics,
mode: sessionMode,
extensionRequest: pendingExt,
);
} catch (e) {
state = const MitraChatErrorData('Gagal terhubung ke chat.');

View File

@@ -127,6 +127,12 @@ class NotificationService {
// Update the notifier state with this session, then navigate
onChatRequestTapped?.call(sessionId);
_router!.go('/home');
} else if (type == 'extension_request' && sessionId != null) {
// Customer requested an extension while mitra was off-WS (Home tab,
// backgrounded, app killed). Push to the chat — mitra_chat_notifier
// pulls the pending row from /chat/:sessionId/info and renders
// _buildExtensionView with the accept/reject CTAs.
_router!.push('/chat/session/$sessionId', extra: {'customerName': 'Customer'});
} else if (type == 'session_closing' || type == 'session_expired') {
// Navigate to the chat session closure screen
if (sessionId != null) {