setPin method
Sets a PIN for the user (first-time creation).
Returns CreatePinResult with success false and an error message on
HTTP 409 (PIN already exists) or any other failure.
Implementation
Future<CreatePinResult> setPin({
required String userId,
required String pin,
required String businessId,
String population = 'External',
}) async {
final trace = await monitoringService.createTrace(name: 'setPin', operation: 'PIN_SET');
try {
_ensureInitialised();
final response = await client.post(
url: '$_iamServiceUrl/iam/api/v1/iam/pin/set',
requestData: {
'userId': userId,
'pin': pin,
'businessId': businessId,
'population': population,
},
user: _user,
);
if (response?.statusCode == 200) {
logger.info(this, 'PIN set successfully for user $userId');
return CreatePinResult(success: true, error: null);
}
if (response?.statusCode == 409) {
return CreatePinResult(
success: false,
error: UserPinsErrorCodes.pinAlreadyExists.message,
);
}
return CreatePinResult(
success: false,
error: response?.data?.toString() ?? UserPinsErrorCodes.setPinFailed.message,
);
} on AuthException catch (e) {
logger.error(this, 'AuthException setting PIN: ${e.message}', error: e);
return CreatePinResult(success: false, error: e.message);
} catch (e, s) {
logger.error(this, 'Error setting PIN', error: e, stackTrace: s);
return CreatePinResult(
success: false,
error: UserPinsErrorCodes.setPinFailed.message,
);
} finally {
trace.stop();
}
}