From 23f259fcc80c0a067163750ed0b5b8394671c7fb Mon Sep 17 00:00:00 2001 From: metatroncubeswdev Date: Thu, 15 Oct 2026 05:01:59 -0400 Subject: [PATCH] fix(admin): create or revive the shop record on first admin load instead of 404 Co-Authored-By: Claude Opus 5.5 --- app/lib/admin.server.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/lib/admin.server.ts b/app/lib/admin.server.ts index b654983..d39fed8 100644 --- a/app/lib/admin.server.ts +++ b/app/lib/admin.server.ts @@ -1,11 +1,19 @@ import { authenticate } from "../shopify.server"; -import { loadShopConfig } from "../services/config.server"; +import { ensureShop, loadShopConfig } from "../services/config.server"; import { features } from "../services/plans"; -/** Authenticate an embedded admin request and load the shop's config + plan features. */ +/** + * Authenticate an embedded admin request and load the shop's config + plan features. + * If the shop record is missing or marked uninstalled (afterAuth didn't run or failed, or a + * reinstall), create/revive it here so the merchant never lands on a 404. + */ export async function adminContext(request: Request, opts: { fresh?: boolean } = {}) { const ctx = await authenticate.admin(request); - const cfg = await loadShopConfig(ctx.session.shop, opts); + const cfg = await loadShopConfig(ctx.session.shop, opts).catch(async (e: unknown) => { + if (!(e instanceof Response) || e.status !== 404) throw e; + await ensureShop(ctx.session.shop, ctx.admin); + return loadShopConfig(ctx.session.shop, { fresh: true }); + }); return { ...ctx, cfg, features: features(cfg.shop.plan), actor: `merchant:${ctx.session.onlineAccessInfo?.associated_user?.email ?? "staff"}` }; }