Skip to content

Commit 5300fc3

Browse files
authored
feat(billing): discount application scope endpoint (#953)
1 parent 529c0d3 commit 5300fc3

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

packages/clients/src/api/billing/v2alpha1/api.gen.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import {
88
} from '../../../bridge'
99
import {
1010
unmarshalGetConsumptionResponse,
11+
unmarshalListDiscountsResponse,
1112
unmarshalListInvoicesResponse,
1213
} from './marshalling.gen'
1314
import type {
1415
DownloadInvoiceRequest,
1516
GetConsumptionRequest,
1617
GetConsumptionResponse,
18+
ListDiscountsRequest,
19+
ListDiscountsResponse,
1720
ListInvoicesRequest,
1821
ListInvoicesResponse,
1922
} from './types.gen'
@@ -94,4 +97,35 @@ export class API extends ParentAPI {
9497
urlParams: urlParams(['dl', 1], ['file_type', request.fileType ?? 'pdf']),
9598
responseType: 'blob',
9699
})
100+
101+
protected pageOfListDiscounts = (
102+
request: Readonly<ListDiscountsRequest> = {},
103+
) =>
104+
this.client.fetch<ListDiscountsResponse>(
105+
{
106+
method: 'GET',
107+
path: `/billing/v2alpha1/discounts`,
108+
urlParams: urlParams(
109+
['order_by', request.orderBy ?? 'creation_date_desc'],
110+
['organization_id', request.organizationId],
111+
['page', request.page],
112+
[
113+
'page_size',
114+
request.pageSize ?? this.client.settings.defaultPageSize,
115+
],
116+
),
117+
},
118+
unmarshalListDiscountsResponse,
119+
)
120+
121+
/**
122+
* List all user's discounts. List all discounts for an organization and
123+
* usable categories/products/offers/references/regions/zones where the
124+
* discount can be applied.
125+
*
126+
* @param request - The request {@link ListDiscountsRequest}
127+
* @returns A Promise of ListDiscountsResponse
128+
*/
129+
listDiscounts = (request: Readonly<ListDiscountsRequest> = {}) =>
130+
enrichForPagination('discounts', this.pageOfListDiscounts, request)
97131
}

packages/clients/src/api/billing/v2alpha1/index.gen.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
// If you have any remark or suggestion do not hesitate to open an issue.
33
export { API } from './api.gen'
44
export type {
5+
Discount,
6+
DiscountCoupon,
7+
DiscountDiscountMode,
8+
DiscountFilter,
9+
DiscountFilterType,
510
DownloadInvoiceRequest,
611
DownloadInvoiceRequestFileType,
712
GetConsumptionRequest,
813
GetConsumptionResponse,
914
GetConsumptionResponseConsumption,
1015
Invoice,
1116
InvoiceType,
17+
ListDiscountsRequest,
18+
ListDiscountsRequestOrderBy,
19+
ListDiscountsResponse,
1220
ListInvoicesRequest,
1321
ListInvoicesRequestOrderBy,
1422
ListInvoicesResponse,

packages/clients/src/api/billing/v2alpha1/marshalling.gen.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,59 @@ import {
77
unmarshalMoney,
88
} from '../../../bridge'
99
import type {
10+
Discount,
11+
DiscountCoupon,
12+
DiscountFilter,
1013
GetConsumptionResponse,
1114
GetConsumptionResponseConsumption,
1215
Invoice,
16+
ListDiscountsResponse,
1317
ListInvoicesResponse,
1418
} from './types.gen'
1519

20+
const unmarshalDiscountCoupon = (data: unknown) => {
21+
if (!isJSONObject(data)) {
22+
throw new TypeError(
23+
`Unmarshalling the type 'DiscountCoupon' failed as data isn't a dictionary.`,
24+
)
25+
}
26+
27+
return { description: data.description } as DiscountCoupon
28+
}
29+
30+
const unmarshalDiscountFilter = (data: unknown) => {
31+
if (!isJSONObject(data)) {
32+
throw new TypeError(
33+
`Unmarshalling the type 'DiscountFilter' failed as data isn't a dictionary.`,
34+
)
35+
}
36+
37+
return { type: data.type, value: data.value } as DiscountFilter
38+
}
39+
40+
const unmarshalDiscount = (data: unknown) => {
41+
if (!isJSONObject(data)) {
42+
throw new TypeError(
43+
`Unmarshalling the type 'Discount' failed as data isn't a dictionary.`,
44+
)
45+
}
46+
47+
return {
48+
coupon: data.coupon ? unmarshalDiscountCoupon(data.coupon) : undefined,
49+
creationDate: unmarshalDate(data.creation_date),
50+
description: data.description,
51+
filters: unmarshalArrayOfObject(data.filters, unmarshalDiscountFilter),
52+
id: data.id,
53+
mode: data.mode,
54+
organizationId: data.organization_id,
55+
startDate: unmarshalDate(data.start_date),
56+
stopDate: unmarshalDate(data.stop_date),
57+
value: data.value,
58+
valueRemaining: data.value_remaining,
59+
valueUsed: data.value_used,
60+
} as Discount
61+
}
62+
1663
const unmarshalGetConsumptionResponseConsumption = (data: unknown) => {
1764
if (!isJSONObject(data)) {
1865
throw new TypeError(
@@ -66,6 +113,19 @@ export const unmarshalGetConsumptionResponse = (data: unknown) => {
66113
} as GetConsumptionResponse
67114
}
68115

116+
export const unmarshalListDiscountsResponse = (data: unknown) => {
117+
if (!isJSONObject(data)) {
118+
throw new TypeError(
119+
`Unmarshalling the type 'ListDiscountsResponse' failed as data isn't a dictionary.`,
120+
)
121+
}
122+
123+
return {
124+
discounts: unmarshalArrayOfObject(data.discounts, unmarshalDiscount),
125+
totalCount: data.total_count,
126+
} as ListDiscountsResponse
127+
}
128+
69129
export const unmarshalListInvoicesResponse = (data: unknown) => {
70130
if (!isJSONObject(data)) {
71131
throw new TypeError(

packages/clients/src/api/billing/v2alpha1/types.gen.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,29 @@
22
// If you have any remark or suggestion do not hesitate to open an issue.
33
import type { Money } from '../../../bridge'
44

5+
export type DiscountDiscountMode =
6+
| 'unknown_discount_mode'
7+
| 'discount_mode_rate'
8+
| 'discount_mode_value'
9+
| 'discount_mode_splittable'
10+
11+
export type DiscountFilterType =
12+
| 'unknown_type'
13+
| 'product_category'
14+
| 'product'
15+
| 'product_offer'
16+
| 'product_reference'
17+
| 'region'
18+
| 'zone'
19+
520
export type DownloadInvoiceRequestFileType = 'pdf'
621

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

24+
export type ListDiscountsRequestOrderBy =
25+
| 'creation_date_desc'
26+
| 'creation_date_asc'
27+
928
export type ListInvoicesRequestOrderBy =
1029
| 'invoice_number_desc'
1130
| 'invoice_number_asc'
@@ -22,6 +41,48 @@ export type ListInvoicesRequestOrderBy =
2241
| 'invoice_type_desc'
2342
| 'invoice_type_asc'
2443

44+
/** Discount. */
45+
export interface Discount {
46+
/** The ID of the discount. */
47+
id: string
48+
/** The creation date of the discount. */
49+
creationDate?: Date
50+
/** The organization ID of the discount. */
51+
organizationId: string
52+
/** The description of the discount. */
53+
description: string
54+
/** The initial value of the discount. */
55+
value: number
56+
/** The value indicating how much of the discount has been used. */
57+
valueUsed: number
58+
/** The remaining value of the discount. */
59+
valueRemaining: number
60+
/** The mode of the discount. */
61+
mode: DiscountDiscountMode
62+
/** The start date of the discount. */
63+
startDate?: Date
64+
/** The stop date of the discount. */
65+
stopDate?: Date
66+
/** The description of the coupon. */
67+
coupon?: DiscountCoupon
68+
/** List of products/ranges/regions/zones to limit the usability of discounts. */
69+
filters: DiscountFilter[]
70+
}
71+
72+
/** Discount. coupon. */
73+
export interface DiscountCoupon {
74+
/** The description of the coupon. */
75+
description?: string
76+
}
77+
78+
/** Discount. filter. */
79+
export interface DiscountFilter {
80+
/** Type of the filter. */
81+
type: DiscountFilterType
82+
/** Value of filter, it can be a product/range/region/zone value. */
83+
value: string
84+
}
85+
2586
/** Get consumption response. */
2687
export interface GetConsumptionResponse {
2788
/** Detailed consumption list. */
@@ -64,6 +125,14 @@ export interface Invoice {
64125
number: number
65126
}
66127

128+
/** List discounts response. */
129+
export interface ListDiscountsResponse {
130+
/** Total number of discounts. */
131+
totalCount: number
132+
/** Paginated returned discounts. */
133+
discounts: Discount[]
134+
}
135+
67136
/** List invoices response. */
68137
export interface ListInvoicesResponse {
69138
/** Total number of invoices. */
@@ -106,3 +175,17 @@ export type DownloadInvoiceRequest = {
106175
/** Wanted file type. */
107176
fileType?: DownloadInvoiceRequestFileType
108177
}
178+
179+
export type ListDiscountsRequest = {
180+
/** Order discounts in the response by their description. */
181+
orderBy?: ListDiscountsRequestOrderBy
182+
/** Positive integer to choose the page to return. */
183+
page?: number
184+
/**
185+
* Positive integer lower or equal to 100 to select the number of items to
186+
* return.
187+
*/
188+
pageSize?: number
189+
/** ID of the organization. */
190+
organizationId?: string
191+
}

0 commit comments

Comments
 (0)