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 of ConsentCreateRequest containing the necessary fields such as status, category, provision, and organizationId.
    • category values include:
      TOS PROA_ATTESTATION DATA_SHARING COMMUNICATION_PREFERENCES_PHI IAS_IMPORT_RECORDS
    • status values include:
      DRAFT PROPOSED ACTIVE REJECTED INACTIVE ENTERED_IN_ERROR
    • provision values include:
      PERMIT DENY
    • organizationId is an optional parameter, only required when category is PROA_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

  1. Consent Creation Process:

    • BWellSdk.user?.createConsent(request): Attempts to create a new consent record using the provided ConsentCreateRequest object.
  2. ConsentCreateRequest Builder:

    • A builder pattern is used to construct a ConsentCreateRequest object, 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., TOS for Terms of Service).
  3. 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.
  4. Flow Usage:

    • The method updateUserConsent returns a Flow<BWellResult<Consent>?>, facilitating asynchronous operation and real-time data handling.

Best Practices

  • Validating Input: Ensure that the fields set in ConsentCreateRequest are validated for correctness before calling the createConsent method.
  • 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_PHI consent 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.