create-signet-app 0.4.8 → 0.6.0
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/README.md +133 -33
- package/{template-full → base/api}/api/app.ts +105 -13
- package/base/api/api/db/open.ts +121 -0
- package/{template-full → base/api}/api/db/schema.ts +34 -7
- package/{template-full → base/api}/api/env.ts +16 -0
- package/base/api/api/server.ts +163 -0
- package/{template-full → base/api}/api/services.ts +103 -10
- package/base/api/drizzle/0002_ai_paywalls_workflows.sql +77 -0
- package/base/api/drizzle/meta/0002_snapshot.json +1759 -0
- package/{template-full → base/api}/drizzle/meta/_journal.json +7 -0
- package/base/api/test/api.test.ts +51 -0
- package/base/api/test/helpers.ts +51 -0
- package/base/both/scripts/dev.ts +44 -0
- package/base/shared/README.md +385 -0
- package/{template-full → base/shared}/_github/workflows/ci.yml +1 -1
- package/{template-full → base/web}/src/lib/api.ts +28 -1
- package/{template-full → base/web}/src/main.tsx +4 -1
- package/{template-full → base/web}/src/routeTree.gen.ts +155 -90
- package/{template-full → base/web}/src/router.test.ts +13 -9
- package/base/web/src/routes/_app/index.tsx +55 -0
- package/{template-full → base/web}/src/routes/_app.tsx +40 -16
- package/{template-full → base/web}/vite.config.ts +3 -1
- package/package.json +9 -5
- package/parts/account/api/test/account.test.ts +30 -0
- package/parts/account/part.ts +6 -0
- package/parts/admin/api/test/admin.test.ts +35 -0
- package/parts/admin/part.ts +6 -0
- package/{template-full → parts/admin/web}/src/routes/_app/admin.tsx +2 -1
- package/parts/ai/api/api/routes/ai.ts +28 -0
- package/parts/ai/api/test/ai.test.ts +52 -0
- package/parts/ai/part.ts +16 -0
- package/{template-full → parts/auth/api}/api/routes/account.ts +32 -3
- package/{template-full → parts/auth/api}/api/routes/auth.ts +5 -3
- package/parts/auth/api/test/auth.test.ts +28 -0
- package/{template-full → parts/auth/both}/e2e/smoke.spec.ts +21 -7
- package/parts/auth/part.ts +22 -0
- package/parts/growth/part.ts +7 -0
- package/{template-full/_mcp → parts/mcp/api}/test/mcp.test.ts +25 -16
- package/parts/mcp/part.ts +8 -0
- package/parts/notifications/api/test/notifications.test.ts +18 -0
- package/parts/notifications/part.ts +6 -0
- package/{template-full → parts/notifications/web}/src/routes/_app/notifications.tsx +1 -1
- package/parts/orgs/api/test/orgs.test.ts +56 -0
- package/parts/orgs/part.ts +7 -0
- package/{template-full → parts/payments/api}/api/routes/billing.ts +3 -2
- package/parts/payments/api/test/payments.test.ts +31 -0
- package/parts/payments/part.ts +12 -0
- package/{template-full → parts/payments/web}/src/routes/_app/billing.tsx +6 -3
- package/parts/paywalls/api/api/routes/paywall.ts +126 -0
- package/parts/paywalls/api/test/paywalls.test.ts +71 -0
- package/parts/paywalls/part.ts +8 -0
- package/parts/privacy/api/test/privacy.test.ts +20 -0
- package/parts/privacy/part.ts +7 -0
- package/parts/workflows/api/api/workflows.ts +33 -0
- package/parts/workflows/api/test/workflows.test.ts +44 -0
- package/parts/workflows/part.ts +7 -0
- package/parts/workflows/web/src/routes/_app/approvals.tsx +34 -0
- package/src/compose.ts +426 -0
- package/src/desktop.ts +10 -10
- package/src/index.ts +407 -174
- package/template-api/_targets/deno/Dockerfile +12 -0
- package/template-api/_targets/deno/_dockerignore +10 -0
- package/template-api/_targets/deno/src/server.ts +43 -0
- package/template-full/README.md +0 -166
- package/template-full/api/db/open.ts +0 -33
- package/template-full/api/server.ts +0 -50
- package/template-full/scripts/dev.ts +0 -17
- package/template-full/src/routes/_app/index.tsx +0 -33
- package/template-full/test/api.test.ts +0 -238
- package/template-full/tsconfig.json +0 -9
- /package/{template-full → base/api}/api/db/index.ts +0 -0
- /package/{template-full → base/api}/api/db/kv.ts +0 -0
- /package/{template-full → base/api}/drizzle/0000_init.sql +0 -0
- /package/{template-full → base/api}/drizzle/0001_orgs.sql +0 -0
- /package/{template-full → base/api}/drizzle/meta/0000_snapshot.json +0 -0
- /package/{template-full → base/api}/drizzle/meta/0001_snapshot.json +0 -0
- /package/{template-full → base/api}/drizzle.config.ts +0 -0
- /package/{template-full → base/api}/tsconfig.api.json +0 -0
- /package/{template-full → base/shared}/_gitignore +0 -0
- /package/{template-full → base/shared}/biome.json +0 -0
- /package/{template-full → base/web}/index.html +0 -0
- /package/{template-full → base/web}/src/app.css +0 -0
- /package/{template-full → base/web}/src/components/toaster.tsx +0 -0
- /package/{template-full → base/web}/src/router.ts +0 -0
- /package/{template-full → base/web}/src/routes/__root.tsx +0 -0
- /package/{template-full → base/web}/tsconfig.app.json +0 -0
- /package/{template-full → base/web}/tsconfig.node.json +0 -0
- /package/{template-full → base/web}/tsconfig.test.json +0 -0
- /package/{template-full → parts/account/web}/src/routes/_app/account.tsx +0 -0
- /package/{template-full → parts/auth/both}/playwright.config.ts +0 -0
- /package/{template-full → parts/auth/web}/src/routes/login.tsx +0 -0
- /package/{template-full → parts/notifications/api}/api/routes/notifications.ts +0 -0
- /package/{template-full → parts/orgs/web}/src/routes/_app/org/index.tsx +0 -0
- /package/{template-full → parts/orgs/web}/src/routes/_app/org/members.tsx +0 -0
- /package/{template-full → parts/orgs/web}/src/routes/_app/org/new.tsx +0 -0
- /package/{template-full → parts/orgs/web}/src/routes/invite.$token.tsx +0 -0
- /package/{template-full → parts/payments/api}/api/plans.ts +0 -0
- /package/{template-full → parts/privacy/api}/api/routes/privacy.ts +0 -0
- /package/{template-full → parts/privacy/web}/src/routes/_app/privacy.tsx +0 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { app, codes, post } from "./helpers.ts"
|
|
3
|
+
|
|
4
|
+
test("sign-up makes a personal org; another org's data is 404", async () => {
|
|
5
|
+
const get = (path: string, cookie: string, org?: string) =>
|
|
6
|
+
app.request(path, { headers: { cookie, ...(org ? { "x-org": org } : {}) } })
|
|
7
|
+
const signInWithOrg = async (phone: string) => {
|
|
8
|
+
expect((await post("/api/auth/otp", { phone })).status).toBe(200)
|
|
9
|
+
const res = await post("/api/auth/verify", { phone, code: codes.get(phone) })
|
|
10
|
+
// Two cookies: the session, and the org the user lands in.
|
|
11
|
+
const cookies = res.headers.getSetCookie().map((c) => c.split(";")[0])
|
|
12
|
+
expect(cookies.some((c) => c?.startsWith("signet_org="))).toBe(true)
|
|
13
|
+
return cookies.join("; ")
|
|
14
|
+
}
|
|
15
|
+
const a = await signInWithOrg("+919000000010")
|
|
16
|
+
const b = await signInWithOrg("+919000000011")
|
|
17
|
+
const mine = (await (await get("/api/orgs", a)).json()) as {
|
|
18
|
+
orgs: { org: { id: string; personal: boolean }; role: string }[]
|
|
19
|
+
}
|
|
20
|
+
expect(mine.orgs).toHaveLength(1)
|
|
21
|
+
expect(mine.orgs[0]).toMatchObject({ role: "owner", org: { personal: true } })
|
|
22
|
+
const aOrg = mine.orgs[0]?.org.id as string
|
|
23
|
+
expect((await get("/api/orgs/current", a)).status).toBe(200)
|
|
24
|
+
// B pointing at A's org, by header: not found, and nothing leaks.
|
|
25
|
+
for (const path of [
|
|
26
|
+
"/api/orgs/current",
|
|
27
|
+
"/api/orgs/current/members",
|
|
28
|
+
"/api/orgs/current/invites",
|
|
29
|
+
])
|
|
30
|
+
expect((await get(path, b, aOrg)).status).toBe(404)
|
|
31
|
+
const join = (await post("/api/orgs/switch", { orgId: aOrg }, b)).status
|
|
32
|
+
expect(join).toBe(404)
|
|
33
|
+
|
|
34
|
+
// A invites B by link; B joins A's org and sees it.
|
|
35
|
+
const invite = await app.request("/api/orgs/current/invites", {
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: { "content-type": "application/json", cookie: a },
|
|
38
|
+
body: JSON.stringify({ role: "member" }),
|
|
39
|
+
})
|
|
40
|
+
const { token } = (await invite.json()) as { token: string }
|
|
41
|
+
expect((await app.request(`/api/orgs/invites/${token}`)).status).toBe(200) // preview, signed out
|
|
42
|
+
expect((await post(`/api/orgs/invites/${token}/accept`, {}, b)).status).toBe(200)
|
|
43
|
+
expect((await get("/api/orgs/current/members", b, aOrg)).status).toBe(200)
|
|
44
|
+
// A member cannot change settings.
|
|
45
|
+
const patch = await app.request("/api/orgs/current", {
|
|
46
|
+
method: "PATCH",
|
|
47
|
+
headers: {
|
|
48
|
+
"content-type": "application/json",
|
|
49
|
+
cookie: b,
|
|
50
|
+
"x-org": aOrg,
|
|
51
|
+
origin: "http://localhost",
|
|
52
|
+
},
|
|
53
|
+
body: JSON.stringify({ name: "x" }),
|
|
54
|
+
})
|
|
55
|
+
expect(patch.status).toBe(403)
|
|
56
|
+
})
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Part } from "../../src/compose.js"
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
description: "organisations: a personal org each, members, roles, invite links, org pages",
|
|
5
|
+
needs: ["auth"],
|
|
6
|
+
tables: ["organizations", "memberships", "invitations", "org_roles"],
|
|
7
|
+
} satisfies Part
|
|
@@ -4,7 +4,8 @@ import { Hono } from "hono"
|
|
|
4
4
|
import { z } from "zod"
|
|
5
5
|
import type { AppEnv } from "../app.ts"
|
|
6
6
|
import { env } from "../env.ts"
|
|
7
|
-
import { PLAN_DISPLAY, type PlanId, plans, type Services } from "../services.ts"
|
|
7
|
+
import { PLAN_DISPLAY, type PlanId, plans, type Services } from "../services.ts" // @notifications
|
|
8
|
+
import { type PlanId, plans, type Services } from "../services.ts" // @!notifications
|
|
8
9
|
|
|
9
10
|
const PLAN_IDS = Object.keys(plans) as [PlanId, ...PlanId[]]
|
|
10
11
|
|
|
@@ -33,7 +34,7 @@ export const billingRoutes = (s: Services) =>
|
|
|
33
34
|
// webhook at /api/billing/webhooks/razorpay then moves the plan.
|
|
34
35
|
throw new ApiException(409, "Real checkout is not wired yet.", "NOT_IMPLEMENTED")
|
|
35
36
|
await s.entitlements.setPlan(user.id, plan, { actor: { type: "user", id: user.id } })
|
|
36
|
-
await s.notifier.notify(user.id, "billing.plan_changed", { plan: PLAN_DISPLAY[plan].name })
|
|
37
|
+
await s.notifier.notify(user.id, "billing.plan_changed", { plan: PLAN_DISPLAY[plan].name }) // @notifications
|
|
37
38
|
return c.json({ plan })
|
|
38
39
|
})
|
|
39
40
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { app, post, signIn } from "./helpers.ts"
|
|
3
|
+
|
|
4
|
+
test("sign in, see the plan, upgrade", async () => {
|
|
5
|
+
const cookie = await signIn("+919000000002")
|
|
6
|
+
const me = (await (await app.request("/api/account/me", { headers: { cookie } })).json()) as {
|
|
7
|
+
user: { role: string }
|
|
8
|
+
plan: string
|
|
9
|
+
}
|
|
10
|
+
expect(me).toMatchObject({ user: { role: "user" }, plan: "free" })
|
|
11
|
+
|
|
12
|
+
expect((await post("/api/billing/checkout", { plan: "pro" }, cookie)).status).toBe(200)
|
|
13
|
+
const billing = (await (await app.request("/api/billing", { headers: { cookie } })).json()) as {
|
|
14
|
+
subscription: { plan: string }
|
|
15
|
+
}
|
|
16
|
+
expect(billing.subscription.plan).toBe("pro")
|
|
17
|
+
// notifications:begin
|
|
18
|
+
|
|
19
|
+
// Sends are deferred; the welcome and plan notices land in the inbox shortly.
|
|
20
|
+
await Bun.sleep(100)
|
|
21
|
+
const inbox = (await (
|
|
22
|
+
await app.request("/api/notifications", { headers: { cookie } })
|
|
23
|
+
).json()) as {
|
|
24
|
+
items: { event: string }[]
|
|
25
|
+
}
|
|
26
|
+
expect(inbox.items.map((i) => i.event).sort()).toEqual([
|
|
27
|
+
"account.welcome",
|
|
28
|
+
"billing.plan_changed",
|
|
29
|
+
])
|
|
30
|
+
// notifications:end
|
|
31
|
+
})
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Part } from "../../src/compose.js"
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
description: "plans, entitlements, the billing page and gateway webhooks",
|
|
5
|
+
needs: ["auth"],
|
|
6
|
+
// The billing page reads the plans, which use signet-db's definePlans.
|
|
7
|
+
web: { deps: { "@dapplelight/signet-db": "^0.2.0" } },
|
|
8
|
+
api: { deps: { "@dapplelight/signet-payments": "^0.1.0" } },
|
|
9
|
+
env: () => ["BILLING=fake"],
|
|
10
|
+
tables: ["subject_plans", "entitlement_overrides", "usage_counters"],
|
|
11
|
+
uiCopies: { "api/plans.ts": "src/lib/plans.ts" },
|
|
12
|
+
} satisfies Part
|
|
@@ -5,16 +5,19 @@ import {
|
|
|
5
5
|
import { useQuery, useQueryClient } from "@tanstack/react-query"
|
|
6
6
|
import { createFileRoute } from "@tanstack/react-router"
|
|
7
7
|
import { toast } from "sonner"
|
|
8
|
-
import { PLAN_DISPLAY, type PlanId, plans } from "../../../api/plans.ts"
|
|
8
|
+
import { PLAN_DISPLAY, type PlanId, plans } from "../../../api/plans.ts" // @api
|
|
9
9
|
import { api } from "../../lib/api"
|
|
10
|
+
import { PLAN_DISPLAY, type PlanId, plans } from "../../lib/plans.ts" // @!api
|
|
10
11
|
|
|
11
12
|
export const Route = createFileRoute("/_app/billing")({
|
|
12
13
|
component: Billing,
|
|
13
14
|
})
|
|
14
15
|
|
|
15
16
|
// What each plan allows (definePlans) plus what the page shows (PLAN_DISPLAY),
|
|
16
|
-
// both from api/plans.ts. The current plan and usage come from the API, from
|
|
17
|
-
// signet-db entitlements. With BILLING=fake, checkout switches at once.
|
|
17
|
+
// both from api/plans.ts. The current plan and usage come from the API, from // @api
|
|
18
|
+
// signet-db entitlements. With BILLING=fake, checkout switches at once. // @api
|
|
19
|
+
// both from src/lib/plans.ts, a copy of your API's. The current plan and usage // @!api
|
|
20
|
+
// come from the API. // @!api
|
|
18
21
|
const PLANS = plansFromEntitlements(plans, PLAN_DISPLAY)
|
|
19
22
|
|
|
20
23
|
function Billing() {
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { ApiException } from "@dapplelight/signet-server/http/errors"
|
|
2
|
+
import { zValidator } from "@dapplelight/signet-server/http/validate"
|
|
3
|
+
import { Hono } from "hono"
|
|
4
|
+
import { z } from "zod"
|
|
5
|
+
import type { AppEnv } from "../app.ts"
|
|
6
|
+
import type { Services } from "../services.ts"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The paywall a placement ("onboarding", "upgrade") serves the caller now:
|
|
10
|
+
* the first audience override that matches, else the placement's offering.
|
|
11
|
+
* What it serves changes without a release: publish a new offering version
|
|
12
|
+
* and point the placement at it, from code with `s.offerings.publish(...)`
|
|
13
|
+
* and `s.offerings.setPlacement(...)` (see README, "Paywalls"). // @!admin
|
|
14
|
+
* and point the placement at it, through /api/admin/paywalls (below) or from // @admin
|
|
15
|
+
* code with `s.offerings.publish(...)` and `setPlacement(...)`. // @admin
|
|
16
|
+
*
|
|
17
|
+
* GET /:placement?country=IN&platform=web { offering }, or 404
|
|
18
|
+
*/
|
|
19
|
+
export const paywallRoutes = (s: Services) =>
|
|
20
|
+
new Hono<AppEnv>().get(
|
|
21
|
+
"/:placement",
|
|
22
|
+
zValidator("param", z.object({ placement: z.string().min(1).max(64) })),
|
|
23
|
+
zValidator(
|
|
24
|
+
"query",
|
|
25
|
+
z.object({
|
|
26
|
+
country: z.string().length(2).optional(),
|
|
27
|
+
platform: z.string().min(1).max(32).optional(),
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
async (c) => {
|
|
31
|
+
const offering = await s.offerings.current(
|
|
32
|
+
c.req.valid("param").placement,
|
|
33
|
+
c.req.valid("query"),
|
|
34
|
+
c.get("user").id,
|
|
35
|
+
)
|
|
36
|
+
if (!offering) throw new ApiException(404, "No paywall for this placement.", "NOT_FOUND")
|
|
37
|
+
return c.json({ offering })
|
|
38
|
+
},
|
|
39
|
+
)
|
|
40
|
+
// admin:begin
|
|
41
|
+
|
|
42
|
+
const product = z.object({
|
|
43
|
+
/** The store product id, or the web gateway's plan id. */
|
|
44
|
+
id: z.string(),
|
|
45
|
+
store: z.string(),
|
|
46
|
+
/** A plan from api/plans.ts. */
|
|
47
|
+
plan: z.string(),
|
|
48
|
+
interval: z.enum(["month", "year", "once"]).optional(),
|
|
49
|
+
/** What the paywall shows, in minor units. */
|
|
50
|
+
price: z.object({ amount: z.number().int(), currency: z.string() }).optional(),
|
|
51
|
+
/** Trials, intro and win-back offers; checked by signet-db's offerings. */
|
|
52
|
+
offers: z.array(z.any()).optional(),
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const paywall = z
|
|
56
|
+
.object({
|
|
57
|
+
title: z.string(),
|
|
58
|
+
subtitle: z.string(),
|
|
59
|
+
plans: z.array(z.string()),
|
|
60
|
+
highlight: z.string(),
|
|
61
|
+
badge: z.string(),
|
|
62
|
+
trial: z.string(),
|
|
63
|
+
cta: z.string(),
|
|
64
|
+
})
|
|
65
|
+
.partial()
|
|
66
|
+
|
|
67
|
+
/** signet-db refuses a product, plan or placement that does not check out: a 400. */
|
|
68
|
+
const invalid = (e: unknown): never => {
|
|
69
|
+
if (e instanceof Error && e.name === "SignetDbError")
|
|
70
|
+
throw new ApiException(400, e.message, "VALIDATION_FAILED")
|
|
71
|
+
throw e
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Publishing, for platform admins and staff. A publish is a new version; a
|
|
76
|
+
* published version never changes, so the admin panel only reads these tables.
|
|
77
|
+
*
|
|
78
|
+
* POST /offerings/:key { products, paywall?, note? } the next version
|
|
79
|
+
* PUT /placements/:placement { offering, version?, overrides? }
|
|
80
|
+
*/
|
|
81
|
+
export const paywallAdminRoutes = (s: Services) =>
|
|
82
|
+
new Hono<AppEnv>()
|
|
83
|
+
.use(async (c, next) => {
|
|
84
|
+
const { role } = c.get("user")
|
|
85
|
+
if (role !== "admin" && role !== "staff")
|
|
86
|
+
throw new ApiException(403, "Admins only.", "FORBIDDEN")
|
|
87
|
+
await next()
|
|
88
|
+
})
|
|
89
|
+
.post(
|
|
90
|
+
"/offerings/:key",
|
|
91
|
+
zValidator(
|
|
92
|
+
"json",
|
|
93
|
+
z.object({
|
|
94
|
+
products: z.array(product),
|
|
95
|
+
paywall: paywall.nullish(),
|
|
96
|
+
note: z.string().optional(),
|
|
97
|
+
}),
|
|
98
|
+
),
|
|
99
|
+
async (c) => {
|
|
100
|
+
const actor = { type: "user" as const, id: c.get("user").id }
|
|
101
|
+
const offering = await s.offerings
|
|
102
|
+
.publish(c.req.param("key"), c.req.valid("json"), { actor })
|
|
103
|
+
.catch(invalid)
|
|
104
|
+
return c.json({ offering }, 201)
|
|
105
|
+
},
|
|
106
|
+
)
|
|
107
|
+
.put(
|
|
108
|
+
"/placements/:placement",
|
|
109
|
+
zValidator(
|
|
110
|
+
"json",
|
|
111
|
+
z.object({
|
|
112
|
+
offering: z.string(),
|
|
113
|
+
version: z.number().int().nullish(),
|
|
114
|
+
/** [{ when: { country?, platform?, attributes? }, offering, version? }], first match wins. */
|
|
115
|
+
overrides: z.array(z.any()).optional(),
|
|
116
|
+
}),
|
|
117
|
+
),
|
|
118
|
+
async (c) => {
|
|
119
|
+
const actor = { type: "user" as const, id: c.get("user").id }
|
|
120
|
+
const placement = await s.offerings
|
|
121
|
+
.setPlacement(c.req.param("placement"), c.req.valid("json"), { actor })
|
|
122
|
+
.catch(invalid)
|
|
123
|
+
return c.json({ placement })
|
|
124
|
+
},
|
|
125
|
+
)
|
|
126
|
+
// admin:end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { createOfferings } from "@dapplelight/signet-db/offerings"
|
|
3
|
+
import { eq } from "drizzle-orm" // @admin
|
|
4
|
+
import * as schema from "../api/db/schema.ts"
|
|
5
|
+
import { app, db, signIn } from "./helpers.ts"
|
|
6
|
+
|
|
7
|
+
const get = (path: string, cookie: string) => app.request(path, { headers: { cookie } })
|
|
8
|
+
|
|
9
|
+
test("a placement serves its offering per audience; an unknown one is a 404", async () => {
|
|
10
|
+
// Published from code, as an app does without the admin panel.
|
|
11
|
+
const offerings = createOfferings(db, { tables: schema })
|
|
12
|
+
const actor = { actor: { type: "system" as const, id: "test" } }
|
|
13
|
+
const products = [
|
|
14
|
+
{ id: "plan_pro_monthly", store: "web", plan: "pro", interval: "month" as const },
|
|
15
|
+
]
|
|
16
|
+
await offerings.publish("standard", { products, paywall: { title: "Go further" } }, actor)
|
|
17
|
+
await offerings.publish("regional", { products, paywall: { title: "Regional" } }, actor)
|
|
18
|
+
await offerings.setPlacement(
|
|
19
|
+
"upgrade",
|
|
20
|
+
{ offering: "standard", overrides: [{ when: { country: ["IN"] }, offering: "regional" }] },
|
|
21
|
+
actor,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
expect((await app.request("/api/paywall/upgrade")).status).toBe(401)
|
|
25
|
+
const cookie = await signIn("+919000000051")
|
|
26
|
+
const res = await get("/api/paywall/upgrade", cookie)
|
|
27
|
+
expect(res.status).toBe(200)
|
|
28
|
+
expect(await res.json()).toMatchObject({
|
|
29
|
+
offering: { key: "standard", version: 1, paywall: { title: "Go further" } },
|
|
30
|
+
})
|
|
31
|
+
expect(await (await get("/api/paywall/upgrade?country=IN", cookie)).json()).toMatchObject({
|
|
32
|
+
offering: { key: "regional" },
|
|
33
|
+
})
|
|
34
|
+
expect((await get("/api/paywall/nowhere", cookie)).status).toBe(404)
|
|
35
|
+
})
|
|
36
|
+
// admin:begin
|
|
37
|
+
|
|
38
|
+
test("admins publish offerings and place them; users get 403", async () => {
|
|
39
|
+
const send = (method: string, path: string, cookie: string, body: unknown) =>
|
|
40
|
+
app.request(path, {
|
|
41
|
+
method,
|
|
42
|
+
headers: { "content-type": "application/json", cookie },
|
|
43
|
+
body: JSON.stringify(body),
|
|
44
|
+
})
|
|
45
|
+
const offering = {
|
|
46
|
+
products: [{ id: "plan_pro_monthly", store: "web", plan: "pro" }],
|
|
47
|
+
paywall: { title: "Pro" },
|
|
48
|
+
}
|
|
49
|
+
const user = await signIn("+919000000052")
|
|
50
|
+
expect((await send("POST", "/api/admin/paywalls/offerings/launch", user, offering)).status).toBe(
|
|
51
|
+
403,
|
|
52
|
+
)
|
|
53
|
+
const staffPhone = "+919000000053"
|
|
54
|
+
const admin = await signIn(staffPhone)
|
|
55
|
+
db.orm.update(schema.users).set({ role: "staff" }).where(eq(schema.users.phone, staffPhone)).run()
|
|
56
|
+
expect((await send("POST", "/api/admin/paywalls/offerings/launch", admin, offering)).status).toBe(
|
|
57
|
+
201,
|
|
58
|
+
)
|
|
59
|
+
const unknownPlan = { products: [{ id: "x", store: "web", plan: "none" }] }
|
|
60
|
+
expect(
|
|
61
|
+
(await send("POST", "/api/admin/paywalls/offerings/launch", admin, unknownPlan)).status,
|
|
62
|
+
).toBe(400)
|
|
63
|
+
const placed = await send("PUT", "/api/admin/paywalls/placements/onboarding", admin, {
|
|
64
|
+
offering: "launch",
|
|
65
|
+
})
|
|
66
|
+
expect(placed.status).toBe(200)
|
|
67
|
+
expect(await (await get("/api/paywall/onboarding", user)).json()).toMatchObject({
|
|
68
|
+
offering: { key: "launch", version: 1 },
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
// admin:end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { app, post, signIn } from "./helpers.ts"
|
|
3
|
+
|
|
4
|
+
test("consent is given and withdrawn; an erasure request opens", async () => {
|
|
5
|
+
const cookie = await signIn("+919000000004")
|
|
6
|
+
expect((await post("/api/privacy/consents/analytics", {}, cookie)).status).toBe(200)
|
|
7
|
+
// A browser sends Origin on a DELETE; the CSRF guard refuses one without it.
|
|
8
|
+
const withdraw = await app.request("/api/privacy/consents/analytics", {
|
|
9
|
+
method: "DELETE",
|
|
10
|
+
headers: { cookie, origin: "http://localhost" },
|
|
11
|
+
})
|
|
12
|
+
expect(withdraw.status).toBe(200)
|
|
13
|
+
expect((await post("/api/privacy/requests", { kind: "erasure" }, cookie)).status).toBe(201)
|
|
14
|
+
const p = (await (await app.request("/api/privacy", { headers: { cookie } })).json()) as {
|
|
15
|
+
consents: { withdrawnAt: string | null }[]
|
|
16
|
+
requests: { kind: string }[]
|
|
17
|
+
}
|
|
18
|
+
expect(p.consents[0]?.withdrawnAt).not.toBeNull()
|
|
19
|
+
expect(p.requests[0]?.kind).toBe("erasure")
|
|
20
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createWorkflows, defineWorkflow } from "@dapplelight/signet-db/workflows"
|
|
2
|
+
import { z } from "zod"
|
|
3
|
+
import type { Db } from "./db/index.ts"
|
|
4
|
+
import * as schema from "./db/schema.ts"
|
|
5
|
+
|
|
6
|
+
const request = z.object({
|
|
7
|
+
/** What is being asked, as the approvers see it. */
|
|
8
|
+
summary: z.string().min(1).max(200),
|
|
9
|
+
/** User ids; any one of them decides. */
|
|
10
|
+
approvers: z.array(z.string().min(1)).min(1).max(20),
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The example: someone asks, one of the approvers approves or rejects, and
|
|
15
|
+
* the run ends with the decision (a week without one counts as rejected).
|
|
16
|
+
* Copy it for your own: put each side effect in `step.run`, so a replay after
|
|
17
|
+
* a restart does not repeat it.
|
|
18
|
+
*/
|
|
19
|
+
export const approvalRequest = defineWorkflow("approval-request", async (step, input: unknown) => {
|
|
20
|
+
const { summary, approvers } = request.parse(input)
|
|
21
|
+
const decision = await step.approval({ approvers, summary, timeout: 7 * 86_400_000 })
|
|
22
|
+
return { approved: decision.approved, by: decision.by ?? null, reason: decision.reason ?? null }
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
/** Every workflow the app runs. A run of one not listed here fails. */
|
|
26
|
+
export const WORKFLOWS = [approvalRequest]
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The engine over the workflow tables. services.ts serves it to the routes;
|
|
30
|
+
* server.ts runs another over the same database, ticking every second.
|
|
31
|
+
*/
|
|
32
|
+
export const createAppWorkflows = (db: Db) =>
|
|
33
|
+
createWorkflows(db, { tables: schema, workflows: WORKFLOWS })
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { createAppWorkflows } from "../api/workflows.ts"
|
|
3
|
+
import { app, db, post, signIn } from "./helpers.ts"
|
|
4
|
+
|
|
5
|
+
// server.ts runs the runner; the tests tick it by hand.
|
|
6
|
+
const runner = createAppWorkflows(db)
|
|
7
|
+
const get = (path: string, cookie: string) => app.request(path, { headers: { cookie } })
|
|
8
|
+
const userId = async (cookie: string) =>
|
|
9
|
+
((await (await get("/api/account/me", cookie)).json()) as { user: { id: string } }).user.id
|
|
10
|
+
|
|
11
|
+
test("an approval request: started, waiting on its approver, decided, finished", async () => {
|
|
12
|
+
const asker = await signIn("+919000000061")
|
|
13
|
+
const approver = await signIn("+919000000062")
|
|
14
|
+
const other = await signIn("+919000000063")
|
|
15
|
+
const input = { summary: "Approve this request", approvers: [await userId(approver)] }
|
|
16
|
+
const started = await post("/api/workflows/runs", { workflow: "approval-request", input }, asker)
|
|
17
|
+
expect(started.status).toBe(201)
|
|
18
|
+
const run = (await started.json()) as { id: string }
|
|
19
|
+
await runner.tick()
|
|
20
|
+
|
|
21
|
+
expect(await (await get("/api/workflows/approvals", approver)).json()).toMatchObject([
|
|
22
|
+
{ runId: run.id, stepId: "approval", summary: "Approve this request" },
|
|
23
|
+
])
|
|
24
|
+
// Someone else's run reads as missing; only a listed approver decides.
|
|
25
|
+
expect((await get(`/api/workflows/runs/${run.id}`, other)).status).toBe(404)
|
|
26
|
+
const approve = `/api/workflows/runs/${run.id}/steps/approval/approve`
|
|
27
|
+
expect((await post(approve, {}, asker)).status).toBe(403)
|
|
28
|
+
expect((await post(approve, { reason: "Looks right" }, approver)).status).toBe(200)
|
|
29
|
+
await runner.tick()
|
|
30
|
+
|
|
31
|
+
const done = (await (await get(`/api/workflows/runs/${run.id}`, asker)).json()) as {
|
|
32
|
+
run: { status: string; output: unknown }
|
|
33
|
+
}
|
|
34
|
+
expect(done.run).toMatchObject({
|
|
35
|
+
status: "completed",
|
|
36
|
+
output: { approved: true, reason: "Looks right" },
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test("over HTTP, people start the example workflow and nothing else", async () => {
|
|
41
|
+
const cookie = await signIn("+919000000064")
|
|
42
|
+
const res = await post("/api/workflows/runs", { workflow: "something-else", input: {} }, cookie)
|
|
43
|
+
expect(res.status).toBe(403)
|
|
44
|
+
})
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ApprovalInboxCard } from "@dapplelight/signet-ui/screens/workflow-screens"
|
|
2
|
+
import { useQuery, useQueryClient } from "@tanstack/react-query"
|
|
3
|
+
import { createFileRoute } from "@tanstack/react-router"
|
|
4
|
+
import { api } from "../../lib/api"
|
|
5
|
+
|
|
6
|
+
export const Route = createFileRoute("/_app/approvals")({
|
|
7
|
+
component: Approvals,
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
// Workflow approval steps waiting on you (signet-db workflows, /api/workflows),
|
|
11
|
+
// each with Approve and Reject. The run carries on at the runner's next tick.
|
|
12
|
+
function Approvals() {
|
|
13
|
+
const qc = useQueryClient()
|
|
14
|
+
const approvals = useQuery({
|
|
15
|
+
queryKey: ["approvals"],
|
|
16
|
+
queryFn: async () => (await api.workflows.approvals.$get()).json(),
|
|
17
|
+
}).data
|
|
18
|
+
if (!approvals) return null
|
|
19
|
+
const decide = (verb: "approve" | "reject") => async (a: { runId: string; stepId: string }) => {
|
|
20
|
+
await api.workflows.runs[":id"].steps[":step"][verb].$post({
|
|
21
|
+
param: { id: a.runId, step: a.stepId },
|
|
22
|
+
})
|
|
23
|
+
await qc.invalidateQueries({ queryKey: ["approvals"] })
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<ApprovalInboxCard
|
|
28
|
+
approvals={approvals}
|
|
29
|
+
withReason={false}
|
|
30
|
+
onApprove={decide("approve")}
|
|
31
|
+
onReject={decide("reject")}
|
|
32
|
+
/>
|
|
33
|
+
)
|
|
34
|
+
}
|