import { BaseService } from "../../base"; import { AbhaSteps } from "./abha.enums"; import { ClientConfig } from "../../base"; import { Utilities } from "./utilities"; /** * ### ABHA Creation Workflow Service * * This service orchestrates the multi-step Ayushman Bharat Health Account (ABHA) creation workflow. * It functions as a state machine, guiding the user through a series of steps from Aadhaar OTP generation * to the final ABHA address creation. * * ### Core Responsibilities: * * - **State Management**: Manages the user's progress through the registration flow using the `AbhaSteps` enum. * - **Data Validation**: Ensures that the data provided at each step is valid. * - **Data Encryption**: Encrypts sensitive information like Aadhaar numbers and OTPs before sending them to the ABHA APIs, as per security requirements. * - **API Interaction**: Communicates with the backend ABHA services for each step of the process. * * The primary entry point is the createAbha method, which dispatches requests to the appropriate handler based on the current step. * */ export declare class AbhaService extends BaseService { constructor(config: ClientConfig); private readonly logger; utilities: Utilities; /** * Initiates or continues the multi-step ABHA creation flow. * * This function serves as the single entry point for the entire ABHA registration process. * It orchestrates the various stages by delegating to internal handlers based on the provided 'step'. * The client should sequentially call this function, passing the output from one step as * input to the next, guided by the `nextStep` field in the response. * * ### Steps Involved: * * 1. **RegisterWithAadhaar**: * Sends an OTP to the Aadhaar-linked mobile number. * - **Input**: `{ aadhaar: string }` * - **Next Step**: `VerifyAadhaarOtp` * * 2. **VerifyAadhaarOtp**: * Verifies the OTP received on the Aadhaar-linked mobile. * - **Input**: `{ otp: string, txnId: string, mobile?: string }` * - **Next Step**: * - If Aadhaar has a linked mobile → `GetAbhaAddressSuggestions` * - If Aadhaar is not linked to mobile → `UpdateMobile` * * 3. **UpdateMobile**: * Sends OTP to the mobile number entered by the user (used when Aadhaar has no mobile linked). * - **Input**: `{ txnId: string, mobile: string }` * - **Next Step**: `VerifyUpdateMobileOtp` * * 4. **VerifyUpdateMobileOtp**: * Verifies the OTP sent to the mobile number entered in the previous step. * - **Input**: `{ otp: string, txnId: string }` * - **Next Step**: `GetAbhaAddressSuggestions` * * 5. **GetAbhaAddressSuggestions**: * Retrieves suggested ABHA addresses for the user to choose from. * - **Input**: `{ txnId: string }` * - **Next Step**: `FinalRegister` * * 6. **FinalRegister**: * Final step where the user selects an ABHA address and completes the registration. * - **Input**: `{ txnId: string, abhaAddress: string }` * - **Next Step**: `null` (workflow complete) * * @param {AbhaSteps | string} step - The current step in the ABHA creation flow. * @param {Record} payload - The data payload required for the current step. * @returns {Promise} A promise that resolves to a structured response object. * @throws {ValidationError | Error} Throws a `ValidationError` if the payload fails validation, or a generic `Error` if an operation fails. * * @example * // Step 1: Register With Aadhaar * // ----------------------------- * // Expected Input: * const requestAadhaarOtpPayload = { aadhaar: '123456789012' }; * const aadhaarOtpResponse = await patient.abha.createAbha('RegisterWithAadhaar', requestAadhaarOtpPayload); * * // Expected Output: * { * "success": true, * "message": "OTP sent successfully...", * "response": { * "txnId": "a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6" * }, * "nextStep": "VerifyAadhaarOtp", * "nextStepHint": "Please enter the OTP..." * } * * * // Step 2: Verify Aadhaar OTP * // -------------------------- * // Expected Input: * const verifyAadhaarOtpPayload = { * otp: '123456', * txnId: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6', * mobile: '9876543210' * }; * const aadhaarVerificationResponse = await patient.abha.createAbha('VerifyAadhaarOtp', verifyAadhaarOtpPayload); * * // Expected Output: * { * "success": true, * "message": "Aadhaar OTP verified successfully.", * "response": { * "ABHAProfile": { "mobile": "9876543210", "...": "..." } * }, * "nextStep": "GetAbhaAddressSuggestions", * "nextStepHint": "Mobile number is already verified. Proceed to select ABHA address." * } * * * // Step 3: Update Mobile * // --------------------- * // Expected Input: * const UpdateMobileOtpRequestDto = { * txnId: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6', * mobile: '9876543210' * }; * const updateMobileOtpResponse = await patient.abha.createAbha('UpdateMobile', UpdateMobileOtpRequestDto); * * // Expected Output: * { * "success": true, * "message": "OTP sent to your new mobile number..", * "response": { * "ABHAProfile": { "mobile": "9876543210", "...": "..." } * }, * "nextStep": "GetAbhaAddressSuggestions", * "nextStepHint": "Please enter the OTP sent to your new mobile number" * } * * * // Step 4: Verify Mobile OTP * // ------------------------- * // Expected Input: * const VerifyUpdateMobileOtpBodyDto = { * otp: '123456', * txnId: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6', * }; * const aadhaarVerificationResponse = await patient.abha.createAbha('VerifyUpdateMobileOtp', VerifyUpdateMobileOtpBodyDto); * * // Expected Output: * { * "success": true, * "message": "Mobile number updated successfully", * "response": { * "ABHAProfile": { "mobile": "9876543210", "...": "..." } * }, * "nextStep": "GetAbhaAddressSuggestions", * "nextStepHint": "Now, select an address from the suggestions." * } * * * // Step 5: Get ABHA Address Suggestions * // ------------------------------------ * // Expected Input: * const addressSuggestionsRequestPayload = { txnId: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6' }; * const addressSuggestionsResponse = await patient.abha.createAbha('GetAbhaAddressSuggestions', addressSuggestionsRequestPayload); * * // Expected Output: * { * "success": true, * "message": "Here are your ABHA address suggestions.", * "response": { * "abhaAddressList": ["user.2025", "user.01", "user.123"] * }, * "nextStep": "FinalRegister", * "nextStepHint": "Select an address and proceed to final registration." * } * * * // Step 6: Final Register * // ---------------------- * // Expected Input: * const createAbhaAddressPayload = { * abhaAddress: 'user.2025', * txnId: 'a1b2c3d4-e5f6-g7h8-i9j0-k1l2m3n4o5p6' * }; * const finalRegistrationResponse = await patient.abha.createAbha('FinalRegister', createAbhaAddressPayload); * * // Expected Output: * { * "success": true, * "message": "ABHA registered successfully.", * "response": { * "healthIdNumber": "91-1234-5678-9012", * "preferredAbhaAddress": "user.2025@sbx" * }, * "nextStep": null, * "nextStepHint": null * } */ createAbha(step: AbhaSteps, payload: Record): Promise; createAbhaFlow(step: AbhaSteps | string, payload: Record): Promise>; /** * Initiates OTP request for Aadhaar-based registration. * * Steps performed: * 1. Validates the Aadhaar number input. * 2. Encrypts the Aadhaar number before transmission. * 3. Sends a request to the ABHA system to generate and send an OTP. * * @param {AadhaarOtpRequestDto} payload - The request payload including: * - aadhaar - Aadhaar number (mandatory) * * @returns {Promise} - Structured response including: * - success - whether OTP was sent successfully * - message - result message * - response - response * - nextStep - VerifyAadhaarOtp * - nextStepHint - instruction to proceed with OTP verification using txnId and mobile * * @throws {ValidationError} - If payload validation fails */ private _handleRegisterWithAadhaar; /** * Verifies OTP for Aadhaar-based registration. * * Steps performed: * 1. Validates the OTP, txnId, and mobile fields. * 2. Encrypts the OTP before transmission. * 3. Sends a request to the ABHA system to verify the OTP and initiate Aadhaar enrollment. * 4. Compares mobile number in request and response to determine the next step. * * @param {VerifyAadhaarOtpBodyDto} payload - The request payload including: * - otp - One-Time Password received on registered mobile (mandatory) * - txnId - Transaction ID received during Aadhaar OTP generation (mandatory) * - mobile - Mobile number used during Aadhaar OTP generation (mandatory) * * @returns {Promise} - Structured response including: * - success - whether OTP verification was successful * - message - result message * - response - response * - nextStep - GetAbhaAddressSuggestions or UpdateMobile * - nextStepHint - instruction to either choose ABHA address or update mobile number * * @throws {ValidationError} - If payload validation fails */ private _handleVerifyAadhaarOtp; /** * Initiates OTP request for updating mobile number in Aadhaar-based registration. * * Steps performed: * 1. Validates the input payload including txnId and new mobile number. * 2. Encrypts the new mobile number before transmission. * 3. Sends a request to the ABHA system to generate and send an OTP to the new mobile number. * * @param {UpdateMobileOtpRequestDto} payload - The request payload including: * - txnId - Transaction ID from previous Aadhaar OTP verification (mandatory) * - mobile - New mobile number to be updated (mandatory) * * @returns {Promise} - Structured response including: * - success - whether OTP was sent successfully * - message - result message * - response - response * - nextStep - VerifyUpdateMobileOtp * - nextStepHint - instruction to proceed with OTP verification for updated mobile number * * @throws {ValidationError} - If payload validation fails */ private _handleUpdateMobile; /** * Verifies OTP sent to the updated mobile number. * * Steps performed: * 1. Validates the input payload including OTP and transaction ID. * 2. Encrypts the OTP before transmission. * 3. Sends a request to the ABHA system to verify the OTP and update the mobile number. * * @param {VerifyUpdateMobileOtpBodyDto} payload - The request payload including: * - otp - OTP sent to the new mobile number (mandatory) * - txnId - Transaction ID associated with the mobile update (mandatory) * * @returns {Promise} - Structured response including: * - success - whether the mobile update was successful * - message - result message * - response - response from the ABHA system * - nextStep - GetAbhaAddressSuggestions * - nextStepHint - instruction to proceed with selecting an ABHA address * * @throws {ValidationError} - If payload validation fails */ private _handleVerifyUpdateMobileOtp; /** * Fetches ABHA address suggestions for final registration. * * Steps performed: * 1. Validates the input payload containing the transaction ID. * 2. Sends a GET request to the ABHA system to retrieve address suggestions. * * @param {AbhaAddressSuggestionsRequest} payload - The request payload including: * - txnId - Transaction ID from the previous steps (mandatory) * * @returns {Promise} - Structured response including: * - success - whether suggestions were fetched successfully * - message - result message * - response - list of suggested ABHA addresses * - nextStep - FinalRegister * - nextStepHint - instruction to proceed with final registration using the selected address * * @throws {ValidationError} - If payload validation fails */ private _handleGetAbhaAddressSuggestions; /** * Completes the final step of ABHA registration by submitting the selected ABHA address. * * Steps performed: * 1. Validates the payload containing the selected ABHA address and transaction ID. * 2. Sends the final registration request to the ABHA system. * * @param {CreateAbhaAddressRequestDto} payload - The request payload including: * - abhaAddress - Chosen ABHA address (mandatory) * - txnId - Transaction ID from previous steps (mandatory) * * @returns {Promise} - Structured response including: * - success - whether the ABHA was registered successfully * - message - result message * - response - ABHA profile or confirmation details * - nextStep - null (final step) * - nextStepHint - null (final step) * * @throws {ValidationError} - If payload validation fails */ private _handleFinalRegister; private _processStep; private extractMobileFromPayload; private extractMobileFromResponse; }