示例#1
0
        public virtual async Task <Status> RemoveChildEnterprise(IApplicationsIoTService appIoTArch,
                                                                 IEnterprisesManagementService entMgr, IIdentityAccessService idMgr,
                                                                 string childEntLookup, string parentEntLookup)
        {
            var childEnt = State.EnterpriseConfig.ChildEnterprises.FirstOrDefault(ent =>
                                                                                  ent.Lookup == childEntLookup
                                                                                  );
            var devices = await appIoTArch.ListEnrolledDevices(childEntLookup);

            //Remove devices

            await devices.Model.Items.Each(async d => {
                await revokeDeviceEnrollment(appIoTArch, childEntLookup, d.DeviceID);
            }, parallel : true);


            //If its the active ent set active to null
            if (State.ActiveEnterpriseConfig.ActiveEnterprise != null && State.ActiveEnterpriseConfig?.ActiveEnterprise.Lookup == childEntLookup)
            {
                State.ActiveEnterpriseConfig.ActiveEnterprise = null;
            }

            var revokePassportRequest = await idMgr.RevokePassport(parentEntLookup, childEnt.Name);

            var revokeAccessCardRequest = await idMgr.RevokeAccessCard(new Host.TempRefit.RevokeAccessCardRequest()
            {
                AccessConfiguration = "LCU",
                Username            = childEnt.Name
            }, childEntLookup);

            if (revokeAccessCardRequest.Status.Code == 1)
            {
                log.LogError($"Unable to revoke access cards: {revokeAccessCardRequest.Status.Message}");
            }

            var revokeLicenceAccess = await idMgr.RevokeLicense(parentEntLookup, childEnt.Name, "iot");

            if (revokeLicenceAccess.Status.Code == 1)
            {
                log.LogError($"Unable to revoke license access: {revokeLicenceAccess.Status.Message}");
            }

            //TODO removing the API Management keys

            var cancelUserSubscription = await entMgr.CancelSubscriptionByUser(childEnt.Name, parentEntLookup, "iot");

            if (cancelUserSubscription.Status.Code == 1)
            {
                log.LogError($"Unable to cancel subscription: {cancelUserSubscription.Status.Message}");
            }

            var deleteRequest = await entMgr.DeleteEnterpriseByLookup(childEntLookup, new Host.TempRefit.DeleteEnterpriseByLookupRequest()
            {
                Password = "******"
            });

            await LoadChildEnterprises(entMgr, parentEntLookup, appIoTArch, idMgr);

            return(Status.Success);
        }
        public virtual async Task ChangeSubscription(IEnterprisesBillingManagerService entBillingMgr, ISecurityDataTokenService secMgr, IIdentityAccessService idMgr, string entLookup,
                                                     string username, string customerName, string plan)
        {
            //cancel existing subscription
            var cancelResp = await entBillingMgr.CancelSubscriptionByUser(username, entLookup);

            var planOption = this.State.Plans.First(p => p.Lookup == plan);

            var licenseType = planOption.Metadata["LicenseType"].ToString();

            //Remove license access
            await idMgr.RevokeLicense(entLookup, username, licenseType);

            // create new subscription
            var completeResp = await entBillingMgr.CompleteStripeSubscription(
                new CompleteStripeSubscriptionRequest()
            {
                CustomerName    = State.CustomerName,
                Plan            = plan,
                Username        = username,
                TrialPeriodDays = 0
            }, entLookup, licenseType);

            State.PaymentStatus = completeResp.Status;

            if (State.PaymentStatus)
            {
                State.PurchasedPlanLookup = plan;

                var tosResp = await secMgr.SetDataToken(new DataToken()
                {
                    Lookup      = "LCU-USER-BILLING.TermsOfService",
                    Name        = "LCU-USER-BILLING.TermsOfService",
                    Description = "Billing Terms of Service",
                    Value       = DateTimeOffset.UtcNow.ToString(),
                });

                var eaResp = await secMgr.SetDataToken(new DataToken()
                {
                    Lookup      = "LCU-USER-BILLING.EnterpriseAgreement",
                    Name        = "LCU-USER-BILLING.EnterpriseAgreement",
                    Description = "Billing Enterprise Agreement",
                    Value       = DateTimeOffset.UtcNow.ToString(),
                });

                //issue new license access
                // var setLicenseAccessResp = await idMgr.IssueLicenseAccess(new LicenseAccessToken()
                // {
                //     AccessStartDate = System.DateTime.Now,
                //     Details = planOption.JSONConvert<MetadataModel>(),
                //     EnterpriseLookup = entLookup,
                //     Lookup = licenseType,
                //     TrialPeriodDays = 0,
                //     Username = username
                // }, entLookup);

                // var setLicenseAccessResp = await idMgr.IssueLicense(new License()
                // {
                //     Details = planOption.JSONConvert<string>(),
                //     Lookup = licenseType,
                //     Type = licenseType,
                // }, entLookup);

                // State.PaymentStatus = setLicenseAccessResp.Status;

                State.SubscriptionID = completeResp.SubscriptionID;

                State.SuccessRedirect = planOption.Metadata["SuccessRedirect"].ToString();
            }

            await ListLicenses(idMgr, entLookup, username, licenseType);

            State.Loading = false;
        }