Skip to content

Create-subscription

sequenceDiagram
    title Create subscription
    actor AppUser
    participant PhonePlanApi
    participant PhonePlanService
    participant PhonePlanDB
    participant TransactionService
    participant GigsApi

    AppUser ->> PhonePlanApi : Create subscription
    PhonePlanApi ->> PhonePlanService : Create subscription
    PhonePlanService ->> PhonePlanService : Validate request (and check displayed price against plan)
rect rgb(238, 225, 255)
    PhonePlanService ->> PhonePlanDb : Get User by UserId
    PhonePlanDb ->> PhonePlanService : Response
    alt If user doesn't exists
        break
            PhonePlanService ->> PhonePlanApi : Error
            PhonePlanApi ->> AppUser : Error
        end
    else If user not active
        break
            PhonePlanService ->> PhonePlanApi : Error
            PhonePlanApi ->> AppUser : Error
        end
    end
end
    PhonePlanService ->> PhonePlanService : Check that user has verified e-mail
rect rgb(225, 238, 255)
    PhonePlanService ->> PhonePlanDB : Get existing subscriptions by UserId
    PhonePlanDB ->> PhonePlanService : Response
    alt If subscriptions exist
        alt If subscription exists with status Pending
            PhonePlanService ->> PhonePlanApi : Response
            PhonePlanApi ->> AppUser : Response
        else If subscription exists with status Created
            PhonePlanService ->> PhonePlanDb : Update existing subscription (in case plan selection changed)
        else If All subscriptions have status Inactive | Ended | Failed
            PhonePlanService ->> PhonePlanDb : Create new subscription for user
        else If All subscriptions have status Active
            break
                PhonePlanService ->> PhonePlanApi : Error
                PhonePlanApi ->> AppUser : Error
            end
        end
    else If not subscriptions exist
        PhonePlanService ->> PhonePlanDb : Create subscription for user
    end
end
rect rgb(238, 225, 255)
    PhonePlanService ->> PhonePlanDB : Get existing subscription schedule by SubscriptionId
    PhonePlanDB ->> PhonePlanService : Response
    alt If subscription schedule exists
        PhonePlanService ->> PhonePlanDb : Update subscription schedule for subscription
    else If not subscription schedule exist
        PhonePlanService ->> PhonePlanDb : Create subscription schedule for subscription
    end
end
rect rgb(225, 238, 255)
    PhonePlanService ->> TransactionService : ReserveFunds
    TransactionService ->> PhonePlanService : Response
    alt If PreAuth fails
        break
            PhonePlanService ->> PhonePlanApi : Payment Error
            PhonePlanApi ->> AppUser : Payment Error
        end
    end
end
rect rgb(238, 225, 255)
    alt If PhonePlan User lacks Gigs UserId
        PhonePlanService ->> GigsApi : Search for existing user by e-mail
        GigsApi ->> PhonePlanService : Response
        alt If Gigs user not exists
            PhonePlanService ->> GigsApi : Create user
        end
        PhonePlanService ->> PhonePlanDb : Update user with Gigs User Id
    end
    break On error
        PhonePlanService ->> TransactionService: ReleaseFunds
        PhonePlanService ->> PhonePlanApi : Error
        PhonePlanApi ->> AppUser : Error
    end
end
rect rgb(225, 238, 255)
    PhonePlanService ->> GigsApi : List user addresses
    GigsApi ->> PhonePlanService : Response
    alt If Gigs user address not exists
        PhonePlanService ->> GigsApi : Create user address
    end
    break On error
        PhonePlanService ->> TransactionService: ReleaseFunds
        PhonePlanService ->> PhonePlanApi : Error
        PhonePlanApi ->> AppUser : Error
    end
end
rect rgb(238, 225, 255)
    alt If Subscription lacks Gigs SubscriptionId
        PhonePlanService ->> GigsApi : Get subscriptions by Gigs User Id 
        GigsApi ->> PhonePlanService : Response
        alt If pending Gigs subscription not exists
            PhonePlanService ->> GigsApi : Create subscription
        end
        PhonePlanService ->> PhonePlanDb : Update subscription with pending Gigs Subscription Id
    end
    break On error
        PhonePlanService ->> TransactionService: ReleaseFunds
        PhonePlanService ->> PhonePlanApi : Error
        PhonePlanApi ->> AppUser : Error
    end
end
    PhonePlanService ->> PhonePlanApi : Response
    PhonePlanApi ->> AppUser : Response