Phase 3.3: topic sensitivity + Phase 3.4: auth foundation

Phase 3.3 — Session Topic Sensitivity (complete):
- Backend: topic_sensitivity column + session_sensitivity_log, sensitivity service
  (flip with one-way-latch + audit), PATCH /api/shared/chat/sessions/:id/topic,
  topic carried in pairing + extension WS payloads, CC filter + sensitive stats
  + per-mitra sensitive columns on activity page
- client_app: TopicSelectionBottomSheet before pricing, topic flows through
  pairing request, silent WS handler for session_topic_updated
- mitra_app: SensitivityBadge + SensitivityTheme + sensitivityConfigProvider,
  overlay badge + yellow accent, chat screen app-bar toggle with configurable
  confirmation + latch, extension card shows current flag, history + transcript
  yellow theme
- control_center: Sensitivitas Topik settings section, topic filter + column
  with inline audit log, sensitive stats dashboard card, mitra activity
  sensitive columns with QC flag

Phase 3.4 — Self-Managed Auth (foundation only):
- Migration: auth_sessions + otp_requests tables, social identity columns on
  customers, password_hash + lockout on control_center_users, OTP + CC lockout
  app_config keys
- New services: password (bcrypt + complexity), token (JWT HS256 + refresh
  rotation, session_id claim pre-wires future Valkey revocation),
  social-identity (Google + Apple JWKS), OTP (Fazpass stub — real API TBD)
- Constants: AuthProvider + OtpChannel
- Middleware, auth route rewrites, WS auth update, Firebase → FCM isolation
  still pending (next chunk); Fazpass docs + Apple Developer setup still
  required before E2E testing

Docs:
- requirement/phase3.3.md, phase3.3-plan.md, phase3.3-testing.md
- requirement/phase3.4.md, phase3.4-plan.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-24 10:15:12 +08:00
parent 97d50a8e08
commit 780cade3db
44 changed files with 3834 additions and 103 deletions

View File

@@ -35,11 +35,13 @@ class ChatRequestIncomingData extends ChatRequestData {
final String sessionId;
final int? durationMinutes;
final bool? isFreeTrial;
final TopicSensitivity topicSensitivity;
final DateTime? createdAt;
const ChatRequestIncomingData(
this.sessionId, {
this.durationMinutes,
this.isFreeTrial,
this.topicSensitivity = TopicSensitivity.regular,
this.createdAt,
});
}
@@ -99,6 +101,7 @@ class ChatRequest extends _$ChatRequest {
'session_id': sessionId,
'duration_minutes': r['duration_minutes'],
'is_free_trial': r['is_free_trial'],
'topic_sensitivity': r['topic_sensitivity'],
'created_at': r['created_at'],
};
@@ -111,6 +114,7 @@ class ChatRequest extends _$ChatRequest {
sessionId,
durationMinutes: r['duration_minutes'] as int?,
isFreeTrial: r['is_free_trial'] as bool?,
topicSensitivity: TopicSensitivity.fromString(r['topic_sensitivity'] as String?),
createdAt: r['created_at'] != null
? DateTime.tryParse(r['created_at'] as String)
: null,
@@ -200,6 +204,7 @@ class ChatRequest extends _$ChatRequest {
sessionId,
durationMinutes: data['duration_minutes'] as int?,
isFreeTrial: data['is_free_trial'] as bool?,
topicSensitivity: TopicSensitivity.fromString(data['topic_sensitivity'] as String?),
createdAt: data['created_at'] != null
? DateTime.tryParse(data['created_at'] as String)
: null,
@@ -279,6 +284,7 @@ class ChatRequest extends _$ChatRequest {
sessionId,
durationMinutes: next['duration_minutes'] as int?,
isFreeTrial: next['is_free_trial'] as bool?,
topicSensitivity: TopicSensitivity.fromString(next['topic_sensitivity'] as String?),
createdAt: next['created_at'] != null
? DateTime.tryParse(next['created_at'] as String)
: null,

View File

@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
@@ -29,6 +30,7 @@ class MitraChatConnectedData extends MitraChatData {
final bool sessionExpired;
final bool sessionClosing;
final Map<String, dynamic>? extensionRequest;
final TopicSensitivity topicSensitivity;
const MitraChatConnectedData({
required this.messages,
@@ -37,6 +39,7 @@ class MitraChatConnectedData extends MitraChatData {
this.sessionExpired = false,
this.sessionClosing = false,
this.extensionRequest,
this.topicSensitivity = TopicSensitivity.regular,
});
MitraChatConnectedData copyWith({
@@ -47,6 +50,7 @@ class MitraChatConnectedData extends MitraChatData {
bool? sessionClosing,
Map<String, dynamic>? extensionRequest,
bool clearExtensionRequest = false,
TopicSensitivity? topicSensitivity,
}) {
return MitraChatConnectedData(
messages: messages ?? this.messages,
@@ -55,6 +59,7 @@ class MitraChatConnectedData extends MitraChatData {
sessionExpired: sessionExpired ?? this.sessionExpired,
sessionClosing: sessionClosing ?? this.sessionClosing,
extensionRequest: clearExtensionRequest ? null : (extensionRequest ?? this.extensionRequest),
topicSensitivity: topicSensitivity ?? this.topicSensitivity,
);
}
}
@@ -119,6 +124,7 @@ class MitraChat extends _$MitraChat {
}
final isClosing = sessionStatus == SessionStatus.closing;
final sessionTopic = TopicSensitivity.fromString(sessionData?['topic_sensitivity'] as String?);
final response = await _apiClient.get('/api/shared/chat/$sessionId/messages');
final messagesData = response['data'] as List<dynamic>;
@@ -153,7 +159,7 @@ class MitraChat extends _$MitraChat {
'session_id': sessionId,
}));
state = MitraChatConnectedData(messages: messages, sessionClosing: isClosing);
state = MitraChatConnectedData(messages: messages, sessionClosing: isClosing, topicSensitivity: sessionTopic);
} catch (e) {
state = const MitraChatErrorData('Gagal terhubung ke chat.');
}
@@ -288,6 +294,31 @@ class MitraChat extends _$MitraChat {
case WsMessage.sessionCompleted:
_cleanup();
break;
case WsMessage.sessionTopicUpdated:
final newValue = TopicSensitivity.fromString(data['topic_sensitivity'] as String?);
state = current.copyWith(topicSensitivity: newValue);
break;
}
}
/// Flip the session's topic sensitivity. Returns error code on failure, null on success.
Future<String?> flipTopic(String sessionId, TopicSensitivity toValue) async {
try {
final response = await _apiClient.patch(
'/api/shared/chat/sessions/$sessionId/topic',
data: {'topic_sensitivity': toValue.value},
);
final updated = TopicSensitivity.fromString(response['data']?['topic_sensitivity'] as String?);
final current = state;
if (current is MitraChatConnectedData) {
state = current.copyWith(topicSensitivity: updated);
}
return null;
} on DioException catch (e) {
return e.response?.data?['error']?['code'] as String? ?? 'FLIP_FAILED';
} catch (_) {
return 'FLIP_FAILED';
}
}

View File

@@ -0,0 +1,31 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../api/api_client_provider.dart';
class SensitivityConfig {
final bool flipConfirmationEnabled;
final bool oneWayLatch;
const SensitivityConfig({
required this.flipConfirmationEnabled,
required this.oneWayLatch,
});
static const defaults = SensitivityConfig(
flipConfirmationEnabled: true,
oneWayLatch: false,
);
}
/// Fetches the sensitivity config once and caches it for the app lifetime.
/// Falls back to defaults if the request fails.
final sensitivityConfigProvider = FutureProvider<SensitivityConfig>((ref) async {
try {
final response = await ref.read(apiClientProvider).get('/api/shared/sensitivity');
final data = response['data'] as Map<String, dynamic>;
return SensitivityConfig(
flipConfirmationEnabled: data['flip_confirmation_enabled'] as bool? ?? true,
oneWayLatch: data['one_way_latch'] as bool? ?? false,
);
} catch (_) {
return SensitivityConfig.defaults;
}
});

View File

@@ -1,7 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../chat_request_notifier.dart';
import '../../constants.dart';
import '../../../router.dart';
import 'sensitivity_badge.dart';
import 'sensitivity_theme.dart';
class ChatRequestOverlay extends ConsumerStatefulWidget {
final Widget child;
@@ -132,12 +135,17 @@ class _ChatRequestOverlayState extends ConsumerState<ChatRequestOverlay>
: data.durationMinutes != null
? '${data.durationMinutes} Menit'
: '';
final isSensitive = data.topicSensitivity == TopicSensitivity.sensitive;
final theme = SensitivityTheme.of(data.topicSensitivity);
return Container(
decoration: const BoxDecoration(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
boxShadow: [BoxShadow(color: Colors.black26, blurRadius: 10, offset: Offset(0, -2))],
borderRadius: const BorderRadius.vertical(top: Radius.circular(20)),
border: isSensitive
? Border(top: BorderSide(color: theme.badgeBg, width: 4))
: null,
boxShadow: const [BoxShadow(color: Colors.black26, blurRadius: 10, offset: Offset(0, -2))],
),
child: SafeArea(
top: false,
@@ -168,6 +176,10 @@ class _ChatRequestOverlayState extends ConsumerState<ChatRequestOverlay>
'Durasi: $durationText',
style: const TextStyle(fontSize: 14, color: Colors.grey),
),
if (isSensitive) ...[
const SizedBox(height: 8),
SensitivityBadge(sensitivity: data.topicSensitivity, fontSize: 12),
],
const SizedBox(height: 8),
const Text(
'Seorang customer ingin curhat denganmu.',

View File

@@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import '../../constants.dart';
import 'sensitivity_theme.dart';
/// Small pill-style badge used across the mitra app to indicate a sensitive session.
/// Renders nothing for regular sessions.
class SensitivityBadge extends StatelessWidget {
final TopicSensitivity sensitivity;
final double fontSize;
final EdgeInsets padding;
const SensitivityBadge({
super.key,
required this.sensitivity,
this.fontSize = 11,
this.padding = const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
});
@override
Widget build(BuildContext context) {
if (sensitivity != TopicSensitivity.sensitive) return const SizedBox.shrink();
const theme = SensitivityTheme.sensitive;
return Container(
padding: padding,
decoration: BoxDecoration(
color: theme.badgeBg,
borderRadius: BorderRadius.circular(999),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.warning_amber_rounded, size: fontSize + 2, color: theme.badgeFg),
const SizedBox(width: 4),
Text(
'Topik sensitif',
style: TextStyle(
color: theme.badgeFg,
fontSize: fontSize,
fontWeight: FontWeight.w600,
),
),
],
),
);
}
}

View File

@@ -0,0 +1,38 @@
import 'package:flutter/material.dart';
import '../../constants.dart';
/// Color tokens used by the mitra chat UI to differentiate regular vs sensitive sessions.
class SensitivityTheme {
final Color bgTint;
final Color accent;
final Color banner;
final Color badgeBg;
final Color badgeFg;
const SensitivityTheme({
required this.bgTint,
required this.accent,
required this.banner,
required this.badgeBg,
required this.badgeFg,
});
static const regular = SensitivityTheme(
bgTint: Color(0xFFF5D0D6),
accent: Color(0xFFBE7C8A),
banner: Color(0xFFC4868F),
badgeBg: Color(0xFFBE7C8A),
badgeFg: Colors.white,
);
static const sensitive = SensitivityTheme(
bgTint: Color(0xFFFFE8A3),
accent: Color(0xFFB88900),
banner: Color(0xFFE0A500),
badgeBg: Color(0xFFF7B500),
badgeFg: Color(0xFF3D2A00),
);
static SensitivityTheme of(TopicSensitivity s) =>
s == TopicSensitivity.sensitive ? sensitive : regular;
}