Workshop timesheets: a text to approve, a PDF to the bookkeeper

A small web app where the staff log a Monday-to-Sunday week, the owners get a text with a link to approve it, and the bookkeeper gets a PDF.

Difficulty
Moderate
Parts cost
Software only, on a server I already had
Build time
A day, from spec to deployed
Skills
React, Node.js, SQLite, Docker, nginx

The problem

At a small repair workshop I ran, payroll ran on ad-hoc messages with the bookkeeper. Our pay week was Monday to Sunday and payday was Thursday, so every week the hours had to get to the bookkeeper, signed off by one of us, before Thursday.

I wanted that to be one tidy path, every week. The staff put their hours in. The other mechanic and I get a text saying a week is waiting, with a link. One of us opens it and approves it. The approved sheet goes to the bookkeeper as a PDF by itself, with a copy to me, in time for Thursday.

It’s a standalone app. It isn’t part of the job system I wrote for the job cards and the counter kiosk. It has its own login, its own database and its own containers.

How it works

How the workshop timesheet app works A staff member logs a Monday-to-Sunday week on their phone and submits it. The timesheet app, a React front end and a Node back end with a SQLite database behind a locked-down proxy, saves it and texts both owners through Twilio. Either owner opens the link in the text and approves it in the app. The app then builds a PDF, emails it through SendGrid to the bookkeeper with Nick copied in, ready for Thursday payroll, and texts both owners that it has gone. Behind the scenes the database is backed up daily to two drives and a dashboard watches both containers. The staff, on a phone log the hours for the week, Monday to Sunday, then submit Timesheet app React front end, Node back end SQLite database, in Docker behind a proxy that only lets the app's own paths through A text to both owners sent through Twilio: a week is waiting One of us approves it in the app, from the link The bookkeeper gets a PDF of the week by email, Nick copied in Approval text sent through Twilio once the email has gone Behind the scenes daily backup to an SSD and a USB drive 30 days kept, integrity checked a dashboard watches both containers A Monday-to-Sunday week, paid on Thursday.

There are two kinds of login. Staff log their hours for the week and submit it. Admins are the other mechanic and me. We only approve: we don’t keep timesheets of our own in it.

The week runs Monday to Sunday, one card per day. For each day the staff member picks a start and a finish time from a drop-down (6 am to 8 pm, in 15-minute steps), or ticks Did not work. Half an hour for lunch comes off automatically, and the card shows the day’s hours as they go. There’s a notes box for anything odd, like “no lunch taken” or “stayed back to finish a job”. Each day has its own Save button, so a week can be filled in a bit at a time.

When they submit the week, the back end sends a text to both of us through Twilio (a service that sends SMS from code). The text says whose week it is and has a link to the approvals page. Nobody replies to the text: one of us taps the link, signs in, looks over the week and presses Approve or Reject. Reject asks for a reason, and the week goes back to the staff member as a draft with that reason showing at the top.

Approving locks the week, builds a PDF of it and emails it to the bookkeeper through SendGrid (a service that sends email from code), with me copied in. Once the email has gone, both of us get a second text saying it’s been approved and sent, with the week’s total hours. There’s also a live page for us that shows everyone’s current week and flags any day that’s gone past without an entry.

New logins get a temporary password in the pattern Adjective-Noun-NN. It’s shown on screen once, when the admin makes the login or resets it, and the app makes the person change it the first time they sign in. Nobody is ever really deleted. Removing a person just marks them inactive, so their old timesheets and who approved them stay on record.

What you need

  • A server that runs Docker. Mine runs on the workshop’s server.
  • Node.js 20 for the back end, with express, better-sqlite3, bcrypt (password hashing), pdfkit (makes the PDF), and the Twilio and SendGrid libraries.
  • Vite, React 18 and Tailwind for the front end.
  • nginx, twice: one inside the front-end container, and one on the server itself as a locked-down proxy (more on that below).
  • A Twilio account for the texts: twilio.com. I reused the one the workshop already had.
  • A SendGrid account for the email: sendgrid.com.
  • A way in from outside, so the staff can use it from their phones. Mine goes through a Cloudflare Tunnel (a connection out from the server to Cloudflare, so no ports are open on the router).
  • Your bookkeeper’s say-so on what they want in the PDF and when they need it by.

How it’s put together

Piece What it does
timesheet-backend container Node 20 and Express. Logins, timesheets, approvals, the PDF, the text and the email
Logins Kept in a second small SQLite file, sessions.db, in the same data/ folder. A sign-in lasts 12 hours, and the login page only takes 10 tries a minute
timesheet-frontend container The React app, served by its own nginx
SQLite database One file, timesheet.db, in a data/ folder next to the app
nginx on the server The only door in from outside. It lets through a short list of paths and refuses the rest
Cloudflare Tunnel Points the public web address at that nginx, not at the containers
The SMS hook The back end calls Twilio to text both admins when a week is submitted, and again once an approved week has been emailed
The email hook The back end calls SendGrid with the PDF attached, to the bookkeeper, copied to me

Two details matter more than they look.

Mount the data folder, not the database file. SQLite in WAL mode (write-ahead log, which lets reads and writes happen at once) keeps two side files next to the database, ending in -wal and -shm. If you mount just timesheet.db into the container, those side files live inside the container and go when it’s rebuilt. Mount the whole data/ folder and they all stay together on the server’s disk.

There’s no extra login in front of it. Some of my other web apps sit behind Cloudflare Access, which asks for a sign-in before the app even loads. This one doesn’t. The app’s own login is the only lock. That’s why the proxy on the server only lets through the paths the app actually uses:

  • / (the front end)
  • /api/auth/... (signing in, changing passwords)
  • /api/timesheets/... (the staff side)
  • /api/admin/... (the approve side)
  • /api/health (is it alive)

Anything else under /api/ gets a 403 (refused).

Config and commands

The locked-down proxy

This is the shape of mine, with my ports swapped out. Put in your own three: the one the tunnel points at, and the ones your two containers listen on.

# /etc/nginx/sites-available/timesheet-proxy.conf
server {
    listen <PROXY_PORT>;
    server_name _;

    # Only these API paths get through to the back end.
    location ^~ /api/auth/ {
        proxy_pass http://127.0.0.1:<BACKEND_PORT>;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
    location ^~ /api/timesheets/ {
        proxy_pass http://127.0.0.1:<BACKEND_PORT>;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
    location ^~ /api/admin/ {
        proxy_pass http://127.0.0.1:<BACKEND_PORT>;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
    location = /api/health {
        proxy_pass http://127.0.0.1:<BACKEND_PORT>;
    }

    # Every other API path is refused.
    location ~ ^/api/ {
        return 403;
    }

    # Everything else is the front end.
    location / {
        proxy_pass http://127.0.0.1:<FRONTEND_PORT>;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
}

The ^~ on the allowed paths matters. The 403 rule is a regex (~), and in nginx a matching regex normally beats a plain path. ^~ tells nginx “if this path matches, stop looking”, so /api/auth/login goes to the back end and /api/anything-else falls through to the 403. Leave the ^~ off and every API call gets refused. The X-Forwarded-Proto https line is there because the back end sets its login cookie as secure-only, and it needs to know the visitor came in over HTTPS.

Link it into sites-enabled, then sudo nginx -t and sudo systemctl reload nginx.

Two things worth tightening if your tunnel runs on the same machine: listen 127.0.0.1:<PROXY_PORT>; instead of just the port, and the same for the container ports in docker-compose.yml ("127.0.0.1:<BACKEND_PORT>:<BACKEND_PORT>"). Docker publishes ports on every network interface unless you tell it otherwise, and anything on the network could then skip the proxy.

The keys

The keys live in a .env file next to docker-compose.yml on the server. It’s never in git. A .env.example with the same names and no values is. Mine has 11 keys:

SESSION_SECRET=<a long random string; signs the login cookie>
SENDGRID_API_KEY=<your SendGrid API key>
TWILIO_ACCOUNT_SID=<from the Twilio console>
TWILIO_AUTH_TOKEN=<from the Twilio console>
TWILIO_FROM_NUMBER=<your Twilio number>
ADMIN_1_MOBILE=<first owner's mobile>
ADMIN_2_MOBILE=<second owner's mobile>
BOOKKEEPER_EMAIL=<the bookkeeper's address>
DIRECTOR_CC_EMAIL=<your address, for the copy>
TIMEZONE=<your time zone>
PUBLIC_URL=<the app's public web address>

My two mobile keys are named after the other mechanic and me. I’ve given them generic names here. The back end won’t start at all without SESSION_SECRET. One honest note: TIMEZONE and PUBLIC_URL are in there, but the code doesn’t read them yet. The time zone and the link in the text are written into the code. If I did it again, I’d read both from the .env.

After changing the file, recreate the back end so it picks the new values up:

docker compose up -d timesheet-backend

Copying code up to the server

I write it on the laptop and copy it up with rsync. The laptop copy has no .env (git ignores it) and no live database, so always exclude both:

rsync -av --delete --exclude='.env' --exclude='data' ./timesheet/ <user>@<server>:<path-to>/timesheet/

Or leave --delete off altogether. The lessons say why.

If the .env gets wiped anyway

The running back-end container still has every value in memory. Pull them back out:

docker exec timesheet-backend env > .env

Then open it and cut it back to just your own keys. env also prints the container’s own variables, like PATH and HOSTNAME, which don’t belong in there.

Step by step

  1. Agree the week and the payday with the bookkeeper. Ours was Monday to Sunday, paid Thursday.
  2. Build the back end: logins with two roles, timesheets, approve and reject, the PDF, the text and the email. Write tests as you go. Mine has 54 Jest tests.
  3. Build the front end in Vite, React and Tailwind. Make it work on a phone first.
  4. Put both in Docker. Mount the data/ folder, not the database file.
  5. Put the locked-down nginx proxy on the server and check it with the curl lines below before you give it a public address.
  6. Point the tunnel at the proxy’s port, not at either container.
  7. Make the first admin login with a temporary password. Sign in and change it straight away.
  8. Add timesheet.db to your daily backup. Mine goes to an SSD and a USB drive, keeps 30 days, runs an integrity check and logs the row counts, so you can see at a glance it isn’t empty.
  9. Add both containers to whatever watches your server. Mine has a dashboard tile for each.
  10. Put the real Twilio and SendGrid keys in .env, recreate the back end, and do a full test week.

Testing it

  • Is it alive? Through the proxy, from the server. It should answer with "ok":true and the time:

    curl -s http://127.0.0.1:<PROXY_PORT>/api/health
  • Is the door shut? Any API path that isn’t on the list should print 403:

    curl -s -o /dev/null -w "%{http_code}\n" http://127.0.0.1:<PROXY_PORT>/api/not-on-the-list
  • Run the tests in the back-end folder: npx jest.

  • A whole week, start to finish. Make a test staff login, sign in on a phone, log a week and submit it. Both admin phones should get a text with the link. Tap it, reject the week once with a reason, and check the reason shows on the staff side. Submit again and approve it. The bookkeeper address (point it at your own inbox first) should get the PDF with you copied in, and then both phones get the approval text. If the approval text doesn’t come, the email didn’t go: the text is only sent after the email.

  • The temporary password should only work once, and should force a change.

  • Remove the test login and check its timesheet is still there.

  • Check the backup has the database in it, and that the row counts in the log aren’t zero.

Lessons learnt

rsync --delete eats the live .env. The laptop copy of the app has no .env, because git ignores it. --delete makes the server match the laptop, so it removes the .env on the server too. Nothing breaks straight away, because the running container already has the values in memory, so it’s easy to miss. You find out the next time the back end is recreated and won’t start. The way back is docker exec timesheet-backend env > .env, then cleaning it up to the 11 keys it should have. The way to never need that: every copy-up excludes .env and data, or skips --delete. I wrote this into my deploy notes as a standing warning.

It went live with a placeholder email key. When I deployed it, the .env had SENDGRID_API_KEY set to a placeholder. The real key was saved only in the secrets of a Cloudflare Worker from another project, and had never been on the server. Until it’s swapped in, approving a week returns a 500 (server error) saying “approved but email failed”. The approval is still saved, but the email doesn’t go, and neither does the approval text, because the code only sends that after the email. My own notes at the time said the text would still go out. Reading the code, it doesn’t. The fix is to paste the real key into .env and run docker compose up -d timesheet-backend. A proper SendGrid key is in there now. Three things to take from it: a placeholder looks just like a real setting until you use it, so send one real test email before going live; make the error say which step failed, like this one did; and know there’s no “send again” button. A week can only be approved once, so any week approved while the email was broken has to go to the bookkeeper by hand. The database keeps an emailed-at time for each week, and it stays empty for those, so they’re easy to find.

A test text can reach a real person. This one’s from another project of mine, not this app. A test SMS went to a made-up mobile number, and that number could easily belong to someone. Australia’s ACMA (the communications regulator) publishes a short list of mobile numbers set aside for films, books and the like, so they never belong to anyone. Use those in your tests, never ones you made up.

Questions and comments

Loading comments…

    Your name and comment show publicly. To stop spam, a scrambled code made from your internet address is kept, never the address itself. Comments with links wait for a quick check before they show. Be decent: I'll remove anything that isn't.