Skip to content

feat(billing): discount application scope endpoint #953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/clients/src/api/billing/v2alpha1/api.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import {
} from '../../../bridge'
import {
unmarshalGetConsumptionResponse,
unmarshalListDiscountsResponse,
unmarshalListInvoicesResponse,
} from './marshalling.gen'
import type {
DownloadInvoiceRequest,
GetConsumptionRequest,
GetConsumptionResponse,
ListDiscountsRequest,
ListDiscountsResponse,
ListInvoicesRequest,
ListInvoicesResponse,
} from './types.gen'
Expand Down Expand Up @@ -94,4 +97,35 @@ export class API extends ParentAPI {
urlParams: urlParams(['dl', 1], ['file_type', request.fileType ?? 'pdf']),
responseType: 'blob',
})

protected pageOfListDiscounts = (
request: Readonly<ListDiscountsRequest> = {},
) =>
this.client.fetch<ListDiscountsResponse>(
{
method: 'GET',
path: `/billing/v2alpha1/discounts`,
urlParams: urlParams(
['order_by', request.orderBy ?? 'creation_date_desc'],
['organization_id', request.organizationId],
['page', request.page],
[
'page_size',
request.pageSize ?? this.client.settings.defaultPageSize,
],
),
},
unmarshalListDiscountsResponse,
)

/**
* List all user's discounts. List all discounts for an organization and
* usable categories/products/offers/references/regions/zones where the
* discount can be applied.
*
* @param request - The request {@link ListDiscountsRequest}
* @returns A Promise of ListDiscountsResponse
*/
listDiscounts = (request: Readonly<ListDiscountsRequest> = {}) =>
enrichForPagination('discounts', this.pageOfListDiscounts, request)
}
8 changes: 8 additions & 0 deletions packages/clients/src/api/billing/v2alpha1/index.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
// If you have any remark or suggestion do not hesitate to open an issue.
export { API } from './api.gen'
export type {
Discount,
DiscountCoupon,
DiscountDiscountMode,
DiscountFilter,
DiscountFilterType,
DownloadInvoiceRequest,
DownloadInvoiceRequestFileType,
GetConsumptionRequest,
GetConsumptionResponse,
GetConsumptionResponseConsumption,
Invoice,
InvoiceType,
ListDiscountsRequest,
ListDiscountsRequestOrderBy,
ListDiscountsResponse,
ListInvoicesRequest,
ListInvoicesRequestOrderBy,
ListInvoicesResponse,
Expand Down
60 changes: 60 additions & 0 deletions packages/clients/src/api/billing/v2alpha1/marshalling.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,59 @@ import {
unmarshalMoney,
} from '../../../bridge'
import type {
Discount,
DiscountCoupon,
DiscountFilter,
GetConsumptionResponse,
GetConsumptionResponseConsumption,
Invoice,
ListDiscountsResponse,
ListInvoicesResponse,
} from './types.gen'

const unmarshalDiscountCoupon = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'DiscountCoupon' failed as data isn't a dictionary.`,
)
}

return { description: data.description } as DiscountCoupon
}

const unmarshalDiscountFilter = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'DiscountFilter' failed as data isn't a dictionary.`,
)
}

return { type: data.type, value: data.value } as DiscountFilter
}

const unmarshalDiscount = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'Discount' failed as data isn't a dictionary.`,
)
}

return {
coupon: data.coupon ? unmarshalDiscountCoupon(data.coupon) : undefined,
creationDate: unmarshalDate(data.creation_date),
description: data.description,
filters: unmarshalArrayOfObject(data.filters, unmarshalDiscountFilter),
id: data.id,
mode: data.mode,
organizationId: data.organization_id,
startDate: unmarshalDate(data.start_date),
stopDate: unmarshalDate(data.stop_date),
value: data.value,
valueRemaining: data.value_remaining,
valueUsed: data.value_used,
} as Discount
}

const unmarshalGetConsumptionResponseConsumption = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
Expand Down Expand Up @@ -66,6 +113,19 @@ export const unmarshalGetConsumptionResponse = (data: unknown) => {
} as GetConsumptionResponse
}

export const unmarshalListDiscountsResponse = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'ListDiscountsResponse' failed as data isn't a dictionary.`,
)
}

return {
discounts: unmarshalArrayOfObject(data.discounts, unmarshalDiscount),
totalCount: data.total_count,
} as ListDiscountsResponse
}

export const unmarshalListInvoicesResponse = (data: unknown) => {
if (!isJSONObject(data)) {
throw new TypeError(
Expand Down
83 changes: 83 additions & 0 deletions packages/clients/src/api/billing/v2alpha1/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@
// If you have any remark or suggestion do not hesitate to open an issue.
import type { Money } from '../../../bridge'

export type DiscountDiscountMode =
| 'unknown_discount_mode'
| 'discount_mode_rate'
| 'discount_mode_value'
| 'discount_mode_splittable'

export type DiscountFilterType =
| 'unknown_type'
| 'product_category'
| 'product'
| 'product_offer'
| 'product_reference'
| 'region'
| 'zone'

export type DownloadInvoiceRequestFileType = 'pdf'

export type InvoiceType = 'unknown_type' | 'periodic' | 'purchase'

export type ListDiscountsRequestOrderBy =
| 'creation_date_desc'
| 'creation_date_asc'

export type ListInvoicesRequestOrderBy =
| 'invoice_number_desc'
| 'invoice_number_asc'
Expand All @@ -22,6 +41,48 @@ export type ListInvoicesRequestOrderBy =
| 'invoice_type_desc'
| 'invoice_type_asc'

/** Discount. */
export interface Discount {
/** The ID of the discount. */
id: string
/** The creation date of the discount. */
creationDate?: Date
/** The organization ID of the discount. */
organizationId: string
/** The description of the discount. */
description: string
/** The initial value of the discount. */
value: number
/** The value indicating how much of the discount has been used. */
valueUsed: number
/** The remaining value of the discount. */
valueRemaining: number
/** The mode of the discount. */
mode: DiscountDiscountMode
/** The start date of the discount. */
startDate?: Date
/** The stop date of the discount. */
stopDate?: Date
/** The description of the coupon. */
coupon?: DiscountCoupon
/** List of products/ranges/regions/zones to limit the usability of discounts. */
filters: DiscountFilter[]
}

/** Discount. coupon. */
export interface DiscountCoupon {
/** The description of the coupon. */
description?: string
}

/** Discount. filter. */
export interface DiscountFilter {
/** Type of the filter. */
type: DiscountFilterType
/** Value of filter, it can be a product/range/region/zone value. */
value: string
}

/** Get consumption response. */
export interface GetConsumptionResponse {
/** Detailed consumption list. */
Expand Down Expand Up @@ -64,6 +125,14 @@ export interface Invoice {
number: number
}

/** List discounts response. */
export interface ListDiscountsResponse {
/** Total number of discounts. */
totalCount: number
/** Paginated returned discounts. */
discounts: Discount[]
}

/** List invoices response. */
export interface ListInvoicesResponse {
/** Total number of invoices. */
Expand Down Expand Up @@ -106,3 +175,17 @@ export type DownloadInvoiceRequest = {
/** Wanted file type. */
fileType?: DownloadInvoiceRequestFileType
}

export type ListDiscountsRequest = {
/** Order discounts in the response by their description. */
orderBy?: ListDiscountsRequestOrderBy
/** Positive integer to choose the page to return. */
page?: number
/**
* Positive integer lower or equal to 100 to select the number of items to
* return.
*/
pageSize?: number
/** ID of the organization. */
organizationId?: string
}