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

Commit

Permalink
feat: getorder
Browse files Browse the repository at this point in the history
  • Loading branch information
gigobyte committed May 14, 2020
1 parent a0711fe commit 5d03ef2
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 97 deletions.
130 changes: 76 additions & 54 deletions src/sections/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,64 +119,59 @@ const Money = Codec.interface({
Amount: optional(number),
})

const Order = Codec.interface({
AmazonOrderId: string,
SellerOrderId: optional(string),
PurchaseDate: mwsDate,
LastUpdateDate: mwsDate,
OrderStatus: orderStatus,
FulfillmentChannel: optional(fulfillmentChannel),
SalesChannel: optional(string),
ShipServiceLevel: optional(string),
ShippingAddress: optional(Address),
OrderTotal: optional(Money),
NumberOfItemsShipped: optional(number),
NumberOfItemsUnshipped: optional(number),
PaymentExecutionDetail: optional(
ensureArray(
'PaymentExecutionDetailItem',
Codec.interface({
Payment: Money,
PaymentMethod: string,
}),
),
),
PaymentMethod: optional(string),
PaymentMethodDetails: ensureArray('PaymentMethodDetail', string),
IsReplacementOrder: optional(boolean),
ReplacedOrderId: optional(string),
MarketplaceId: optional(string),
BuyerEmail: optional(string),
BuyerName: optional(string),
BuyerCounty: optional(string),
BuyerTaxInfo: optional(BuyerTaxInfo),
ShipmentServiceLevelCategory: optional(string),
EasyShipShipmentStatus: optional(string),
OrderType: optional(string),
EarliestShipDate: optional(mwsDate),
LatestShipDate: optional(mwsDate),
EarliestDeliveryDate: optional(mwsDate),
LatestDeliveryDate: optional(mwsDate),
IsBusinessOrder: optional(boolean),
IsSoldByAB: optional(boolean),
PurchaseOrderNumber: optional(string),
IsPrime: optional(boolean),
IsPremiumOrder: optional(boolean),
IsGlobalExpressEnabled: optional(boolean),
PromiseResponseDueDate: optional(mwsDate),
IsEstimatedShipDateSet: optional(boolean),
})

const ListOrders = Codec.interface({
NextToken: optional(nextTokenCodec('ListOrders')),
LastUpdatedBefore: optional(mwsDate),
CreatedBefore: optional(mwsDate),
Orders: ensureArray(
'Order',
Codec.interface({
AmazonOrderId: string,
SellerOrderId: optional(string),
PurchaseDate: mwsDate,
LastUpdateDate: mwsDate,
OrderStatus: orderStatus,
FulfillmentChannel: optional(fulfillmentChannel),
SalesChannel: optional(string),
ShipServiceLevel: optional(string),
ShippingAddress: optional(Address),
OrderTotal: optional(Money),
NumberOfItemsShipped: optional(number),
NumberOfItemsUnshipped: optional(number),
PaymentExecutionDetail: optional(
ensureArray(
'PaymentExecutionDetailItem',
Codec.interface({
Payment: Money,
PaymentMethod: string,
}),
),
),
PaymentMethod: optional(string),
PaymentMethodDetails: optional(
Codec.interface({
PaymentMethodDetail: optional(string),
}),
),
IsReplacementOrder: optional(boolean),
ReplacedOrderId: optional(string),
MarketplaceId: optional(string),
BuyerEmail: optional(string),
BuyerName: optional(string),
BuyerCounty: optional(string),
BuyerTaxInfo: optional(BuyerTaxInfo),
ShipmentServiceLevelCategory: optional(string),
EasyShipShipmentStatus: optional(string),
OrderType: optional(string),
EarliestShipDate: optional(mwsDate),
LatestShipDate: optional(mwsDate),
EarliestDeliveryDate: optional(mwsDate),
LatestDeliveryDate: optional(mwsDate),
IsBusinessOrder: optional(boolean),
IsSoldByAB: optional(boolean),
PurchaseOrderNumber: optional(string),
IsPrime: optional(boolean),
IsPremiumOrder: optional(boolean),
IsGlobalExpressEnabled: optional(boolean),
PromiseResponseDueDate: optional(mwsDate),
IsEstimatedShipDateSet: optional(boolean),
}),
),
Orders: ensureArray('Order', Order),
})

const ListOrdersResponse = Codec.interface({
Expand All @@ -191,7 +186,16 @@ const ListOrdersByNextTokenResponse = Codec.interface({
}),
})

const GetOrderResponse = Codec.interface({
GetOrderResponse: Codec.interface({
GetOrderResult: Codec.interface({
Orders: ensureArray('Order', Order),
}),
}),
})

type ListOrders = GetInterface<typeof ListOrders>
type Order = GetInterface<typeof Order>

const canonicalizeParameters = (parameters: ListOrderParameters) => {
return {
Expand Down Expand Up @@ -251,6 +255,24 @@ export class Orders {
})
}

async getOrder(parameters: { AmazonOrderId: string[] }): Promise<[Order[], RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Orders,
version: ORDERS_API_VERSION,
action: 'GetOrder',
parameters: {
'AmazonOrderId.Id': parameters.AmazonOrderId,
},
})

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

async getServiceStatus() {
return getServiceStatusByResource(this.httpClient, Resource.Orders, ORDERS_API_VERSION)
}
Expand Down
33 changes: 33 additions & 0 deletions test/unit/__fixtures__/orders_get_order.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<GetOrderResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01">
<GetOrderResult>
<Orders>
<Order>
<AmazonOrderId>902-3159896-1390916</AmazonOrderId>
<PurchaseDate>2017-01-20T19:49:35Z</PurchaseDate>
<LastUpdateDate>2017-01-20T19:49:35Z</LastUpdateDate>
<OrderStatus>Pending</OrderStatus>
<FulfillmentChannel>MFN</FulfillmentChannel>
<NumberOfItemsShipped>0</NumberOfItemsShipped>
<NumberOfItemsUnshipped>0</NumberOfItemsUnshipped>
<PaymentMethod>Other</PaymentMethod>
<PaymentMethodDetails>
<PaymentMethodDetail>CreditCard</PaymentMethodDetail>
<PaymentMethodDetail>GiftCerificate</PaymentMethodDetail>
</PaymentMethodDetails>
<MarketplaceId>ATVPDKIKX0DER</MarketplaceId>>
<ShipmentServiceLevelCategory>Standard
</ShipmentServiceLevelCategory>
<OrderType>StandardOrder</OrderType>
<EarliestShipDate>2017-01-20T19:51:16Z</EarliestShipDate>
<LatestShipDate>2017-01-25T19:49:35Z</LatestShipDate>
<IsBusinessOrder>false</IsBusinessOrder>
<IsPrime>false</IsPrime>
<IsPremiumOrder>false</IsPremiumOrder>
<IsGlobalExpressEnabled>false</IsGlobalExpressEnabled>
</Order>
</Orders>
</GetOrderResult>
<ResponseMetadata>
<RequestId>88faca76-b600-46d2-b53c-0c8c4533e43a</RequestId>
</ResponseMetadata>
</GetOrderResponse>
88 changes: 72 additions & 16 deletions test/unit/__snapshots__/orders.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`sellers getServiceStatus returns a parsed model when the status response is valid 1`] = `
exports[`orders getOrder returns an array of orders when the response is valid 1`] = `
Array [
Array [
Object {
"AmazonOrderId": "902-3159896-1390916",
"BuyerCounty": undefined,
"BuyerEmail": undefined,
"BuyerName": undefined,
"BuyerTaxInfo": undefined,
"EarliestDeliveryDate": undefined,
"EarliestShipDate": 2017-01-20T19:51:16.000Z,
"EasyShipShipmentStatus": undefined,
"FulfillmentChannel": "MFN",
"IsBusinessOrder": false,
"IsEstimatedShipDateSet": undefined,
"IsGlobalExpressEnabled": false,
"IsPremiumOrder": false,
"IsPrime": false,
"IsReplacementOrder": undefined,
"IsSoldByAB": undefined,
"LastUpdateDate": 2017-01-20T19:49:35.000Z,
"LatestDeliveryDate": undefined,
"LatestShipDate": 2017-01-25T19:49:35.000Z,
"MarketplaceId": "ATVPDKIKX0DER",
"NumberOfItemsShipped": 0,
"NumberOfItemsUnshipped": 0,
"OrderStatus": "Pending",
"OrderTotal": undefined,
"OrderType": "StandardOrder",
"PaymentExecutionDetail": undefined,
"PaymentMethod": "Other",
"PaymentMethodDetails": Array [
"CreditCard",
"GiftCerificate",
],
"PromiseResponseDueDate": undefined,
"PurchaseDate": 2017-01-20T19:49:35.000Z,
"PurchaseOrderNumber": undefined,
"ReplacedOrderId": undefined,
"SalesChannel": undefined,
"SellerOrderId": undefined,
"ShipServiceLevel": undefined,
"ShipmentServiceLevelCategory": "Standard",
"ShippingAddress": undefined,
},
],
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": "2020-04-06T10:22:23.582Z",
"requestId": "0",
"timestamp": "2020-05-06T09:22:23.582Z",
},
]
`;

exports[`orders getServiceStatus returns a parsed model when the status response is valid 1`] = `
Array [
Object {
"Status": "GREEN",
Expand All @@ -9,14 +65,14 @@ Array [
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": "2020-05-06T10:22:23.582Z",
"quotaResetOn": "2020-04-06T10:22:23.582Z",
"requestId": "0",
"timestamp": "2020-05-06T08:22:23.582Z",
"timestamp": "2020-05-06T09:22:23.582Z",
},
]
`;

exports[`sellers listMarketplaceParticipations returns a parsed model when the response is valid 1`] = `
exports[`orders listOrders returns a parsed model when the response is valid 1`] = `
Array [
Object {
"CreatedBefore": undefined,
Expand Down Expand Up @@ -66,9 +122,9 @@ Array [
"OrderType": "StandardOrder",
"PaymentExecutionDetail": undefined,
"PaymentMethod": "Other",
"PaymentMethodDetails": Object {
"PaymentMethodDetail": "CreditCard",
},
"PaymentMethodDetails": Array [
"CreditCard",
],
"PromiseResponseDueDate": undefined,
"PurchaseDate": 2017-02-20T19:49:35.000Z,
"PurchaseOrderNumber": "PO12345678",
Expand Down Expand Up @@ -133,9 +189,9 @@ Array [
"OrderType": undefined,
"PaymentExecutionDetail": undefined,
"PaymentMethod": "Other",
"PaymentMethodDetails": Object {
"PaymentMethodDetail": "CreditCard",
},
"PaymentMethodDetails": Array [
"CreditCard",
],
"PromiseResponseDueDate": undefined,
"PurchaseDate": 2017-02-20T19:49:35.000Z,
"PurchaseOrderNumber": undefined,
Expand Down Expand Up @@ -213,9 +269,9 @@ Array [
},
],
"PaymentMethod": "COD",
"PaymentMethodDetails": Object {
"PaymentMethodDetail": "COD",
},
"PaymentMethodDetails": Array [
"COD",
],
"PromiseResponseDueDate": 2017-08-31T23:58:44.000Z,
"PurchaseDate": 2017-02-05T00:06:07.000Z,
"PurchaseOrderNumber": undefined,
Expand Down Expand Up @@ -252,7 +308,7 @@ Array [
]
`;

exports[`sellers listMarketplaceParticipationsByNextToken returns a parsed model when the response is valid 1`] = `
exports[`orders listOrdersByNextToken returns a parsed model when the response is valid 1`] = `
Array [
Object {
"CreatedBefore": undefined,
Expand All @@ -266,9 +322,9 @@ Array [
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": "2020-05-06T10:22:23.582Z",
"quotaResetOn": "2020-04-06T10:22:23.582Z",
"requestId": "0",
"timestamp": "2020-05-06T08:22:23.582Z",
"timestamp": "2020-05-06T09:22:23.582Z",
},
]
`;
6 changes: 3 additions & 3 deletions test/unit/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('httpClient', () => {
it('should throw a HttpError on failure', async () => {
expect.assertions(1)

const httpClient = httpClientThatThrows(getFixture('error-response'))
const httpClient = httpClientThatThrows(getFixture('error_response'))

await expect(() => httpClient.request('POST', mockRequest)).rejects.toStrictEqual(
new InvalidParameterValue('GetServiceStatus request failed'),
Expand All @@ -45,7 +45,7 @@ describe('httpClient', () => {
it('should throw a HttpError that can be handled', async () => {
expect.assertions(7)

const httpClient = httpClientThatThrows(getFixture('error-response'))
const httpClient = httpClientThatThrows(getFixture('error_response'))

let expectedError!: InvalidParameterValue

Expand Down Expand Up @@ -79,7 +79,7 @@ describe('httpClient', () => {
it('should propagate API errors that are not valid', async () => {
expect.assertions(1)

const fixture = getFixture('invalid-error-response')
const fixture = getFixture('invalid_error_response')
const httpClient = httpClientThatThrows(fixture)

await expect(() => httpClient.request('POST', mockRequest)).rejects.toStrictEqual(fixture)
Expand Down

0 comments on commit 5d03ef2

Please sign in to comment.