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

Commit

Permalink
feat: create GetFbaOutboundShipmentDetail
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Jul 7, 2020
1 parent 40c3b9f commit f259195
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 2 deletions.
108 changes: 107 additions & 1 deletion src/sections/shipment-invoicing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { HttpClient, Resource } from '../http'
import { Codec, enumeration, GetInterface, number, optional, string, unknown } from 'purify-ts'

import { ParsingError } from '../error'
import { HttpClient, RequestMeta, Resource } from '../http'
import { ensureArray, ensureString } from '../parsing'
import { getServiceStatusByResource } from './shared'

/**
Expand All @@ -9,9 +13,111 @@ import { getServiceStatusByResource } from './shared'
*/
const SHIPMENT_INVOICING_API_VERSION = '2018-11-01'
// const SHIPMENT_INVOICING_API_VERSION = '2018-09-01'

export interface GetFbaOutboundShipmentDetailParameters {
MarketplaceId: string
AmazonShipmentId: string
}

enum AddressTypeEnum {
Commercial = 'Commercial',
Residential = 'Residential',
}

const AddressType = enumeration(AddressTypeEnum)

/**
* Documentation has this as different from
* MerchantFulfillment's address
*/
export const Address = Codec.interface({
Name: string,
AddressLine1: optional(string),
AddressLine2: optional(string),
AddressLine3: optional(string),
City: optional(string),
County: optional(string),
District: optional(string),
StateOrRegion: optional(string),
PostalCode: optional(ensureString),
CountryCode: optional(string),
Phone: optional(ensureString),
AddressType: optional(AddressType),
})

export const Money = Codec.interface({
CurrencyCode: optional(string),
Amount: optional(ensureString),
})

const ShipmentItem = Codec.interface({
ASIN: string,
SellerSKU: optional(string),
OrderItemId: ensureString,
Title: optional(string),
QuantityOrdered: number,
ItemPrice: optional(Money),
ShippingPrice: optional(Money),
GiftWrapPrice: optional(Money),
ShippingDiscount: optional(Money),
PromotionDiscount: optional(Money),
SerialNumbers: optional(ensureArray('SerialNumber', ensureString)),
})

const ShipmentDetail = Codec.interface({
AmazonOrderId: string,
AmazonShipmentId: string,
PurchaseDate: string,
ShippingAddress: optional(Address),
PaymentMethodDetails: ensureArray('PaymentMethodDetail', string),
MarketplaceId: string,
BuyerName: optional(string),
BuyerCountry: optional(string),
// Documentation type it as just a string, but the sample response is an object,
// Unfortunately Amazon official docs don't have module for `ShipmentInvoicing`
BuyerTaxInfo: optional(unknown),
ShipmentItems: ensureArray('ShipmentItem', ShipmentItem),
WarehouseId: optional(string),
})

const GetFBAOutboundShipmentDetail = Codec.interface({
ShipmentDetail,
})

const GetFBAOutboundShipmentDetailResponse = Codec.interface({
GetFBAOutboundShipmentDetailResponse: Codec.interface({
GetFBAOutboundShipmentDetailResult: GetFBAOutboundShipmentDetail,
}),
})

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

async getFbaOutboundShipmentDetail(
parameters: GetFbaOutboundShipmentDetailParameters,
): Promise<[GetFBAOutboundShipmentDetail, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Sellers,
version: SHIPMENT_INVOICING_API_VERSION,
action: 'ListMarketplaceParticipationsByNextToken',
parameters: {
MarketplaceId: parameters.MarketplaceId,
AmazonShipmentId: parameters.AmazonShipmentId,
},
})

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

async getServiceStatus() {
return getServiceStatusByResource(
this.httpClient,
Expand Down
76 changes: 76 additions & 0 deletions test/unit/__snapshots__/shipment-invoicing.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,81 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shipmentInvoicing getFbaOutboundShipmentDetail returns the shipment detail if succesful 1`] = `
Array [
Object {
"ShipmentDetail": Object {
"AmazonOrderId": "701-0000000-0000000",
"AmazonShipmentId": "DrLqQwqvb",
"BuyerName": "Fulano da Silva",
"BuyerTaxInfo": Object {
"TaxClassifications": Object {
"TaxClassification": Object {
"Name": "CPF",
"Value": 99999999999,
},
},
},
"MarketplaceId": "A2Q3Y263D00KWC",
"PaymentMethodDetails": Array [
"CreditCard",
],
"PurchaseDate": "2018-04-16T14:44:38Z",
"ShipmentItems": Array [
Object {
"ASIN": "B078HVJB69",
"GiftWrapPrice": Object {
"Amount": "0",
"CurrencyCode": "BRL",
},
"ItemPrice": Object {
"Amount": "5099.99",
"CurrencyCode": "BRL",
},
"OrderItemId": "19441439621890",
"PromotionDiscount": Object {
"Amount": "0",
"CurrencyCode": "BRL",
},
"QuantityOrdered": 2,
"SellerSKU": "XY-1A0O-B1CD",
"SerialNumbers": Array [
"1234567890",
"1234567687",
],
"ShippingDiscount": Object {
"Amount": "0",
"CurrencyCode": "BRL",
},
"ShippingPrice": Object {
"Amount": "30",
"CurrencyCode": "BRL",
},
"Title": "iPhone X Apple Cinza Espacial, 64 GB e Câmera 12 MP",
},
],
"ShippingAddress": Object {
"AddressLine1": "Rua da Consolação, 700",
"AddressLine2": "Apto 32",
"City": "SAO PAULO",
"CountryCode": "BR",
"Name": "Beltrano da Silva",
"Phone": "11912345678",
"PostalCode": "00000-000",
"StateOrRegion": "SP",
},
"WarehouseId": "XXXX",
},
},
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": 2020-04-06T10:22:23.582Z,
"requestId": "0",
"timestamp": 2020-05-06T09:22:23.582Z,
},
]
`;

exports[`shipmentInvoicing getServiceStatus returns a parsed model when the status response is valid 1`] = `
Array [
Object {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/shipment-invoicing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('shipmentInvoicing', () => {
describe('getFbaOutboundShipmentDetail', () => {
const parameters = {
MarketplaceId: '',
AmazonShipmenId: '',
AmazonShipmentId: '',
}

it('returns the shipment detail if succesful', async () => {
Expand Down

0 comments on commit f259195

Please sign in to comment.