fix(admin): create or revive the shop record on first admin load instead of 404

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
metatroncubeswdev 2026-10-15 05:01:59 -04:00
parent 59de2b9d34
commit 23f259fcc8

View File

@ -1,11 +1,19 @@
import { authenticate } from "../shopify.server"; import { authenticate } from "../shopify.server";
import { loadShopConfig } from "../services/config.server"; import { ensureShop, loadShopConfig } from "../services/config.server";
import { features } from "../services/plans"; 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 } = {}) { export async function adminContext(request: Request, opts: { fresh?: boolean } = {}) {
const ctx = await authenticate.admin(request); 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"}` }; return { ...ctx, cfg, features: features(cfg.shop.plan), actor: `merchant:${ctx.session.onlineAccessInfo?.associated_user?.email ?? "staff"}` };
} }