Skip to content

Commit

Permalink
Fix free broker limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
filipbekic01 committed Sep 23, 2024
1 parent daef34e commit 3ad1316
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
25 changes: 23 additions & 2 deletions backend/ResQueue/ResQueue/Endpoints/BrokerEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,39 @@ public static void MapBrokerEndpoints(this IEndpointRouteBuilder routes)
});

group.MapPost("",
async (IMongoCollection<Broker> collection, [FromBody] CreateBrokerDto dto, UserManager<User> userManager,
async (IMongoCollection<Broker> brokersCollection, [FromBody] CreateBrokerDto dto,
UserManager<User> userManager,
HttpContext httpContext) =>
{
// Get user
var user = await userManager.GetUserAsync(httpContext.User);
if (user == null)
{
return Results.Unauthorized();
}

// Validate free plan broker count
if (user.Subscription is null)
{
var filter = Builders<Broker>.Filter.And(
Builders<Broker>.Filter.Eq(b => b.CreatedByUserId, user.Id),
Builders<Broker>.Filter.Eq(b => b.DeletedAt, null)
);

if (await brokersCollection.Find(filter).AnyAsync())
{
return Results.Problem(new ProblemDetails
{
Title = "Free Plan Limit",
Detail = "Upgrade to paid plan to unlock more broker slots.",
Status = StatusCodes.Status403Forbidden
});
}
}

var broker = CreateBrokerDtoMapper.ToBroker(user.Id, dto);

await collection.InsertOneAsync(broker);
await brokersCollection.InsertOneAsync(broker);

return Results.Ok(BrokerMapper.ToDto(broker));
});
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/dialogs/CreateBrokerDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ const newBroker = reactive<CreateBrokerDto>({
})
const createBroker = () => {
createBrokerAsync(newBroker).then((data) => {
dialogRef?.value.close(data)
})
createBrokerAsync(newBroker)
.then((data) => {
dialogRef?.value.close(data)
})
.catch((e) => toast.add(errorToToast(e)))
}
const testConnection = () => {
Expand Down

0 comments on commit 3ad1316

Please sign in to comment.