-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(customers): add customers handler with count and getAll methods
- Loading branch information
Showing
6 changed files
with
369 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Client } from '../client' | ||
import * as errors from '../errors' | ||
|
||
export interface CustomersOptions { | ||
user?: string | ||
base?: string | ||
} | ||
|
||
export interface CustomersQuery { | ||
limit?: number | ||
uri?: string | ||
} | ||
|
||
export interface CustomersResponse { | ||
data: object[] | ||
metadata: object | ||
} | ||
|
||
export class Customers { | ||
endpoint: string | ||
http: Client | ||
public options: CustomersOptions | ||
|
||
constructor(options: CustomersOptions, http: Client) { | ||
this.options = options | ||
this.http = http | ||
|
||
this.endpoint = '/api/v0/customers' | ||
this.options.base = this.options.base || 'https://api.tillhub.com' | ||
} | ||
|
||
getAll(query?: CustomersQuery | undefined): Promise<CustomersResponse> { | ||
return new Promise(async (resolve, reject) => { | ||
let next | ||
|
||
try { | ||
let uri | ||
if (query && query.uri) { | ||
uri = query.uri | ||
} else { | ||
uri = `${this.options.base}${this.endpoint}/${this.options.user}` | ||
} | ||
|
||
const response = await this.http.getClient().get(uri) | ||
if (response.status !== 200) reject(new errors.CustomersFetchFailed()) | ||
|
||
if (response.data.cursor && response.data.cursor.next) { | ||
next = this.getAll({ uri: response.data.cursor.next }) | ||
} | ||
|
||
return resolve({ | ||
data: response.data.results, | ||
metadata: { cursor: response.data.cursor }, | ||
next | ||
} as CustomersResponse) | ||
} catch (err) { | ||
return reject(new errors.CustomersFetchFailed()) | ||
} | ||
}) | ||
} | ||
|
||
count(): Promise<CustomersResponse> { | ||
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) reject(new errors.CustomersCountFailed()) | ||
|
||
return resolve({ | ||
data: response.data.results, | ||
metadata: { count: response.data.count } | ||
} as CustomersResponse) | ||
} catch (err) { | ||
return reject(new errors.CustomersCountFailed()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
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 | ||
} | ||
|
||
describe('v0: Branches: can get count number of all customers', () => { | ||
it("Tillhub's branches are instantiable", async () => { | ||
if (process.env.SYSTEM_TEST !== 'true') { | ||
const mock = new MockAdapter(axios) | ||
const legacyId = '4564' | ||
|
||
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/customers/${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 customers = th.customers() | ||
|
||
expect(customers).toBeInstanceOf(v0.Customers) | ||
|
||
const { data } = await customers.count() | ||
|
||
expect(Array.isArray(data)).toBe(true) | ||
}) | ||
|
||
it('rejects on status codes that are not 200', async () => { | ||
if (process.env.SYSTEM_TEST !== 'true') { | ||
const mock = new MockAdapter(axios) | ||
const legacyId = '4564' | ||
|
||
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/customers/${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.customers().count() | ||
} catch (err) { | ||
expect(err.name).toBe('CustomersCountFailed') | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
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 | ||
} | ||
|
||
describe('v0: customers: can get all', () => { | ||
const legacyId = '4564' | ||
const mock = new MockAdapter(axios) | ||
afterEach(() => { | ||
mock.reset() | ||
}) | ||
|
||
it("Tillhub's customers 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/customers/${legacyId}`).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 customers = th.customers() | ||
|
||
expect(customers).toBeInstanceOf(v0.Customers) | ||
|
||
const { data } = await customers.getAll() | ||
|
||
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/customers/${legacyId}`).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.customers().getAll() | ||
} catch (err) { | ||
expect(err.name).toBe('CustomersFetchFailed') | ||
} | ||
}) | ||
}) |