Opening the STH Trackerโฆ
Log in with your team username to see your Ship To Home orders.
Team usernames: noah ยท marcus ยท james ยท justice ยท emma.
First time in? Use the temporary password Justice gave you, then change it under Account.
iPhone: open this page in Safari โ tap the Share button โ Add to Home Screen โ Add.
Android: open it in Chrome โ tap the โฎ menu โ Add to Home screen (or Install app) โ Add.
Then it opens like an app โ no browser bar. After you log in, go to Account to turn on notifications for your STHs. Full steps are in Account too.
The tracker page is working, but the database tables behind it haven't been created yet โ so there's nothing to log in to. This part is for Justice (the owner) only, and it only has to happen once.
Accounts to create (the email is just how Supabase stores the login โ nobody needs to check it):
noah@sth-tracker.local โ Noah (rep)marcus@sth-tracker.local โ Marcus (rep)james@sth-tracker.local โ James (rep)justice@sth-tracker.local โ Justice (manager)emma@sth-tracker.local โ Emma (manager)The SQL also links each login to its username and role, and turns on Row Level Security: reps can only ever see their own orders, and only Justice and Emma can see everyone's. Reps can edit their own orders to fix mistakes, but they cannot delete any order โ pending or cashed out. Only managers (Justice and Emma) can delete, after they've reviewed and verified it.
Already set up before this update? You don't need to re-run everything. Just run this small update in SQL Editor to lock deletes to managers only:
-- Lock deletes to managers only (run once if you set up before this update) drop policy if exists "orders delete own or manager" on public.sth_orders; drop policy if exists "orders delete manager only" on public.sth_orders; create policy "orders delete manager only" on public.sth_orders for delete to authenticated using (public.is_manager()); grant select on public.profiles to authenticated; grant select, insert, update, delete on public.sth_orders to authenticated;
Add the Invoice # column (one-time, for the RQ invoice number): every STH now carries its RQ invoice # โ always TRUROIN + the numbers, e.g. TRUROIN3114 โ so Justice and Emma can look the sales order up in RQ. If you set up before this update, run this once in SQL Editor:
-- Add Invoice # (RQ) column (run once if you set up before this update) alter table public.sth_orders add column if not exists invoice_number text;
Turn on real-time push (one-time): so when Emma marks an STH Shipped, it flips on the rep's already-open screen right away โ plus an on-screen notification pops up. Run this once in SQL Editor too. If it says the table is already a member of the publication, that's fine โ it's already on.
-- Real-time push for sth_orders (run once) do $$ begin alter publication supabase_realtime add table public.sth_orders; exception when duplicate_object then null; end $$; alter table public.sth_orders replica identity full;
Even without this, the tracker now auto-refreshes on its own (when you come back to it, and on a timer) and has a Refresh now button โ real-time push just makes it instant.
Copied โ-- ============================================================
-- STH Tracker โ one-time database setup
-- Run this whole block once in Supabase: SQL Editor -> Run
-- ============================================================
create table if not exists public.profiles (
id uuid primary key references auth.users(id) on delete cascade,
username text unique not null,
display_name text not null,
email text unique not null,
role text not null default 'rep' check (role in ('rep','manager')),
created_at timestamptz not null default now()
);
create table if not exists public.sth_orders (
id uuid primary key default gen_random_uuid(),
owner_id uuid not null references public.profiles(id) on delete cascade,
date_processed date not null default current_date,
customer_first_name text not null,
order_number_es text not null,
postal_code text not null,
invoice_number text,
item text not null,
expected_ship_date date,
est_delivery_date date,
actual_delivery_date date,
status text not null default 'Processed' check (status in ('Processed','Shipped','Delivered')),
date_emailed_retail_orders date,
cashed_out_rq_date date,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
alter table public.profiles enable row level security;
alter table public.sth_orders enable row level security;
-- Helper: is the signed-in user a manager? (security definer avoids RLS loops)
create or replace function public.is_manager()
returns boolean language sql security definer set search_path = public stable as $$
select exists (select 1 from public.profiles where id = auth.uid() and role = 'manager');
$$;
-- Login helper: the app looks up a username's login email BEFORE sign-in.
-- Only the matching email is returned; no other profile data is exposed.
create or replace function public.get_login_email(p_username text)
returns text language sql security definer set search_path = public stable as $$
select email from public.profiles where username = lower(trim(p_username)) limit 1;
$$;
grant execute on function public.get_login_email(text) to anon, authenticated;
grant execute on function public.is_manager() to authenticated;
-- Profiles policies: you can read your own; managers can read everyone.
drop policy if exists "profiles read own or manager" on public.profiles;
create policy "profiles read own or manager" on public.profiles
for select to authenticated using (id = auth.uid() or public.is_manager());
drop policy if exists "profiles update own" on public.profiles;
create policy "profiles update own" on public.profiles
for update to authenticated using (id = auth.uid()) with check (id = auth.uid());
-- Orders policies: reps only ever touch their own; managers see and can update all.
drop policy if exists "orders read own or manager" on public.sth_orders;
create policy "orders read own or manager" on public.sth_orders
for select to authenticated using (owner_id = auth.uid() or public.is_manager());
drop policy if exists "orders insert own" on public.sth_orders;
create policy "orders insert own" on public.sth_orders
for insert to authenticated with check (owner_id = auth.uid());
drop policy if exists "orders update own or manager" on public.sth_orders;
create policy "orders update own or manager" on public.sth_orders
for update to authenticated using (owner_id = auth.uid() or public.is_manager())
with check (owner_id = auth.uid() or public.is_manager());
drop policy if exists "orders delete own or manager" on public.sth_orders;
drop policy if exists "orders delete manager only" on public.sth_orders;
create policy "orders delete manager only" on public.sth_orders
for delete to authenticated using (public.is_manager());
grant select on public.profiles to authenticated;
grant select, insert, update, delete on public.sth_orders to authenticated;
-- Real-time push: let open trackers receive changes instantly.
do $$ begin
alter publication supabase_realtime add table public.sth_orders;
exception when duplicate_object then null;
end $$;
alter table public.sth_orders replica identity full;
-- Link the 5 Auth users to usernames + roles.
-- Run AFTER creating the users in Authentication -> Users.
insert into public.profiles (id, username, display_name, email, role)
select id, 'noah', 'Noah', email, 'rep' from auth.users where email = 'noah@sth-tracker.local'
on conflict (id) do update set username = excluded.username, display_name = excluded.display_name, email = excluded.email, role = excluded.role;
insert into public.profiles (id, username, display_name, email, role)
select id, 'marcus', 'Marcus', email, 'rep' from auth.users where email = 'marcus@sth-tracker.local'
on conflict (id) do update set username = excluded.username, display_name = excluded.display_name, email = excluded.email, role = excluded.role;
insert into public.profiles (id, username, display_name, email, role)
select id, 'james', 'James', email, 'rep' from auth.users where email = 'james@sth-tracker.local'
on conflict (id) do update set username = excluded.username, display_name = excluded.display_name, email = excluded.email, role = excluded.role;
insert into public.profiles (id, username, display_name, email, role)
select id, 'justice', 'Justice', email, 'manager' from auth.users where email = 'justice@sth-tracker.local'
on conflict (id) do update set username = excluded.username, display_name = excluded.display_name, email = excluded.email, role = excluded.role;
insert into public.profiles (id, username, display_name, email, role)
select id, 'emma', 'Emma', email, 'manager' from auth.users where email = 'emma@sth-tracker.local'
on conflict (id) do update set username = excluded.username, display_name = excluded.display_name, email = excluded.email, role = excluded.role;
Nothing is public here โ without a login, no order information can be seen at all.
Newest first. Cashed-out orders stay at the bottom so you can still look them up. Tap Edit on any order to fix a mistake. Only Justice and Emma can delete an order โ if something needs removing, ask a manager to review it. When Emma or Justice updates an STH, it updates here live โ watch for the green live dot and the pop-up notification up top.
Every rep's open, overdue, and cashed-out counts โ across the whole store.
Grouped by rep, and every card shows its rep's name right at the top โ so you can see at a glance whose STH is whose. Includes cashed out. As a manager you can edit any order and delete an order after you've reviewed and verified it.
Fill this in right when the order is processed. At this stage you only have the expected ship date โ there's no delivery estimate yet, and that's fine.
Right now you're using the tracker in a browser tab
Adding it to your home screen makes it open like a real app โ one tap from your home screen, full-screen, no browser bar. Notifications only work properly from the home-screen version (that's an iPhone rule especially), so do this step first, then turn notifications on below.
In Chrome or Edge, look for the install icon at the right end of the address bar (a little monitor with a down-arrow) and click it โ or just bookmark the page. Notifications work in the browser there too, once you turn them on below.
Status: Checkingโฆ
This switch is per person, per phone โ it's off until you turn it on here, on this device. When it's on and Emma or Justice marks one of your STHs Shipped, Delivered, emailed, or cashed out, your phone shows a real notification โ even if you've switched to another app or your screen is off โ as long as the tracker is still open in the background. The in-app pop-ups keep working either way.
Turn notifications on first, then send yourself a test to see exactly what a โjust shipped!โ alert looks and sounds like on this phone.
And if the tracker is fully closed (swiped away)? This phone's switch also registers it for true push, so once Justice finishes the one-time sender setup in the manager section below, updates buzz this phone even then. Until that setup is done, keeping the tracker open in the background covers the whole shift.
Five minutes, two phones โ this is the Noah test run, and it works the same for Marcus and James:
Nothing? Check in order: notifications switch on for that phone ยท tracker added to the home screen (iPhone) ยท tracker still open in the background ยท the one-time real-time SQL in the manager section below has been run. The green live dot at the top of his tracker should say updates are live.
Once the manager sender setup below is done, run it once more with the tracker swiped fully closed on Noah's phone โ that's the locked-phone test, and it should buzz the same way.
Reps can't delete โ only you and Emma can. The app hides Delete from reps, and the database rule below blocks them even if they tried another way. If you set this tracker up before this update, run this once in Supabase โ SQL Editor:
drop policy if exists "orders delete own or manager" on public.sth_orders; drop policy if exists "orders delete manager only" on public.sth_orders; create policy "orders delete manager only" on public.sth_orders for delete to authenticated using (public.is_manager());Copied โ
After running it, reps will still be able to add and edit their own STHs โ they just won't be able to delete pending or cashed-out ones.
For looking orders up in RQ. Each STH now shows its Invoice # right next to the postal code โ always TRUROIN + the numbers (e.g. TRUROIN3114). If you set this tracker up before this update, run this once in Supabase โ SQL Editor to add the column, otherwise saving an invoice # will fail:
alter table public.sth_orders add column if not exists invoice_number text;Copied โ
Reps only type the numbers after TRUROIN when they add or edit an STH โ the tracker adds the TRUROIN part automatically, so it always matches how you search in RQ.
So Emma's updates land instantly. When this is on, marking an STH Shipped (or Delivered, emailed, cashed out) pops up on the rep's already-open tracker right away, with a little notification โ nobody has to keep re-checking. Run this once in Supabase โ SQL Editor if you haven't already:
do $$ begin alter publication supabase_realtime add table public.sth_orders; exception when duplicate_object then null; end $$; alter table public.sth_orders replica identity full;Copied โ
The tracker also auto-refreshes by itself โ when someone comes back to the app, on a short timer, and with the โป Refresh now button up top โ so updates still arrive even before this SQL is run. The live dot at the top shows whether the instant connection is on.
What's already shipped in the tracker: the push service worker, the sign-up that saves each phone's push subscription when a rep flips the Account switch, and the public half of the notification key pair โ they're all live in this page. Background notifications (tracker open in the background) already work with no setup at all.
What only you can do โ because it lives inside your Supabase project, same as the other one-time SQL steps: deploy the little sender function below so updates also buzz a phone when the tracker is fully closed. Three short parts, about ten minutes, once:
send-push, paste the function code below, and Deploy.VAPID_PUBLIC_KEY (the public value shown below โ it matches the one built into this page), VAPID_PRIVATE_KEY (the private half Spark gave Justice privately when this update shipped โ it's a password, so it is never printed in this page), and VAPID_SUBJECT as mailto:justice.brooks95@gmail.com. (SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY are already available to Edge Functions โ no need to add them.)sth_orders, events Insert and Update, type Supabase Edge Functions, function send-push. Save.-- Push subscriptions (run once, for the locked-phone level) create table if not exists public.push_subscriptions ( id uuid primary key default gen_random_uuid(), user_id uuid not null references public.profiles(id) on delete cascade, endpoint text unique not null, subscription jsonb not null, created_at timestamptz not null default now() ); alter table public.push_subscriptions enable row level security; drop policy if exists "push subs own" on public.push_subscriptions; create policy "push subs own" on public.push_subscriptions for all to authenticated using (user_id = auth.uid()) with check (user_id = auth.uid()); grant select, insert, update, delete on public.push_subscriptions to authenticated;Copied โ
Function code โ paste this whole block as the send-push Edge Function:
// send-push โ Supabase Edge Function for the STH Tracker.
// Fires when the sth_orders database webhook calls it (Insert/Update).
// Sends a Web Push to the order's owner for the changes that matter:
// shipped, delivered, emailed Retail Orders, cashed out.
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
import webpush from "https://esm.sh/web-push@3.6.7";
const VAPID_PUBLIC = Deno.env.get("VAPID_PUBLIC_KEY")!;
const VAPID_PRIVATE = Deno.env.get("VAPID_PRIVATE_KEY")!;
const VAPID_SUBJECT = Deno.env.get("VAPID_SUBJECT") ?? "mailto:justice.brooks95@gmail.com";
webpush.setVapidDetails(VAPID_SUBJECT, VAPID_PUBLIC, VAPID_PRIVATE);
function messageFor(payload: any): { title: string; body: string } | null {
const n = payload.record ?? {};
const o = payload.old_record ?? {};
const label = `${n.customer_first_name ?? "An STH"}${n.item ? " ยท " + n.item : ""}`;
const es = n.order_number_es ? `ES# ${n.order_number_es}` : "";
if (payload.type === "INSERT")
return { title: "๐ฆ New STH added โ " + label, body: es };
if (payload.type !== "UPDATE") return null;
if (n.status === "Shipped" && o.status !== "Shipped")
return { title: "๐ " + label + " just shipped!", body: (n.est_delivery_date ? "Est. delivery " + n.est_delivery_date + ". " : "") + es };
if (n.status === "Delivered" && o.status !== "Delivered")
return { title: "๐ฌ " + label + " was delivered!", body: "Next step: email Retail Orders for this ES#. " + es };
if (n.date_emailed_retail_orders && !o.date_emailed_retail_orders)
return { title: "โ๏ธ " + label + " โ Retail Orders emailed", body: "Ready to cash out in RQ. " + es };
if (n.cashed_out_rq_date && !o.cashed_out_rq_date)
return { title: "๐ต " + label + " cashed out in RQ โ", body: es };
return null; // other edits don't buzz anyone
}
Deno.serve(async (req) => {
try {
const payload = await req.json();
const msg = messageFor(payload);
if (!msg) return new Response("no notification needed", { status: 200 });
const ownerId = payload.record?.owner_id;
if (!ownerId) return new Response("no owner", { status: 200 });
const admin = createClient(
Deno.env.get("SUPABASE_URL")!,
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!
);
const { data: subs } = await admin
.from("push_subscriptions").select("id, subscription").eq("user_id", ownerId);
const body = JSON.stringify({ title: msg.title, body: msg.body, tag: "sth-update", url: "../index.html" });
for (const row of subs ?? []) {
try {
await webpush.sendNotification(row.subscription, body);
} catch (err: any) {
// 404/410 = that phone unsubscribed or the app was removed โ clean it up.
if (err?.statusCode === 404 || err?.statusCode === 410) {
await admin.from("push_subscriptions").delete().eq("id", row.id);
}
}
}
return new Response("sent", { status: 200 });
} catch (e) {
return new Response("error: " + String(e), { status: 500 });
}
});
Copied โ
Public value for step 3 (the private half is deliberately not in this page โ Spark handed it to Justice privately when this update shipped; if you lose it, ask Spark to generate a fresh pair, which means a small tracker update too):
VAPID_PUBLIC_KEY = BCTYMKRwp5k7YgNC2SeS0sgq63Nucnwbzsx0o-JzvVENKSteKjHkRpmRwBX8EQK124hBXSN6EHpdeSCNCwl-Z1M VAPID_SUBJECT = mailto:justice.brooks95@gmail.com
Until this sender is deployed, nothing breaks: the Account switch, test notifications, in-app pop-ups, live updates, and auto-refresh all work as they are.
Ship To Home tracker ยท Processed โ Shipped โ Delivered โ emailed Retail Orders โ cashed out in RQ