%% Bill Creation
sequenceDiagram
    %%  participant EventSubscriberFactory
    participant EventManager
    participant ReferralBonusSubscriber
    participant ReferralBonusService
    participant ReferralMasterService
    participant ConfigurationService
    participant WorklistTaskService
    participant AuditService

    %% Subscription
     %% EventSubscriberFactory->>EventManager: subscribeEvents(eventTypes, new ReferralBonusSubscriber())
    
    %% Event Raised
    EventManager->>EventManager: _raiseEvent(EventType eventType)
    EventManager->>ReferralBonusSubscriber: handleEvent(serviceSession, event)

    %% Handle Event
    ReferralBonusSubscriber->>ReferralBonusSubscriber: handleEvent(event)
    ReferralBonusSubscriber->>ConfigurationService: isReferralBonusOpted(entityId)
    alt Entity has opted for referral bonus
        ConfigurationService-->>ReferralBonusSubscriber: true
        ReferralBonusSubscriber->>ReferralBonusService: processReferralBonus(bill)
        ReferralBonusService->>AuditService: audit("Referral bonus processing started")
        ReferralBonusService->>ReferralMasterService: getReferralDetails(bill.getReferralId())
        ReferralMasterService-->>ReferralBonusService: referral

        alt Referral is null or inactive
            ReferralBonusService-->>ReferralBonusSubscriber: return
        else
            loop charges in bill
                ReferralBonusService->>ReferralBonusService: calculateChargeBonus(charge)
                ReferralBonusService-->>ReferralBonusService: bonusAmount
                alt bonusAmount > 0
                    ReferralBonusService->>ReferralBonusService: createBonus(referral.getId(), bill.getId(), bonusAmount)
                end
            end
            ReferralBonusService->>ReferralBonusService: createReferralBonusRequest(bill)
            ReferralBonusService->>ConfigurationService: getApprovalPolicy(referral.getEntityId())
            ConfigurationService-->>ReferralBonusService: approvalPolicy

            ReferralBonusService->>AuditService: audit("Referral bonus request created")
            ReferralBonusService->>WorklistTaskService: createWorklistTask(referralBonusRequest, approvalPolicy)
        end
    else
        ConfigurationService-->>ReferralBonusSubscriber: false
        ReferralBonusSubscriber-->>EventManager: return
    end
sequenceDiagram
    participant WorklistTaskService
    participant ReferralBonusService
    participant ConfigurationService
    participant AuditService

    %% Approval/Rejection Process
    WorklistTaskService->>ReferralBonusService: approveReferralBonusRequest(requestId)
    ReferralBonusService->>AuditService: audit("Referral bonus request approval started")
    ReferralBonusService->>ReferralBonusService: findReferralRequestById(requestId)
    ReferralBonusService-->>ReferralBonusService: referralBonusRequest

    alt referralBonusRequest exists
        loop bonuses in referralBonusRequest
            alt approval policy is CHILD_LEVEL
                ReferralBonusService->>ReferralBonusService: updateBonusStatus(bonusId, "CONFIRMED_CHILD_APPROVAL")
            else approval policy is CORPORATE_LEVEL
                ReferralBonusService->>ReferralBonusService: updateBonusStatus(bonusId, "CONFIRMED_CORPORATE_APPROVAL")
            else approval policy is BOTH
                ReferralBonusService->>ReferralBonusService: updateBonusStatus(bonusId, "CONFIRMED_CORPORATE_APPROVAL")
            end
        end
        ReferralBonusService->>ConfigurationService: isCorporateApprovalRequired(requestId)
        ConfigurationService-->>ReferralBonusService: required

        alt required
            ReferralBonusService->>WorklistTaskService: createWorklistTask(requestId, "REFERRAL_PENDING_CORPORATE")
            ReferralBonusService->>ReferralBonusService: updateReferralRequestStatus(requestId, "REFERRAL_PENDING_CORPORATE")
        else
            ReferralBonusService->>ReferralBonusService: updateReferralRequestStatus(requestId, "APPROVED")
        end

        ReferralBonusService->>AuditService: audit("Referral bonus request approved")
    else
        ReferralBonusService-->>WorklistTaskService: return
    end

    alt referral request rejected
        WorklistTaskService->>ReferralBonusService: rejectReferralRequest(requestId)
        ReferralBonusService->>AuditService: audit("Referral bonus request rejection started")
        ReferralBonusService->>ReferralBonusService: updateReferralRequestStatus(requestId, "REFERRAL_REJECTED")
        ReferralBonusService->>AuditService: audit("Referral bonus request rejected")
    end


sequenceDiagram
      %% participant EventSubscriberFactory
    participant EventManager
    participant ReferralBonusSubscriber
    participant ReferralBonusService
    participant ConfigurationService
    participant ReferralMasterService
    participant AuditService

    %% Subscription
     %% EventSubscriberFactory->>EventManager: subscribeEvents(eventTypes, new ReferralBonusSubscriber())
    
    %% Event Raised
    EventManager->>EventManager: _raiseEvent(EventType.BILL_CANCELLED)
    EventManager->>ReferralBonusSubscriber: handleEvent(serviceSession, event)

    %% Handle Event
    ReferralBonusSubscriber->>ReferralBonusSubscriber: handleEvent(event)
    ReferralBonusSubscriber->>ConfigurationService: isReferralBonusOpted(entityId)
    alt Entity has opted for referral bonus
        ConfigurationService-->>ReferralBonusSubscriber: true
        ReferralBonusSubscriber->>ReferralBonusService: processReferralBonusCancellation(bill)
        ReferralBonusService->>AuditService: audit("Referral bonus cancellation started")
        ReferralBonusService->>ReferralMasterService: getReferralDetails(bill.getReferralId())
        ReferralMasterService-->>ReferralBonusService: referral

        alt Referral is null or inactive
            ReferralBonusService-->>ReferralBonusSubscriber: return
        else
            ReferralBonusService->>ReferralBonusService: findReferralBonusRequest(bill.getId())
            alt Referral bonus request exists
                alt Referral bonus request is pending approval
                    ReferralBonusService->>ReferralBonusService: cancelReferralBonusRequest()
                    ReferralBonusService->>AuditService: audit("Referral bonus request cancelled")
                else Referral bonus payment has been paid
                    ReferralBonusService->>ReferralBonusService: createNegativeReferralBonus()
                    ReferralBonusService->>AuditService: audit("Negative referral bonus created")
                else Referral bonus is not paid
                    ReferralBonusService->>ReferralBonusService: cancelReferralBonusRequest()
                    ReferralBonusService->>AuditService: audit("Referral bonus request cancelled")
                end
            else
                ReferralBonusService-->>ReferralBonusService: No action needed
            end
        end
    else
        ConfigurationService-->>ReferralBonusSubscriber: false
        ReferralBonusSubscriber-->>EventManager: return
    end


These diagrams went for the review
These are the suggestions/ changes suggested

ReferralBonusSubscriber should be responsible to build the complete object of the Bill with the refrral details or whatever required by the ReferralBonusService by calling other funcitons within this class or other classes and then the final constructed object with all required information will be passed to the ReferralBonusService.

Referral bonus service will just then do some highlevel things like calling and saving the referral bonus request in the worklist , updating the status of that referral bonus request, creating bonus request etc..

And we need to have one more file called ReferralBonusWorkflowManager, which should be responsible for handling all the worklist related tasks and only this file should communicate to the common generic service called "TaskService".
TaskService is the common and generic class which will take care of the creation, updation, marking the tasks with their status etc...

ReferralBonusWorkflowManager is reponsible for every worklist task related things like adding to worklist , once it is approved or reject what to do, to check the configurations for the corporate entity approval etc... will be handled only by this manager.
ReferralBonusWorkflowManager usually will have the funcitons like handleReferralbonusRequestCreation, approve referralbonusrequest, reject referralrequestbonusbonus
So that means in the application , when any action performs on the work list of the referral bonus request entry, then the task service will call the ReferralBonusWorkflowManager with the action code (eg.., APPROVE, REJECT) then the ReferralBonusWorkflowManager will decide what to do based on the action and the configuration.
For example, the action is APPROVE that means the referral bonus request is approved then ReferralBonusWorkflowManager will update its status and will check whether that particular entity has a coporate or not , if it has a coporate then ReferralBonusWorkflowManager will call the ConfigurationService to get the configurations and check whether the coporate approval is required or not.
If required it will initiate a call to the TaskService to create one more entry for the same request in the coporate entity.
So ultimately it will check what should happen based on the state and status of the request , what will happen and what should be the next status and state.