Skip to content

Commit

Permalink
Split endpoints for FF 'AC-1607_present-user-offboarding-survey' (#3814)
Browse files Browse the repository at this point in the history
  • Loading branch information
amorask-bitwarden authored Feb 16, 2024
1 parent d187487 commit b866353
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 57 deletions.
64 changes: 33 additions & 31 deletions src/Api/AdminConsole/Controllers/OrganizationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,50 +464,52 @@ public async Task PostVerifyBank(string id, [FromBody] OrganizationVerifyBankReq
await _organizationService.VerifyBankAsync(orgIdGuid, model.Amount1.Value, model.Amount2.Value);
}

[HttpPost("{id}/cancel")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostCancel(Guid id, [FromBody] SubscriptionCancellationRequestModel request)
[HttpPost("{id}/churn")]
public async Task PostChurn(Guid id, [FromBody] SubscriptionCancellationRequestModel request)
{
if (!await _currentContext.EditSubscription(id))
{
throw new NotFoundException();
}

var presentUserWithOffboardingSurvey =
_featureService.IsEnabled(FeatureFlagKeys.AC1607_PresentUsersWithOffboardingSurvey);
var organization = await _organizationRepository.GetByIdAsync(id);

if (presentUserWithOffboardingSurvey)
if (organization == null)
{
var organization = await _organizationRepository.GetByIdAsync(id);
throw new NotFoundException();
}

if (organization == null)
{
throw new NotFoundException();
}
var subscription = await _getSubscriptionQuery.GetSubscription(organization);

var subscription = await _getSubscriptionQuery.GetSubscription(organization);

await _cancelSubscriptionCommand.CancelSubscription(subscription,
new OffboardingSurveyResponse
{
UserId = _currentContext.UserId!.Value,
Reason = request.Reason,
Feedback = request.Feedback
},
organization.IsExpired());

await _referenceEventService.RaiseEventAsync(new ReferenceEvent(
ReferenceEventType.CancelSubscription,
organization,
_currentContext)
await _cancelSubscriptionCommand.CancelSubscription(subscription,
new OffboardingSurveyResponse
{
EndOfPeriod = organization.IsExpired()
});
}
else
UserId = _currentContext.UserId!.Value,
Reason = request.Reason,
Feedback = request.Feedback
},
organization.IsExpired());

await _referenceEventService.RaiseEventAsync(new ReferenceEvent(
ReferenceEventType.CancelSubscription,
organization,
_currentContext)
{
await _organizationService.CancelSubscriptionAsync(id);
EndOfPeriod = organization.IsExpired()
});
}

[HttpPost("{id}/cancel")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostCancel(string id)
{
var orgIdGuid = new Guid(id);
if (!await _currentContext.EditSubscription(orgIdGuid))
{
throw new NotFoundException();
}

await _organizationService.CancelSubscriptionAsync(orgIdGuid);
}

[HttpPost("{id}/reinstate")]
Expand Down
54 changes: 28 additions & 26 deletions src/Api/Auth/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -821,9 +821,8 @@ public async Task PostLicense(LicenseRequestModel model)
await _userService.UpdateLicenseAsync(user, license);
}

[HttpPost("cancel-premium")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostCancel([FromBody] SubscriptionCancellationRequestModel request)
[HttpPost("churn-premium")]
public async Task PostChurn([FromBody] SubscriptionCancellationRequestModel request)
{
var user = await _userService.GetUserByPrincipalAsync(User);

Expand All @@ -832,34 +831,37 @@ public async Task PostCancel([FromBody] SubscriptionCancellationRequestModel req
throw new UnauthorizedAccessException();
}

var presentUserWithOffboardingSurvey =
_featureService.IsEnabled(FeatureFlagKeys.AC1607_PresentUsersWithOffboardingSurvey);
var subscription = await _getSubscriptionQuery.GetSubscription(user);

if (presentUserWithOffboardingSurvey)
{
var subscription = await _getSubscriptionQuery.GetSubscription(user);
await _cancelSubscriptionCommand.CancelSubscription(subscription,
new OffboardingSurveyResponse
{
UserId = user.Id,
Reason = request.Reason,
Feedback = request.Feedback
},
user.IsExpired());

await _cancelSubscriptionCommand.CancelSubscription(subscription,
new OffboardingSurveyResponse
{
UserId = user.Id,
Reason = request.Reason,
Feedback = request.Feedback
},
user.IsExpired());
await _referenceEventService.RaiseEventAsync(new ReferenceEvent(
ReferenceEventType.CancelSubscription,
user,
_currentContext)
{
EndOfPeriod = user.IsExpired()
});
}

await _referenceEventService.RaiseEventAsync(new ReferenceEvent(
ReferenceEventType.CancelSubscription,
user,
_currentContext)
{
EndOfPeriod = user.IsExpired()
});
}
else
[HttpPost("cancel-premium")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostCancel()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
await _userService.CancelPremiumAsync(user);
throw new UnauthorizedAccessException();
}

await _userService.CancelPremiumAsync(user);
}

[HttpPost("reinstate-premium")]
Expand Down

0 comments on commit b866353

Please sign in to comment.