Skip to content

Commit

Permalink
feat: balances meta handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jmy-kellogg committed Mar 1, 2019
1 parent dabc79a commit 5a9c8b2
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/errors.ts
Expand Up @@ -1387,3 +1387,10 @@ export class BalancesFetchOneFailed extends BaseError {
super(message, properties)
}
}

export class BalancesMetaFailed extends BaseError {
public name = 'BalancesMetaFailed'
constructor(public message: string = 'Could not fetch meta data for balances', properties?: any) {
super(message, properties)
}
}
24 changes: 24 additions & 0 deletions src/v1/balances.ts
Expand Up @@ -76,6 +76,30 @@ export class Balances {
})
}

meta(): Promise<BalancesResponse> {
return new Promise(async (resolve, reject) => {
let uri = `${this.options.base}${this.endpoint}/${this.options.user}/meta`

try {
const response = await this.http.getClient().get(uri)
if (response.status !== 200) return reject(new errors.BalancesMetaFailed(undefined, { status: response.status }))

if (!response.data.results[0]) {
return reject(
new errors.BalancesMetaFailed('could not get balances metadata unexpectedly')
)
}

return resolve({
data: response.data.results[0],
metadata: { count: response.data.count }
} as BalancesResponse)
} catch (err) {
return reject(new errors.BalancesMetaFailed())
}
})
}

async get(transactionId: string): Promise<BalancesResponse> {
const uri = `${this.options.base}${this.endpoint}/${this.options.user}/${transactionId}`

Expand Down
125 changes: 125 additions & 0 deletions test/balances/meta.test.ts
@@ -0,0 +1,125 @@
import * as dotenv from 'dotenv'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
dotenv.config()
import { TillhubClient, v1 } from '../../src/tillhub-js'

let user = {
username: 'test@example.com',
password: '12345678',
clientAccount: 'someuuid',
apiKey: '12345678'
}

if (process.env.SYSTEM_TEST) {
user.username = process.env.SYSTEM_TEST_USERNAME || user.username
user.password = process.env.SYSTEM_TEST_PASSWORD || user.password
user.clientAccount = process.env.SYSTEM_TEST_CLIENT_ACCOUNT_ID || user.clientAccount
user.apiKey = process.env.SYSTEM_TEST_API_KEY || user.apiKey
}

const legacyId = '4564'

describe('v1: Balances: can get count number of all balances', () => {
const mock = new MockAdapter(axios)
afterEach(() => {
mock.reset()
})
it("Tillhub's balances are instantiable", async () => {
if (process.env.SYSTEM_TEST !== 'true') {
mock.onPost('https://api.tillhub.com/api/v0/users/login').reply(function (config) {
return [
200,
{
token: '',
user: {
id: '123',
legacy_id: legacyId
}
}
]
})

mock
.onGet(`https://api.tillhub.com/api/v1/balances/${legacyId}/meta`)
.reply(function (config) {
return [
200,
{
count: 50,
results: [{ count: 50 }]
}
]
})
}

const options = {
credentials: {
username: user.username,
password: user.password
},
base: process.env.TILLHUB_BASE
}

const th = new TillhubClient()

th.init(options)
await th.auth.loginUsername({
username: user.username,
password: user.password
})

const balances = th.balances()

expect(balances).toBeInstanceOf(v1.Balances)

const { data } = await balances.meta()

expect(data).toEqual({ count: 50 })
})

it('rejects on status codes that are not 200', async () => {
if (process.env.SYSTEM_TEST !== 'true') {
mock.onPost('https://api.tillhub.com/api/v0/users/login').reply(function (config) {
return [
200,
{
token: '',
user: {
id: '123',
legacy_id: legacyId
}
}
]
})

mock
.onGet(`https://api.tillhub.com/api/v1/balances/${legacyId}/meta`)
.reply(function (config) {
return [205]
})
}

const options = {
credentials: {
username: user.username,
password: user.password
},
base: process.env.TILLHUB_BASE
}

const th = new TillhubClient()

th.init(options)
await th.auth.loginUsername({
username: user.username,
password: user.password
})

try {
await th.balances().meta()
} catch (err) {
expect(err.name).toBe('BalancesMetaFailed')
}
})
})

0 comments on commit 5a9c8b2

Please sign in to comment.