-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (126 loc) · 3.64 KB
/
index.js
File metadata and controls
143 lines (126 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const BASE_URL = 'https://ecmr.api.cargoffer.com';
class ECMRClient {
constructor(apiKey, baseUrl = BASE_URL) {
this.apiKey = apiKey;
this.baseUrl = baseUrl;
}
_headers() {
return {
'Authorization': `Apikey ${this.apiKey}`,
'Content-Type': 'application/json'
};
}
// ECMR Operations
async createEcmr(payload) {
const res = await fetch(`${this.baseUrl}/ecmr`, {
method: 'POST',
headers: this._headers(),
body: JSON.stringify(payload)
});
const data = await res.json();
return data.data;
}
async getEcmr(serviceCode) {
const res = await fetch(`${this.baseUrl}/ecmr/${serviceCode}`, {
headers: this._headers()
});
const data = await res.json();
return data.data;
}
async updateEcmr(serviceCode, payload) {
const res = await fetch(`${this.baseUrl}/ecmr/${serviceCode}`, {
method: 'PUT',
headers: this._headers(),
body: JSON.stringify(payload)
});
const data = await res.json();
return data.data;
}
async deleteEcmr(serviceCode) {
const res = await fetch(`${this.baseUrl}/ecmr/${serviceCode}`, {
method: 'DELETE',
headers: this._headers()
});
return res.status === 200;
}
async lockEcmr(serviceCode) {
const res = await fetch(`${this.baseUrl}/ecmr/lock/${serviceCode}`, {
method: 'PUT',
headers: this._headers()
});
const data = await res.json();
return data.data;
}
// Signatures
async signSender(serviceCode, signature) {
const res = await fetch(`${this.baseUrl}/ecmr/sign/sender/${serviceCode}`, {
method: 'PUT',
headers: this._headers(),
body: JSON.stringify({ signature, signed_at: new Date().toISOString() })
});
return res.json();
}
async signPickup(serviceCode, signature) {
const res = await fetch(`${this.baseUrl}/ecmr/sign/pickup/${serviceCode}`, {
method: 'PUT',
headers: this._headers(),
body: JSON.stringify({ signature, signed_at: new Date().toISOString() })
});
return res.json();
}
async signDelivery(serviceCode, signature) {
const res = await fetch(`${this.baseUrl}/ecmr/sign/delivery/${serviceCode}`, {
method: 'PUT',
headers: this._headers(),
body: JSON.stringify({ signature, signed_at: new Date().toISOString() })
});
return res.json();
}
// QR
async getQR(serviceCode) {
const res = await fetch(`${this.baseUrl}/ecmr/qr/${serviceCode}`, {
headers: this._headers()
});
const data = await res.json();
return data.data?.qr_url;
}
async validateQR(serviceCode) {
const res = await fetch(`${this.baseUrl}/ecmr/qr/check/${serviceCode}`, {
headers: this._headers()
});
return res.json();
}
// Drivers
async createDriver(payload) {
const res = await fetch(`${this.baseUrl}/drivers/`, {
method: 'POST',
headers: this._headers(),
body: JSON.stringify(payload)
});
return res.json();
}
async listDrivers(limit = 50) {
const res = await fetch(`${this.baseUrl}/drivers/?limit=${limit}`, {
headers: this._headers()
});
const data = await res.json();
return data.data?.docs;
}
// Vehicles
async createVehicle(payload) {
const res = await fetch(`${this.baseUrl}/vehicles/`, {
method: 'POST',
headers: this._headers(),
body: JSON.stringify(payload)
});
return res.json();
}
async listVehicles(limit = 50) {
const res = await fetch(`${this.baseUrl}/vehicles/?limit=${limit}`, {
headers: this._headers()
});
const data = await res.json();
return data.data?.docs;
}
}
module.exports = { ECMRClient };