Add fuel, maintenance, document and reminder tracking
Four features layered onto cars, each following the existing parts/services pattern: a Go handler gated on requireCarAccess, snake_case PocketBase mappers, a Vue form modal, and a tab on CarDetail (now driven by an array rather than repeated markup). Fuel: refills logged with odometer, litres and cost. Consumption is derived on read from the whole history rather than stored, so correcting an old fill re-derives every window it touches with no rows to migrate. Efficiency uses the full-tank method — two consecutive full tanks are the same known level, so the fuel burned between them is exactly what was poured in. Partial fills roll into the window that closes them; a missed-fill flag leaves that window uncomputed rather than reporting an implausibly good figure. Averages in the stats rollup are distance-weighted, so a long motorway run counts for more than a trip across town — which is what actually happened to the fuel. Maintenance: workshop visits and repairs, deliberately separate from service_records. That collection is the routine interval schedule and drives next-service-due; this one is unplanned garage work with a workshop, an invoice and a labour bill, and no bearing on the interval. Documents: insurance, pollution certificates and registration papers. The renewal date is the point of the record, so expiry is assessed live on every read instead of stored and left to go stale. Scans are proxied through the API — PocketBase's collections have no public read rule, so an attachment is never a public URL and car access is re-checked per fetch. Reminders: fire on a date, an odometer reading, or both (whichever comes first). Stored reminders sit alongside read-only ones derived from document expiry and next-service-due, so a renewal date is never typed twice and can never drift from the document it came from. Derived ids are namespaced "auto:" and every write endpoint rejects them. A refill or a completed visit also writes the car's odometer forward, since it is the freshest reading there is — never backwards, so backfilling old history can't rewind the car. Adds fuel_entries, maintenance_entries, car_documents and reminders to the idempotent schema script, plus a file-field builder for attachments. Verified end-to-end against a live PocketBase with a throwaway account: 39 checks covering the efficiency maths, expiry states, the derived reminders, the upload/download round-trip, and that a stranger can reach none of it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
ae6ed4ac1e
commit
e64c89a564
@@ -0,0 +1,149 @@
|
||||
<script setup>
|
||||
import { ref, computed } from "vue";
|
||||
import { api } from "../api";
|
||||
import { formatKm } from "../lib/format.js";
|
||||
import Modal from "./Modal.vue";
|
||||
|
||||
const props = defineProps({
|
||||
carId: { type: String, required: true },
|
||||
car: { type: Object, default: null },
|
||||
reminder: { type: Object, default: null },
|
||||
});
|
||||
const emit = defineEmits(["saved", "close"]);
|
||||
|
||||
const isEdit = !!props.reminder;
|
||||
const saving = ref(false);
|
||||
const error = ref("");
|
||||
|
||||
const TYPES = [
|
||||
{ value: "maintenance", label: "Maintenance" },
|
||||
{ value: "document", label: "Document renewal" },
|
||||
{ value: "service", label: "Service" },
|
||||
{ value: "inspection", label: "Inspection" },
|
||||
{ value: "other", label: "Other" },
|
||||
];
|
||||
|
||||
const form = ref({
|
||||
title: props.reminder?.title ?? "",
|
||||
type: props.reminder?.type ?? "maintenance",
|
||||
dueDate: props.reminder?.dueDate ? toDateInput(props.reminder.dueDate) : "",
|
||||
dueKm: props.reminder?.dueKm || "",
|
||||
repeatDays: props.reminder?.repeatDays || "",
|
||||
repeatKm: props.reminder?.repeatKm || "",
|
||||
notes: props.reminder?.notes ?? "",
|
||||
});
|
||||
|
||||
function toDateInput(value) {
|
||||
const d = new Date(value);
|
||||
return isNaN(d) ? "" : d.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
// Mirrors the server's rule: a reminder with neither trigger would never fire.
|
||||
const hasTrigger = computed(() => !!form.value.dueDate || Number(form.value.dueKm) > 0);
|
||||
const isRecurring = computed(() => Number(form.value.repeatDays) > 0 || Number(form.value.repeatKm) > 0);
|
||||
|
||||
async function submit() {
|
||||
if (!hasTrigger.value) {
|
||||
error.value = "Set a due date, a due odometer reading, or both.";
|
||||
return;
|
||||
}
|
||||
saving.value = true;
|
||||
error.value = "";
|
||||
try {
|
||||
const saved = await (isEdit
|
||||
? api.updateReminder(props.reminder.id, payload())
|
||||
: api.createReminder(payload()));
|
||||
emit("saved", saved);
|
||||
} catch (e) {
|
||||
error.value = e.message;
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function payload() {
|
||||
return {
|
||||
car: props.carId,
|
||||
title: form.value.title.trim(),
|
||||
type: form.value.type,
|
||||
dueDate: form.value.dueDate ? new Date(form.value.dueDate).toISOString() : null,
|
||||
dueKm: form.value.dueKm ? Number(form.value.dueKm) : 0,
|
||||
repeatDays: form.value.repeatDays ? Number(form.value.repeatDays) : 0,
|
||||
repeatKm: form.value.repeatKm ? Number(form.value.repeatKm) : 0,
|
||||
// Editing never silently closes a reminder; that is what Done does.
|
||||
done: props.reminder?.done ?? false,
|
||||
notes: form.value.notes.trim(),
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal :title="isEdit ? 'Edit reminder' : 'Add reminder'" @close="emit('close')">
|
||||
<p v-if="error" class="mb-3 rounded-control bg-danger-soft px-3 py-2 text-sm font-medium text-danger">{{ error }}</p>
|
||||
<form class="space-y-3" @submit.prevent="submit">
|
||||
<div>
|
||||
<label class="dh-label">Title *</label>
|
||||
<input v-model="form.title" required placeholder="Swap to winter tyres" class="dh-input" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="dh-label">Type</label>
|
||||
<select v-model="form.type" class="dh-input">
|
||||
<option v-for="t in TYPES" :key="t.value" :value="t.value">{{ t.label }}</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<fieldset class="rounded-control border border-subtle p-3">
|
||||
<legend class="eyebrow px-1">Remind me</legend>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div>
|
||||
<label class="dh-label">On date</label>
|
||||
<input v-model="form.dueDate" type="date" class="dh-input data" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="dh-label">At odometer (km)</label>
|
||||
<input v-model="form.dueKm" type="number" min="0" placeholder="30000" class="dh-input data" />
|
||||
</div>
|
||||
</div>
|
||||
<p class="mt-1.5 text-xs text-muted">
|
||||
Set either or both — with both, whichever comes first wins.
|
||||
<span v-if="car?.currentKm"> The car is at {{ formatKm(car.currentKm) }} now.</span>
|
||||
</p>
|
||||
</fieldset>
|
||||
|
||||
<fieldset class="rounded-control border border-subtle p-3">
|
||||
<legend class="eyebrow px-1">Repeat (optional)</legend>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div>
|
||||
<label class="dh-label">Every … days</label>
|
||||
<input v-model="form.repeatDays" type="number" min="0" placeholder="365" class="dh-input data" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="dh-label">Every … km</label>
|
||||
<input v-model="form.repeatKm" type="number" min="0" placeholder="15000" class="dh-input data" />
|
||||
</div>
|
||||
</div>
|
||||
<p class="mt-1.5 text-xs text-muted">
|
||||
<template v-if="isRecurring">
|
||||
Marking this done will roll it forward instead of closing it.
|
||||
</template>
|
||||
<template v-else>
|
||||
Leave blank for a one-off reminder that closes when you mark it done.
|
||||
</template>
|
||||
</p>
|
||||
</fieldset>
|
||||
|
||||
<div>
|
||||
<label class="dh-label">Notes</label>
|
||||
<input v-model="form.notes" class="dh-input" />
|
||||
</div>
|
||||
|
||||
<div class="mt-4 flex justify-end gap-2">
|
||||
<button type="button" class="dh-btn dh-btn-ghost" @click="emit('close')">Cancel</button>
|
||||
<button type="submit" :disabled="saving || !hasTrigger" class="dh-btn dh-btn-primary">
|
||||
{{ saving ? "Saving…" : isEdit ? "Save changes" : "Add reminder" }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
</template>
|
||||
Reference in New Issue
Block a user