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

Commit

Permalink
feat: list financial events group
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Jun 18, 2020
1 parent 0ab2f7f commit ea86bc9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
73 changes: 72 additions & 1 deletion src/sections/finances.ts
Original file line number Diff line number Diff line change
@@ -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<typeof ListFinancialEventGroups>

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)
}
Expand Down
41 changes: 40 additions & 1 deletion test/unit/__snapshots__/finances.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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,
},
]
`;
6 changes: 3 additions & 3 deletions test/unit/finances.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})

Expand Down

0 comments on commit ea86bc9

Please sign in to comment.