Skip to content
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
26 changes: 26 additions & 0 deletions packages_generated/billing/src/v2beta1/api.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
validatePathParam,
} from '@scaleway/sdk-client'
import {
unmarshalDiscount,
unmarshalInvoice,
unmarshalListConsumptionsResponse,
unmarshalListDiscountsResponse,
unmarshalListInvoicesResponse,
unmarshalListTaxesResponse,
} from './marshalling.gen'
import type {
Discount,
DownloadInvoiceRequest,
ExportInvoicesRequest,
GetInvoiceRequest,
Expand All @@ -27,6 +29,7 @@ import type {
ListInvoicesResponse,
ListTaxesRequest,
ListTaxesResponse,
RedeemCouponRequest,
} from './types.gen'

/**
Expand Down Expand Up @@ -227,4 +230,27 @@ export class API extends ParentAPI {
*/
listDiscounts = (request: Readonly<ListDiscountsRequest> = {}) =>
enrichForPagination('discounts', this.pageOfListDiscounts, request)

/**
* Redeem coupon. Redeem a coupon given the related code.
*
* @param request - The request {@link RedeemCouponRequest}
* @returns A Promise of Discount
*/
redeemCoupon = (request: Readonly<RedeemCouponRequest>) =>
this.client.fetch<Discount>(
{
method: 'POST',
path: `/billing/v2beta1/redeem-coupon`,
urlParams: urlParams(
['code', request.code],
[
'organization_id',
request.organizationId ??
this.client.settings.defaultOrganizationId,
],
),
},
unmarshalDiscount,
)
}
1 change: 1 addition & 0 deletions packages_generated/billing/src/v2beta1/index.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export type {
ListTaxesRequestOrderBy,
ListTaxesResponse,
ListTaxesResponseTax,
RedeemCouponRequest,
} from './types.gen'
98 changes: 49 additions & 49 deletions packages_generated/billing/src/v2beta1/marshalling.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,55 @@ import type {
ListTaxesResponseTax,
} from './types.gen'

const unmarshalDiscountCoupon = (data: unknown): DiscountCoupon => {
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): DiscountFilter => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'DiscountFilter' failed as data isn't a dictionary.`,
)
}

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

export const unmarshalDiscount = (data: unknown): Discount => {
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
}

export const unmarshalInvoice = (data: unknown): Invoice => {
if (!isJSONObject(data)) {
throw new TypeError(
Expand Down Expand Up @@ -94,55 +143,6 @@ export const unmarshalListConsumptionsResponse = (
} as ListConsumptionsResponse
}

const unmarshalDiscountCoupon = (data: unknown): DiscountCoupon => {
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): DiscountFilter => {
if (!isJSONObject(data)) {
throw new TypeError(
`Unmarshalling the type 'DiscountFilter' failed as data isn't a dictionary.`,
)
}

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

const unmarshalDiscount = (data: unknown): Discount => {
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
}

export const unmarshalListDiscountsResponse = (
data: unknown,
): ListDiscountsResponse => {
Expand Down
11 changes: 11 additions & 0 deletions packages_generated/billing/src/v2beta1/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,14 @@ export interface ListTaxesResponse {
*/
updatedAt?: Date
}

export type RedeemCouponRequest = {
/**
* The Organization ID of the discount.
*/
organizationId?: string
/**
* The code of the coupon to redeem.
*/
code: string
}
Loading