|
1
|
|
|
import axios, { AxiosError, AxiosResponse } from 'axios'; |
|
2
|
|
|
import { API_URL, getHeader } from '../helpers/config'; |
|
3
|
|
|
import { toast } from 'react-toastify'; |
|
4
|
|
|
import { toastOptionsError } from '../helpers/config'; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
export type City = { |
|
8
|
|
|
id: string; |
|
9
|
|
|
name: string; |
|
10
|
|
|
latitude: number | null; |
|
11
|
|
|
longitude: number | null; |
|
12
|
|
|
createdAt: string; |
|
13
|
|
|
updatedAt: string; |
|
14
|
|
|
}; |
|
15
|
|
|
|
|
16
|
|
|
export type BikeStatus = { |
|
17
|
|
|
batteryLevel: number; |
|
18
|
|
|
city: City; |
|
19
|
|
|
createdAt: string; |
|
20
|
|
|
id: string; |
|
21
|
|
|
latitude: number; |
|
22
|
|
|
longitude: number; |
|
23
|
|
|
status: "Rented" | "Available" | "Service" | string; // Add other possible status values if needed |
|
24
|
|
|
updatedAt: string; // ISO timestamp as a string |
|
25
|
|
|
}; |
|
26
|
|
|
|
|
27
|
|
|
export type Scooter = { |
|
28
|
|
|
id: string; |
|
29
|
|
|
batteryLevel: number; |
|
30
|
|
|
latitude: number; |
|
31
|
|
|
longitude: number; |
|
32
|
|
|
status: string; |
|
33
|
|
|
city: string; |
|
34
|
|
|
createdAt: string; |
|
35
|
|
|
updatedAt: string; |
|
36
|
|
|
}; |
|
37
|
|
|
|
|
38
|
|
|
export type Rental = { |
|
39
|
|
|
|
|
40
|
|
|
id: number | string, |
|
41
|
|
|
startTime: string, |
|
42
|
|
|
latStart: number, |
|
43
|
|
|
longStart: number, |
|
44
|
|
|
startZoneType: string, |
|
45
|
|
|
endZoneType: string, |
|
46
|
|
|
cost: number |
|
47
|
|
|
stopTime?: string, |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
export const bikePerCity = async (city: string, token: string, status = '') : Promise<BikeStatus[]> => |
|
51
|
|
|
{ |
|
52
|
|
|
let data; |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
|
|
const response = await axios.get(`${API_URL}/bike/city/${city}`, getHeader(token)); |
|
56
|
|
|
if (status === '') |
|
57
|
|
|
{ |
|
58
|
|
|
data = response; |
|
59
|
|
|
} else { |
|
60
|
|
|
data = response.data.filter((item : BikeStatus) => item.status === status); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
catch(error) { |
|
64
|
|
|
const err = error as AxiosError<{message: string}>; |
|
65
|
|
|
toast.error(err?.response?.data.message, toastOptionsError) |
|
66
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
console.log(data); |
|
69
|
|
|
return data; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
export const allBikes = async ( token:string ) : Promise<Scooter[]> => |
|
74
|
|
|
{ |
|
75
|
|
|
let data; |
|
76
|
|
|
try { |
|
77
|
|
|
const response = await axios.get(`${API_URL}/bike`, getHeader(token)); |
|
78
|
|
|
data = response.data; |
|
79
|
|
|
} |
|
80
|
|
|
catch(error) { |
|
81
|
|
|
const err = error as AxiosError<{ message: string }>; |
|
82
|
|
|
toast.error(err.response?.data?.message, toastOptionsError) |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
return data; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
export const rentBike = async (bikeId: string, token: string) : Promise<AxiosResponse> => |
|
89
|
|
|
{ |
|
90
|
|
|
let data: AxiosResponse["data"]; |
|
91
|
|
|
try |
|
92
|
|
|
{ |
|
93
|
|
|
const response = await axios.post(`${API_URL}/rental/bike/${bikeId}`, {}, getHeader(token)); |
|
94
|
|
|
|
|
95
|
|
|
data = {... response.data, "statusCode": 200 }; |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
catch(error) { |
|
100
|
|
|
const err = error as AxiosError<{message: string}>; |
|
101
|
|
|
toast.error(err?.response?.data.message, toastOptionsError) |
|
102
|
|
|
data = err?.response?.data; |
|
103
|
|
|
} |
|
104
|
|
|
return data; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
export const returnBike = async (tripID: string | number | null, token: string) : Promise<AxiosResponse["data"]> => |
|
108
|
|
|
{ |
|
109
|
|
|
let data: AxiosResponse["data"]; |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
try { |
|
113
|
|
|
const response = await axios.post(`${API_URL}/rental/${tripID}/end`, {}, getHeader(token)); |
|
114
|
|
|
data = {... response.data, "statusCode": 200 }; |
|
115
|
|
|
} |
|
116
|
|
|
catch(error) { |
|
117
|
|
|
|
|
118
|
|
|
const err = error as AxiosError<{message: string}>; |
|
119
|
|
|
toast.error(err?.response?.data.message, toastOptionsError) |
|
120
|
|
|
data = err?.response?.data; |
|
121
|
|
|
} |
|
122
|
|
|
return data; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
export const allRentals = async (token: string): Promise<Rental[]> => { |
|
127
|
|
|
let data = []; |
|
128
|
|
|
try { |
|
129
|
|
|
const me = await axios.get(`${API_URL}/auth/me`, getHeader(token)); |
|
130
|
|
|
const userId = me.data.githubId; |
|
131
|
|
|
const response = await axios.get(`${API_URL}/rental/customer/${userId}`, getHeader(token)); |
|
132
|
|
|
data = response.data; |
|
133
|
|
|
console.log(data) |
|
134
|
|
|
} catch(error) |
|
135
|
|
|
{ |
|
136
|
|
|
console.log(`No rentals found for user`); |
|
137
|
|
|
if (error instanceof AxiosError && error.response?.data?.message) { |
|
138
|
|
|
console.log(`Rentals error: ${error.response.data.message}`); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
return data; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
export const bikeIdByFive = async (bikeIdFive: string, token: string): Promise<string | undefined> => { |
|
146
|
|
|
const data = await allBikes(token); |
|
147
|
|
|
const bike = data.find((item: Scooter) => item.id.startsWith(bikeIdFive.toLowerCase())); |
|
148
|
|
|
|
|
149
|
|
|
return bike?.id; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
export const returnAllRentals = async (token: string): Promise<AxiosResponse | undefined> => { |
|
153
|
|
|
let data; |
|
154
|
|
|
try { |
|
155
|
|
|
const me = await axios.get(`${API_URL}/auth/me`, getHeader(token)); |
|
156
|
|
|
const userId = me.data.githubId; |
|
157
|
|
|
data = await axios.post(`${API_URL}/rental/${userId}/end-all-travels`,{}, getHeader(token)); |
|
158
|
|
|
|
|
159
|
|
|
} catch(error) |
|
160
|
|
|
{ |
|
161
|
|
|
if (error instanceof AxiosError && error.response?.data?.message) { |
|
162
|
|
|
toast.error(`Rentals were not returned: ${error.response.data.message}`, toastOptionsError); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
return data |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
export function formatTimestamp(isoString: string) { |
|
169
|
|
|
const date = new Date(isoString); |
|
170
|
|
|
const yyyy = date.getFullYear(); |
|
171
|
|
|
const mm = String(date.getMonth() + 1).padStart(2, '0'); |
|
172
|
|
|
const dd = String(date.getDate()).padStart(2, '0'); |
|
173
|
|
|
const hh = String(date.getHours()).padStart(2, '0'); |
|
174
|
|
|
const min = String(date.getMinutes()).padStart(2, '0'); |
|
175
|
|
|
const ss = String(date.getSeconds()).padStart(2, '0'); |
|
176
|
|
|
|
|
177
|
|
|
return `${yyyy}-${mm}-${dd} ${hh}:${min}:${ss}`; |
|
178
|
|
|
} |
|
179
|
|
|
|