Phase 3.7: paid pairing flow + returning chat + extension flip
- Backend: payment_sessions + pairing_failures tables; payment.service.js and pairing-failure.service.js (new); rewritten pairing.service.js (payment-gated blast + targeted "Curhat lagi" + cancel + fallback); rewritten extension.service.js (data-driven auto-approve with offline safeguard, charge-at-approval); pricing.service.js (extension tiers without free trial); mitra-status.service.js (countAvailableMitras cached path); 60s sweeper for stale payment sessions - Backend routes: client.payment.routes, client.mitra-availability.routes, internal/failed-pairings.routes; client.chat.routes rewritten for payment-gated start + /returning + /cancel + /fallback-to-blast; internal/config.routes adds 4 new keys with Valkey invalidate publish - client_app: mitra-availability poll, payment screen + notifier, pairing notifier rewrite (PairingTargetedWaiting + PairingFailed states), targeted-waiting overlay + bestie-unavailable dialog, "Curhat lagi" CTA, failed-pairing terminal, extension via payment-session - mitra_app: PairingRequestType enum, returning-chat 20s countdown auto-dismiss, extension card "otomatis disetujui" copy - control_center: 4 new config rows in Settings, Failed Pairings page (filter + paginate + action menu), sidebar + route registered - Test infrastructure: Vitest backend (7/7 pass), Playwright CC (4/4 pass), Maestro mobile scaffold (CLI install pending) - Bugs found via Playwright + fixed: LoginPage labels not associated with inputs (a11y); backend internal CORS missing PATCH/PUT/DELETE in allow-methods (silent settings breakage in browsers since Stage 4) - Docs: phase3.7.md PRD, phase3.7-plan.md, phase3.7-questions.md (Q&A), phase3.7-testing.md (E2E checklist), phase3.7-test-run-2026-05-03.md (today's run results) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -37,12 +37,19 @@ class ChatRequestIncomingData extends ChatRequestData {
|
||||
final bool? isFreeTrial;
|
||||
final TopicSensitivity topicSensitivity;
|
||||
final DateTime? createdAt;
|
||||
// Distinguishes general blast vs targeted "Curhat lagi" requests.
|
||||
// Returning requests carry a server-driven confirmation window; the overlay shows a
|
||||
// countdown but the server is the source of truth on auto-reject.
|
||||
final PairingRequestType requestType;
|
||||
final int? confirmationTimeoutSeconds;
|
||||
const ChatRequestIncomingData(
|
||||
this.sessionId, {
|
||||
this.durationMinutes,
|
||||
this.isFreeTrial,
|
||||
this.topicSensitivity = TopicSensitivity.regular,
|
||||
this.createdAt,
|
||||
this.requestType = PairingRequestType.general,
|
||||
this.confirmationTimeoutSeconds,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -103,6 +110,8 @@ class ChatRequest extends _$ChatRequest {
|
||||
'is_free_trial': r['is_free_trial'],
|
||||
'topic_sensitivity': r['topic_sensitivity'],
|
||||
'created_at': r['created_at'],
|
||||
'request_type': r['request_type'],
|
||||
'confirmation_timeout_seconds': r['confirmation_timeout_seconds'],
|
||||
};
|
||||
|
||||
if (state is ChatRequestIncomingData ||
|
||||
@@ -118,6 +127,8 @@ class ChatRequest extends _$ChatRequest {
|
||||
createdAt: r['created_at'] != null
|
||||
? DateTime.tryParse(r['created_at'] as String)
|
||||
: null,
|
||||
requestType: PairingRequestType.fromString(r['request_type'] as String?),
|
||||
confirmationTimeoutSeconds: r['confirmation_timeout_seconds'] as int?,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -207,6 +218,8 @@ class ChatRequest extends _$ChatRequest {
|
||||
createdAt: data['created_at'] != null
|
||||
? DateTime.tryParse(data['created_at'] as String)
|
||||
: null,
|
||||
requestType: PairingRequestType.fromString(data['request_type'] as String?),
|
||||
confirmationTimeoutSeconds: data['confirmation_timeout_seconds'] as int?,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -287,6 +300,8 @@ class ChatRequest extends _$ChatRequest {
|
||||
createdAt: next['created_at'] != null
|
||||
? DateTime.tryParse(next['created_at'] as String)
|
||||
: null,
|
||||
requestType: PairingRequestType.fromString(next['request_type'] as String?),
|
||||
confirmationTimeoutSeconds: next['confirmation_timeout_seconds'] as int?,
|
||||
);
|
||||
validateIncomingRequest();
|
||||
} else {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../chat_request_notifier.dart';
|
||||
@@ -20,6 +21,13 @@ class _ChatRequestOverlayState extends ConsumerState<ChatRequestOverlay>
|
||||
late final Animation<Offset> _slideAnimation;
|
||||
bool _visible = false;
|
||||
|
||||
// Returning-chat countdown. Server is the source of truth on auto-reject;
|
||||
// this is purely visual. When it hits 0 we dismiss the overlay and let the server's
|
||||
// chat_request_closed event (or stale state) take over.
|
||||
Timer? _countdownTimer;
|
||||
int? _secondsRemaining;
|
||||
String? _countdownSessionId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -35,6 +43,7 @@ class _ChatRequestOverlayState extends ConsumerState<ChatRequestOverlay>
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_stopCountdown();
|
||||
_animController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
@@ -47,11 +56,52 @@ class _ChatRequestOverlayState extends ConsumerState<ChatRequestOverlay>
|
||||
}
|
||||
|
||||
void _hide() {
|
||||
_stopCountdown();
|
||||
_animController.reverse().then((_) {
|
||||
if (mounted) setState(() => _visible = false);
|
||||
});
|
||||
}
|
||||
|
||||
void _startCountdown(String sessionId, int seconds) {
|
||||
_stopCountdown();
|
||||
_countdownSessionId = sessionId;
|
||||
_secondsRemaining = seconds;
|
||||
_countdownTimer = Timer.periodic(const Duration(seconds: 1), (_) {
|
||||
if (!mounted) return;
|
||||
final remaining = (_secondsRemaining ?? 0) - 1;
|
||||
if (remaining <= 0) {
|
||||
// Auto-dismiss UI only — server fires the actual auto-reject and will follow up
|
||||
// with a chat_request_closed event. Do NOT call decline from the client here.
|
||||
setState(() => _secondsRemaining = 0);
|
||||
_stopCountdown();
|
||||
_hide();
|
||||
} else {
|
||||
setState(() => _secondsRemaining = remaining);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _stopCountdown() {
|
||||
_countdownTimer?.cancel();
|
||||
_countdownTimer = null;
|
||||
_countdownSessionId = null;
|
||||
_secondsRemaining = null;
|
||||
}
|
||||
|
||||
void _maybeStartCountdownFor(ChatRequestIncomingData data) {
|
||||
final timeout = data.confirmationTimeoutSeconds;
|
||||
if (data.requestType == PairingRequestType.returning &&
|
||||
timeout != null &&
|
||||
timeout > 0) {
|
||||
// Restart only if this is a different session than the one we're already counting.
|
||||
if (_countdownSessionId != data.sessionId) {
|
||||
_startCountdown(data.sessionId, timeout);
|
||||
}
|
||||
} else {
|
||||
_stopCountdown();
|
||||
}
|
||||
}
|
||||
|
||||
void _onSwipeDown(DragEndDetails details) {
|
||||
if (details.primaryVelocity != null && details.primaryVelocity! > 200) {
|
||||
final state = ref.read(chatRequestProvider);
|
||||
@@ -66,7 +116,12 @@ class _ChatRequestOverlayState extends ConsumerState<ChatRequestOverlay>
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
ref.listen(chatRequestProvider, (prev, next) {
|
||||
if (next is ChatRequestIncomingData || next is ChatRequestStaleData) {
|
||||
if (next is ChatRequestIncomingData) {
|
||||
_show();
|
||||
_maybeStartCountdownFor(next);
|
||||
} else if (next is ChatRequestStaleData) {
|
||||
// Stale message replaces the active request — kill any returning-chat countdown.
|
||||
_stopCountdown();
|
||||
_show();
|
||||
} else if (next is ChatRequestAcceptedData) {
|
||||
_hide();
|
||||
@@ -137,6 +192,15 @@ class _ChatRequestOverlayState extends ConsumerState<ChatRequestOverlay>
|
||||
: '';
|
||||
final isSensitive = data.topicSensitivity == TopicSensitivity.sensitive;
|
||||
final theme = SensitivityTheme.of(data.topicSensitivity);
|
||||
final isReturning = data.requestType == PairingRequestType.returning;
|
||||
final showCountdown = isReturning &&
|
||||
_countdownSessionId == data.sessionId &&
|
||||
_secondsRemaining != null;
|
||||
final headlineText =
|
||||
isReturning ? 'Customer ingin chat lagi!' : 'Ada permintaan chat baru!';
|
||||
final subtitleText = isReturning
|
||||
? 'Seorang customer yang pernah chat denganmu ingin lanjut.'
|
||||
: 'Seorang customer ingin curhat denganmu.';
|
||||
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
@@ -164,11 +228,15 @@ class _ChatRequestOverlayState extends ConsumerState<ChatRequestOverlay>
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
),
|
||||
),
|
||||
const Icon(Icons.chat, size: 48, color: Colors.blue),
|
||||
Icon(
|
||||
isReturning ? Icons.replay_circle_filled : Icons.chat,
|
||||
size: 48,
|
||||
color: isReturning ? Colors.deepPurple : Colors.blue,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const Text(
|
||||
'Ada permintaan chat baru!',
|
||||
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
||||
Text(
|
||||
headlineText,
|
||||
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
if (durationText.isNotEmpty)
|
||||
@@ -181,10 +249,36 @@ class _ChatRequestOverlayState extends ConsumerState<ChatRequestOverlay>
|
||||
SensitivityBadge(sensitivity: data.topicSensitivity, fontSize: 12),
|
||||
],
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'Seorang customer ingin curhat denganmu.',
|
||||
style: TextStyle(fontSize: 14, color: Colors.grey),
|
||||
Text(
|
||||
subtitleText,
|
||||
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
||||
),
|
||||
if (showCountdown) ...[
|
||||
const SizedBox(height: 12),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.orange.shade50,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: Colors.orange.shade200),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.timer_outlined, size: 16, color: Colors.orange.shade800),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
'Konfirmasi dalam ${_secondsRemaining}s',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.orange.shade800,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 20),
|
||||
Row(
|
||||
children: [
|
||||
|
||||
@@ -74,6 +74,19 @@ enum TopicSensitivity {
|
||||
values.firstWhere((e) => e.value == v, orElse: () => TopicSensitivity.regular);
|
||||
}
|
||||
|
||||
/// Pairing request type — distinguishes general blast from targeted
|
||||
/// "Curhat lagi" returning-chat requests.
|
||||
enum PairingRequestType {
|
||||
general('general'),
|
||||
returning('returning');
|
||||
|
||||
final String value;
|
||||
const PairingRequestType(this.value);
|
||||
|
||||
static PairingRequestType fromString(String? v) =>
|
||||
values.firstWhere((e) => e.value == v, orElse: () => PairingRequestType.general);
|
||||
}
|
||||
|
||||
/// WebSocket message types
|
||||
class WsMessage {
|
||||
// Auth
|
||||
|
||||
@@ -459,6 +459,10 @@ class _MitraChatScreenState extends ConsumerState<MitraChatScreen> {
|
||||
final isResponding = extState is ExtensionRespondingData;
|
||||
final topic = TopicSensitivity.fromString(request['topic_sensitivity'] as String?);
|
||||
final isSensitive = topic == TopicSensitivity.sensitive;
|
||||
// Extensions auto-approve on mitra non-response (server-side, with connectivity
|
||||
// safeguards). Surface the configured timeout to the mitra so they know what
|
||||
// "no response" means in this card.
|
||||
final timeoutSeconds = request['timeout_seconds'] as int?;
|
||||
|
||||
return Container(
|
||||
color: isSensitive ? SensitivityTheme.sensitive.bgTint : null,
|
||||
@@ -477,6 +481,18 @@ class _MitraChatScreenState extends ConsumerState<MitraChatScreen> {
|
||||
],
|
||||
const SizedBox(height: 8),
|
||||
Text('Customer ingin perpanjang $duration menit', textAlign: TextAlign.center),
|
||||
if (timeoutSeconds != null) ...[
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'Tidak menjawab dalam $timeoutSeconds detik = otomatis disetujui',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: Colors.grey.shade700,
|
||||
fontStyle: FontStyle.italic,
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 24),
|
||||
if (isResponding)
|
||||
const CircularProgressIndicator()
|
||||
|
||||
Reference in New Issue
Block a user