using Bit.Api.Billing.Models.Responses; using Bit.Core.Billing.Services; using Bit.Core.Context; using Bit.Core.Repositories; using Bit.Core.Services; using Bit.Core.Utilities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Bit.Api.Billing.Controllers; [Route("organizations/{organizationId:guid}/billing")] [Authorize("Application")] public class OrganizationBillingController( ICurrentContext currentContext, IOrganizationBillingService organizationBillingService, IOrganizationRepository organizationRepository, IPaymentService paymentService) : Controller { [HttpGet("metadata")] public async Task GetMetadataAsync([FromRoute] Guid organizationId) { var metadata = await organizationBillingService.GetMetadata(organizationId); if (metadata == null) { return TypedResults.NotFound(); } var response = OrganizationMetadataResponse.From(metadata); return TypedResults.Ok(response); } [HttpGet("history")] public async Task GetHistoryAsync([FromRoute] Guid organizationId) { var organization = await organizationRepository.GetByIdAsync(organizationId); if (organization == null) { return TypedResults.NotFound(); } var billingInfo = await paymentService.GetBillingHistoryAsync(organization); return TypedResults.Ok(billingInfo); } [HttpGet] [SelfHosted(NotSelfHostedOnly = true)] public async Task GetBillingAsync(Guid organizationId) { if (!await currentContext.ViewBillingHistory(organizationId)) { return TypedResults.Unauthorized(); } var organization = await organizationRepository.GetByIdAsync(organizationId); if (organization == null) { return TypedResults.NotFound(); } var billingInfo = await paymentService.GetBillingAsync(organization); var response = new BillingResponseModel(billingInfo); return TypedResults.Ok(response); } }