2024-04-16 13:55:00 -04:00
|
|
|
|
using Bit.Api.Billing.Models.Requests;
|
|
|
|
|
|
using Bit.Core.AdminConsole.Repositories;
|
|
|
|
|
|
using Bit.Core.AdminConsole.Services;
|
2024-05-23 10:17:00 -04:00
|
|
|
|
using Bit.Core.Billing.Services;
|
2024-04-16 13:55:00 -04:00
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
|
using Bit.Core.Models.Business;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Billing.Controllers;
|
|
|
|
|
|
|
|
|
|
|
|
[Route("providers/{providerId:guid}/clients")]
|
|
|
|
|
|
public class ProviderClientsController(
|
|
|
|
|
|
ICurrentContext currentContext,
|
|
|
|
|
|
IFeatureService featureService,
|
2024-07-31 09:26:44 -04:00
|
|
|
|
ILogger<BaseProviderController> logger,
|
2024-04-16 13:55:00 -04:00
|
|
|
|
IOrganizationRepository organizationRepository,
|
2024-05-23 10:17:00 -04:00
|
|
|
|
IProviderBillingService providerBillingService,
|
2024-04-16 13:55:00 -04:00
|
|
|
|
IProviderOrganizationRepository providerOrganizationRepository,
|
|
|
|
|
|
IProviderRepository providerRepository,
|
|
|
|
|
|
IProviderService providerService,
|
2024-07-31 09:26:44 -04:00
|
|
|
|
IUserService userService) : BaseProviderController(currentContext, featureService, logger, providerRepository, userService)
|
2024-04-16 13:55:00 -04:00
|
|
|
|
{
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
|
public async Task<IResult> CreateAsync(
|
|
|
|
|
|
[FromRoute] Guid providerId,
|
|
|
|
|
|
[FromBody] CreateClientOrganizationRequestBody requestBody)
|
|
|
|
|
|
{
|
2024-06-24 11:15:47 -04:00
|
|
|
|
var (provider, result) = await TryGetBillableProviderForAdminOperation(providerId);
|
|
|
|
|
|
|
|
|
|
|
|
if (provider == null)
|
2024-04-16 13:55:00 -04:00
|
|
|
|
{
|
2024-06-24 11:15:47 -04:00
|
|
|
|
return result;
|
2024-04-16 13:55:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-31 09:26:44 -04:00
|
|
|
|
var user = await UserService.GetUserByPrincipalAsync(User);
|
2024-04-16 13:55:00 -04:00
|
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
|
{
|
2024-08-28 10:48:14 -04:00
|
|
|
|
return Error.Unauthorized();
|
2024-04-16 13:55:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var organizationSignup = new OrganizationSignup
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = requestBody.Name,
|
|
|
|
|
|
Plan = requestBody.PlanType,
|
|
|
|
|
|
AdditionalSeats = requestBody.Seats,
|
|
|
|
|
|
Owner = user,
|
|
|
|
|
|
BillingEmail = provider.BillingEmail,
|
|
|
|
|
|
OwnerKey = requestBody.Key,
|
|
|
|
|
|
PublicKey = requestBody.KeyPair.PublicKey,
|
|
|
|
|
|
PrivateKey = requestBody.KeyPair.EncryptedPrivateKey,
|
2024-09-11 09:04:15 -04:00
|
|
|
|
CollectionName = requestBody.CollectionName,
|
|
|
|
|
|
IsFromProvider = true
|
2024-04-16 13:55:00 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var providerOrganization = await providerService.CreateOrganizationAsync(
|
|
|
|
|
|
providerId,
|
|
|
|
|
|
organizationSignup,
|
|
|
|
|
|
requestBody.OwnerEmail,
|
|
|
|
|
|
user);
|
|
|
|
|
|
|
|
|
|
|
|
var clientOrganization = await organizationRepository.GetByIdAsync(providerOrganization.OrganizationId);
|
|
|
|
|
|
|
2024-05-23 10:17:00 -04:00
|
|
|
|
await providerBillingService.ScaleSeats(
|
2024-04-16 13:55:00 -04:00
|
|
|
|
provider,
|
|
|
|
|
|
requestBody.PlanType,
|
|
|
|
|
|
requestBody.Seats);
|
|
|
|
|
|
|
2024-05-23 10:17:00 -04:00
|
|
|
|
await providerBillingService.CreateCustomerForClientOrganization(
|
2024-04-16 13:55:00 -04:00
|
|
|
|
provider,
|
|
|
|
|
|
clientOrganization);
|
|
|
|
|
|
|
|
|
|
|
|
clientOrganization.Status = OrganizationStatusType.Managed;
|
|
|
|
|
|
|
|
|
|
|
|
await organizationRepository.ReplaceAsync(clientOrganization);
|
|
|
|
|
|
|
|
|
|
|
|
return TypedResults.Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("{providerOrganizationId:guid}")]
|
|
|
|
|
|
public async Task<IResult> UpdateAsync(
|
|
|
|
|
|
[FromRoute] Guid providerId,
|
|
|
|
|
|
[FromRoute] Guid providerOrganizationId,
|
|
|
|
|
|
[FromBody] UpdateClientOrganizationRequestBody requestBody)
|
|
|
|
|
|
{
|
2024-06-24 11:15:47 -04:00
|
|
|
|
var (provider, result) = await TryGetBillableProviderForServiceUserOperation(providerId);
|
2024-04-16 13:55:00 -04:00
|
|
|
|
|
2024-06-24 11:15:47 -04:00
|
|
|
|
if (provider == null)
|
2024-04-16 13:55:00 -04:00
|
|
|
|
{
|
2024-06-24 11:15:47 -04:00
|
|
|
|
return result;
|
2024-04-16 13:55:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var providerOrganization = await providerOrganizationRepository.GetByIdAsync(providerOrganizationId);
|
|
|
|
|
|
|
2024-06-24 11:15:47 -04:00
|
|
|
|
if (providerOrganization == null)
|
2024-04-16 13:55:00 -04:00
|
|
|
|
{
|
2024-08-28 10:48:14 -04:00
|
|
|
|
return Error.NotFound();
|
2024-04-16 13:55:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var clientOrganization = await organizationRepository.GetByIdAsync(providerOrganization.OrganizationId);
|
|
|
|
|
|
|
2024-05-14 11:26:08 -04:00
|
|
|
|
if (clientOrganization.Seats != requestBody.AssignedSeats)
|
|
|
|
|
|
{
|
2024-05-23 10:17:00 -04:00
|
|
|
|
await providerBillingService.AssignSeatsToClientOrganization(
|
2024-05-14 11:26:08 -04:00
|
|
|
|
provider,
|
|
|
|
|
|
clientOrganization,
|
|
|
|
|
|
requestBody.AssignedSeats);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
clientOrganization.Name = requestBody.Name;
|
|
|
|
|
|
|
|
|
|
|
|
await organizationRepository.ReplaceAsync(clientOrganization);
|
2024-04-16 13:55:00 -04:00
|
|
|
|
|
|
|
|
|
|
return TypedResults.Ok();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|