-
Notifications
You must be signed in to change notification settings - Fork 24
/
fuel-consumption.js
141 lines (112 loc) · 3.71 KB
/
fuel-consumption.js
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
// vehicleService.js
export default function FuelConsumptionAPI(db) {
async function addVehicle({ description, regNumber }) {
if (!description) {
return {
status: "error",
message: "description should not be blank"
}
}
if (!regNumber) {
return {
status: "error",
message: "regNumber should not be blank"
}
}
const regex = /^(CA|CY|CF|CAA) \d{3}-\d{3}$/;
if (!regex.test(regNumber)) {
return {
status: "error",
message: "regNumber is invalid - should by CA, CY, CF, CAA followed by 3 numbers - 3 numbers"
}
}
const result = await db.oneOrNone(`insert into vehicles (description, reg_number) values ($1, $2) returning id`, [description, regNumber]);
return {
status: "success",
id: result.id
};
}
async function vehicles() {
return await db.manyOrNone(`select * from vehicles`);
}
async function vehicle(id) {
return await db.oneOrNone(`select * from vehicles where id = $1`, [id]);
}
async function refuel(vehicleId, liters, amount, distance, filled_up) {
//
if (!vehicleId) {
return {
status: "error",
message: "vehicleId not specified"
}
}
if (!liters) {
return {
status: "error",
message: "liters not specified"
}
}
if (!amount) {
return {
status: "error",
message: "amount not specified"
}
}
const result = await db.oneOrNone(`
insert into fuel_entries(
vehicle_id,
liters,
amount_paid,
distance,
full_tank_refill
) values (
$1, $2, $3, $4, $5
) returning id
`, [
vehicleId,
liters,
amount,
distance,
filled_up
]);
//
const results = await db.oneOrNone(`
select sum(liters) as total_liters, sum(amount_paid) as total_amount
from fuel_entries
where vehicle_id = $1`, [vehicleId])
await db.none(`update vehicles set
total_amount = $1,
total_liters = $2 where id = $3`,
[results.total_amount, results.total_liters, vehicleId])
// update fuel consumption
// // (45690 - 45011) / (23 + 21)
const lastTwoEntriesForCar = await db.manyOrNone(`
select * from fuel_entries
where vehicle_id = $1
order by id desc limit 2`, [vehicleId]);
let fuelConsumption = null;
if (lastTwoEntriesForCar.length == 2) {
const last = lastTwoEntriesForCar[0];
const previous = lastTwoEntriesForCar[1];
const fullTankRefuel = last.full_tank_refill && previous.full_tank_refill;
if (fullTankRefuel) {
const distanceTraveled = last.distance - previous.distance;
const litersBought = Number(previous.liters) + Number(last.liters);
fuelConsumption = (distanceTraveled) / litersBought;
fuelConsumption = fuelConsumption.toFixed(2);
}
}
await db.none(`update vehicles set fuel_consumption = $1 where id = $2`,
[fuelConsumption, vehicleId]);
return {
status: "success",
id: result.id
}
}
return {
addVehicle,
vehicle,
vehicles,
refuel
}
}