@haatlabs-core/templates 0.11.0 → 0.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/files/.github/workflows/deploy-pages.yml +16 -0
- package/files/infra/ansible/playbooks/setup.yml +4 -2
- package/files/infra/scripts/gh-secrets.sh +13 -0
- package/files/make/platform.mk +13 -1
- package/package.json +3 -3
- package/starter/apps/web/dot-env.example +7 -0
- package/starter/apps/web/src/main.tsx +10 -0
- package/starter/dot-env.example +2 -0
- package/starter/packages/database/src/repositories/feature.int.test.ts +19 -13
- package/starter/packages/database/src/repositories/system-store.repository.ts +13 -16
- package/starter/packages/shopify/src/index.test.ts +8 -0
- package/starter/packages/shopify/src/index.ts +6 -2
- package/starter/packages/web-ui/src/App.tsx +17 -2
- package/starter/packages/web-ui/src/api-client.ts +0 -9
- package/starter/packages/web-ui/src/pages/LoginPage.tsx +1 -1
- package/starter/packages/web-ui/src/pages/stores/Stores.test.tsx +40 -35
- package/starter/packages/web-ui/src/pages/stores/StoresPage.tsx +26 -52
- package/starter/packages/web-ui/src/testing/fake-client.ts +0 -1
- package/starter/packages/web-ui/src/pages/stores/leave.ts +0 -4
|
@@ -18,6 +18,12 @@
|
|
|
18
18
|
# variable SHOPIFY_CLIENT_ID required for the embedded app (both, shopify).
|
|
19
19
|
# SHOPIFY_API_KEY: public, it is in the page.
|
|
20
20
|
# variable PAGES_CONNECT_SRC optional. More origins the dashboards may call.
|
|
21
|
+
#@if web
|
|
22
|
+
#@if shopify
|
|
23
|
+
# variable SHOPIFY_LISTING_URL optional. The app's Shopify App Store listing, which
|
|
24
|
+
# the dashboard links to. Public, it is in the page.
|
|
25
|
+
#@end
|
|
26
|
+
#@end
|
|
21
27
|
#
|
|
22
28
|
# `make gh-secrets Env=<env>` sets them from .env.<env>.
|
|
23
29
|
#
|
|
@@ -90,6 +96,11 @@ jobs:
|
|
|
90
96
|
API_HOST: ${{ vars.API_HOST }}
|
|
91
97
|
SHOPIFY_CLIENT_ID: ${{ vars.SHOPIFY_CLIENT_ID }}
|
|
92
98
|
PAGES_CONNECT_SRC: ${{ vars.PAGES_CONNECT_SRC }}
|
|
99
|
+
#@if web
|
|
100
|
+
#@if shopify
|
|
101
|
+
SHOPIFY_LISTING_URL: ${{ vars.SHOPIFY_LISTING_URL }}
|
|
102
|
+
#@end
|
|
103
|
+
#@end
|
|
93
104
|
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
|
|
94
105
|
steps:
|
|
95
106
|
- name: Refuse to publish prod from a branch that is neither main nor a release branch
|
|
@@ -147,6 +158,11 @@ jobs:
|
|
|
147
158
|
echo "APP_HOST_${upper}=${API_HOST}"
|
|
148
159
|
echo "SHOPIFY_API_KEY=${SHOPIFY_CLIENT_ID}"
|
|
149
160
|
echo "PAGES_CONNECT_SRC=${PAGES_CONNECT_SRC}"
|
|
161
|
+
#@if web
|
|
162
|
+
#@if shopify
|
|
163
|
+
echo "SHOPIFY_LISTING_URL=${SHOPIFY_LISTING_URL}"
|
|
164
|
+
#@end
|
|
165
|
+
#@end
|
|
150
166
|
} > ".env.${DEPLOY_ENV}"
|
|
151
167
|
make web-build "Env=${DEPLOY_ENV}"
|
|
152
168
|
|
|
@@ -138,14 +138,16 @@
|
|
|
138
138
|
tags: [logging]
|
|
139
139
|
|
|
140
140
|
# Merged into what is there, so settings someone else put in daemon.json survive.
|
|
141
|
-
# The values are strings: Docker refuses numbers in log-opts.
|
|
142
|
-
|
|
141
|
+
# The values are strings: Docker refuses numbers in log-opts. One layer is downloaded
|
|
142
|
+
# at a time: several at once, unpacked together, have run a 512 MB host out of memory.
|
|
143
|
+
- name: Log as JSON files, with a size limit and rotation; pull one layer at a time
|
|
143
144
|
ansible.builtin.copy:
|
|
144
145
|
content: "{{ (_daemon_json.content | default('e30=') | b64decode | from_json) | combine(_logging, recursive=true) | to_nice_json }}\n"
|
|
145
146
|
dest: /etc/docker/daemon.json
|
|
146
147
|
mode: '0644'
|
|
147
148
|
vars:
|
|
148
149
|
_logging:
|
|
150
|
+
max-concurrent-downloads: 1
|
|
149
151
|
log-driver: json-file
|
|
150
152
|
log-opts:
|
|
151
153
|
max-size: '{{ docker_log_max_size | string }}'
|
|
@@ -19,6 +19,9 @@
|
|
|
19
19
|
# for publishing the dashboards to Cloudflare Pages (deploy-pages.yml), each when set:
|
|
20
20
|
#@if shopify
|
|
21
21
|
# variables API_HOST, SHOPIFY_CLIENT_ID, CLOUDFLARE_ACCOUNT_ID, PAGES_CONNECT_SRC
|
|
22
|
+
#@if web
|
|
23
|
+
# variable SHOPIFY_LISTING_URL (the App Store listing the dashboard links to)
|
|
24
|
+
#@end
|
|
22
25
|
#@end
|
|
23
26
|
#@if !shopify
|
|
24
27
|
# variables API_HOST, CLOUDFLARE_ACCOUNT_ID, PAGES_CONNECT_SRC
|
|
@@ -255,6 +258,16 @@ if [ -n "$client_id" ]; then
|
|
|
255
258
|
else
|
|
256
259
|
drop_variable SHOPIFY_CLIENT_ID "SHOPIFY_API_KEY is empty"
|
|
257
260
|
fi
|
|
261
|
+
#@if web
|
|
262
|
+
# What deploy-pages.yml builds the dashboard with: the app's Shopify App Store listing,
|
|
263
|
+
# which it links to. Not a secret: it is public, and in the page.
|
|
264
|
+
listing="$(value_of SHOPIFY_LISTING_URL)"
|
|
265
|
+
if [ -n "$listing" ]; then
|
|
266
|
+
set_variable SHOPIFY_LISTING_URL "$listing"
|
|
267
|
+
else
|
|
268
|
+
drop_variable SHOPIFY_LISTING_URL "SHOPIFY_LISTING_URL is empty"
|
|
269
|
+
fi
|
|
270
|
+
#@end
|
|
258
271
|
#@end
|
|
259
272
|
#@if dashboards
|
|
260
273
|
# What deploy-pages.yml uploads with. The token is the one secret among them.
|
package/files/make/platform.mk
CHANGED
|
@@ -314,6 +314,10 @@ tunnel: $(ENV_PATH) ## public HTTPS addresses for the dev servers (Cloudflare Tu
|
|
|
314
314
|
# from .env.<Env>. Neither is a secret: both are visible in the pages. Without a client
|
|
315
315
|
# ID the embedded app is not built, and an earlier build of it is removed, so a stale one
|
|
316
316
|
# cannot be published.
|
|
317
|
+
#@if shopify
|
|
318
|
+
# The dashboard links to the app's Shopify App Store listing, SHOPIFY_LISTING_URL in
|
|
319
|
+
# .env.<Env> (public too); empty, the link is left out.
|
|
320
|
+
#@end
|
|
317
321
|
.PHONY: web-build
|
|
318
322
|
|
|
319
323
|
WEB_OUT := $(abspath dist-web/$(Env))
|
|
@@ -325,7 +329,13 @@ web-build: $(ENV_PATH) ## the dashboards as static files, for an environment
|
|
|
325
329
|
set -e; \
|
|
326
330
|
echo "building for $(Env): API https://$$host"; \
|
|
327
331
|
#@if web
|
|
332
|
+
#@if shopify
|
|
333
|
+
listing=$(call web_setting,SHOPIFY_LISTING_URL); \
|
|
334
|
+
VITE_API_BASE_URL="https://$$host" VITE_SHOPIFY_LISTING_URL="$$listing" pnpm --filter @@@name@@/web exec vite build --outDir "$(WEB_OUT)/web" --emptyOutDir; \
|
|
335
|
+
#@end
|
|
336
|
+
#@if !shopify
|
|
328
337
|
VITE_API_BASE_URL="https://$$host" pnpm --filter @@@name@@/web exec vite build --outDir "$(WEB_OUT)/web" --emptyOutDir; \
|
|
338
|
+
#@end
|
|
329
339
|
echo "built $(WEB_OUT)/web"; \
|
|
330
340
|
#@end
|
|
331
341
|
#@if admin
|
|
@@ -511,9 +521,11 @@ build-seq: $(ENV_PATH) ## one environment's images, one at a time, for a small h
|
|
|
511
521
|
# The other way to have the images: fetched from the registry, for the commit this
|
|
512
522
|
# checkout is at. What a host does, so that it never builds. Fails when the workflow has
|
|
513
523
|
# not built that commit yet.
|
|
524
|
+
# One image after the other: pulling and unpacking them together has run a 512 MB host out
|
|
525
|
+
# of memory, with the running stacks still on it.
|
|
514
526
|
pull: $(ENV_PATH) ## the images from the registry (IMAGE_REGISTRY), built by GitHub
|
|
515
527
|
$(require-registry)
|
|
516
|
-
$(
|
|
528
|
+
@for app in $(STACK_APPS); do $(STACK_COMPOSE) pull $$app || exit 1; done
|
|
517
529
|
|
|
518
530
|
# Signs docker in to the registry with GHCR_USER and GHCR_TOKEN from the env file: a
|
|
519
531
|
# token that can only read packages. It goes to docker on standard input, never on a
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haatlabs-core/templates",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.2",
|
|
4
4
|
"description": "The starter files and shared project files that @haatlabs-core/cli writes.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"haatlabs"
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@haatlabs-core/tsconfig": "0.11.
|
|
39
|
-
"@haatlabs-core/vitest-config": "0.11.
|
|
38
|
+
"@haatlabs-core/tsconfig": "0.11.2",
|
|
39
|
+
"@haatlabs-core/vitest-config": "0.11.2"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
@@ -18,3 +18,10 @@ WEB_BASE_URL=http://localhost:@@port.local.web@@
|
|
|
18
18
|
# hosting, for example). Empty in development: the dev server proxies /api itself.
|
|
19
19
|
# `make web-build Env=<env>` sets it from the environment's API hostname.
|
|
20
20
|
VITE_API_BASE_URL=
|
|
21
|
+
#@if kindShopify
|
|
22
|
+
|
|
23
|
+
# The app's Shopify App Store listing (https://apps.shopify.com/<handle>), which the Stores
|
|
24
|
+
# page links to for adding a store. Empty: no link. `make web-build Env=<env>` sets it from
|
|
25
|
+
# SHOPIFY_LISTING_URL in the root .env.<env>.
|
|
26
|
+
VITE_SHOPIFY_LISTING_URL=
|
|
27
|
+
#@end
|
|
@@ -12,6 +12,16 @@ const client = createApiClient({
|
|
|
12
12
|
|
|
13
13
|
createRoot(document.getElementById('root') as HTMLElement).render(
|
|
14
14
|
<StrictMode>
|
|
15
|
+
#@if kindShopify
|
|
16
|
+
<App
|
|
17
|
+
client={client}
|
|
18
|
+
// The Stores page's link to the app's Shopify App Store listing (VITE_SHOPIFY_LISTING_URL,
|
|
19
|
+
// see .env.example). Empty: no link.
|
|
20
|
+
shopifyListingUrl={import.meta.env.VITE_SHOPIFY_LISTING_URL || undefined}
|
|
21
|
+
/>
|
|
22
|
+
#@end
|
|
23
|
+
#@if !kindShopify
|
|
15
24
|
<App client={client} />
|
|
25
|
+
#@end
|
|
16
26
|
</StrictMode>,
|
|
17
27
|
);
|
package/starter/dot-env.example
CHANGED
|
@@ -65,6 +65,8 @@ SHOPIFY_API_VERSION=2026-07
|
|
|
65
65
|
# development store accepts only test charges, so set true for local and dev. Leave false
|
|
66
66
|
# in production: with true nobody is ever charged.
|
|
67
67
|
SHOPIFY_BILLING_TEST=false
|
|
68
|
+
# The app's Shopify App Store listing (https://apps.shopify.com/<handle>), on the Stores page.
|
|
69
|
+
SHOPIFY_LISTING_URL=
|
|
68
70
|
#@end
|
|
69
71
|
|
|
70
72
|
# Worker: messages handled at once, and attempts before dead-lettering.
|
|
@@ -245,28 +245,34 @@ describe("feature switches and an administrator's plan change", () => {
|
|
|
245
245
|
(error: { code?: string }) => error.code ?? 'failed',
|
|
246
246
|
);
|
|
247
247
|
|
|
248
|
-
it('
|
|
248
|
+
it('counts a store that was uninstalled until it is deleted, and always lets it come back', async () => {
|
|
249
249
|
const ctx = await tenant();
|
|
250
250
|
const [first] = (await listed(ctx)).stores;
|
|
251
251
|
if (!first) throw new Error('Expected the organization to have a store');
|
|
252
252
|
await allow(1);
|
|
253
253
|
try {
|
|
254
|
-
// The first store is uninstalled,
|
|
254
|
+
// The first store is uninstalled: the organization still holds it, so there is no
|
|
255
|
+
// room for another.
|
|
255
256
|
await stores().markUninstalled(first.id);
|
|
256
|
-
await connect(ctx.organizationId, `second-${first.id}.myshopify.com`)
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
//
|
|
260
|
-
|
|
261
|
-
expect(await active(ctx)).toBe(1);
|
|
262
|
-
|
|
263
|
-
// With room for it, it comes back.
|
|
264
|
-
await allow(2);
|
|
257
|
+
expect(await code(connect(ctx.organizationId, `second-${first.id}.myshopify.com`))).toBe(
|
|
258
|
+
'PLAN_LIMIT_REACHED',
|
|
259
|
+
);
|
|
260
|
+
// It comes back whatever the limit: from Shopify with no account named, or from the
|
|
261
|
+
// dashboard.
|
|
265
262
|
expect(await code(connect(undefined, first.shopDomain))).toBe('connected');
|
|
266
|
-
expect(await active(ctx)).toBe(
|
|
263
|
+
expect(await active(ctx)).toBe(1);
|
|
264
|
+
await stores().markUninstalled(first.id);
|
|
265
|
+
expect(await code(connect(ctx.organizationId, first.shopDomain))).toBe('connected');
|
|
267
266
|
// A store that is connected is connected again (a new token) whatever the limit.
|
|
268
|
-
await allow(1);
|
|
269
267
|
expect(await code(connect(undefined, first.shopDomain))).toBe('connected');
|
|
268
|
+
|
|
269
|
+
// shop/redact deletes it, which frees its place.
|
|
270
|
+
await stores().markUninstalled(first.id);
|
|
271
|
+
await stores().deleteStoreData(first.id);
|
|
272
|
+
expect(await code(connect(ctx.organizationId, `second-${first.id}.myshopify.com`))).toBe(
|
|
273
|
+
'connected',
|
|
274
|
+
);
|
|
275
|
+
expect(await active(ctx)).toBe(1);
|
|
270
276
|
} finally {
|
|
271
277
|
await allow(null);
|
|
272
278
|
}
|
|
@@ -59,19 +59,16 @@ const recordColumns = {
|
|
|
59
59
|
status: stores.status,
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
-
/**
|
|
63
|
-
|
|
62
|
+
/**
|
|
63
|
+
* The stores the organization holds, for the plan's limit on them: connected or
|
|
64
|
+
* uninstalled, until shop/redact deletes the store.
|
|
65
|
+
*/
|
|
66
|
+
const heldStores = (organizationId: string) => async (tx: Tx) =>
|
|
64
67
|
(
|
|
65
68
|
await tx
|
|
66
69
|
.select({ id: stores.id })
|
|
67
70
|
.from(stores)
|
|
68
|
-
.where(
|
|
69
|
-
and(
|
|
70
|
-
eq(stores.organizationId, organizationId),
|
|
71
|
-
eq(stores.status, 'ACTIVE'),
|
|
72
|
-
isNull(stores.deletedAt),
|
|
73
|
-
),
|
|
74
|
-
)
|
|
71
|
+
.where(and(eq(stores.organizationId, organizationId), isNull(stores.deletedAt)))
|
|
75
72
|
).map((row) => row.id);
|
|
76
73
|
|
|
77
74
|
/**
|
|
@@ -173,7 +170,7 @@ export class SystemStoreRepository {
|
|
|
173
170
|
): Promise<{ storeId: string; organizationId: string; created: boolean }> {
|
|
174
171
|
return this.systemDb.transaction(async (tx) => {
|
|
175
172
|
const [existing] = await tx
|
|
176
|
-
.select(recordColumns)
|
|
173
|
+
.select({ ...recordColumns, deletedAt: stores.deletedAt })
|
|
177
174
|
.from(stores)
|
|
178
175
|
.where(eq(stores.shopDomain, input.shopDomain))
|
|
179
176
|
.for('update')
|
|
@@ -188,12 +185,12 @@ export class SystemStoreRepository {
|
|
|
188
185
|
'This shop is already connected to another organization',
|
|
189
186
|
);
|
|
190
187
|
}
|
|
191
|
-
//
|
|
192
|
-
//
|
|
193
|
-
//
|
|
194
|
-
if (existing.
|
|
188
|
+
// A store its organization still holds, connected or uninstalled, already counts
|
|
189
|
+
// against the plan's limit: coming back is never one more. One marked deleted is
|
|
190
|
+
// no longer held, so coming back is.
|
|
191
|
+
if (existing.deletedAt !== null) {
|
|
195
192
|
await this.billing.assertRoomForStoreIn(tx, existing.organizationId, {
|
|
196
|
-
connected:
|
|
193
|
+
connected: heldStores(existing.organizationId),
|
|
197
194
|
returning: existing.id,
|
|
198
195
|
});
|
|
199
196
|
}
|
|
@@ -214,7 +211,7 @@ export class SystemStoreRepository {
|
|
|
214
211
|
// and no plan's limit is asked of it.
|
|
215
212
|
if (input.organizationId) {
|
|
216
213
|
await this.billing.assertRoomForStoreIn(tx, input.organizationId, {
|
|
217
|
-
connected:
|
|
214
|
+
connected: heldStores(input.organizationId),
|
|
218
215
|
returning: null,
|
|
219
216
|
});
|
|
220
217
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { RECONNECT_MESSAGE } from './index';
|
|
3
|
+
|
|
4
|
+
describe('what a merchant is told when the store must reconnect', () => {
|
|
5
|
+
it('sends them to open the app from Shopify admin, where a new token is exchanged', () => {
|
|
6
|
+
expect(RECONNECT_MESSAGE).toBe('The store must reopen the app in Shopify admin to reconnect it');
|
|
7
|
+
});
|
|
8
|
+
});
|
|
@@ -15,8 +15,12 @@ export const WEBHOOK_TOPICS = ['APP_UNINSTALLED', 'APP_SUBSCRIPTIONS_UPDATE'] as
|
|
|
15
15
|
export const ensureWebhookSubscriptions = (client: GraphQLRequester, callbackUrl: string) =>
|
|
16
16
|
ensurePlatformWebhooks(client, callbackUrl, WEBHOOK_TOPICS);
|
|
17
17
|
|
|
18
|
-
/**
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* What a merchant is told when the store must be connected again: opening the app from
|
|
20
|
+
* Shopify admin exchanges a new token. Never installed again from the dashboard (App Store
|
|
21
|
+
* requirement 2.3.1).
|
|
22
|
+
*/
|
|
23
|
+
export const RECONNECT_MESSAGE = 'The store must reopen the app in Shopify admin to reconnect it';
|
|
20
24
|
|
|
21
25
|
/** The store's access tokens, kept alive; a merchant who must reconnect is told how, in this app's words. */
|
|
22
26
|
export class StoreAccessTokens extends PlatformStoreAccessTokens {
|
|
@@ -38,10 +38,12 @@ function RedirectTo({ pathname }: { pathname: string }): ReactElement {
|
|
|
38
38
|
function Shell({
|
|
39
39
|
client,
|
|
40
40
|
mode,
|
|
41
|
+
shopifyListingUrl,
|
|
41
42
|
reload,
|
|
42
43
|
}: {
|
|
43
44
|
client: ApiClient;
|
|
44
45
|
mode: AppMode;
|
|
46
|
+
shopifyListingUrl: string | undefined;
|
|
45
47
|
reload: () => void;
|
|
46
48
|
}): ReactElement {
|
|
47
49
|
const queryClient = useQueryClient();
|
|
@@ -124,7 +126,12 @@ function Shell({
|
|
|
124
126
|
)}
|
|
125
127
|
<Routes>
|
|
126
128
|
<Route path="/home" element={<HomePage organizationName={me.data.organization.name} />} />
|
|
127
|
-
<Route
|
|
129
|
+
<Route
|
|
130
|
+
path="/stores"
|
|
131
|
+
element={
|
|
132
|
+
<StoresPage client={client} embedded={embedded} listingUrl={shopifyListingUrl} />
|
|
133
|
+
}
|
|
134
|
+
/>
|
|
128
135
|
<Route
|
|
129
136
|
path="/usage"
|
|
130
137
|
element={
|
|
@@ -172,11 +179,14 @@ function ScrollToTop(): null {
|
|
|
172
179
|
export function App({
|
|
173
180
|
client,
|
|
174
181
|
mode = 'standalone',
|
|
182
|
+
shopifyListingUrl,
|
|
175
183
|
reload = reloadPage,
|
|
176
184
|
}: {
|
|
177
185
|
client: ApiClient;
|
|
178
186
|
/** Standalone unless said otherwise. */
|
|
179
187
|
mode?: AppMode;
|
|
188
|
+
/** The app's Shopify App Store listing, which the Stores page links to. */
|
|
189
|
+
shopifyListingUrl?: string;
|
|
180
190
|
/** Loads the page again; the browser's own way unless a test gives another. */
|
|
181
191
|
reload?: () => void;
|
|
182
192
|
}): ReactElement {
|
|
@@ -192,7 +202,12 @@ export function App({
|
|
|
192
202
|
<QueryClientProvider client={queryClient}>
|
|
193
203
|
<BrowserRouter>
|
|
194
204
|
<ScrollToTop />
|
|
195
|
-
<Shell
|
|
205
|
+
<Shell
|
|
206
|
+
client={client}
|
|
207
|
+
mode={mode}
|
|
208
|
+
shopifyListingUrl={shopifyListingUrl}
|
|
209
|
+
reload={reload}
|
|
210
|
+
/>
|
|
196
211
|
</BrowserRouter>
|
|
197
212
|
</QueryClientProvider>
|
|
198
213
|
);
|
|
@@ -121,8 +121,6 @@ export interface ApiClient {
|
|
|
121
121
|
#@if kindShopify
|
|
122
122
|
/** The organization's Shopify stores, and whether this server can connect one. */
|
|
123
123
|
listStores(): Promise<StoreListResponse>;
|
|
124
|
-
/** Where the browser goes to connect a store: the API sends it on to Shopify. */
|
|
125
|
-
installUrl(shopDomain: string): string;
|
|
126
124
|
#@end
|
|
127
125
|
cancelRenewal(): Promise<{ cancelled: true }>;
|
|
128
126
|
#@if standalone
|
|
@@ -151,12 +149,7 @@ export function createApiClient(options: {
|
|
|
151
149
|
fetch: options.fetch,
|
|
152
150
|
envelope: ErrorEnvelopeSchema,
|
|
153
151
|
});
|
|
154
|
-
#@if kindShopify
|
|
155
|
-
const { baseUrl, call, adminCall, queryOf } = transport;
|
|
156
|
-
#@end
|
|
157
|
-
#@if !kindShopify
|
|
158
152
|
const { call, adminCall, queryOf } = transport;
|
|
159
|
-
#@end
|
|
160
153
|
const remember = transport.rememberProfile;
|
|
161
154
|
const rememberAdmin = (me: AdminMe): AdminMe => {
|
|
162
155
|
transport.setAdminCsrf(me.csrfToken);
|
|
@@ -240,8 +233,6 @@ export function createApiClient(options: {
|
|
|
240
233
|
cancelShopifyPlan: () =>
|
|
241
234
|
call('POST', '/api/v1/billing/shopify/cancel', CancelRenewalResponseSchema),
|
|
242
235
|
listStores: () => call('GET', '/api/v1/stores', StoreListResponseSchema),
|
|
243
|
-
installUrl: (shopDomain) =>
|
|
244
|
-
`${baseUrl}/api/v1/auth/shopify/install?${new URLSearchParams({ shop: shopDomain }).toString()}`,
|
|
245
236
|
#@end
|
|
246
237
|
};
|
|
247
238
|
}
|
|
@@ -18,7 +18,7 @@ export function LoginPage({ client }: { client: ApiClient }): ReactElement {
|
|
|
18
18
|
<h2>Say here, in one line, what @@Name@@ does for a merchant.</h2>
|
|
19
19
|
<ul>
|
|
20
20
|
#@if kindShopify
|
|
21
|
-
<li>Connect your Shopify store
|
|
21
|
+
<li>Connect your Shopify store: install the app from the Shopify App Store.</li>
|
|
22
22
|
#@end
|
|
23
23
|
<li>Pick a plan, and see what you have used of it each month.</li>
|
|
24
24
|
</ul>
|
|
@@ -8,9 +8,6 @@ import type { AppMode } from '../../App';
|
|
|
8
8
|
import { ApiError } from '../../api-client';
|
|
9
9
|
import type { ApiClient } from '../../api-client';
|
|
10
10
|
import { fakeClient, me } from '../../testing/fake-client';
|
|
11
|
-
import { leaveFor } from './leave';
|
|
12
|
-
|
|
13
|
-
vi.mock('./leave', () => ({ leaveFor: vi.fn() }));
|
|
14
11
|
|
|
15
12
|
const open = (path: string, client: ApiClient = fakeClient()): ApiClient => {
|
|
16
13
|
window.history.pushState({}, '', path);
|
|
@@ -22,7 +19,6 @@ const as = (role: 'VIEWER' | 'MEMBER', overrides: Partial<ApiClient> = {}) =>
|
|
|
22
19
|
|
|
23
20
|
beforeEach(() => {
|
|
24
21
|
vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
25
|
-
vi.mocked(leaveFor).mockClear();
|
|
26
22
|
});
|
|
27
23
|
afterEach(() => {
|
|
28
24
|
cleanup();
|
|
@@ -39,34 +35,53 @@ describe('the stores page', () => {
|
|
|
39
35
|
expect(within(stores).getByText('Connected')).toBeTruthy();
|
|
40
36
|
});
|
|
41
37
|
|
|
42
|
-
it(
|
|
38
|
+
it("links an admin to the app's Shopify App Store listing, when there is one", async () => {
|
|
39
|
+
const listing = 'https://apps.shopify.com/an-app';
|
|
40
|
+
window.history.pushState({}, '', '/stores');
|
|
41
|
+
render(<App client={fakeClient()} shopifyListingUrl={listing} />);
|
|
42
|
+
const link = await screen.findByRole('link', { name: 'Install from the Shopify App Store' });
|
|
43
|
+
expect(link.getAttribute('href')).toBe(listing);
|
|
44
|
+
expect(link.getAttribute('rel')).toBe('noopener noreferrer');
|
|
45
|
+
cleanup();
|
|
46
|
+
// Without one, or for someone who cannot connect a store, there is no link.
|
|
43
47
|
open('/stores');
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
);
|
|
49
|
-
await
|
|
50
|
-
expect(
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
await screen.findByRole('list', { name: 'Connected stores' });
|
|
49
|
+
expect(screen.queryByRole('link', { name: 'Install from the Shopify App Store' })).toBeNull();
|
|
50
|
+
cleanup();
|
|
51
|
+
window.history.pushState({}, '', '/stores');
|
|
52
|
+
render(<App client={as('MEMBER')} shopifyListingUrl={listing} />);
|
|
53
|
+
await screen.findByRole('list', { name: 'Connected stores' });
|
|
54
|
+
expect(screen.queryByRole('link', { name: 'Install from the Shopify App Store' })).toBeNull();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("never asks for a store's address: an install starts in Shopify (App Store 2.3.1)", async () => {
|
|
58
|
+
window.history.pushState({}, '', '/stores');
|
|
59
|
+
render(<App client={fakeClient()} shopifyListingUrl="https://apps.shopify.com/an-app" />);
|
|
60
|
+
await screen.findByRole('link', { name: 'Install from the Shopify App Store' });
|
|
61
|
+
expect(screen.queryByRole('textbox')).toBeNull();
|
|
62
|
+
expect(screen.queryByRole('button', { name: 'Connect' })).toBeNull();
|
|
63
|
+
expect(screen.queryByRole('form', { name: 'Connect a store' })).toBeNull();
|
|
53
64
|
});
|
|
54
65
|
|
|
55
|
-
it('
|
|
66
|
+
it('says where the app is installed from when there is no listing', async () => {
|
|
56
67
|
open('/stores');
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
68
|
+
expect(
|
|
69
|
+
await screen.findByText(
|
|
70
|
+
'To add a store, install the app on it: it is installed from the Shopify App Store, or from your Shopify admin.',
|
|
71
|
+
),
|
|
72
|
+
).toBeTruthy();
|
|
73
|
+
expect(screen.queryByRole('textbox')).toBeNull();
|
|
74
|
+
// Not for someone who cannot connect a store.
|
|
75
|
+
cleanup();
|
|
76
|
+
open('/stores', as('MEMBER'));
|
|
77
|
+
await screen.findByRole('list', { name: 'Connected stores' });
|
|
78
|
+
expect(screen.queryByText(/To add a store, install the app on it/)).toBeNull();
|
|
62
79
|
});
|
|
63
80
|
|
|
64
|
-
it('
|
|
81
|
+
it('offers no Reconnect: a store gets a new token when the app is opened from Shopify admin', async () => {
|
|
65
82
|
open('/stores');
|
|
66
|
-
await
|
|
67
|
-
|
|
68
|
-
);
|
|
69
|
-
expect(leaveFor).toHaveBeenCalledWith('/api/v1/auth/shopify/install?shop=acme.myshopify.com');
|
|
83
|
+
await screen.findByRole('list', { name: 'Connected stores' });
|
|
84
|
+
expect(screen.queryByRole('button', { name: /^Reconnect/ })).toBeNull();
|
|
70
85
|
});
|
|
71
86
|
|
|
72
87
|
it('says a store was connected', async () => {
|
|
@@ -74,15 +89,6 @@ describe('the stores page', () => {
|
|
|
74
89
|
expect(await screen.findByText('acme.myshopify.com is connected.')).toBeTruthy();
|
|
75
90
|
});
|
|
76
91
|
|
|
77
|
-
it('starts with an empty shop domain, whatever the address says', async () => {
|
|
78
|
-
// The API no longer sends anyone to /stores?connect=<shop>: an install that starts at
|
|
79
|
-
// Shopify is finished there.
|
|
80
|
-
open('/stores?connect=other.myshopify.com');
|
|
81
|
-
expect(
|
|
82
|
-
(await screen.findByRole<HTMLInputElement>('textbox', { name: 'Shop domain' })).value,
|
|
83
|
-
).toBe('');
|
|
84
|
-
});
|
|
85
|
-
|
|
86
92
|
it('offers no connection where the server cannot make one, or to someone who is not an admin', async () => {
|
|
87
93
|
open(
|
|
88
94
|
'/stores',
|
|
@@ -139,7 +145,6 @@ describe('the store page inside Shopify admin', () => {
|
|
|
139
145
|
expect(screen.queryByRole('textbox', { name: 'Shop domain' })).toBeNull();
|
|
140
146
|
expect(screen.queryByRole('button', { name: 'Connect' })).toBeNull();
|
|
141
147
|
expect(screen.queryByRole('button', { name: /^Reconnect/ })).toBeNull();
|
|
142
|
-
expect(leaveFor).not.toHaveBeenCalled();
|
|
143
148
|
});
|
|
144
149
|
});
|
|
145
150
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
2
2
|
import type { MeResponse } from '@@@name@@/contracts';
|
|
3
|
-
import {
|
|
4
|
-
import type { FormEvent, ReactElement } from 'react';
|
|
3
|
+
import type { ReactElement } from 'react';
|
|
5
4
|
import { useSearchParams } from 'react-router-dom';
|
|
6
5
|
import type { ApiClient } from '../../api-client';
|
|
7
6
|
import { EmptyState } from '@haatlabs-core/ui';
|
|
@@ -9,10 +8,6 @@ import { Loading } from '@haatlabs-core/ui';
|
|
|
9
8
|
import { TabTitle } from '../../components/StoreNav';
|
|
10
9
|
import { useStoreOnly } from '../../store-only';
|
|
11
10
|
import { EmailSignInPanel } from '@haatlabs-core/ui/client';
|
|
12
|
-
import { leaveFor } from './leave';
|
|
13
|
-
|
|
14
|
-
/** What Shopify names a store: the API checks it too. */
|
|
15
|
-
const SHOP_DOMAIN = /^[a-z0-9][a-z0-9-]*\.myshopify\.com$/;
|
|
16
11
|
|
|
17
12
|
/** Connecting a store hands the store's data to @@Name@@: an admin or the owner decides. */
|
|
18
13
|
function useIsAdmin(): boolean {
|
|
@@ -21,40 +16,32 @@ function useIsAdmin(): boolean {
|
|
|
21
16
|
}
|
|
22
17
|
|
|
23
18
|
/**
|
|
24
|
-
* The organization's stores, and
|
|
25
|
-
* (`embedded`) it is the one store
|
|
26
|
-
*
|
|
27
|
-
* Shopify's frame. Its owner may give the organization an email to sign in with there.
|
|
19
|
+
* The organization's stores, and where an admin adds one: an install that starts in Shopify,
|
|
20
|
+
* never a store's address typed here. Inside Shopify admin (`embedded`) it is the one store
|
|
21
|
+
* the app was opened from (the API shows no other): it is installed from Shopify, so there is
|
|
22
|
+
* nothing to connect, and the page never leaves Shopify's frame. Its owner may give the organization an email to sign in with there.
|
|
28
23
|
*/
|
|
29
24
|
export function StoresPage({
|
|
30
25
|
client,
|
|
31
26
|
embedded = false,
|
|
27
|
+
listingUrl,
|
|
32
28
|
}: {
|
|
33
29
|
client: ApiClient;
|
|
34
30
|
embedded?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* The app's Shopify App Store listing. A store is added only by an install that starts in
|
|
33
|
+
* Shopify (App Store requirement 2.3.1), so the page never asks for a store's address;
|
|
34
|
+
* without a listing it says where the app is installed from.
|
|
35
|
+
*/
|
|
36
|
+
listingUrl?: string;
|
|
35
37
|
}): ReactElement {
|
|
36
38
|
const [params] = useSearchParams();
|
|
37
39
|
const isAdmin = useIsAdmin();
|
|
38
40
|
// The email sign-in is the organization's owner's: not for a dashboard account's store staff.
|
|
39
41
|
const storeOnly = useStoreOnly();
|
|
40
42
|
const stores = useQuery({ queryKey: ['stores'], queryFn: () => client.listStores() });
|
|
41
|
-
const [domain, setDomain] = useState('');
|
|
42
|
-
const [problem, setProblem] = useState<string | null>(null);
|
|
43
43
|
const connected = params.get('connected');
|
|
44
44
|
|
|
45
|
-
const onConnect = (event: FormEvent) => {
|
|
46
|
-
event.preventDefault();
|
|
47
|
-
const shop = domain.trim().toLowerCase();
|
|
48
|
-
if (!SHOP_DOMAIN.test(shop)) {
|
|
49
|
-
setProblem(
|
|
50
|
-
"Enter the store's address, ending in .myshopify.com (for example acme.myshopify.com)",
|
|
51
|
-
);
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
setProblem(null);
|
|
55
|
-
leaveFor(client.installUrl(shop));
|
|
56
|
-
};
|
|
57
|
-
|
|
58
45
|
if (stores.isPending) return <Loading label="Loading stores" rows />;
|
|
59
46
|
if (stores.error) return <p role="alert">{stores.error.message}</p>;
|
|
60
47
|
const { connectable } = stores.data;
|
|
@@ -74,7 +61,7 @@ export function StoresPage({
|
|
|
74
61
|
{embedded
|
|
75
62
|
? 'Open the app again from your Shopify admin to connect this store.'
|
|
76
63
|
: canConnect
|
|
77
|
-
? '
|
|
64
|
+
? 'Install the app on your Shopify store to connect it.'
|
|
78
65
|
: 'An admin can connect a Shopify store.'}
|
|
79
66
|
</EmptyState>
|
|
80
67
|
) : (
|
|
@@ -87,36 +74,23 @@ export function StoresPage({
|
|
|
87
74
|
{store.status === 'ACTIVE' ? 'Connected' : 'Disconnected in Shopify'}
|
|
88
75
|
</span>
|
|
89
76
|
</span>
|
|
90
|
-
{/* Also for a connected store: Shopify may have stopped accepting its authorization. */}
|
|
91
|
-
{canConnect && (
|
|
92
|
-
<button
|
|
93
|
-
type="button"
|
|
94
|
-
aria-label={`Reconnect ${store.shopDomain}`}
|
|
95
|
-
onClick={() => leaveFor(client.installUrl(store.shopDomain))}
|
|
96
|
-
>
|
|
97
|
-
Reconnect
|
|
98
|
-
</button>
|
|
99
|
-
)}
|
|
100
77
|
</li>
|
|
101
78
|
))}
|
|
102
79
|
</ul>
|
|
103
80
|
)}
|
|
104
|
-
{canConnect &&
|
|
105
|
-
|
|
106
|
-
<
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
{problem && <span role="alert">{problem}</span>}
|
|
118
|
-
</form>
|
|
119
|
-
)}
|
|
81
|
+
{canConnect &&
|
|
82
|
+
(listingUrl ? (
|
|
83
|
+
<p>
|
|
84
|
+
<a className="button" href={listingUrl} target="_blank" rel="noopener noreferrer">
|
|
85
|
+
Install from the Shopify App Store
|
|
86
|
+
</a>
|
|
87
|
+
</p>
|
|
88
|
+
) : (
|
|
89
|
+
<p className="muted">
|
|
90
|
+
To add a store, install the app on it: it is installed from the Shopify App Store, or
|
|
91
|
+
from your Shopify admin.
|
|
92
|
+
</p>
|
|
93
|
+
))}
|
|
120
94
|
{embedded && isAdmin && !storeOnly && <EmailSignInPanel client={client} />}
|
|
121
95
|
</main>
|
|
122
96
|
);
|
|
@@ -123,7 +123,6 @@ export const fakeClient = (overrides: Partial<ApiClient> = {}): ApiClient => ({
|
|
|
123
123
|
confirmShopifyPlan: vi.fn().mockResolvedValue({ plan: null }),
|
|
124
124
|
cancelShopifyPlan: vi.fn().mockResolvedValue({ cancelled: true }),
|
|
125
125
|
listStores: vi.fn().mockResolvedValue({ stores: [shopStore], connectable: true }),
|
|
126
|
-
installUrl: (shop: string) => `/api/v1/auth/shopify/install?shop=${shop}`,
|
|
127
126
|
#@end
|
|
128
127
|
admin: {
|
|
129
128
|
login: vi.fn().mockResolvedValue(adminMe),
|