1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00

separate plans for month vs annual. users => seats

This commit is contained in:
Kyle Spearrin 2017-04-10 10:44:27 -04:00
parent bb0555a6d9
commit 5705f4f736
12 changed files with 82 additions and 93 deletions

View File

@ -21,8 +21,7 @@ namespace Bit.Core.Models.Api
public string Key { get; set; } public string Key { get; set; }
public string PaymentToken { get; set; } public string PaymentToken { get; set; }
[Range(0, double.MaxValue)] [Range(0, double.MaxValue)]
public short AdditionalUsers { get; set; } public short AdditionalSeats { get; set; }
public bool Monthly { get; set; }
public virtual OrganizationSignup ToOrganizationSignup(User user) public virtual OrganizationSignup ToOrganizationSignup(User user)
{ {
@ -33,10 +32,9 @@ namespace Bit.Core.Models.Api
Name = Name, Name = Name,
Plan = PlanType, Plan = PlanType,
PaymentToken = PaymentToken, PaymentToken = PaymentToken,
AdditionalUsers = AdditionalUsers, AdditionalSeats = AdditionalSeats,
BillingEmail = BillingEmail, BillingEmail = BillingEmail,
BusinessName = BusinessName, BusinessName = BusinessName
Monthly = Monthly
}; };
} }

View File

@ -23,7 +23,7 @@ namespace Bit.Core.Models.Api
BillingEmail = organization.BillingEmail; BillingEmail = organization.BillingEmail;
Plan = organization.Plan; Plan = organization.Plan;
PlanType = organization.PlanType; PlanType = organization.PlanType;
MaxUsers = organization.MaxUsers; Seats = organization.Seats;
} }
public string Id { get; set; } public string Id { get; set; }
@ -32,7 +32,7 @@ namespace Bit.Core.Models.Api
public string BillingEmail { get; set; } public string BillingEmail { get; set; }
public string Plan { get; set; } public string Plan { get; set; }
public Enums.PlanType PlanType { get; set; } public Enums.PlanType PlanType { get; set; }
public short? MaxUsers { get; set; } public short? Seats { get; set; }
} }
public class OrganizationBillingResponseModel : OrganizationResponseModel public class OrganizationBillingResponseModel : OrganizationResponseModel

View File

@ -1,13 +0,0 @@
using Bit.Core.Enums;
using System;
namespace Bit.Core.Models.Business
{
public class OrganizationChangePlan
{
public Guid OrganizationId { get; set; }
public PlanType PlanType { get; set; }
public short AdditionalUsers { get; set; }
public bool Monthly { get; set; }
}
}

View File

@ -11,8 +11,7 @@ namespace Bit.Core.Models.Business
public User Owner { get; set; } public User Owner { get; set; }
public string OwnerKey { get; set; } public string OwnerKey { get; set; }
public Enums.PlanType Plan { get; set; } public Enums.PlanType Plan { get; set; }
public short AdditionalUsers { get; set; } public short AdditionalSeats { get; set; }
public string PaymentToken { get; set; } public string PaymentToken { get; set; }
public bool Monthly { get; set; }
} }
} }

View File

@ -6,13 +6,13 @@ namespace Bit.Core.Models.StaticStore
{ {
public string Name { get; set; } public string Name { get; set; }
public string StripePlanId { get; set; } public string StripePlanId { get; set; }
public string StripeUserPlanId { get; set; } public string StripeSeatPlanId { get; set; }
public PlanType Type { get; set; } public PlanType Type { get; set; }
public short BaseUsers { get; set; } public short BaseSeats { get; set; }
public bool CanBuyAdditionalUsers { get; set; } public bool CanBuyAdditionalSeats { get; set; }
public short? MaxAdditionalUsers { get; set; } public short? MaxAdditionalSeats { get; set; }
public decimal BasePrice { get; set; } public decimal BasePrice { get; set; }
public decimal UserPrice { get; set; } public decimal SeatPrice { get; set; }
public short? MaxSubvaults { get; set; } public short? MaxSubvaults { get; set; }
public int UpgradeSortOrder { get; set; } public int UpgradeSortOrder { get; set; }
public bool Disabled { get; set; } public bool Disabled { get; set; }

View File

@ -12,7 +12,7 @@ namespace Bit.Core.Models.Table
public string BillingEmail { get; set; } public string BillingEmail { get; set; }
public string Plan { get; set; } public string Plan { get; set; }
public PlanType PlanType { get; set; } public PlanType PlanType { get; set; }
public short? MaxUsers { get; set; } public short? Seats { get; set; }
public short? MaxSubvaults { get; set; } public short? MaxSubvaults { get; set; }
public string StripeCustomerId { get; set; } public string StripeCustomerId { get; set; }
public string StripeSubscriptionId { get; set; } public string StripeSubscriptionId { get; set; }

View File

@ -9,6 +9,7 @@ using Bit.Core.Exceptions;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection;
using Stripe; using Stripe;
using Bit.Core.Enums;
namespace Bit.Core.Services namespace Bit.Core.Services
{ {
@ -160,9 +161,9 @@ namespace Bit.Core.Services
} }
} }
public async Task UpgradePlanAsync(OrganizationChangePlan model) public async Task UpgradePlanAsync(Guid organizationId, PlanType plan, short additionalSeats)
{ {
var organization = await _organizationRepository.GetByIdAsync(model.OrganizationId); var organization = await _organizationRepository.GetByIdAsync(organizationId);
if(organization == null) if(organization == null)
{ {
throw new NotFoundException(); throw new NotFoundException();
@ -179,7 +180,7 @@ namespace Bit.Core.Services
throw new BadRequestException("Existing plan not found."); throw new BadRequestException("Existing plan not found.");
} }
var newPlan = StaticStore.Plans.FirstOrDefault(p => p.Type == model.PlanType && !p.Disabled); var newPlan = StaticStore.Plans.FirstOrDefault(p => p.Type == plan && !p.Disabled);
if(newPlan == null) if(newPlan == null)
{ {
throw new BadRequestException("Plan not found."); throw new BadRequestException("Plan not found.");
@ -195,26 +196,26 @@ namespace Bit.Core.Services
throw new BadRequestException("You cannot upgrade to this plan."); throw new BadRequestException("You cannot upgrade to this plan.");
} }
if(!newPlan.CanBuyAdditionalUsers && model.AdditionalUsers > 0) if(!newPlan.CanBuyAdditionalSeats && additionalSeats > 0)
{ {
throw new BadRequestException("Plan does not allow additional users."); throw new BadRequestException("Plan does not allow additional seats.");
} }
if(newPlan.CanBuyAdditionalUsers && newPlan.MaxAdditionalUsers.HasValue && if(newPlan.CanBuyAdditionalSeats && newPlan.MaxAdditionalSeats.HasValue &&
model.AdditionalUsers > newPlan.MaxAdditionalUsers.Value) additionalSeats > newPlan.MaxAdditionalSeats.Value)
{ {
throw new BadRequestException($"Selected plan allows a maximum of " + throw new BadRequestException($"Selected plan allows a maximum of " +
$"{newPlan.MaxAdditionalUsers.Value} additional users."); $"{newPlan.MaxAdditionalSeats.Value} additional seats.");
} }
var newPlanMaxUsers = (short)(newPlan.BaseUsers + (newPlan.CanBuyAdditionalUsers ? model.AdditionalUsers : 0)); var newPlanSeats = (short)(newPlan.BaseSeats + (newPlan.CanBuyAdditionalSeats ? additionalSeats : 0));
if(!organization.MaxUsers.HasValue || organization.MaxUsers.Value > newPlanMaxUsers) if(!organization.Seats.HasValue || organization.Seats.Value > newPlanSeats)
{ {
var userCount = await _organizationUserRepository.GetCountByOrganizationIdAsync(organization.Id); var userCount = await _organizationUserRepository.GetCountByOrganizationIdAsync(organization.Id);
if(userCount >= newPlanMaxUsers) if(userCount >= newPlanSeats)
{ {
throw new BadRequestException($"Your organization currently has {userCount} users. Your new plan " + throw new BadRequestException($"Your organization currently has {userCount} seats filled. Your new plan " +
$"allows for a maximum of ({newPlanMaxUsers}) users. Remove some users."); $"only has ({newPlanSeats}) seats. Remove some users.");
} }
} }
@ -225,7 +226,8 @@ namespace Bit.Core.Services
if(subvaultCount > newPlan.MaxSubvaults.Value) if(subvaultCount > newPlan.MaxSubvaults.Value)
{ {
throw new BadRequestException($"Your organization currently has {subvaultCount} subvaults. " + throw new BadRequestException($"Your organization currently has {subvaultCount} subvaults. " +
$"Your new plan allows for a maximum of ({newPlan.MaxSubvaults.Value}) users. Remove some subvaults."); $"Your new plan allows for a maximum of ({newPlan.MaxSubvaults.Value}) subvaults. " +
"Remove some subvaults.");
} }
} }
@ -245,12 +247,12 @@ namespace Bit.Core.Services
} }
}; };
if(model.AdditionalUsers > 0) if(additionalSeats > 0)
{ {
subCreateOptions.Items.Add(new StripeSubscriptionItemOption subCreateOptions.Items.Add(new StripeSubscriptionItemOption
{ {
PlanId = newPlan.StripeUserPlanId, PlanId = newPlan.StripeSeatPlanId,
Quantity = model.AdditionalUsers Quantity = additionalSeats
}); });
} }
@ -271,12 +273,12 @@ namespace Bit.Core.Services
} }
}; };
if(model.AdditionalUsers > 0) if(additionalSeats > 0)
{ {
subUpdateOptions.Items.Add(new StripeSubscriptionItemUpdateOption subUpdateOptions.Items.Add(new StripeSubscriptionItemUpdateOption
{ {
PlanId = newPlan.StripeUserPlanId, PlanId = newPlan.StripeSeatPlanId,
Quantity = model.AdditionalUsers Quantity = additionalSeats
}); });
} }
@ -284,7 +286,7 @@ namespace Bit.Core.Services
} }
} }
public async Task AdjustAdditionalUsersAsync(Guid organizationId, short additionalUsers) public async Task AdjustAdditionalSeatsAsync(Guid organizationId, short additionalSeats)
{ {
var organization = await _organizationRepository.GetByIdAsync(organizationId); var organization = await _organizationRepository.GetByIdAsync(organizationId);
if(organization == null) if(organization == null)
@ -308,25 +310,25 @@ namespace Bit.Core.Services
throw new BadRequestException("Existing plan not found."); throw new BadRequestException("Existing plan not found.");
} }
if(!plan.CanBuyAdditionalUsers) if(!plan.CanBuyAdditionalSeats)
{ {
throw new BadRequestException("Plan does not allow additional users."); throw new BadRequestException("Plan does not allow additional seats.");
} }
if(plan.MaxAdditionalUsers.HasValue && additionalUsers > plan.MaxAdditionalUsers.Value) if(plan.MaxAdditionalSeats.HasValue && additionalSeats > plan.MaxAdditionalSeats.Value)
{ {
throw new BadRequestException($"Organization plan allows a maximum of " + throw new BadRequestException($"Organization plan allows a maximum of " +
$"{plan.MaxAdditionalUsers.Value} additional users."); $"{plan.MaxAdditionalSeats.Value} additional seats.");
} }
var planNewMaxUsers = (short)(plan.BaseUsers + additionalUsers); var planNewSeats = (short)(plan.BaseSeats + additionalSeats);
if(!organization.MaxUsers.HasValue || organization.MaxUsers.Value > planNewMaxUsers) if(!organization.Seats.HasValue || organization.Seats.Value > planNewSeats)
{ {
var userCount = await _organizationUserRepository.GetCountByOrganizationIdAsync(organization.Id); var userCount = await _organizationUserRepository.GetCountByOrganizationIdAsync(organization.Id);
if(userCount >= planNewMaxUsers) if(userCount >= planNewSeats)
{ {
throw new BadRequestException($"Your organization currently has {userCount} users. Your new plan " + throw new BadRequestException($"Your organization currently has {userCount} seats filled. Your new plan " +
$"allows for a maximum of ({planNewMaxUsers}) users. Remove some users."); $"only has ({planNewSeats}) seats. Remove some users.");
} }
} }
@ -343,12 +345,12 @@ namespace Bit.Core.Services
} }
}; };
if(additionalUsers > 0) if(additionalSeats > 0)
{ {
subUpdateOptions.Items.Add(new StripeSubscriptionItemUpdateOption subUpdateOptions.Items.Add(new StripeSubscriptionItemUpdateOption
{ {
PlanId = plan.StripeUserPlanId, PlanId = plan.StripeSeatPlanId,
Quantity = additionalUsers Quantity = additionalSeats
}); });
} }
@ -368,16 +370,16 @@ namespace Bit.Core.Services
StripeCustomer customer = null; StripeCustomer customer = null;
StripeSubscription subscription = null; StripeSubscription subscription = null;
if(!plan.CanBuyAdditionalUsers && signup.AdditionalUsers > 0) if(!plan.CanBuyAdditionalSeats && signup.AdditionalSeats > 0)
{ {
throw new BadRequestException("Plan does not allow additional users."); throw new BadRequestException("Plan does not allow additional users.");
} }
if(plan.CanBuyAdditionalUsers && plan.MaxAdditionalUsers.HasValue && if(plan.CanBuyAdditionalSeats && plan.MaxAdditionalSeats.HasValue &&
signup.AdditionalUsers > plan.MaxAdditionalUsers.Value) signup.AdditionalSeats > plan.MaxAdditionalSeats.Value)
{ {
throw new BadRequestException($"Selected plan allows a maximum of " + throw new BadRequestException($"Selected plan allows a maximum of " +
$"{plan.MaxAdditionalUsers.GetValueOrDefault(0)} additional users."); $"{plan.MaxAdditionalSeats.GetValueOrDefault(0)} additional users.");
} }
if(plan.Type == Enums.PlanType.Free) if(plan.Type == Enums.PlanType.Free)
@ -410,12 +412,12 @@ namespace Bit.Core.Services
} }
}; };
if(signup.AdditionalUsers > 0) if(signup.AdditionalSeats > 0)
{ {
subCreateOptions.Items.Add(new StripeSubscriptionItemOption subCreateOptions.Items.Add(new StripeSubscriptionItemOption
{ {
PlanId = plan.StripeUserPlanId, PlanId = plan.StripeSeatPlanId,
Quantity = signup.AdditionalUsers Quantity = signup.AdditionalSeats
}); });
} }
@ -428,7 +430,7 @@ namespace Bit.Core.Services
BillingEmail = signup.BillingEmail, BillingEmail = signup.BillingEmail,
BusinessName = signup.BusinessName, BusinessName = signup.BusinessName,
PlanType = plan.Type, PlanType = plan.Type,
MaxUsers = (short)(plan.BaseUsers + signup.AdditionalUsers), Seats = (short)(plan.BaseSeats + signup.AdditionalSeats),
MaxSubvaults = plan.MaxSubvaults, MaxSubvaults = plan.MaxSubvaults,
Plan = plan.Name, Plan = plan.Name,
StripeCustomerId = customer?.Id, StripeCustomerId = customer?.Id,
@ -484,13 +486,13 @@ namespace Bit.Core.Services
throw new NotFoundException(); throw new NotFoundException();
} }
if(organization.MaxUsers.HasValue) if(organization.Seats.HasValue)
{ {
var userCount = await _organizationUserRepository.GetCountByOrganizationIdAsync(organizationId); var userCount = await _organizationUserRepository.GetCountByOrganizationIdAsync(organizationId);
if(userCount >= organization.MaxUsers.Value) if(userCount >= organization.Seats.Value)
{ {
throw new BadRequestException("You have reached the maximum number of users " + throw new BadRequestException("You have reached the maximum number of users " +
$"({organization.MaxUsers.Value}) for this organization."); $"({organization.Seats.Value}) for this organization.");
} }
} }

View File

@ -94,8 +94,8 @@ namespace Bit.Core.Utilities
new Plan new Plan
{ {
Type = PlanType.Free, Type = PlanType.Free,
BaseUsers = 2, BaseSeats = 2,
CanBuyAdditionalUsers = false, CanBuyAdditionalSeats = false,
MaxSubvaults = 2, MaxSubvaults = 2,
Name = "Free", Name = "Free",
UpgradeSortOrder = -1 // Always the lowest plan, cannot be upgraded to UpgradeSortOrder = -1 // Always the lowest plan, cannot be upgraded to
@ -103,38 +103,38 @@ namespace Bit.Core.Utilities
new Plan new Plan
{ {
Type = PlanType.PersonalAnnually, Type = PlanType.PersonalAnnually,
BaseUsers = 5, BaseSeats = 5,
BasePrice = 12, BasePrice = 12,
UserPrice = 12, SeatPrice = 12,
CanBuyAdditionalUsers = true, CanBuyAdditionalSeats = true,
MaxAdditionalUsers = 5, MaxAdditionalSeats = 5,
Name = "Personal", Name = "Personal",
StripePlanId = "personal-annual", StripePlanId = "personal-annual",
StripeUserPlanId = "personal-user-annual", StripeSeatPlanId = "personal-user-annual",
UpgradeSortOrder = 1 UpgradeSortOrder = 1
}, },
new Plan new Plan
{ {
Type = PlanType.TeamsMonthly, Type = PlanType.TeamsMonthly,
BaseUsers = 5, BaseSeats = 5,
BasePrice = 8, BasePrice = 8,
UserPrice = 2.5M, SeatPrice = 2.5M,
CanBuyAdditionalUsers = true, CanBuyAdditionalSeats = true,
Name = "Teams (Monthly)", Name = "Teams (Monthly)",
StripePlanId = "teams-monthly", StripePlanId = "teams-monthly",
StripeUserPlanId = "teams-user-monthly", StripeSeatPlanId = "teams-user-monthly",
UpgradeSortOrder = 2 UpgradeSortOrder = 2
}, },
new Plan new Plan
{ {
Type = PlanType.TeamsAnnually, Type = PlanType.TeamsAnnually,
BaseUsers = 5, BaseSeats = 5,
BasePrice = 60, BasePrice = 60,
UserPrice = 24, SeatPrice = 24,
CanBuyAdditionalUsers = true, CanBuyAdditionalSeats = true,
Name = "Teams (Annually)", Name = "Teams (Annually)",
StripePlanId = "teams-annual", StripePlanId = "teams-annual",
StripeUserPlanId = "teams-user-annual", StripeSeatPlanId = "teams-user-annual",
UpgradeSortOrder = 2 UpgradeSortOrder = 2
} }
}; };

View File

@ -184,4 +184,7 @@
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationId.sql" /> <Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationId.sql" />
<Build Include="dbo\Stored Procedures\Subvault_ReadCountByOrganizationId.sql" /> <Build Include="dbo\Stored Procedures\Subvault_ReadCountByOrganizationId.sql" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<RefactorLog Include="Sql.refactorlog" />
</ItemGroup>
</Project> </Project>

View File

@ -5,7 +5,7 @@
@BillingEmail NVARCHAR(50), @BillingEmail NVARCHAR(50),
@Plan NVARCHAR(20), @Plan NVARCHAR(20),
@PlanType TINYINT, @PlanType TINYINT,
@MaxUsers SMALLINT, @Seats SMALLINT,
@MaxSubvaults SMALLINT, @MaxSubvaults SMALLINT,
@StripeCustomerId VARCHAR(50), @StripeCustomerId VARCHAR(50),
@StripeSubscriptionId VARCHAR(50), @StripeSubscriptionId VARCHAR(50),
@ -23,7 +23,7 @@ BEGIN
[BillingEmail], [BillingEmail],
[Plan], [Plan],
[PlanType], [PlanType],
[MaxUsers], [Seats],
[MaxSubvaults], [MaxSubvaults],
[StripeCustomerId], [StripeCustomerId],
[StripeSubscriptionId], [StripeSubscriptionId],
@ -38,7 +38,7 @@ BEGIN
@BillingEmail, @BillingEmail,
@Plan, @Plan,
@PlanType, @PlanType,
@MaxUsers, @Seats,
@MaxSubvaults, @MaxSubvaults,
@StripeCustomerId, @StripeCustomerId,
@StripeSubscriptionId, @StripeSubscriptionId,

View File

@ -5,7 +5,7 @@
@BillingEmail NVARCHAR(50), @BillingEmail NVARCHAR(50),
@Plan NVARCHAR(20), @Plan NVARCHAR(20),
@PlanType TINYINT, @PlanType TINYINT,
@MaxUsers SMALLINT, @Seats SMALLINT,
@MaxSubvaults SMALLINT, @MaxSubvaults SMALLINT,
@StripeCustomerId VARCHAR(50), @StripeCustomerId VARCHAR(50),
@StripeSubscriptionId VARCHAR(50), @StripeSubscriptionId VARCHAR(50),
@ -24,7 +24,7 @@ BEGIN
[BillingEmail] = @BillingEmail, [BillingEmail] = @BillingEmail,
[Plan] = @Plan, [Plan] = @Plan,
[PlanType] = @PlanType, [PlanType] = @PlanType,
[MaxUsers] = @MaxUsers, [Seats] = @Seats,
[MaxSubvaults] = @MaxSubvaults, [MaxSubvaults] = @MaxSubvaults,
[StripeCustomerId] = @StripeCustomerId, [StripeCustomerId] = @StripeCustomerId,
[StripeSubscriptionId] = @StripeSubscriptionId, [StripeSubscriptionId] = @StripeSubscriptionId,

View File

@ -5,7 +5,7 @@
[BillingEmail] NVARCHAR (50) NOT NULL, [BillingEmail] NVARCHAR (50) NOT NULL,
[Plan] NVARCHAR (20) NOT NULL, [Plan] NVARCHAR (20) NOT NULL,
[PlanType] TINYINT NOT NULL, [PlanType] TINYINT NOT NULL,
[MaxUsers] SMALLINT NULL, [Seats] SMALLINT NULL,
[MaxSubvaults] SMALLINT NULL, [MaxSubvaults] SMALLINT NULL,
[StripeCustomerId] VARCHAR (50) NULL, [StripeCustomerId] VARCHAR (50) NULL,
[StripeSubscriptionId] VARCHAR (50) NULL, [StripeSubscriptionId] VARCHAR (50) NULL,