Skip to content

aryan/save data on phone - #40

Open
AryanNair-24 wants to merge 11 commits into
mainfrom
aryan/save-data-on-phone
Open

AryanNair-24 wants to merge 11 commits into
mainfrom
aryan/save-data-on-phone

Conversation

@AryanNair-24

Copy link
Copy Markdown
Collaborator

What this changes

Closes issue #33

Adds a local SQLite layer (expo-sqlite) so downloaded lists, unfinished visits and visits waiting to send survive closing the app or losing signal.

  • db.ts: one shared connection, versioned migrations (PRAGMA user_version), and tables for the catalogue, drafts and the visit queue. Every table is keyed by worker_id.
  • catalogue.ts: save/load for programmes, form definitions, regions, villages, farms and supply items. Each list is replaced in one transaction, so a failed save keeps the previous list.
  • drafts.ts: create, load, list, save and discard drafts, plus add/remove photo paths. uuid and recorded_at are set once at creation and never rewritten. updated_at is bumped on every edit and used to order drafts.
  • queue.ts: enqueueDraft validates the draft, freezes the request body, inserts the queued visit and its photos, and deletes the draft in one transaction. Repeated taps or reopening the app return the existing visit instead of queueing twice. Also adds queue reads and per-step status tracking (visit created, photo uploaded, photo registered, errors, attempts). A visit only becomes sent once creation is confirmed and every photo is registered.
  • Tests (Jest, run in Node against real SQLite through a small adapter) cover restarting offline, failed saves, changing accounts, and submitting the same draft twice, plus blank delivery rows, pending/completed lists and draft ordering.

How to test it

  1. cd frontend && npm install
  2. npm test (all tests should pass)
  3. npx tsc --noEmit (no errors in the files above)
  4. Optional, on a device: call getDb() and confirm the tables exist and data survives a force-quit.

Checklist

  • After initial download, farms, locations, form definitions and supply dropdown choices survive offline restart.
  • Draft and queued visit values retain the same identity, delivery rows, recorded time and photo references.
  • A failed save leaves the draft there. A successful save creates one queued visit that can't be edited.
  • Logout preserves pending work; another worker cannot see or send it.
  • A visit is Sent only after its record and every declared photo are confirmed.
  • Refreshing/removing catalogue choices does not delete referenced drafts or queued records.

Notes for the reviewer

  • Worker ID: every function takes workerId: string. I'm assuming String(user.id) from the UserSerializer.
  • Photos: only file paths are stored. Callers should copy files to permanent storage first and delete only the paths discardDraft and removeDraftPhoto return.
  • Timestamps: recorded_at is the visit start time, sent in the request body. visit_created_at marks the creation step as confirmed. updated_at exists on drafts only.
  • Blank delivery rows are dropped at enqueue; half-filled rows are rejected. Zero deliveries and repeated items are currently allowed (pending confirmation).
    • Visits without deliveries are allowed. enqueueDraft accepts a draft with no delivery rows, and fully blank rows are dropped rather than rejected. If a visit must have at least one delivery, that's one extra check required in enqueueDraft.
  • Visits without photos are allowed. Such a visit becomes sent as soon as its creation is confirmed, since there are no photos left to register.
  • Types: Project, Region and Site come from the shared types file. Village and SupplyItem are defined in catalogue.ts because the shared file doesn't have them yet.
  • Not included: the sender (network calls), screens, and photo file handling. A visit stuck in sending after an app kill needs resetting to queued by the sender.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant