diff --git a/src/sections/finances.ts b/src/sections/finances.ts index 642bd675..5f44fe85 100644 --- a/src/sections/finances.ts +++ b/src/sections/finances.ts @@ -1,11 +1,82 @@ -import { HttpClient, Resource } from '../http' +import { Codec, enumeration, GetInterface, number, optional, string } from 'purify-ts' + +import { ParsingError } from '../error' +import { HttpClient, RequestMeta, Resource } from '../http' +import { ensureArray, ensureString, mwsDate, nextToken as nextTokenCodec } from '../parsing' import { getServiceStatusByResource } from './shared' const FINANCES_API_VERSION = '2015-05-01' +interface ListFinancialEventGroupsParameters { + MaxResultsPerPage?: number + FinancialEventGroupStartedAfter: Date + FinancialEventGroupStartedBefore?: Date +} + +enum ProcessingStatusEnum { + Open = 'Open', + Closed = 'Closed', +} + +const ProcessingStatus = enumeration(ProcessingStatusEnum) + +const CurrencyAmount = Codec.interface({ + CurrencyCode: optional(string), + CurrencyAmount: optional(number), +}) + +const FinancialEventGroup = Codec.interface({ + FinancialEventGroupId: optional(string), + ProcessingStatus: optional(ProcessingStatus), + FundTransferStatus: optional(string), + OriginalTotal: optional(CurrencyAmount), + ConvertedTotal: optional(CurrencyAmount), + FundTransferDate: optional(mwsDate), + TraceId: optional(ensureString), + AccountTail: optional(ensureString), + BeginningBalance: optional(CurrencyAmount), + FinancialEventGroupStart: optional(mwsDate), + FinancialEventGroupEnd: optional(mwsDate), +}) + +const ListFinancialEventGroups = Codec.interface({ + NextToken: optional(nextTokenCodec('ListFinancialEventGroups')), + FinancialEventGroupList: ensureArray('FinancialEventGroup', FinancialEventGroup), +}) + +type ListFinancialEventGroups = GetInterface + +const ListFinancialEventGroupsResponse = Codec.interface({ + ListFinancialEventGroupsResponse: Codec.interface({ + ListFinancialEventGroupsResult: ListFinancialEventGroups, + }), +}) + export class Finances { constructor(private httpClient: HttpClient) {} + async listFinancialEventGroups( + parameters: ListFinancialEventGroupsParameters, + ): Promise<[ListFinancialEventGroups, RequestMeta]> { + const [response, meta] = await this.httpClient.request('POST', { + resource: Resource.Finances, + version: FINANCES_API_VERSION, + action: 'ListFinancialEventGroups', + parameters: { + MaxResultsPerPage: parameters.MaxResultsPerPage, + FinancialEventGroupStartedAfter: parameters.FinancialEventGroupStartedAfter.toISOString(), + FinancialEventGroupStartedBefore: parameters.FinancialEventGroupStartedBefore?.toISOString(), + }, + }) + + return ListFinancialEventGroupsResponse.decode(response).caseOf({ + Right: (x) => [x.ListFinancialEventGroupsResponse.ListFinancialEventGroupsResult, meta], + Left: (error) => { + throw new ParsingError(error) + }, + }) + } + async getServiceStatus() { return getServiceStatusByResource(this.httpClient, Resource.Finances, FINANCES_API_VERSION) } diff --git a/test/unit/__snapshots__/finances.test.ts.snap b/test/unit/__snapshots__/finances.test.ts.snap index d06a202f..b57fa06f 100644 --- a/test/unit/__snapshots__/finances.test.ts.snap +++ b/test/unit/__snapshots__/finances.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`sellers getServiceStatus returns a parsed model when the status response is valid 1`] = ` +exports[`finances getServiceStatus returns a parsed model when the status response is valid 1`] = ` Array [ Object { "Status": "GREEN", @@ -15,3 +15,42 @@ Array [ }, ] `; + +exports[`finances listFinancialEventGroups returns a next token and financial event groups list if succesful 1`] = ` +Array [ + Object { + "FinancialEventGroupList": Array [ + Object { + "AccountTail": "1212", + "BeginningBalance": Object { + "CurrencyCode": "USD", + }, + "ConvertedTotal": Object { + "CurrencyCode": "USD", + }, + "FinancialEventGroupEnd": 2014-09-09T07:30:00.000Z, + "FinancialEventGroupId": "22YgYW55IGNhcm5hbCBwbGVhEXAMPLE", + "FinancialEventGroupStart": 2014-09-01T07:30:00.000Z, + "FundTransferDate": 2014-09-09T07:30:00.000Z, + "FundTransferStatus": "Successful", + "OriginalTotal": Object { + "CurrencyCode": "USD", + }, + "ProcessingStatus": "Closed", + "TraceId": "128311029381HSADJEXAMPLE", + }, + ], + "NextToken": NextToken { + "action": "ListFinancialEventGroups", + "token": "2YgYW55IGNhcm5hbCBwbGVhcEXAMPLE", + }, + }, + Object { + "quotaMax": 1000, + "quotaRemaining": 999, + "quotaResetOn": 2020-04-06T10:22:23.582Z, + "requestId": "0", + "timestamp": 2020-05-06T09:22:23.582Z, + }, +] +`; diff --git a/test/unit/finances.test.ts b/test/unit/finances.test.ts index ac9bab2d..ac55fc98 100644 --- a/test/unit/finances.test.ts +++ b/test/unit/finances.test.ts @@ -53,9 +53,9 @@ describe('finances', () => { it('throws a parsing error when the status response is not valid', async () => { expect.assertions(1) - await expect(() => mockMwsFail.finances.listFinancialEventGroups()).rejects.toStrictEqual( - new ParsingError(parsingError), - ) + await expect(() => + mockMwsFail.finances.listFinancialEventGroups(parameters), + ).rejects.toStrictEqual(new ParsingError(parsingError)) }) })