resetPin method

Future<ResetPinResult> resetPin({
  1. required String userId,
  2. required String newPin,
  3. required String businessId,
  4. String population = 'External',
})

Resets the user's PIN (self-service).

Does NOT require the current PIN — user is authenticated via Bearer token. This also clears a locked PIN state.

Implementation

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

  try {
    _ensureInitialised();

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

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

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