public async Task <IActionResult> CreatePlan([FromBody] DtoNewSubscriptionPlan plan) { return(await companies.FetchCompany(plan.CompanyId) .Ensure(company => company.HasValue, "Employee's company was found") .OnSuccess(company => RegisterPlanWithStripe(plan.Packages, company.Value.ExternalAccountId)) .OnSuccess(subscriptionId => plans.CreateSubscriptionPlan(new SubscriptionPlan { CompanyId = plan.CompanyId, ExternalPlanId = subscriptionId })) .OnSuccess(planId => packages.ReplaceSubscriptionPackageEntriesForPlanId(planId.Value, plan.Packages)) .OnBoth(p => p.IsFailure ? StatusCode(404) : StatusCode(201)) .ConfigureAwait(false)); }
public async Task <IActionResult> ReplacePlan([FromRoute(Name = "planId")] int planId, [FromBody] DtoNewSubscriptionPlan updatedPlan) { Company existingCompany = null; return(await companies.FetchCompany(updatedPlan.CompanyId) .Ensure(company => company.HasValue, "Employee's company was found") .OnSuccess(company => { existingCompany = company.Value; return plans.FetchSubscriptionPlan(planId); }) .Ensure(plan => plan.HasValue, "Company plan was found") .OnSuccess(plan => UpdatePlanWithStripe(updatedPlan.Packages, existingCompany.ExternalAccountId, plan.Value.ExternalPlanId)) .OnSuccess(() => packages.ReplaceSubscriptionPackageEntriesForPlanId(planId, updatedPlan.Packages)) .OnBoth(p => p.IsFailure ? StatusCode(404) : StatusCode(200)) .ConfigureAwait(false)); }