Skip to content

Commit e8135d4

Browse files
authored
Merge pull request #10 from thexdev/feature
feat(api): add `RetrieveTracking` and `UpdateOrder` command
2 parents 8577a27 + 83920b7 commit e8135d4

5 files changed

Lines changed: 246 additions & 0 deletions

File tree

src/commands/retrieve-tracking.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Command from './command';
2+
3+
export class RetrieveTracking extends Command {
4+
execute(): Promise<Response> {
5+
return this.http.get(`v1/trackings/${this.extra.id}`);
6+
}
7+
}

src/commands/update-order.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Command from './command';
2+
3+
export class UpdateOrder extends Command {
4+
execute(): Promise<Response> {
5+
return this.http.setBody(this.extra).post(`v1/orders/${this.extra.id}`);
6+
}
7+
}

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { DeleteOrder } from './commands/delete-order';
44
import { RetrieveArea } from './commands/retrieve-area';
55
import { RetrieveCourierRates } from './commands/retrieve-courier-rates';
66
import { RetrieveOrder } from './commands/retrieve-order';
7+
import { RetrieveTracking } from './commands/retrieve-tracking';
78
import { SearchRates } from './commands/search-rates';
9+
import { UpdateOrder } from './commands/update-order';
810

911
export {
1012
Biteship,
@@ -13,5 +15,7 @@ export {
1315
RetrieveArea,
1416
RetrieveOrder,
1517
RetrieveCourierRates,
18+
RetrieveTracking,
1619
SearchRates,
20+
UpdateOrder,
1721
};
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import Command from '../../src/commands/command';
2+
import { RetrieveTracking } from '../../src';
3+
import { rest } from 'msw';
4+
import { baseUrl } from '../fixtures/base-url';
5+
import {
6+
invokerWithApiKey,
7+
invokerWithInvalidApiKey,
8+
invokerWithMissingApiKey,
9+
} from '../fixtures/invoker';
10+
import { server } from '../fixtures/server';
11+
12+
describe('retrieve-tracking.ts', () => {
13+
it('should be able to instantiated', async () => {
14+
const command = new RetrieveTracking();
15+
expect(command).toBeInstanceOf(Command);
16+
});
17+
18+
describe('when API_KEY is not provided', () => {
19+
it('should returns authorization failure', async () => {
20+
const payload = {
21+
success: true,
22+
error: 'Authorization failed',
23+
code: 40101001,
24+
};
25+
26+
server.use(
27+
rest.get(baseUrl('trackings/1'), (_, res, ctx) => {
28+
return res(ctx.status(401), ctx.json(payload));
29+
}),
30+
);
31+
32+
const command = new RetrieveTracking({ id: 1 });
33+
34+
const response = await invokerWithMissingApiKey.send(command);
35+
36+
expect(response).toEqual(payload);
37+
});
38+
});
39+
40+
describe('when API_KEY is invalid', () => {
41+
it('should return invalid api key warnings', async () => {
42+
const payload = {
43+
success: false,
44+
error:
45+
'Authentication for your key is failed. Please make sure input the right key or contact info@biteship.com for more information.',
46+
code: 40000001,
47+
};
48+
49+
server.use(
50+
rest.get(baseUrl('trackings/1'), (_, res, ctx) => {
51+
return res(ctx.status(400), ctx.json(payload));
52+
}),
53+
);
54+
55+
const command = new RetrieveTracking({ id: 1 });
56+
57+
const response = await invokerWithInvalidApiKey.send(command);
58+
59+
expect(response).toEqual(payload);
60+
});
61+
});
62+
63+
describe('when tracking found', () => {
64+
it('should returns list of current tracking histories', async () => {
65+
const payload = {
66+
success: true,
67+
messsage: 'Successfully get tracking info',
68+
object: 'tracking',
69+
id: '6051861741a37414e6637fab',
70+
waybill_id: '0123082100003094',
71+
courier: {
72+
company: 'jne',
73+
name: null,
74+
phone: null,
75+
},
76+
origin: {
77+
contact_name: '[INSTANT COURIER] BITESHIP/FIE',
78+
address:
79+
'JALAN TANJUNG 16 NO.5, RT.8/RW.2, WEST TANJUNG, SOUTH JAKARTA CITY, JAKARTA, IN',
80+
},
81+
destination: {
82+
contact_name: 'ADITARA MADJID',
83+
address:
84+
'THE PAKUBUWONO RESIDENCE, JALAN PAKUBUWONO VI, RW.1, GUNUNG, SOUTH JAKARTA CITY',
85+
},
86+
history: [
87+
{
88+
note: 'SHIPMENT RECEIVED BY JNE COUNTER OFFICER AT [JAKARTA]',
89+
updated_at: '2021-03-16T18:17:00+07:00',
90+
status: 'dropping_off',
91+
},
92+
{
93+
note: 'RECEIVED AT SORTING CENTER [JAKARTA]',
94+
updated_at: '2021-03-16T21:15:00+07:00',
95+
status: 'dropping_off',
96+
},
97+
{
98+
note: 'SHIPMENT FORWARDED TO DESTINATION [JAKARTA , HUB VETERAN BINTARO]',
99+
updated_at: '2021-03-16T23:12:00+07:00',
100+
status: 'dropping_off',
101+
},
102+
{
103+
note: 'RECEIVED AT INBOUND STATION [JAKARTA , HUB VETERAN BINTARO]',
104+
updated_at: '2021-03-16T23:43:00+07:00',
105+
status: 'dropping_off',
106+
},
107+
{
108+
note: 'WITH DELIVERY COURIER [JAKARTA , HUB VETERAN BINTARO]',
109+
updated_at: '2021-03-17T09:29:00+07:00',
110+
status: 'dropping_off',
111+
},
112+
{
113+
note: 'DELIVERED TO [ainul yakin | 17-03-2021 11:15 | JAKARTA ]',
114+
updated_at: '2021-03-17T11:15:00+07:00',
115+
status: 'delivered',
116+
},
117+
],
118+
link: '-',
119+
order_id: '6251863341sa3714e6637fab',
120+
status: 'delivered',
121+
};
122+
123+
const params = {
124+
id: 1,
125+
};
126+
127+
server.use(
128+
rest.get(baseUrl('trackings/1'), (_, res, ctx) => {
129+
return res(ctx.json(payload));
130+
}),
131+
);
132+
133+
const command = new RetrieveTracking(params);
134+
135+
const response = await invokerWithApiKey.send(command);
136+
137+
expect(response).toEqual(payload);
138+
});
139+
});
140+
});
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import Command from '../../src/commands/command';
2+
import { CreateOrder, UpdateOrder } from '../../src';
3+
import { rest } from 'msw';
4+
import { baseUrl } from '../fixtures/base-url';
5+
import {
6+
invokerWithApiKey,
7+
invokerWithInvalidApiKey,
8+
invokerWithMissingApiKey,
9+
} from '../fixtures/invoker';
10+
import { server } from '../fixtures/server';
11+
12+
describe('update-order.ts', () => {
13+
it('should be able to instantiated', async () => {
14+
const command = new CreateOrder();
15+
expect(command).toBeInstanceOf(Command);
16+
});
17+
18+
describe('when API_KEY is not provided', () => {
19+
it('should returns authorization failure', async () => {
20+
const payload = {
21+
success: true,
22+
error: 'Authorization failed',
23+
code: 40101001,
24+
};
25+
26+
server.use(
27+
rest.post(baseUrl('orders/1'), (_, res, ctx) => {
28+
return res(ctx.status(401), ctx.json(payload));
29+
}),
30+
);
31+
32+
const command = new UpdateOrder({ id: 1 });
33+
34+
const response = await invokerWithMissingApiKey.send(command);
35+
36+
expect(response).toEqual(payload);
37+
});
38+
});
39+
40+
describe('when API_KEY is invalid', () => {
41+
it('should return invalid api key warnings', async () => {
42+
const payload = {
43+
success: false,
44+
error:
45+
'Authentication for your key is failed. Please make sure input the right key or contact info@biteship.com for more information.',
46+
code: 40000001,
47+
};
48+
49+
server.use(
50+
rest.post(baseUrl('orders/1'), (_, res, ctx) => {
51+
return res(ctx.status(400), ctx.json(payload));
52+
}),
53+
);
54+
55+
const command = new UpdateOrder({ id: 1 });
56+
57+
const response = await invokerWithInvalidApiKey.send(command);
58+
59+
expect(response).toEqual(payload);
60+
});
61+
});
62+
63+
describe('when order successfully updated', () => {
64+
it('should returns 200 OK', async () => {
65+
const payload = {
66+
success: true,
67+
message: 'Order has been updated',
68+
object: 'order',
69+
};
70+
71+
const params = {
72+
origin_address: 'jl sana sini suka',
73+
};
74+
75+
server.use(
76+
rest.post(baseUrl('orders'), (_, res, ctx) => {
77+
return res(ctx.json(payload));
78+
}),
79+
);
80+
81+
const command = new CreateOrder(params);
82+
83+
const response = await invokerWithApiKey.send(command);
84+
85+
expect(response).toEqual(payload);
86+
});
87+
});
88+
});

0 commit comments

Comments
 (0)