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

Commit

Permalink
feat: manage report schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Jun 11, 2020
1 parent 7612084 commit 7faa1ac
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
94 changes: 93 additions & 1 deletion src/sections/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import {

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

const REPORTS_API_VERSION = '2009-01-01'
/**
Expand Down Expand Up @@ -63,6 +69,44 @@ type ReportProcessing =
| '_DONE_'
| '_DONE_NO_DATA_'
| 'All'

enum ScheduleEnum {
_15_MINUTES_ = '_15_MINUTES_',
_30_MINUTES_ = '_30_MINUTES_',
_1_HOUR_ = '_1_HOUR_',
_2_HOURS_ = '_2_HOURS_',
_4_HOURS_ = '_4_HOURS_',
_8_HOURS_ = '_8_HOURS_',
_12_HOURS_ = '_12_HOURS_',
_1_DAY_ = '_1_DAY_',
_2_DAYS_ = '_2_DAYS_',
_72_HOURS_ = '_72_HOURS_',
_1_WEEK_ = '_1_WEEK_',
_14_DAYS_ = '_14_DAYS_',
_15_DAYS_ = '_15_DAYS_',
_30_DAYS_ = '_30_DAYS_',
_NEVER_ = '_NEVER_',
}

const ScheduleCodec = oneOfEnum(ScheduleEnum)

export type ScheduleType =
| '_15_MINUTES_'
| '_30_MINUTES_'
| '_1_HOUR_'
| '_2_HOURS_'
| '_4_HOURS_'
| '_8_HOURS_'
| '_12_HOURS_'
| '_1_DAY_'
| '_2_DAYS_'
| '_72_HOURS_'
| '_1_WEEK_'
| '_14_DAYS_'
| '_15_DAYS_'
| '_30_DAYS_'
| '_NEVER_'

interface GetReportRequestListParameters {
ReportRequestIdList?: string[]
ReportTypeList?: ReportType[]
Expand Down Expand Up @@ -211,9 +255,57 @@ const GetReportResponse = Codec.custom<string>({
type Report = string

type GetReportCount = GetInterface<typeof GetReportCount>

interface ManageReportScheduleParameters {
ReportType: ReportType
Schedule: ScheduleType
ScheduleDate?: Date
}

const ReportSchedule = Codec.interface({
ReportType: optional(ReportType),
Schedule: optional(ScheduleCodec),
ScheduleDate: optional(mwsDate),
})

const ManageReportSchedule = Codec.interface({
Count: number,
ReportSchedule: optional(ReportSchedule),
})

type ManageReportSchedule = GetInterface<typeof ManageReportSchedule>

const ManageReportScheduleResponse = Codec.interface({
ManageReportScheduleResponse: Codec.interface({
ManageReportScheduleResult: ManageReportSchedule,
}),
})

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

async manageReportSchedule(
parameters: ManageReportScheduleParameters,
): Promise<[ManageReportSchedule, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Reports,
version: REPORTS_API_VERSION,
action: 'ManageReportSchedule',
parameters: {
ReportType: parameters.ReportType,
Schedule: parameters.Schedule,
ScheduleDate: parameters.ScheduleDate?.toISOString(),
},
})

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

async getReport(parameters: { ReportId: string }): Promise<[Report, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Reports,
Expand Down
20 changes: 20 additions & 0 deletions test/unit/__snapshots__/reports.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ Array [
]
`;

exports[`reports manageReportSchedule returns count and report schedule if succesful 1`] = `
Array [
Object {
"Count": 1,
"ReportSchedule": Object {
"ReportType": "_GET_ORDERS_DATA_",
"Schedule": "_30_DAYS_",
"ScheduleDate": undefined,
},
},
Object {
"quotaMax": 1000,
"quotaRemaining": 999,
"quotaResetOn": 2020-04-06T10:22:23.582Z,
"requestId": "0",
"timestamp": 2020-05-06T09:22:23.582Z,
},
]
`;

exports[`reports requestReport returns a parsed model when the response is valid 1`] = `
Array [
Object {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/reports.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { amazonMarketplaces, HttpClient, ParsingError } from '../../src'
import { MWS } from '../../src/mws'
import { NextToken } from '../../src/parsing'
import { ScheduleType } from '../../src/sections/reports'
import { getFixture } from '../utils'

const httpConfig = {
Expand Down Expand Up @@ -39,7 +40,7 @@ describe('reports', () => {
describe('manageReportSchedule', () => {
const parameters = {
ReportType: '_GET_ORDERS_DATA_',
Schedule: '_30_DAYS_',
Schedule: '_30_DAYS_' as ScheduleType,
}

it('returns count and report schedule if succesful', async () => {
Expand Down

0 comments on commit 7faa1ac

Please sign in to comment.