Some checks failed
CI / Lint, Unit & Integration Tests (push) Has been cancelled
Add real Shopify Billing API integration: Free/Starter/Growth/Pro plans (app/lib/billing-plans.ts, priced per PRODUCT_STRATEGY.md §6) wired into shopify.server.ts's billing config, a merchant-facing plan page (app/routes/app.billing.tsx) using billing.request/billing.cancel, and webhooks.app_subscriptions.update.tsx as the durable sync path for Shop.tier (fires even when a merchant cancels from Shopify's own billing page, not just from this app). Gate the features actually built so far in both loader and action (never just hidden in the UI, so a direct POST can't bypass a tier limit): delivery zones/rates require Growth+, the dispatch dashboard requires Starter+, and location count is capped per tier (Free=1, Starter=3, Growth/Pro=unlimited). Split pure tier logic (app/lib/billing-plans.ts) from DB-backed reads/writes (app/services/billing.server.ts) so the client-rendered UpsellState component can import the Tier type without pulling server code into the client bundle — same split as currency.ts. Covered by tests/unit/billing-plans.test.ts (pure tier ranking/mapping) and tests/integration/billing.test.ts (tier persistence and location-limit enforcement against live Postgres). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
89 lines
3.1 KiB
TypeScript
89 lines
3.1 KiB
TypeScript
import { useState } from "react";
|
|
import { redirect, type ActionFunctionArgs } from "@remix-run/node";
|
|
import { Form, useActionData, useNavigation } from "@remix-run/react";
|
|
import { Page, Card, BlockStack, Banner, FormLayout, TextField, Button } from "@shopify/polaris";
|
|
import { TitleBar } from "@shopify/app-bridge-react";
|
|
import { authenticate } from "../shopify.server";
|
|
import db from "../db.server";
|
|
import { canAddLocation, getShopTier, locationLimitFor } from "../services/billing.server";
|
|
|
|
export const action = async ({ request }: ActionFunctionArgs) => {
|
|
const { session } = await authenticate.admin(request);
|
|
const formData = await request.formData();
|
|
|
|
const name = String(formData.get("name") || "").trim();
|
|
const address = String(formData.get("address") || "").trim();
|
|
const timezone = String(formData.get("timezone") || "").trim();
|
|
|
|
const errors: Record<string, string> = {};
|
|
if (!name) errors.name = "Name is required";
|
|
if (!timezone) errors.timezone = "Timezone is required";
|
|
if (Object.keys(errors).length > 0) {
|
|
return { errors };
|
|
}
|
|
|
|
if (!(await canAddLocation(session.shop))) {
|
|
const tier = await getShopTier(session.shop);
|
|
return {
|
|
errors: {
|
|
plan: `Your ${tier} plan allows up to ${locationLimitFor(tier)} location(s). Upgrade to add more.`,
|
|
},
|
|
};
|
|
}
|
|
|
|
const location = await db.location.create({
|
|
data: { shopDomain: session.shop, name, address, timezone },
|
|
});
|
|
|
|
return redirect(`/app/locations/${location.id}`);
|
|
};
|
|
|
|
export default function NewLocation() {
|
|
const actionData = useActionData<typeof action>();
|
|
const navigation = useNavigation();
|
|
const [timezone, setTimezone] = useState(Intl.DateTimeFormat().resolvedOptions().timeZone);
|
|
|
|
return (
|
|
<Page>
|
|
<TitleBar title="Add location" />
|
|
<Card>
|
|
<Form method="post">
|
|
<FormLayout>
|
|
{actionData?.errors?.plan && (
|
|
<Banner tone="warning" title="Location limit reached">
|
|
<p>
|
|
{actionData.errors.plan}{" "}
|
|
<a href="/app/billing">View plans</a>
|
|
</p>
|
|
</Banner>
|
|
)}
|
|
<TextField
|
|
label="Location name"
|
|
name="name"
|
|
autoComplete="off"
|
|
error={actionData?.errors?.name}
|
|
requiredIndicator
|
|
/>
|
|
<TextField label="Address" name="address" autoComplete="off" multiline={2} />
|
|
<TextField
|
|
label="Timezone (IANA, e.g. America/Toronto)"
|
|
name="timezone"
|
|
autoComplete="off"
|
|
value={timezone}
|
|
onChange={setTimezone}
|
|
error={actionData?.errors?.timezone}
|
|
requiredIndicator
|
|
helpText="All slot math for this location is computed in this timezone."
|
|
/>
|
|
<BlockStack>
|
|
<Button submit variant="primary" loading={navigation.state === "submitting"}>
|
|
Create location
|
|
</Button>
|
|
</BlockStack>
|
|
</FormLayout>
|
|
</Form>
|
|
</Card>
|
|
</Page>
|
|
);
|
|
}
|