submitNpsResponse method

Future<void> submitNpsResponse({
  1. required String token,
  2. required int score,
  3. String? comment,
})

Submits an NPS survey response to the server. Failures are swallowed with one retry — the thank-you screen is shown regardless of network outcome (per spec).

Implementation

Future<void> submitNpsResponse({
  required String token,
  required int score,
  String? comment,
}) async {
  assert(score >= 0 && score <= 10, 'NPS score must be 0–10');
  final storage = HiveStorageService.instance;
  final body = {
    'profileId': await storage.getProfileId(),
    'deviceId': await storage.getDeviceId(),
    'sessionId': await SessionService.instance.getSessionId(),
    'score': score,
    if (comment != null && comment.isNotEmpty) 'comment': comment,
  };

  Future<void> doPost() async {
    final response = await http.post(
      Uri.parse('${_getEndpoint()}/api/v0/nps/${Uri.encodeComponent(token)}/submissions'),
      headers: {
        'content-type': 'application/json',
        'authorization': 'Bearer $_accessToken',
      },
      body: jsonEncode(body),
    );
    if (response.statusCode != 202) {
      throw Exception(
          'NPS submit error: ${response.statusCode} - ${response.body}');
    }
  }

  try {
    await doPost();
  } catch (e) {
    // One silent retry on failure.
    try {
      await doPost();
    } catch (retryError) {
      debugPrint('[Releva] NPS submission failed (silent): $retryError');
    }
  }
}