Create User Consent Data
Creating Consents for a User
Overview
This section outlines how to create a new consent record for a user using the SDK. The createConsent method is essential for managing user consents.
Method Signature
The createConsent method is defined as follows:
suspend fun createConsent(request: ConsentCreateRequest): BWellResult<Consent>request: An instance ofConsentCreateRequestcontaining the necessary fields such asstatus,category,provision, andorganizationId.categoryvalues include:
TOSPROA_ATTESTATIONDATA_SHARINGCOMMUNICATION_PREFERENCES_PHIIAS_IMPORT_RECORDSstatusvalues include:
DRAFTPROPOSEDACTIVEREJECTEDINACTIVEENTERED_IN_ERRORprovisionvalues include:
PERMITDENYorganizationIdis an optional parameter, only required whencategoryisPROA_ATTESTATION
BWellResult<Consent>: The method returns an object representing the created consent data.
Sample Code
Here's an example demonstrating how to use the createConsent method:
val myConsentCreateRequest: ConsentCreateRequest = ConsentCreateRequest.Builder()
.status(ConsentStatus.ACTIVE)
.provision(ConsentProvisionType.PERMIT)
.category(ConsentCategoryCode.TOS)
.build()
createConsent(myConsentCreateRequest)
suspend fun updateUserConsent(request: ConsentCreateRequest): Flow<BWellResult<Consent>?> = flow {
val updateOutcome = BWellSdk.user?.createConsent(request)
emit(updateOutcome)
}Detailed Explanation
-
Consent Creation Process:
BWellSdk.user?.createConsent(request): Attempts to create a new consent record using the providedConsentCreateRequestobject.
-
ConsentCreateRequest Builder:
- A builder pattern is used to construct a
ConsentCreateRequestobject, allowing for easy setting of various consent properties:status: The status of the consent (e.g.,ACTIVE).provision: The type of consent provision (e.g.,PERMIT).category: The category code for the consent (e.g.,TOSfor Terms of Service).
- A builder pattern is used to construct a
-
Handling the Operation Outcome:
- The outcome of the consent creation (
updateOutcome) is emitted to the caller, which can be used to verify the success of the operation and access the newly created consent data.
- The outcome of the consent creation (
-
Flow Usage:
- The method
updateUserConsentreturns aFlow<BWellResult<Consent>?>, facilitating asynchronous operation and real-time data handling.
- The method
Best Practices
- Validating Input: Ensure that the fields set in
ConsentCreateRequestare validated for correctness before calling thecreateConsentmethod. - Null Safety: Utilize null-safe operations when dealing with user objects and consent data.
- Error Handling: Implement robust error handling to manage potential failures in consent creation, such as invalid data or network issues.
- User Feedback: Provide clear feedback to the user regarding the outcome of the consent creation process.
- For the
COMMUNICATION_PREFERENCES_PHIconsent type, ensure the user's communication preferences are set and confirmed, e.g., mobile push, mobile phone for SMS, and email address.
To better understand how the createConsent method is used during user onboarding, please reference this diagram.
Updated about 2 months ago
