Skip to content

Commit

Permalink
feat(analytics): added vouchers reports handler in analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
jmy-kellogg committed Nov 22, 2018
1 parent 2095f5c commit 3afabd0
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/errors.ts
Expand Up @@ -548,3 +548,10 @@ export class RefundsReportOneStaffFetchFailed extends BaseError {
super(message)
}
}

export class VouchersReportFetchFailed extends BaseError {
public name = 'VouchersReportFetchFailed'
constructor(public message: string = 'Could not fetch the vouchers report') {
super(message)
}
}
18 changes: 18 additions & 0 deletions src/v0/analytics.ts
Expand Up @@ -239,4 +239,22 @@ export class Analytics {
}
})
}

getVouchersReports(): Promise<AnalyticsResponse> {
return new Promise(async (resolve, reject) => {
try {
const uri = `${this.options.base}${this.endpoint}/${this.options.user}/reports/vouchers`

const response = await this.http.getClient().get(uri)
response.status !== 200 && reject(new errors.VouchersReportFetchFailed())

return resolve({
data: response.data.results,
metadata: { count: response.data.count }
} as AnalyticsResponse)
} catch (err) {
return reject(new errors.VouchersReportFetchFailed())
}
})
}
}
126 changes: 126 additions & 0 deletions test/analytics/get_vouchers_reports.test.ts
@@ -0,0 +1,126 @@
import * as dotenv from 'dotenv'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
dotenv.config()
import { TillhubClient, v0 } 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'

const mock = new MockAdapter(axios)
afterEach(() => {
mock.reset()
})

describe('v0: Analytics: reports gets all vouchers', () => {
it("Tillhub's Analytics 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/v0/analytics/${legacyId}/reports/vouchers`)
.reply(function(config) {
return [
200,
{
count: 1,
results: [{}]
}
]
})
}

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 analytics = th.analytics()

expect(analytics).toBeInstanceOf(v0.Analytics)

const { data } = await analytics.getVouchersReports()

expect(Array.isArray(data)).toBe(true)
})

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/v0/analytics/${legacyId}/reports/vouchers`)
.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.analytics().getVouchersReports()
} catch (err) {
expect(err.name).toBe('VouchersReportFetchFailed')
}
})
})

0 comments on commit 3afabd0

Please sign in to comment.