Exemple #1
0
        public async Task CreateConnection_OnlyOneConnectionOfEachType(OrganizationConnectionType type,
                                                                       OrganizationConnectionRequestModel model, BillingSyncConfig config, Guid existingEntityId,
                                                                       SutProvider <OrganizationConnectionsController> sutProvider)
        {
            model.Type   = type;
            model.Config = JsonDocumentFromObject(config);
            var typedModel = new OrganizationConnectionRequestModel <BillingSyncConfig>(model);
            var existing   = typedModel.ToData(existingEntityId).ToEntity();

            sutProvider.GetDependency <ICurrentContext>().OrganizationOwner(model.OrganizationId).Returns(true);

            sutProvider.GetDependency <IOrganizationConnectionRepository>().GetByOrganizationIdTypeAsync(model.OrganizationId, type).Returns(new[] { existing });

            var exception = await Assert.ThrowsAsync <BadRequestException>(() => sutProvider.Sut.CreateConnection(model));

            Assert.Contains($"The requested organization already has a connection of type {model.Type}. Only one of each connection type may exist per organization.", exception.Message);
        }
        public async Task <OrganizationConnectionResponseModel> UpdateConnection(Guid organizationConnectionId, [FromBody] OrganizationConnectionRequestModel model)
        {
            if (!await HasPermissionAsync(model?.OrganizationId))
            {
                throw new BadRequestException("Only the owner of an organization can update a connection.");
            }

            if (await HasConnectionTypeAsync(model, organizationConnectionId))
            {
                throw new BadRequestException($"The requested organization already has a connection of type {model.Type}. Only one of each connection type may exist per organization.");
            }

            switch (model.Type)
            {
            case OrganizationConnectionType.CloudBillingSync:
                var typedModel = new OrganizationConnectionRequestModel <BillingSyncConfig>(model);
                var connection = await _updateOrganizationConnectionCommand.UpdateAsync(typedModel.ToData(organizationConnectionId));

                return(new OrganizationConnectionResponseModel(connection, typeof(BillingSyncConfig)));

            default:
                throw new BadRequestException($"Unkown Organization connection Type: {model.Type}");
            }
        }
        public async Task <OrganizationConnectionResponseModel> CreateConnection([FromBody] OrganizationConnectionRequestModel model)
        {
            if (!await HasPermissionAsync(model?.OrganizationId))
            {
                throw new BadRequestException("Only the owner of an organization can create a connection.");
            }

            if (await HasConnectionTypeAsync(model))
            {
                throw new BadRequestException($"The requested organization already has a connection of type {model.Type}. Only one of each connection type may exist per organization.");
            }

            switch (model.Type)
            {
            case OrganizationConnectionType.CloudBillingSync:
                var typedModel = new OrganizationConnectionRequestModel <BillingSyncConfig>(model);
                var license    = await _licensingService.ReadOrganizationLicenseAsync(model.OrganizationId);

                typedModel.ParsedConfig.CloudOrganizationId = license.Id;
                var connection = await _createOrganizationConnectionCommand.CreateAsync(typedModel.ToData());

                return(new OrganizationConnectionResponseModel(connection, typeof(BillingSyncConfig)));

            default:
                throw new BadRequestException($"Unknown Organization connection Type: {model.Type}");
            }
        }