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

Commit

Permalink
feat: get report request list api
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Jun 5, 2020
1 parent c010368 commit db4d26d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
60 changes: 58 additions & 2 deletions src/sections/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { boolean, Codec, GetInterface, optional, string } from 'purify-ts'

import { ParsingError } from '../error'
import { HttpClient, RequestMeta, Resource } from '../http'
import { ensureString, mwsDate } from '../parsing'
import { ensureString, mwsDate, nextToken as nextTokenCodec } from '../parsing'
import { getServiceStatusByResource } from './shared'

const REPORTS_API_VERSION = '2009-01-01'
Expand All @@ -13,6 +13,7 @@ const REPORTS_API_VERSION = '2009-01-01'
*/
const ReportType = string
type ReportType = GetInterface<typeof ReportType>

interface RequestReportParameters {
ReportType: ReportType
StartDate?: Date
Expand All @@ -30,7 +31,7 @@ const ReportRequestInfo = Codec.interface({
Scheduled: optional(boolean),
SubmittedDate: optional(mwsDate),
ReportProcessingStatus: optional(string),
GeneratedReportId: optional(string),
GeneratedReportId: optional(ensureString),
StartedProcessingDate: optional(mwsDate),
CompletedDate: optional(mwsDate),
})
Expand All @@ -45,9 +46,64 @@ const RequestReportResponse = Codec.interface({
}),
})

type ReportProcessing =
| '_SUBMITTED_'
| '_IN_PROGRESS_'
| '_CANCELLED_'
| '_DONE_'
| '_DONE_NO_DATA_'
| 'All'
interface GetReportRequestListParameters {
ReportRequestIdList?: string[]
ReportTypeList?: ReportType[]
ReportProcessingStatusList?: ReportProcessing[]
MaxCount?: number
RequestedFromDate?: Date
RequestedToDate?: Date
}

const GetReportRequestListResult = Codec.interface({
NextToken: optional(nextTokenCodec('GetReportRequestList')),
HasNext: optional(boolean),
ReportRequestInfo: optional(ReportRequestInfo),
})

type GetReportRequestListResult = GetInterface<typeof GetReportRequestListResult>

const GetReportRequestListResponse = Codec.interface({
GetReportRequestListResponse: Codec.interface({
GetReportRequestListResult,
}),
})

export class Reports {
constructor(private httpClient: HttpClient) {}

async getReportRequestList(
parameters: GetReportRequestListParameters,
): Promise<[GetReportRequestListResult, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Report,
version: REPORTS_API_VERSION,
action: 'GetReportRequestList',
parameters: {
'ReportRequestIdList.Id': parameters.ReportRequestIdList,
'ReportTypeList.Type': parameters.ReportRequestIdList,
'ReportProcessingStatusList.Status': parameters.ReportProcessingStatusList,
MaxCount: parameters.MaxCount,
RequestedFromDate: parameters.RequestedFromDate?.toISOString(),
RequestedToDate: parameters.RequestedToDate?.toISOString(),
},
})

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

async requestReport(
parameters: RequestReportParameters,
): Promise<[ReportRequestInfo, RequestMeta]> {
Expand Down
35 changes: 33 additions & 2 deletions test/unit/__snapshots__/reports.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`reports getReportRequestList returns a parsed model when the response is valid 1`] = `
Array [
Object {
"HasNext": true,
"NextToken": NextToken {
"action": "GetReportRequestList",
"token": "2YgYW55IPQhcm5hbCBwbGVhc3VyZS4=",
},
"ReportRequestInfo": Object {
"CompletedDate": 2011-02-17T23:44:48.000Z,
"EndDate": 2011-02-13T02:10:39.000Z,
"GeneratedReportId": "3538561173",
"ReportProcessingStatus": "_DONE_",
"ReportRequestId": "2291326454",
"ReportType": "_GET_MERCHANT_LISTINGS_DATA_",
"Scheduled": false,
"StartDate": 2011-01-21T02:10:39.000Z,
"StartedProcessingDate": 2011-02-17T23:44:43.000Z,
"SubmittedDate": 2011-02-17T23:44:09.000Z,
},
},
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": 2020-04-06T10:22:23.582Z,
"requestId": "0",
"timestamp": 2020-05-06T09:22:23.582Z,
},
]
`;

exports[`reports getServiceStatus returns a parsed model when the status response is valid 1`] = `
Array [
Object {
Expand All @@ -9,9 +40,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,
},
]
`;
Expand Down

0 comments on commit db4d26d

Please sign in to comment.