Skip to content

Commit 86ae4e0

Browse files
committed
fix historical call
1 parent a3296b4 commit 86ae4e0

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

src/examples/get_detours.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,57 @@ describe('When deactivating a detour by customer', () => {
6666
const detoursPromise = api.customer('SYNC').detours()
6767
.deactivate(detourId);
6868

69+
return detoursPromise;
70+
});
71+
});
72+
73+
describe('When retrieving historical detours by customer', () => {
74+
const api = new Track({ autoRenew: false });
75+
76+
beforeEach(() => charlie.setUpSuccessfulMock(api.client));
77+
beforeEach(() => mockDetours.setUpSuccessfulMock(api.client));
78+
beforeEach(() => fetchMock.catch(503));
79+
afterEach(fetchMock.restore);
80+
81+
it('should get all historical detours without date parameters', () => {
82+
api.logIn({ username: 'charlie@example.com', password: 'securepassword' });
83+
84+
const detoursPromise = api.customer('SYNC').detours()
85+
.getHistoricalDetours()
86+
.then(detours => {
87+
detours.should.be.an('array');
88+
// Add more specific assertions if needed
89+
});
90+
91+
return detoursPromise;
92+
});
93+
94+
it('should get historical detours with start date parameter', () => {
95+
api.logIn({ username: 'charlie@example.com', password: 'securepassword' });
96+
97+
const startDate = new Date('2024-01-01T00:00:00Z');
98+
const detoursPromise = api.customer('SYNC').detours()
99+
.getHistoricalDetours(startDate)
100+
.then(detours => {
101+
detours.should.be.an('array');
102+
// Add more specific assertions if needed
103+
});
104+
105+
return detoursPromise;
106+
});
107+
108+
it('should get historical detours with start and end date parameters', () => {
109+
api.logIn({ username: 'charlie@example.com', password: 'securepassword' });
110+
111+
const startDate = new Date('2024-01-01T00:00:00Z');
112+
const endDate = new Date('2024-02-01T00:00:00Z');
113+
const detoursPromise = api.customer('SYNC').detours()
114+
.getHistoricalDetours(startDate, endDate)
115+
.then(detours => {
116+
detours.should.be.an('array');
117+
// Add more specific assertions if needed
118+
});
119+
69120
return detoursPromise;
70121
});
71122
});

src/mocks/detour.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import Client from '../Client';
55
const detours = {
66
setUpSuccessfulMock: (client) => {
77
const singleResponse = () => new Response(Client.toBlob(detours.response));
8+
const historicalResponse = () => new Response(Client.toBlob(detours.historicalResponse));
89

910
fetchMock
1011
.get(client.resolve('/2/SYNC/serviceadjustments/detours'), singleResponse)
12+
.get(client.resolve('/2/SYNC/serviceadjustments/detours/historical'), historicalResponse)
13+
.get(client.resolve('/2/SYNC/serviceadjustments/detours/historical?startDate=2024-01-01T00:00:00.000Z'), historicalResponse)
14+
.get(client.resolve('/2/SYNC/serviceadjustments/detours/historical?startDate=2024-01-01T00:00:00.000Z&endDate=2024-02-01T00:00:00.000Z'), historicalResponse)
1115
.post(client.resolve('/2/SYNC/serviceadjustments/detours'), () => singleResponse())
1216
.delete(client.resolve(`/2/SYNC/serviceadjustments/detours/2`), () => singleResponse())
1317
},
@@ -74,7 +78,39 @@ const detours = {
7478
}
7579
]
7680
}
77-
}
81+
},
82+
historicalResponse: [
83+
{
84+
detour_id: 189,
85+
customer_id: 1,
86+
pattern_id: 96,
87+
detour_pattern_id: 44420,
88+
title: "Historical Detour 1",
89+
should_match_scheduled_stops: true,
90+
creator_user_id: 397,
91+
created_date_time: "2024-01-15T16:45:57.168336+00:00",
92+
deactivator_user_id: 397,
93+
deactivated_date_time: "2024-01-16T18:58:29+00:00",
94+
start_date_time: "2024-01-15T01:00:00-07:00",
95+
end_date_time: "2024-01-16T01:30:00-07:00",
96+
href: "/2/SYNC/serviceadjustments/detours/189"
97+
},
98+
{
99+
detour_id: 194,
100+
customer_id: 1,
101+
pattern_id: 40062,
102+
detour_pattern_id: 12206,
103+
title: "Historical Detour 2",
104+
should_match_scheduled_stops: true,
105+
creator_user_id: 397,
106+
created_date_time: "2024-01-20T12:55:25.8659593+00:00",
107+
deactivator_user_id: 397,
108+
deactivated_date_time: "2024-01-21T12:55:25.8659593+00:00",
109+
start_date_time: "2024-01-20T01:00:00-07:00",
110+
end_date_time: "2024-01-21T01:15:00-07:00",
111+
href: "/2/SYNC/serviceadjustments/detours/194"
112+
}
113+
]
78114
};
79115

80116
export default detours;

src/resources/Detour.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,28 @@ class Detour extends Resource {
2121

2222
/**
2323
* Fetches historical detours for a given customer.
24-
* @param {Client} client Instance of pre-configured client
25-
* @param {string} customerCode - The customer code.
24+
* @param {Date} [startDate] - Optional start date to filter detours from (the date any applicable detours started)
25+
* @param {Date} [endDate] - Optional end date to filter detours to (the date any applicable detours started)
2626
* @returns {Promise<Array<Detour>>} A promise that resolves to an array of historical detours.
2727
*/
28-
static async getHistoricalDetours(client, customerCode) {
29-
const endpoint = `/2/${customerCode}/serviceadjustments/detours/historical`;
30-
return this.client.get(endpoint)
28+
async getHistoricalDetours(startDate, endDate) {
29+
const customerCode = this.href.split('/')[2]; // Extract customer code from href
30+
let endpoint = `/2/${customerCode}/serviceadjustments/detours/historical`;
31+
32+
const params = [];
33+
if (startDate instanceof Date) {
34+
params.push(`startDate=${encodeURIComponent(startDate.toISOString())}`);
35+
}
36+
if (endDate instanceof Date) {
37+
params.push(`endDate=${encodeURIComponent(endDate.toISOString())}`);
38+
}
39+
40+
if (params.length > 0) {
41+
endpoint += `?${params.join('&')}`;
42+
}
43+
44+
const { client } = this;
45+
return client.get(endpoint)
3146
.then(response => response.json())
3247
.then(detours => detours.map(detour => new Detour(client, detour)));
3348
}

0 commit comments

Comments
 (0)