requestPinOtp method

Future<OtpRequestResult> requestPinOtp({
  1. required String userId,
})

Requests an OTP for admin PIN reset via the merchant API.

Sends a one-time code to the admin's registered email and/or phone. The OTP is verified by calling verifyPinOtp with the same userId.

Implementation

Future<OtpRequestResult> requestPinOtp({
  required String userId,
}) async {
  final trace = await monitoringService.createTrace(name: 'requestPinOtp', operation: 'PIN_OTP_REQUEST');

  try {
    _ensureInitialised();
    _ensureMerchantApiConfigured();

    final response = await client.post(
      url: '$_merchantApiUrl/PinResetOtp/Request',
      requestData: {
        'userId': userId,
      },
      user: _user,
    );

    if (response?.statusCode == 200 && response?.data != null) {
      final data = response!.data as Map<String, dynamic>;
      return OtpRequestResult(
        success: data['success'] as bool? ?? true,
        error: data['error'] as String?,
        maskedEmail: data['maskedEmail'] as String?,
        maskedPhone: data['maskedPhone'] as String?,
        channelsSent: (data['channelsSent'] as List<dynamic>?)
                ?.map((e) => e as String)
                .toList() ??
            [],
      );
    }

    return OtpRequestResult(
      success: false,
      error: response?.data?.toString() ?? UserPinsErrorCodes.otpRequestFailed.message,
    );
  } on AuthException catch (e) {
    logger.error(this, 'AuthException requesting PIN OTP: ${e.message}', error: e);
    return OtpRequestResult(success: false, error: e.message);
  } catch (e, s) {
    logger.error(this, 'Error requesting PIN OTP', error: e, stackTrace: s);
    return OtpRequestResult(
      success: false,
      error: UserPinsErrorCodes.otpRequestFailed.message,
    );
  } finally {
    trace.stop();
  }
}