-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathOrganizationPlanMigrationCohortsController.cs
More file actions
274 lines (230 loc) · 9.98 KB
/
OrganizationPlanMigrationCohortsController.cs
File metadata and controls
274 lines (230 loc) · 9.98 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System.ComponentModel.DataAnnotations;
using Bit.Admin.Billing.Models.OrganizationPlanMigrationCohorts;
using Bit.Admin.Enums;
using Bit.Admin.Utilities;
using Bit.Core;
using Bit.Core.Billing.Organizations.PlanMigration.Entities;
using Bit.Core.Billing.Organizations.PlanMigration.Queries;
using Bit.Core.Billing.Organizations.PlanMigration.Repositories;
using Bit.Core.Billing.Services;
using Bit.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Stripe;
// ReSharper disable InconsistentNaming
namespace Bit.Admin.Billing.Controllers;
[Authorize]
[Route("migration-cohorts")]
public class OrganizationPlanMigrationCohortsController(
IOrganizationPlanMigrationCohortRepository cohortRepository,
IStripeAdapter stripeAdapter,
ILogger<OrganizationPlanMigrationCohortsController> logger,
IFeatureService featureService,
IGetCohortAssignmentStateQuery getCohortAssignmentStateQuery) : Controller
{
private const int _defaultPageSize = 25;
private bool PlanMigrationCohortsFeatureEnabled() =>
featureService.IsEnabled(FeatureFlagKeys.PM35215_BusinessPlanPriceMigration);
[HttpGet("")]
[RequirePermission(Permission.Tools_ManagePlanMigrationCohorts)]
public async Task<IActionResult> Index(string? name = null, int page = 1, int count = _defaultPageSize)
{
if (!PlanMigrationCohortsFeatureEnabled()) return NotFound();
if (page < 1) page = 1;
if (count < 1) count = 1;
var skip = (page - 1) * count;
var items = await cohortRepository.SearchWithCountsAsync(name, skip, count);
return View(new CohortsPagedModel
{
NameSearch = name,
Items = items.Select(CohortListItemViewModel.From).ToList(),
Page = page,
Count = count,
});
}
[HttpGet("create")]
[RequirePermission(Permission.Tools_ManagePlanMigrationCohorts)]
public IActionResult Create()
{
if (!PlanMigrationCohortsFeatureEnabled()) return NotFound();
return View(new CohortFormModel());
}
[HttpPost("create")]
[ValidateAntiForgeryToken]
[RequirePermission(Permission.Tools_ManagePlanMigrationCohorts)]
public async Task<IActionResult> Create(CohortFormModel model)
{
if (!PlanMigrationCohortsFeatureEnabled()) return NotFound();
MergeCrossFieldValidationErrors(model);
if (!ModelState.IsValid)
{
return View(model);
}
try
{
if (!await ValidateNameAsync(model.Name) || !await ValidateCouponsAsync(model))
{
return View(model);
}
var cohort = new OrganizationPlanMigrationCohort
{
Name = model.Name,
MigrationPathId = model.GetMigrationPathId(),
ProactiveDiscountCouponCode = NormalizeCouponCode(model.ProactiveDiscountCouponCode),
ChurnDiscountCouponCode = NormalizeCouponCode(model.ChurnDiscountCouponCode),
};
await cohortRepository.CreateAsync(cohort);
TempData["Success"] = $"Cohort '{cohort.Name}' created.";
return RedirectToAction(nameof(Index));
}
catch (Exception ex)
{
logger.LogError(ex, "Error creating cohort. Name: {Name}", model.Name);
ModelState.AddModelError(string.Empty, "An error occurred while saving the cohort.");
return View(model);
}
}
[HttpGet("{id:guid}")]
[RequirePermission(Permission.Tools_ManagePlanMigrationCohorts)]
public async Task<IActionResult> Edit(Guid id)
{
if (!PlanMigrationCohortsFeatureEnabled()) return NotFound();
var cohort = await cohortRepository.GetByIdAsync(id);
if (cohort == null) return NotFound();
var assignmentState = await getCohortAssignmentStateQuery.Run(cohort);
return View(EditCohortViewModel.From(cohort, CohortFormModel.From(cohort), assignmentState));
}
[HttpPost("{id:guid}")]
[ValidateAntiForgeryToken]
[RequirePermission(Permission.Tools_ManagePlanMigrationCohorts)]
public async Task<IActionResult> Edit(Guid id, CohortFormModel model)
{
if (!PlanMigrationCohortsFeatureEnabled()) return NotFound();
model.Id = id;
var cohort = await cohortRepository.GetByIdAsync(id);
if (cohort == null) return NotFound();
var assignmentState = await getCohortAssignmentStateQuery.Run(cohort);
if (assignmentState.HasNonPendingAssignments)
{
// The locked migration path view doesn't post a value for MigrationPathSelection.
// Restore from the persisted cohort so [Required] passes and the eventual
// ReplaceAsync writes back the unchanged path.
model.MigrationPathSelection = cohort.MigrationPathId switch
{
null => CohortFormModel.NoMigrationPath,
var pathId => ((byte)pathId).ToString(),
};
}
MergeCrossFieldValidationErrors(model);
if (!ModelState.IsValid)
{
return View(EditCohortViewModel.From(cohort, model, assignmentState));
}
try
{
if (!await ValidateNameAsync(model.Name, id)
|| !await ValidateCouponsAsync(model))
{
return View(EditCohortViewModel.From(cohort, model, assignmentState));
}
cohort.Name = model.Name;
cohort.MigrationPathId = model.GetMigrationPathId();
cohort.ProactiveDiscountCouponCode = NormalizeCouponCode(model.ProactiveDiscountCouponCode);
cohort.ChurnDiscountCouponCode = NormalizeCouponCode(model.ChurnDiscountCouponCode);
cohort.IsActive = model.IsActive;
cohort.RevisionDate = DateTime.UtcNow;
await cohortRepository.ReplaceAsync(cohort);
TempData["Success"] = $"Cohort '{cohort.Name}' updated.";
return RedirectToAction(nameof(Index));
}
catch (Exception ex)
{
logger.LogError(ex, "Error updating cohort. Id: {Id}", id);
ModelState.AddModelError(string.Empty, "An error occurred while saving the cohort.");
return View(EditCohortViewModel.From(cohort, model, assignmentState));
}
}
[HttpPost("{id:guid}/delete")]
[ValidateAntiForgeryToken]
[RequirePermission(Permission.Tools_ManagePlanMigrationCohorts)]
public async Task<IActionResult> Delete(Guid id)
{
if (!PlanMigrationCohortsFeatureEnabled()) return NotFound();
var cohort = await cohortRepository.GetByIdAsync(id);
if (cohort == null) return NotFound();
try
{
var assignmentState = await getCohortAssignmentStateQuery.Run(cohort);
if (assignmentState.HasNonPendingAssignments)
{
TempData["Error"] =
$"Cannot delete cohort '{cohort.Name}' because {assignmentState.NonPendingAssignmentCount:N0} " +
"assignment(s) have left the Pending state. Historical migration and " +
"save-offer records are preserved.";
return RedirectToAction(nameof(Edit), new { id });
}
await cohortRepository.DeleteAsync(cohort);
TempData["Success"] = $"Cohort '{cohort.Name}' deleted.";
return RedirectToAction(nameof(Index));
}
catch (Exception ex)
{
logger.LogError(ex, "Error deleting cohort. Id: {Id}", id);
TempData["Error"] = "An error occurred while attempting to delete the cohort.";
return RedirectToAction(nameof(Edit), new { id });
}
}
private static string? NormalizeCouponCode(string? value) =>
string.IsNullOrWhiteSpace(value) ? null : value.Trim();
// MVC skips IValidatableObject.Validate when any property-level attribute already failed, hiding cross-field
// rules until the operator resubmits. Run it explicitly so every error surfaces on a single submit.
// See https://github.com/dotnet/aspnetcore/issues/1899.
private void MergeCrossFieldValidationErrors(CohortFormModel model)
{
foreach (var result in model.Validate(new ValidationContext(model)))
{
foreach (var memberName in result.MemberNames.DefaultIfEmpty(string.Empty))
{
ModelState.AddModelError(memberName, result.ErrorMessage ?? string.Empty);
}
}
}
private async Task<bool> ValidateNameAsync(string name, Guid? excludeId = null)
{
var existing = await cohortRepository.GetByNameAsync(name);
if (existing == null || existing.Id == excludeId)
{
return true;
}
ModelState.AddModelError(nameof(CohortFormModel.Name), "A cohort with this name already exists.");
return false;
}
private async Task<bool> ValidateCouponsAsync(CohortFormModel model)
{
var proactive = NormalizeCouponCode(model.ProactiveDiscountCouponCode);
var churn = NormalizeCouponCode(model.ChurnDiscountCouponCode);
var ok = !(proactive != null && !await TryValidateCouponAsync(proactive, nameof(model.ProactiveDiscountCouponCode)));
if (churn != null && !await TryValidateCouponAsync(churn, nameof(model.ChurnDiscountCouponCode)))
{
ok = false;
}
return ok;
}
private async Task<bool> TryValidateCouponAsync(string couponId, string fieldName)
{
try
{
await stripeAdapter.GetCouponAsync(couponId);
return true;
}
catch (StripeException ex)
{
var message = ex.StripeError?.Code == "resource_missing"
? "Coupon not found in Stripe. Please verify the coupon ID."
: "An error occurred while fetching the coupon from Stripe.";
logger.LogError(ex, "Stripe coupon error: {CouponId}", couponId);
ModelState.AddModelError(fieldName, message);
return false;
}
}
}