changePin method

Future<ChangePinResult> changePin({
  1. required String userId,
  2. required String currentPin,
  3. required String newPin,
  4. required String businessId,
  5. String population = 'External',
})

Changes the user's existing PIN.

Requires the current PIN for verification.

Implementation

Future<ChangePinResult> changePin({
  required String userId,
  required String currentPin,
  required String newPin,
  required String businessId,
  String population = 'External',
}) async {
  final trace = await monitoringService.createTrace(name: 'changePin', operation: 'PIN_CHANGE');

  try {
    _ensureInitialised();

    final response = await client.post(
      url: '$_iamServiceUrl/iam/api/v1/iam/pin/change',
      requestData: {
        'userId': userId,
        'currentPin': currentPin,
        'newPin': newPin,
        'businessId': businessId,
        'population': population,
      },
      user: _user,
    );

    if (response?.statusCode == 200) {
      logger.info(this, 'PIN changed successfully for user $userId');
      return ChangePinResult(success: true, error: null);
    }

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