Skip to content

Commit

Permalink
refactor: billing api drop customer parts (#1945)
Browse files Browse the repository at this point in the history
  • Loading branch information
turip authored Dec 9, 2024
1 parent 4ac3621 commit 8eb5d7b
Show file tree
Hide file tree
Showing 16 changed files with 1,655 additions and 2,417 deletions.
2,088 changes: 905 additions & 1,183 deletions api/api.gen.go

Large diffs are not rendered by default.

376 changes: 116 additions & 260 deletions api/openapi.cloud.yaml

Large diffs are not rendered by default.

376 changes: 116 additions & 260 deletions api/openapi.yaml

Large diffs are not rendered by default.

103 changes: 26 additions & 77 deletions api/spec/src/billing/invoices.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,12 @@ using TypeSpec.OpenAPI;
namespace OpenMeter.Billing;

/**
* Invoices is a collection of endpoints that allow invoice operations without prior
* knowledge of the customer ID.
*/
@route("/api/v1/billing/invoices")
@tag("Billing (Experimental)")
interface InvoiceEndpoints {
/**
* List invoices
*/
@get
@summary("List invoices")
@operationId("listInvoices")
@extension("x-internal", true)
list(
@query
@summary("Filter by the customer ID or key")
customers?: ULID[],

...InvoiceListParams,
...OpenMeter.QueryPagination,
...OpenMeter.QueryLimitOffset,
...OpenMeter.QueryOrdering<InvoiceOrderBy>,
): OpenMeter.PaginatedResponse<Invoices.Invoice> | OpenMeter.CommonErrors;
}

/**
* CustomerInvoices is a collection of endpoints that allow invoice operations without prior
* InvoicesEndpoints is a collection of endpoints that allow invoice operations without prior
* knowledge of the invoice ID.
*/
@route("/api/v1/billing/invoices/{customerId}")
@route("/api/v1/billing/invoices")
@tag("Billing (Experimental)")
interface CustomerInvoicesEndpoints {
interface InvoicesEndpoints {
/**
* Create a new invoice from the pending line items.
*
Expand All @@ -53,9 +27,6 @@ interface CustomerInvoicesEndpoints {
@operationId("invoicePendingLinesAction")
@extension("x-internal", true)
invoicePendingLinesAction(
@path
customerId: ULID,

@body
request: InvoicePendingLinesActionInput,
): {
Expand All @@ -70,10 +41,9 @@ interface CustomerInvoicesEndpoints {
*/
@get
@summary("List invoices")
@operationId("listInvoicesByCustomer")
@operationId("listInvoices")
@extension("x-internal", true)
listInvoicesByCustomer(
@path customerId: ULID,
list(
...InvoiceListParams,
...OpenMeter.QueryPagination,
...OpenMeter.QueryLimitOffset,
Expand All @@ -83,27 +53,21 @@ interface CustomerInvoicesEndpoints {
/**
* Create a new pending line item (charge).
*
* This call is used to create a new pending line item for the customer without explicitly
* assigning it to an invoice.
*
* The line item will be either allocated to an existing invoice in gathering state or a new invoice is
* created for the line item.
* This call is used to create a new pending line item for the customer if required a new
* gathering invoice will be created.
*
* A new invoice will be created if:
* - there is no invoice in gathering state
* - the currency of the line item doesn't match the currency of any invoices in gathering state
*/
@post
@route("/lines")
@summary("Create line items")
@operationId("createInvoiceLineByCustomer")
@summary("Create pending line items")
@operationId("createPendingInvoiceLine")
@extension("x-internal", true)
createInvoiceLine(
@path
customerId: ULID,

createPendingInvoiceLine(
@body
request: Invoices.InvoiceLineCreate[],
request: Invoices.InvoicePendingLineCreate[],
): {
@statusCode _: 201;
@body body: Invoices.InvoiceLine[];
Expand All @@ -113,9 +77,9 @@ interface CustomerInvoicesEndpoints {
/**
* CustomerInvoice is a collection of endpoints that allow operations on a specific invoice.
*/
@route("/api/v1/billing/invoices/{customerId}/invoices/{invoiceId}")
@route("/api/v1/billing/invoices/{invoiceId}")
@tag("Billing (Experimental)")
interface CustomerInvoiceEndpoints {
interface InvoiceEndpoints {
/**
* Get an invoice by ID.
*/
Expand All @@ -124,9 +88,6 @@ interface CustomerInvoiceEndpoints {
@operationId("getInvoice")
@extension("x-internal", true)
getInvoice(
@path
customerId: ULID,

@path
invoiceId: ULID,

Expand All @@ -147,9 +108,6 @@ interface CustomerInvoiceEndpoints {
@operationId("deleteInvoice")
@extension("x-internal", true)
deleteInvoice(
@path
customerId: ULID,

@path
invoiceId: ULID,
): {
Expand All @@ -167,9 +125,6 @@ interface CustomerInvoiceEndpoints {
@operationId("recalculateInvoiceTaxAction")
@extension("x-internal", true)
recalculateTaxAction(
@path
customerId: ULID,

@path
invoiceId: ULID,
): Invoices.Invoice | OpenMeter.NotFoundError | OpenMeter.CommonErrors;
Expand All @@ -189,9 +144,6 @@ interface CustomerInvoiceEndpoints {
@operationId("approveInvoiceAction")
@extension("x-internal", true)
approveAction(
@path
customerId: ULID,

@path
invoiceId: ULID,
): Invoices.Invoice | OpenMeter.NotFoundError | OpenMeter.CommonErrors;
Expand All @@ -209,9 +161,6 @@ interface CustomerInvoiceEndpoints {
@operationId("voidInvoiceAction")
@extension("x-internal", true)
voidInvoiceAction(
@path
customerId: ULID,

@path
invoiceId: ULID,

Expand All @@ -232,9 +181,6 @@ interface CustomerInvoiceEndpoints {
@operationId("advanceInvoiceAction")
@extension("x-internal", true)
advanceAction(
@path
customerId: ULID,

@path
invoiceId: ULID,
): Invoices.Invoice | OpenMeter.NotFoundError | OpenMeter.CommonErrors;
Expand All @@ -250,9 +196,6 @@ interface CustomerInvoiceEndpoints {
@operationId("retryInvoiceAction")
@extension("x-internal", true)
retryAction(
@path
customerId: ULID,

@path
invoiceId: ULID,
): Invoices.Invoice | OpenMeter.NotFoundError | OpenMeter.CommonErrors;
Expand All @@ -268,7 +211,6 @@ interface CustomerInvoiceEndpoints {
@operationId("getInvoiceLine")
@extension("x-internal", true)
getInvoiceLine(
@path customerId: ULID,
@path invoiceId: ULID,
@path lineId: ULID,
): Invoices.InvoiceLine | OpenMeter.NotFoundError | OpenMeter.CommonErrors;
Expand All @@ -281,11 +223,7 @@ interface CustomerInvoiceEndpoints {
@summary("Delete an invoice line")
@operationId("deleteInvoiceLine")
@extension("x-internal", true)
deleteInvoiceLine(
@path customerId: ULID,
@path invoiceId: ULID,
@path lineId: ULID,
): {
deleteInvoiceLine(@path invoiceId: ULID, @path lineId: ULID): {
@statusCode _: 204;
} | OpenMeter.NotFoundError | OpenMeter.CommonErrors;

Expand All @@ -298,7 +236,6 @@ interface CustomerInvoiceEndpoints {
@operationId("updateInvoiceLine")
@extension("x-internal", true)
updateInvoiceLine(
@path customerId: ULID,
@path invoiceId: ULID,
@path lineId: ULID,

Expand Down Expand Up @@ -468,6 +405,12 @@ model InvoiceListParams {
*/
@query
expand?: InvoiceExpand[];

/**
* Filter by customer ID
*/
@query
customers?: ULID[];
}

/**
Expand Down Expand Up @@ -506,4 +449,10 @@ model InvoicePendingLinesActionInput {
*/
@visibility("create")
asOf?: DateTime;

/**
* The customer ID for which to create the invoice.
*/
@visibility("create")
customerId: ULID;
}
44 changes: 40 additions & 4 deletions api/spec/src/billing/invoices/invoice.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,47 @@ union InvoiceLineReplaceUpdate {
/**
* InvoiceLineCreate represents the create model for an invoice line.
*/
@friendlyName("InvoiceLineCreate")
@friendlyName("InvoicePendingLineCreate")
@discriminator("type")
union InvoiceLineCreate {
usageBased: Rest.Resource.ResourceCreateModel<InvoiceUsageBasedLine>,
flatFee: Rest.Resource.ResourceCreateModel<InvoiceFlatFeeLine>,
union InvoicePendingLineCreate {
usageBased: InvoiceUsageBasedPendingLineCreate,
flatFee: InvoiceFlatFeePendingLineCreate,
}

/**
* InvoiceUsageBasedLineCreateWithCustomer represents the create model for an invoice line that is sold to the customer based on usage.
*/
@friendlyName("InvoiceUsageBasedPendingLineCreate")
model InvoiceUsageBasedPendingLineCreate {
// Note: invoice is omitted as the line is assigned a new or existing gathering invoice
...Rest.Resource.ResourceCreateModel<OmitProperties<
InvoiceUsageBasedLine,
"invoice"
>>;

/**
* The customer this line item belongs to.
*/
@visibility("create")
customerId: ULID;
}

/**
* InvoiceFlatFeePendingLineCreate represents the create model for an invoice line that is sold to the customer as a manually added fee.
*/
@friendlyName("InvoiceFlatFeePendingLineCreate")
model InvoiceFlatFeePendingLineCreate {
// Note: invoice is omitted as the line is assigned a new or existing gathering invoice
...Rest.Resource.ResourceCreateModel<OmitProperties<
InvoiceFlatFeeLine,
"invoice"
>>;

/**
* The customer this line item belongs to.
*/
@visibility("create")
customerId: ULID;
}

/**
Expand Down
12 changes: 3 additions & 9 deletions api/spec/src/cloud/main.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,11 @@ namespace OpenMeterCloud.Billing {

@route("/api/v1/billing/invoices")
@tag("Billing (Experimental)")
interface Invoices extends OpenMeter.Billing.InvoiceEndpoints {}
interface InvoicesEndpoints extends OpenMeter.Billing.InvoicesEndpoints {}

@route("/api/v1/billing/invoices/{customerId}")
@route("/api/v1/billing/invoices/{invoiceId}")
@tag("Billing (Experimental)")
interface CustomerInvoicesEndpoints
extends OpenMeter.Billing.CustomerInvoicesEndpoints {}

@route("/api/v1/billing/invoices/{customerId}/invoices/{invoiceId}")
@tag("Billing (Experimental)")
interface CustomerInvoiceEndpoints
extends OpenMeter.Billing.CustomerInvoiceEndpoints {}
interface InvoiceEndpoints extends OpenMeter.Billing.InvoiceEndpoints {}
}

namespace OpenMeterCloud.ProductCatalog {
Expand Down
7 changes: 1 addition & 6 deletions openmeter/billing/httpdriver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"net/http"

"github.com/openmeterio/openmeter/api"
"github.com/openmeterio/openmeter/openmeter/billing"
"github.com/openmeterio/openmeter/openmeter/namespace/namespacedriver"
"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
Expand All @@ -27,7 +26,7 @@ type ProfileHandler interface {
}

type InvoiceLineHandler interface {
CreateLineByCustomer() CreateLineByCustomerHandler
CreatePendingLine() CreatePendingLineHandler
GetLine() GetLineHandler
DeleteLine() DeleteLineHandler
UpdateLine() UpdateLineHandler
Expand All @@ -39,10 +38,6 @@ type InvoiceHandler interface {
DeleteInvoice() DeleteInvoiceHandler
GetInvoice() GetInvoiceHandler
ProgressInvoice(ProgressAction) ProgressInvoiceHandler

// ConvertListInvoicesByCustomerToListInvoices converts the input parameters for listing invoices by customer, so that we
// can use the same handler for both listing all invoices and listing invoices by customer.
ConvertListInvoicesByCustomerToListInvoices(customerID string, params api.ListInvoicesByCustomerParams) api.ListInvoicesParams
}

type handler struct {
Expand Down
Loading

0 comments on commit 8eb5d7b

Please sign in to comment.