Skip to content

Commit be05dd3

Browse files
authored
feat(billing): list discounts in v2beta1 and update list consumptions response (#1099)
1 parent cdca383 commit be05dd3

File tree

4 files changed

+194
-7
lines changed

4 files changed

+194
-7
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import {
1111
unmarshalInvoice,
1212
unmarshalListConsumptionsResponse,
13+
unmarshalListDiscountsResponse,
1314
unmarshalListInvoicesResponse,
1415
unmarshalListTaxesResponse,
1516
} from './marshalling.gen'
@@ -20,6 +21,8 @@ import type {
2021
Invoice,
2122
ListConsumptionsRequest,
2223
ListConsumptionsResponse,
24+
ListDiscountsRequest,
25+
ListDiscountsResponse,
2326
ListInvoicesRequest,
2427
ListInvoicesResponse,
2528
ListTaxesRequest,
@@ -201,4 +204,27 @@ export class API extends ParentAPI {
201204
urlParams: urlParams(['dl', 1], ['file_type', request.fileType]),
202205
responseType: 'blob',
203206
})
207+
208+
protected pageOfListDiscounts = (
209+
request: Readonly<ListDiscountsRequest> = {},
210+
) =>
211+
this.client.fetch<ListDiscountsResponse>(
212+
{
213+
method: 'GET',
214+
path: `/billing/v2beta1/discounts`,
215+
urlParams: urlParams(
216+
['order_by', request.orderBy],
217+
['organization_id', request.organizationId],
218+
['page', request.page],
219+
[
220+
'page_size',
221+
request.pageSize ?? this.client.settings.defaultPageSize,
222+
],
223+
),
224+
},
225+
unmarshalListDiscountsResponse,
226+
)
227+
228+
listDiscounts = (request: Readonly<ListDiscountsRequest> = {}) =>
229+
enrichForPagination('discounts', this.pageOfListDiscounts, request)
204230
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
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
ExportInvoicesRequest,
@@ -14,6 +19,9 @@ export type {
1419
ListConsumptionsRequestOrderBy,
1520
ListConsumptionsResponse,
1621
ListConsumptionsResponseConsumption,
22+
ListDiscountsRequest,
23+
ListDiscountsRequestOrderBy,
24+
ListDiscountsResponse,
1725
ListInvoicesRequest,
1826
ListInvoicesRequestOrderBy,
1927
ListInvoicesResponse,

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import {
77
unmarshalMoney,
88
} from '../../../bridge'
99
import type {
10+
Discount,
11+
DiscountCoupon,
12+
DiscountFilter,
1013
Invoice,
1114
ListConsumptionsResponse,
1215
ListConsumptionsResponseConsumption,
16+
ListDiscountsResponse,
1317
ListInvoicesResponse,
1418
ListTaxesResponse,
1519
ListTaxesResponseTax,
@@ -24,7 +28,6 @@ export const unmarshalInvoice = (data: unknown): Invoice => {
2428

2529
return {
2630
billingPeriod: unmarshalDate(data.billing_period),
27-
customerName: data.customer_name,
2831
dueDate: unmarshalDate(data.due_date),
2932
id: data.id,
3033
issuedDate: unmarshalDate(data.issued_date),
@@ -60,11 +63,13 @@ const unmarshalListConsumptionsResponseConsumption = (
6063
}
6164

6265
return {
66+
billedQuantity: data.billed_quantity,
6367
categoryName: data.category_name,
6468
productName: data.product_name,
6569
projectId: data.project_id,
6670
resourceName: data.resource_name,
6771
sku: data.sku,
72+
unit: data.unit,
6873
value: data.value ? unmarshalMoney(data.value) : undefined,
6974
} as ListConsumptionsResponseConsumption
7075
}
@@ -89,6 +94,69 @@ export const unmarshalListConsumptionsResponse = (
8994
} as ListConsumptionsResponse
9095
}
9196

97+
const unmarshalDiscountCoupon = (data: unknown): DiscountCoupon => {
98+
if (!isJSONObject(data)) {
99+
throw new TypeError(
100+
`Unmarshalling the type 'DiscountCoupon' failed as data isn't a dictionary.`,
101+
)
102+
}
103+
104+
return {
105+
description: data.description,
106+
} as DiscountCoupon
107+
}
108+
109+
const unmarshalDiscountFilter = (data: unknown): DiscountFilter => {
110+
if (!isJSONObject(data)) {
111+
throw new TypeError(
112+
`Unmarshalling the type 'DiscountFilter' failed as data isn't a dictionary.`,
113+
)
114+
}
115+
116+
return {
117+
type: data.type,
118+
value: data.value,
119+
} as DiscountFilter
120+
}
121+
122+
const unmarshalDiscount = (data: unknown): Discount => {
123+
if (!isJSONObject(data)) {
124+
throw new TypeError(
125+
`Unmarshalling the type 'Discount' failed as data isn't a dictionary.`,
126+
)
127+
}
128+
129+
return {
130+
coupon: data.coupon ? unmarshalDiscountCoupon(data.coupon) : undefined,
131+
creationDate: unmarshalDate(data.creation_date),
132+
description: data.description,
133+
filters: unmarshalArrayOfObject(data.filters, unmarshalDiscountFilter),
134+
id: data.id,
135+
mode: data.mode,
136+
organizationId: data.organization_id,
137+
startDate: unmarshalDate(data.start_date),
138+
stopDate: unmarshalDate(data.stop_date),
139+
value: data.value,
140+
valueRemaining: data.value_remaining,
141+
valueUsed: data.value_used,
142+
} as Discount
143+
}
144+
145+
export const unmarshalListDiscountsResponse = (
146+
data: unknown,
147+
): ListDiscountsResponse => {
148+
if (!isJSONObject(data)) {
149+
throw new TypeError(
150+
`Unmarshalling the type 'ListDiscountsResponse' failed as data isn't a dictionary.`,
151+
)
152+
}
153+
154+
return {
155+
discounts: unmarshalArrayOfObject(data.discounts, unmarshalDiscount),
156+
totalCount: data.total_count,
157+
} as ListDiscountsResponse
158+
}
159+
92160
export const unmarshalListInvoicesResponse = (
93161
data: unknown,
94162
): ListInvoicesResponse => {

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

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
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+
| 'category_name'
14+
| 'product_name'
15+
| 'product_range'
16+
| 'resource_name'
17+
| 'region'
18+
| 'zone'
19+
520
export type DownloadInvoiceRequestFileType = 'pdf'
621

722
export type ExportInvoicesRequestFileType = 'csv'
@@ -25,11 +40,15 @@ export type ExportInvoicesRequestOrderBy =
2540
export type InvoiceType = 'unknown_type' | 'periodic' | 'purchase'
2641

2742
export type ListConsumptionsRequestOrderBy =
28-
| 'updated_at_date_desc'
29-
| 'updated_at_date_asc'
43+
| 'updated_at_desc'
44+
| 'updated_at_asc'
3045
| 'category_name_desc'
3146
| 'category_name_asc'
3247

48+
export type ListDiscountsRequestOrderBy =
49+
| 'creation_date_desc'
50+
| 'creation_date_asc'
51+
3352
export type ListInvoicesRequestOrderBy =
3453
| 'invoice_number_desc'
3554
| 'invoice_number_asc'
@@ -47,11 +66,26 @@ export type ListInvoicesRequestOrderBy =
4766
| 'invoice_type_asc'
4867

4968
export type ListTaxesRequestOrderBy =
50-
| 'updated_at_date_desc'
51-
| 'updated_at_date_asc'
69+
| 'updated_at_desc'
70+
| 'updated_at_asc'
5271
| 'category_name_desc'
5372
| 'category_name_asc'
5473

74+
export interface DiscountCoupon {
75+
/** The description of the coupon. */
76+
description?: string
77+
}
78+
79+
export interface DiscountFilter {
80+
/**
81+
* Type of the filter (category name, product name, product range, resource
82+
* name, region or zone).
83+
*/
84+
type: DiscountFilterType
85+
/** Value of filter. */
86+
value: string
87+
}
88+
5589
export interface ListConsumptionsResponseConsumption {
5690
/** Monetary value of the consumption. */
5791
value?: Money
@@ -68,6 +102,37 @@ export interface ListConsumptionsResponseConsumption {
68102
projectId: string
69103
/** Name of consumption category. */
70104
categoryName: string
105+
/** Unit of consumed quantity. */
106+
unit: string
107+
/** Consumed quantity. */
108+
billedQuantity: string
109+
}
110+
111+
export interface Discount {
112+
/** The ID of the discount. */
113+
id: string
114+
/** The creation date of the discount. */
115+
creationDate?: Date
116+
/** The organization ID of the discount. */
117+
organizationId: string
118+
/** The description of the discount. */
119+
description: string
120+
/** The initial value of the discount. */
121+
value: number
122+
/** The value indicating how much of the discount has been used. */
123+
valueUsed: number
124+
/** The remaining value of the discount. */
125+
valueRemaining: number
126+
/** The mode of the discount. */
127+
mode: DiscountDiscountMode
128+
/** The start date of the discount. */
129+
startDate?: Date
130+
/** The stop date of the discount. */
131+
stopDate?: Date
132+
/** The description of the coupon. */
133+
coupon?: DiscountCoupon
134+
/** List of the discount scopes. */
135+
filters: DiscountFilter[]
71136
}
72137

73138
export interface Invoice {
@@ -100,9 +165,8 @@ export interface Invoice {
100165
state: string
101166
/** Invoice number. */
102167
number: number
168+
/** The name of the seller (Scaleway). */
103169
sellerName: string
104-
/** Customer name associated to this organization. */
105-
customerName: string
106170
}
107171

108172
export interface ListTaxesResponseTax {
@@ -203,6 +267,27 @@ export interface ListConsumptionsResponse {
203267
updatedAt?: Date
204268
}
205269

270+
export type ListDiscountsRequest = {
271+
/** Order discounts in the response by their description. */
272+
orderBy?: ListDiscountsRequestOrderBy
273+
/** Positive integer to choose the page to return. */
274+
page?: number
275+
/**
276+
* Positive integer lower or equal to 100 to select the number of items to
277+
* return.
278+
*/
279+
pageSize?: number
280+
/** ID of the organization. */
281+
organizationId?: string
282+
}
283+
284+
export interface ListDiscountsResponse {
285+
/** Total number of discounts. */
286+
totalCount: number
287+
/** Paginated returned discounts. */
288+
discounts: Discount[]
289+
}
290+
206291
export type ListInvoicesRequest = {
207292
/**
208293
* Organization ID. If specified, only invoices from this Organization will be

0 commit comments

Comments
 (0)