Skip to content

Commit

Permalink
fix: Change all delivery IDs to be moved out of query in request object
Browse files Browse the repository at this point in the history
  • Loading branch information
qtotuan committed Sep 13, 2018
1 parent 22212e4 commit 903f5e1
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 72 deletions.
97 changes: 46 additions & 51 deletions src/v0/deliveries.ts
@@ -1,51 +1,25 @@
import { Client } from '../client'
import * as errors from '../errors'

export interface DeliveriesCreateRequestObject {
query?: DeliveriesCreateQuery
body: DeliveriesCreateBody
}

export interface DeliveriesUpdateRequestObject {
query: DeliveriesUpdateQuery
body: DeliveriesUpdateBody
}

export interface DeliveriesOptions {
user?: string
base?: string
}

export interface DeliveriesGetAllQuery {
export interface DeliveriesQuery {
limit?: number
embed?: string[]
uri?: string
}

export interface DeliveriesGetOneQuery {
export interface DeliveriesGetOneRequestObject {
deliveryId: string
embed?: string[]
query?: DeliveriesQuery
}

export interface DeliveriesCreateQuery {
limit?: number
embed?: string[]
}

export interface DeliveriesUpdateQuery {
deliveryId: string
embed?: string[]
}

export interface DeliveriesDeleteQuery {
deliveryId: string
}

export interface DeliveriesResponse {
data: object[]
metadata: object
next?: Promise<DeliveriesResponse>
msg?: string
export interface DeliveriesCreateRequestObject {
body: DeliveriesCreateBody
query?: DeliveriesQuery
}

export interface DeliveriesCreateBody {
Expand Down Expand Up @@ -74,6 +48,12 @@ export interface DeliveriesCreateBody {
status?: string | null
}

export interface DeliveriesUpdateRequestObject {
body: DeliveriesUpdateBody
deliveryId: string
query?: DeliveriesQuery
}

export interface DeliveriesUpdateBody {
order?: string | null
open?: boolean
Expand All @@ -99,6 +79,18 @@ export interface DeliveriesUpdateBody {
status?: string | null
}

export interface DeliveriesSimpleUpdateRequestBody {
deliveryId: string
query?: DeliveriesQuery
}

export interface DeliveriesResponse {
data: object[]
metadata: object
next?: Promise<DeliveriesResponse>
msg?: string
}

export class Deliveries {
endpoint: string
http: Client
Expand All @@ -112,7 +104,7 @@ export class Deliveries {
this.options.base = this.options.base || 'https://api.tillhub.com'
}

getAll(query?: DeliveriesGetAllQuery | undefined): Promise<DeliveriesResponse> {
getAll(query?: DeliveriesQuery | undefined): Promise<DeliveriesResponse> {
return new Promise(async (resolve, reject) => {
let uri
let next
Expand Down Expand Up @@ -151,12 +143,13 @@ export class Deliveries {
})
}

getOne(query: DeliveriesGetOneQuery): Promise<DeliveriesResponse> {
getOne(requestObject: DeliveriesGetOneRequestObject): Promise<DeliveriesResponse> {
return new Promise(async (resolve, reject) => {
let uri = `${this.options.base}${this.endpoint}/${this.options.user}/${query.deliveryId}`
const { deliveryId, query } = requestObject
let uri = `${this.options.base}${this.endpoint}/${this.options.user}/${deliveryId}`

try {
if (query.embed) {
if (query && query.embed) {
const queryString = query.embed
.map(item => {
return `embed[]=${item}`
Expand Down Expand Up @@ -209,11 +202,11 @@ export class Deliveries {

updateDelivery(requestObject: DeliveriesUpdateRequestObject): Promise<DeliveriesResponse> {
return new Promise(async (resolve, reject) => {
const { body, query } = requestObject
const { body, query, deliveryId } = requestObject

let uri = `${this.options.base}${this.endpoint}/${this.options.user}/${query.deliveryId}`
let uri = `${this.options.base}${this.endpoint}/${this.options.user}/${deliveryId}`

if (query.embed) {
if (query && query.embed) {
const queryString = query.embed
.map(item => {
return `embed[]=${item}`
Expand All @@ -236,13 +229,15 @@ export class Deliveries {
})
}

setInProgress(query: DeliveriesUpdateQuery): Promise<DeliveriesResponse> {
setInProgress(requestObject: DeliveriesSimpleUpdateRequestBody): Promise<DeliveriesResponse> {
return new Promise(async (resolve, reject) => {
let uri = `${this.options.base}${this.endpoint}/${this.options.user}/${
query.deliveryId
}/in_progress`
const { deliveryId, query } = requestObject

if (query.embed) {
let uri = `${this.options.base}${this.endpoint}/${
this.options.user
}/${deliveryId}/in_progress`

if (query && query.embed) {
const queryString = query.embed
.map(item => {
return `embed[]=${item}`
Expand All @@ -265,13 +260,13 @@ export class Deliveries {
})
}

dispatchDelivery(query: DeliveriesUpdateQuery): Promise<DeliveriesResponse> {
dispatchDelivery(requestObject: DeliveriesSimpleUpdateRequestBody): Promise<DeliveriesResponse> {
return new Promise(async (resolve, reject) => {
let uri = `${this.options.base}${this.endpoint}/${this.options.user}/${
query.deliveryId
}/dispatch`
const { deliveryId, query } = requestObject

let uri = `${this.options.base}${this.endpoint}/${this.options.user}/${deliveryId}/dispatch`

if (query.embed) {
if (query && query.embed) {
const queryString = query.embed
.map(item => {
return `embed[]=${item}`
Expand All @@ -294,9 +289,9 @@ export class Deliveries {
})
}

deleteDelivery(query: DeliveriesDeleteQuery): Promise<DeliveriesResponse> {
deleteDelivery(deliveryId: string): Promise<DeliveriesResponse> {
return new Promise(async (resolve, reject) => {
const uri = `${this.options.base}${this.endpoint}/${this.options.user}/${query.deliveryId}`
const uri = `${this.options.base}${this.endpoint}/${this.options.user}/${deliveryId}`
try {
const response = await this.http.getClient().delete(uri)

Expand Down
11 changes: 4 additions & 7 deletions test/deliveries/delete-delivery.test.ts
Expand Up @@ -18,11 +18,8 @@ if (process.env.SYSTEM_TEST) {
user.apiKey = process.env.SYSTEM_TEST_API_KEY || user.apiKey
}

const query = {
deliveryId: 'abc123'
}

const respMsg = `Deleted delivery ${query.deliveryId}`
const deliveryId = 'abc123'
const respMsg = `Deleted delivery ${deliveryId}`

describe('v0: Deliveries', () => {
it('can delete one', async () => {
Expand All @@ -44,7 +41,7 @@ describe('v0: Deliveries', () => {
})

mock
.onDelete(`https://api.tillhub.com/api/v0/deliveries/${legacyId}/${query.deliveryId}`)
.onDelete(`https://api.tillhub.com/api/v0/deliveries/${legacyId}/${deliveryId}`)
.reply(function(config) {
return [
200,
Expand Down Expand Up @@ -73,7 +70,7 @@ describe('v0: Deliveries', () => {

expect(delivery).toBeInstanceOf(v0.Deliveries)

const { msg } = await delivery.deleteDelivery(query)
const { msg } = await delivery.deleteDelivery(deliveryId)

expect(msg).toEqual(respMsg)
})
Expand Down
10 changes: 6 additions & 4 deletions test/deliveries/dispatch-delivery.test.ts
Expand Up @@ -18,9 +18,11 @@ if (process.env.SYSTEM_TEST) {
user.apiKey = process.env.SYSTEM_TEST_API_KEY || user.apiKey
}

const query = {
const requestObject = {
deliveryId: 'abc123',
embed: ['location']
query: {
embed: ['location']
}
}

const responseObj = [
Expand Down Expand Up @@ -63,7 +65,7 @@ describe('v0: Deliveries', () => {
mock
.onPost(
`https://api.tillhub.com/api/v0/deliveries/${legacyId}/${
query.deliveryId
requestObject.deliveryId
}/dispatch?embed[]=location`
)
.reply(function(config) {
Expand Down Expand Up @@ -94,7 +96,7 @@ describe('v0: Deliveries', () => {

expect(delivery).toBeInstanceOf(v0.Deliveries)

const { data } = await delivery.dispatchDelivery(query)
const { data } = await delivery.dispatchDelivery(requestObject)

expect(data).toEqual(responseObj)
})
Expand Down
10 changes: 6 additions & 4 deletions test/deliveries/get-one-delivery.test.ts
Expand Up @@ -18,9 +18,11 @@ if (process.env.SYSTEM_TEST) {
user.apiKey = process.env.SYSTEM_TEST_API_KEY || user.apiKey
}

const query = {
const requestObject = {
deliveryId: 'abc123',
embed: ['location']
query: {
embed: ['location']
}
}

describe('v0: Deliveries: can get one', () => {
Expand All @@ -45,7 +47,7 @@ describe('v0: Deliveries: can get one', () => {
mock
.onGet(
`https://api.tillhub.com/api/v0/deliveries/${legacyId}/${
query.deliveryId
requestObject.deliveryId
}?embed[]=location`
)
.reply(function(config) {
Expand Down Expand Up @@ -79,7 +81,7 @@ describe('v0: Deliveries: can get one', () => {

expect(deliveries).toBeInstanceOf(v0.Deliveries)

const { data } = await deliveries.getOne(query)
const { data } = await deliveries.getOne(requestObject)

expect(Array.isArray(data)).toBe(true)
})
Expand Down
10 changes: 6 additions & 4 deletions test/deliveries/set-in-progress.test.ts
Expand Up @@ -18,9 +18,11 @@ if (process.env.SYSTEM_TEST) {
user.apiKey = process.env.SYSTEM_TEST_API_KEY || user.apiKey
}

const query = {
const requestObject = {
deliveryId: 'abc123',
embed: ['location']
query: {
embed: ['location']
}
}

const responseObj = [
Expand Down Expand Up @@ -63,7 +65,7 @@ describe('v0: Deliveries', () => {
mock
.onPost(
`https://api.tillhub.com/api/v0/deliveries/${legacyId}/${
query.deliveryId
requestObject.deliveryId
}/in_progress?embed[]=location`
)
.reply(function(config) {
Expand Down Expand Up @@ -94,7 +96,7 @@ describe('v0: Deliveries', () => {

expect(delivery).toBeInstanceOf(v0.Deliveries)

const { data } = await delivery.setInProgress(query)
const { data } = await delivery.setInProgress(requestObject)

expect(data).toEqual(responseObj)
})
Expand Down
4 changes: 2 additions & 2 deletions test/deliveries/udpate-delivery.test.ts
Expand Up @@ -19,8 +19,8 @@ if (process.env.SYSTEM_TEST) {
}

const requestObject = {
deliveryId: 'abc123',
query: {
deliveryId: 'abc123',
embed: ['location']
},
body: {
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('v0: Deliveries', () => {
mock
.onPut(
`https://api.tillhub.com/api/v0/deliveries/${legacyId}/${
query.deliveryId
requestObject.deliveryId
}?embed[]=location`
)
.reply(function(config) {
Expand Down

0 comments on commit 903f5e1

Please sign in to comment.