# Explanation ## Database Design ### ER Diagram ![ER Diagram](assets/er-diagram.svg) ### Design Decisions The database is structured around customers, their cars and the work orders created when those cars need repair. I separated car brands and model into their own tables to avoid storing the same information and prevent inaccurate data.
Each car references a model, which references a brand.
So this disallows the Car table to have a row with a "Toyota M3" (aka, a BMW model on the Toyota brand), without having to add the model on the CarBrand table. Invoice.workOrderId is both an id/PK and FK to only allow one invoice per work order.
So the backend don't have to update WorkOrder.invoice.total on every update of WorkOrder while it's status == IN_PROGRESS, it is currently optional with the plan the backend only adds and keeps track of it after WorkOrder.status == DONE. Though this is not enforced by the DB.
When a WorkOrder is done it should in a transaction both set WorkOrder.status = DONE and create the invoice. All prices/rates is using an integer of "Øre" to avoid floating point precision problems, especially on larger amounts. I used explicit junction tables to handle many-to-many relationships betwithout editing migration files.ween mechanics and skills, mechanics and work orders and spare parts and work orders. Some of those tables store extra information about the relationship itself, such as how many minutes a mechanic worked on a work order or the quantity and price of spare parts used.
I store the price charged for each spare part in WorkOrderToSparePart rather than SparePart so previous work orders keep their original prices even if the price of the part changes. I used composite PKs in the junction tables to prevent duplicates.
I also added composite unique constraints where it depends on multiple columns, such as the car model's name within a brand. ### PostgreSQL constraints. I wanted to add the CHECK constraint in PostgreSQL.
It is currently only possible in Prisma 8 without editing migration files.
So for now I only have the one migration file with checks, the Prisma schema does not include these checks. ## AI AI have been used to explain concepts and having the correct lingo. prisma/seed.ts is fully AI generated, just so I could see the db in action.