diff --git a/packages_generated/billing/src/v2beta1/api.gen.ts b/packages_generated/billing/src/v2beta1/api.gen.ts index 2b99c5cf6..70b412987 100644 --- a/packages_generated/billing/src/v2beta1/api.gen.ts +++ b/packages_generated/billing/src/v2beta1/api.gen.ts @@ -8,6 +8,7 @@ import { validatePathParam, } from '@scaleway/sdk-client' import { + unmarshalDiscount, unmarshalInvoice, unmarshalListConsumptionsResponse, unmarshalListDiscountsResponse, @@ -15,6 +16,7 @@ import { unmarshalListTaxesResponse, } from './marshalling.gen' import type { + Discount, DownloadInvoiceRequest, ExportInvoicesRequest, GetInvoiceRequest, @@ -27,6 +29,7 @@ import type { ListInvoicesResponse, ListTaxesRequest, ListTaxesResponse, + RedeemCouponRequest, } from './types.gen' /** @@ -227,4 +230,27 @@ export class API extends ParentAPI { */ listDiscounts = (request: Readonly = {}) => 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) => + this.client.fetch( + { + method: 'POST', + path: `/billing/v2beta1/redeem-coupon`, + urlParams: urlParams( + ['code', request.code], + [ + 'organization_id', + request.organizationId ?? + this.client.settings.defaultOrganizationId, + ], + ), + }, + unmarshalDiscount, + ) } diff --git a/packages_generated/billing/src/v2beta1/index.gen.ts b/packages_generated/billing/src/v2beta1/index.gen.ts index e946ff71a..fb74b2fb3 100644 --- a/packages_generated/billing/src/v2beta1/index.gen.ts +++ b/packages_generated/billing/src/v2beta1/index.gen.ts @@ -30,4 +30,5 @@ export type { ListTaxesRequestOrderBy, ListTaxesResponse, ListTaxesResponseTax, + RedeemCouponRequest, } from './types.gen' diff --git a/packages_generated/billing/src/v2beta1/marshalling.gen.ts b/packages_generated/billing/src/v2beta1/marshalling.gen.ts index ca982b47b..91a5ffcd8 100644 --- a/packages_generated/billing/src/v2beta1/marshalling.gen.ts +++ b/packages_generated/billing/src/v2beta1/marshalling.gen.ts @@ -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( @@ -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 => { diff --git a/packages_generated/billing/src/v2beta1/types.gen.ts b/packages_generated/billing/src/v2beta1/types.gen.ts index 9553d0e0b..97fee5c39 100644 --- a/packages_generated/billing/src/v2beta1/types.gen.ts +++ b/packages_generated/billing/src/v2beta1/types.gen.ts @@ -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 +}