-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ProviderClientsController.cs
143 lines (116 loc) · 4.67 KB
/
ProviderClientsController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Bit.Api.Billing.Models.Requests;
using Bit.Core;
using Bit.Core.AdminConsole.Repositories;
using Bit.Core.AdminConsole.Services;
using Bit.Core.Billing.Commands;
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(
IAssignSeatsToClientOrganizationCommand assignSeatsToClientOrganizationCommand,
ICreateCustomerCommand createCustomerCommand,
ICurrentContext currentContext,
IFeatureService featureService,
ILogger<ProviderClientsController> logger,
IOrganizationRepository organizationRepository,
IProviderOrganizationRepository providerOrganizationRepository,
IProviderRepository providerRepository,
IProviderService providerService,
IScaleSeatsCommand scaleSeatsCommand,
IUserService userService) : Controller
{
[HttpPost]
public async Task<IResult> CreateAsync(
[FromRoute] Guid providerId,
[FromBody] CreateClientOrganizationRequestBody requestBody)
{
if (!featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling))
{
return TypedResults.NotFound();
}
var user = await userService.GetUserByPrincipalAsync(User);
if (user == null)
{
return TypedResults.Unauthorized();
}
if (!currentContext.ManageProviderOrganizations(providerId))
{
return TypedResults.Unauthorized();
}
var provider = await providerRepository.GetByIdAsync(providerId);
if (provider == null)
{
return TypedResults.NotFound();
}
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,
CollectionName = requestBody.CollectionName
};
var providerOrganization = await providerService.CreateOrganizationAsync(
providerId,
organizationSignup,
requestBody.OwnerEmail,
user);
var clientOrganization = await organizationRepository.GetByIdAsync(providerOrganization.OrganizationId);
if (clientOrganization == null)
{
logger.LogError("Newly created client organization ({ID}) could not be found", providerOrganization.OrganizationId);
return TypedResults.Problem();
}
await scaleSeatsCommand.ScalePasswordManagerSeats(
provider,
requestBody.PlanType,
requestBody.Seats);
await createCustomerCommand.CreateCustomer(
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)
{
if (!featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling))
{
return TypedResults.NotFound();
}
if (!currentContext.ProviderProviderAdmin(providerId))
{
return TypedResults.Unauthorized();
}
var provider = await providerRepository.GetByIdAsync(providerId);
var providerOrganization = await providerOrganizationRepository.GetByIdAsync(providerOrganizationId);
if (provider == null || providerOrganization == null)
{
return TypedResults.NotFound();
}
var clientOrganization = await organizationRepository.GetByIdAsync(providerOrganization.OrganizationId);
if (clientOrganization == null)
{
logger.LogError("The client organization ({OrganizationID}) represented by provider organization ({ProviderOrganizationID}) could not be found.", providerOrganization.OrganizationId, providerOrganization.Id);
return TypedResults.Problem();
}
await assignSeatsToClientOrganizationCommand.AssignSeatsToClientOrganization(
provider,
clientOrganization,
requestBody.AssignedSeats);
return TypedResults.Ok();
}
}