Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
feat: make submitFbaOutboundShipmentInvoice
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Jul 7, 2020
1 parent 103f3fd commit a5541be
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ export interface RequestResponse {
headers: Record<string, string>
}

/**
* `SubmitFeed` and `SubmitFBAOutboundShipmentInvoice` has the feed passed as an XML file in the body
* and the other parameters as query parameters
*/
const REQUEST_HAS_BODY = new Set(['SubmitFeed', 'SubmitFBAOutboundShipmentInvoice'])

const canonicalizeParameters = (parameters: CleanParameters): string => {
const sp = new URLSearchParams(parameters)
sp.sort()
Expand Down Expand Up @@ -315,11 +321,7 @@ export class HttpClient {
method,
headers,
}
/**
* `SubmitFeed` has the feed passed as an XML file in the body
* and the other parameters as query parameters
*/
} else if (body && info.action === 'SubmitFeed') {
} else if (body && REQUEST_HAS_BODY.has(info.action)) {
config = {
url: `${url}?${canonicalizeParameters(parametersWithSignature)}`,
method,
Expand Down
64 changes: 61 additions & 3 deletions src/sections/shipment-invoicing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Codec, enumeration, GetInterface, number, optional, string, unknown } from 'purify-ts'
import crypto from 'crypto'
import {
Codec,
enumeration,
exactly,
GetInterface,
number,
optional,
string,
unknown,
} from 'purify-ts'

import { ParsingError } from '../error'
import { HttpClient, RequestMeta, Resource } from '../http'
Expand Down Expand Up @@ -83,17 +93,65 @@ const GetFBAOutboundShipmentDetailResponse = Codec.interface({
}),
})

interface SubmitFBAOutboundShipmentInvoiceParameters {
MarketplaceId: string
AmazonShipmentId: string
InvoiceContent: string
}

const SubmitFBAOutboundShipmentInvoiceResult = optional(exactly(''))

type SubmitFBAOutboundShipmentInvoiceResult = GetInterface<
typeof SubmitFBAOutboundShipmentInvoiceResult
>

const SubmitFBAOutboundShipmentInvoiceResponse = Codec.interface({
SubmitFBAOutboundShipmentInvoiceResponse: Codec.interface({
SubmitFBAOutboundShipmentInvoiceResult,
}),
})

export type GetFBAOutboundShipmentDetail = GetInterface<typeof GetFBAOutboundShipmentDetail>
export class ShipmentInvoicing {
constructor(private httpClient: HttpClient) {}

async submitFbaOutboundShipmentInvoice(
parameters: SubmitFBAOutboundShipmentInvoiceParameters,
): Promise<[SubmitFBAOutboundShipmentInvoiceResult, RequestMeta]> {
const hash = crypto.createHash('md5').update(parameters.InvoiceContent).digest('base64')
const [response, meta] = await this.httpClient.request(
'POST',
{
resource: Resource.ShipmentInvoicing,
version: SHIPMENT_INVOICING_API_VERSION,
action: 'SubmitFBAOutboundShipmentInvoice',
parameters: {
MarketplaceId: parameters.MarketplaceId,
AmazonShipmentId: parameters.AmazonShipmentId,
ContentMD5Value: hash,
},
},
parameters.InvoiceContent,
)

return SubmitFBAOutboundShipmentInvoiceResponse.decode(response).caseOf({
Right: (x) => [
x.SubmitFBAOutboundShipmentInvoiceResponse.SubmitFBAOutboundShipmentInvoiceResult,
meta,
],
Left: (error) => {
throw new ParsingError(error)
},
})
}

async getFbaOutboundShipmentDetail(
parameters: GetFbaOutboundShipmentDetailParameters,
): Promise<[GetFBAOutboundShipmentDetail, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Sellers,
resource: Resource.ShipmentInvoicing,
version: SHIPMENT_INVOICING_API_VERSION,
action: 'ListMarketplaceParticipationsByNextToken',
action: 'GetFBAOutboundShipmentDetail',
parameters: {
MarketplaceId: parameters.MarketplaceId,
AmazonShipmentId: parameters.AmazonShipmentId,
Expand Down
13 changes: 13 additions & 0 deletions test/unit/__snapshots__/shipment-invoicing.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,16 @@ Array [
},
]
`;

exports[`shipmentInvoicing submitFbaOutboundShipmentInvoice returns the statndard response if succesful 1`] = `
Array [
undefined,
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": 2020-04-06T10:22:23.582Z,
"requestId": "0",
"timestamp": 2020-05-06T09:22:23.582Z,
},
]
`;

0 comments on commit a5541be

Please sign in to comment.