@pplethai/cli 0.22.0 → 0.24.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 +12 -14
- package/dist/bin.js +109 -109
- package/package.json +3 -6
- package/templates/golden-path/.dev.vars.example +0 -33
- package/templates/golden-path/.env.example +0 -10
- package/templates/golden-path/.github/workflows/ci.yml +0 -22
- package/templates/golden-path/AGENTS.md +0 -1
- package/templates/golden-path/CLAUDE.md +0 -191
- package/templates/golden-path/README.md +0 -121
- package/templates/golden-path/gitignore +0 -17
- package/templates/golden-path/index.html +0 -16
- package/templates/golden-path/migrations/0001_init.sql +0 -10
- package/templates/golden-path/overlay-mtime.mjs +0 -30
- package/templates/golden-path/package-lock.json +0 -4318
- package/templates/golden-path/package.json +0 -41
- package/templates/golden-path/scripts/assert-dev-compiled-out.mjs +0 -45
- package/templates/golden-path/shared/api.ts +0 -325
- package/templates/golden-path/src/auth.ts +0 -230
- package/templates/golden-path/src/call-service.ts +0 -147
- package/templates/golden-path/src/dataset-query.ts +0 -470
- package/templates/golden-path/src/dataset-schema.ts +0 -398
- package/templates/golden-path/src/db.ts +0 -32
- package/templates/golden-path/src/dev-call-service.ts +0 -392
- package/templates/golden-path/src/dev-personas.ts +0 -186
- package/templates/golden-path/src/dev-read-dataset.ts +0 -444
- package/templates/golden-path/src/dev-registry.ts +0 -294
- package/templates/golden-path/src/email.ts +0 -49
- package/templates/golden-path/src/entry-name.ts +0 -114
- package/templates/golden-path/src/env.ts +0 -38
- package/templates/golden-path/src/files.ts +0 -38
- package/templates/golden-path/src/identity-header.ts +0 -17
- package/templates/golden-path/src/index.ts +0 -282
- package/templates/golden-path/src/legal-identity.ts +0 -180
- package/templates/golden-path/src/levels.ts +0 -132
- package/templates/golden-path/src/notification-response.ts +0 -55
- package/templates/golden-path/src/notify.ts +0 -311
- package/templates/golden-path/src/party-email.ts +0 -183
- package/templates/golden-path/src/payment.ts +0 -676
- package/templates/golden-path/src/read-dataset.ts +0 -490
- package/templates/golden-path/src/registry.ts +0 -128
- package/templates/golden-path/src/vite-env.d.ts +0 -27
- package/templates/golden-path/test/apply-migrations.ts +0 -4
- package/templates/golden-path/test/call-service.test.ts +0 -288
- package/templates/golden-path/test/dev-call-service.test.ts +0 -455
- package/templates/golden-path/test/dev-read-dataset.test.ts +0 -379
- package/templates/golden-path/test/dev-registry.test.ts +0 -404
- package/templates/golden-path/test/effective-sub.test.ts +0 -101
- package/templates/golden-path/test/email-offline.test.ts +0 -51
- package/templates/golden-path/test/email.test.ts +0 -117
- package/templates/golden-path/test/env.d.ts +0 -17
- package/templates/golden-path/test/fixtures/overlay/branch-list/entry.md +0 -85
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/audience.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/named.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/offices.json +0 -6
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/roster.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/summary.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/catalog/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +0 -11
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +0 -24
- package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/orders/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/orders/mocks/responses.json +0 -36
- package/templates/golden-path/test/fixtures/overlay/platform/email/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/email/mocks/responses.json +0 -11
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/mocks/responses.json +0 -13
- package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +0 -98
- package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/score/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/harness.ts +0 -153
- package/templates/golden-path/test/interaction.test.ts +0 -230
- package/templates/golden-path/test/legal-identity.test.ts +0 -323
- package/templates/golden-path/test/membership.test.ts +0 -105
- package/templates/golden-path/test/notification-response.test.ts +0 -79
- package/templates/golden-path/test/payment-app.test.ts +0 -211
- package/templates/golden-path/test/payment-offline.test.ts +0 -91
- package/templates/golden-path/test/payment.test.ts +0 -518
- package/templates/golden-path/test/payments-offline.ts +0 -33
- package/templates/golden-path/test/read-dataset.test.ts +0 -387
- package/templates/golden-path/test/service-call.test.ts +0 -166
- package/templates/golden-path/test/smoke.test.ts +0 -464
- package/templates/golden-path/tsconfig.json +0 -20
- package/templates/golden-path/tsconfig.worker.json +0 -21
- package/templates/golden-path/vite.config.ts +0 -12
- package/templates/golden-path/vitest.config.ts +0 -28
- package/templates/golden-path/web/App.tsx +0 -478
- package/templates/golden-path/web/app.css +0 -83
- package/templates/golden-path/web/main.tsx +0 -51
- package/templates/golden-path/web/pple.ts +0 -124
- package/templates/golden-path/web/vite-env.d.ts +0 -10
- package/templates/golden-path/wrangler.jsonc +0 -89
- package/templates/payment-bridge/.github/workflows/ci.yml +0 -21
- package/templates/payment-bridge/CLAUDE.md +0 -9
- package/templates/payment-bridge/README.md +0 -57
- package/templates/payment-bridge/docs/verify-webhook.md +0 -80
- package/templates/payment-bridge/docs/verify-webhook.ts +0 -29
- package/templates/payment-bridge/gitignore +0 -5
- package/templates/payment-bridge/package-lock.json +0 -4198
- package/templates/payment-bridge/package.json +0 -21
- package/templates/payment-bridge/src/bridge-key.ts +0 -18
- package/templates/payment-bridge/src/env.ts +0 -14
- package/templates/payment-bridge/src/index.ts +0 -39
- package/templates/payment-bridge/src/platform.ts +0 -49
- package/templates/payment-bridge/src/webhook.ts +0 -86
- package/templates/payment-bridge/test/docs.test.ts +0 -9
- package/templates/payment-bridge/test/fixtures/payments-responses.json +0 -98
- package/templates/payment-bridge/test/harness.ts +0 -78
- package/templates/payment-bridge/test/inbound.test.ts +0 -138
- package/templates/payment-bridge/test/webhook.test.ts +0 -174
- package/templates/payment-bridge/tsconfig.json +0 -22
- package/templates/payment-bridge/vitest.config.ts +0 -8
- package/templates/payment-bridge/wrangler.jsonc +0 -15
- package/templates/payment-bridge.version +0 -1
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
// Push notifications: Broadcast and Direct.
|
|
2
|
-
//
|
|
3
|
-
// Every send states its audience, and the two kinds follow from that:
|
|
4
|
-
//
|
|
5
|
-
// • Broadcast (การแจ้งเตือนทุกคน) — `{ kind: "all" }`, to everyone this app
|
|
6
|
-
// can reach: its App Users ∩ its current publication-tier audience.
|
|
7
|
-
// • Direct (การแจ้งเตือนรายบุคคล) — `{ kind: "direct", recipients }`, to the
|
|
8
|
-
// people this app names, by PPLE ID `sub` or mobile number.
|
|
9
|
-
//
|
|
10
|
-
// Naming narrows the audience and never widens it — the rule, and what
|
|
11
|
-
// `not_reachable` may be read as, are in shared/api.ts. The privacy model in
|
|
12
|
-
// one line: an app can reach the people who use it, say which of them it
|
|
13
|
-
// means, and reach no one else; and it can deep-link into itself, not into
|
|
14
|
-
// another team's mini app.
|
|
15
|
-
//
|
|
16
|
-
// See docs/integration/direct-notifications.md for the contract and
|
|
17
|
-
// docs/adr/0017 for why naming recipients is safe but per-recipient *reasons*
|
|
18
|
-
// are not.
|
|
19
|
-
//
|
|
20
|
-
// Wiring: the platform provisions NOTIFY_API_BASE and this app's own
|
|
21
|
-
// NotificationApiKey as NOTIFY_API_KEY, so `POST {NOTIFY_API_BASE}/notifications`
|
|
22
|
-
// lands on PPLE Today's send route. The key is the whole address — PPLE Today
|
|
23
|
-
// resolves it to this app and derives the reachable set from that, so the
|
|
24
|
-
// request carries no app id.
|
|
25
|
-
//
|
|
26
|
-
// The wire shape below is PPLE Today's, not this app's: your code passes the
|
|
27
|
-
// friendlier `{ audience, title, body, linkPath? }` and this is where it
|
|
28
|
-
// becomes `{ audience, content: { header, message }, linkPath? }`. Keeping that
|
|
29
|
-
// translation in one place means the rest of the app never has to know the
|
|
30
|
-
// platform's vocabulary.
|
|
31
|
-
import type { Notification, NotificationDelivery, NotificationRecipient } from "../shared/api";
|
|
32
|
-
|
|
33
|
-
interface NotifyEnv {
|
|
34
|
-
NOTIFY_API_BASE: string;
|
|
35
|
-
NOTIFY_API_KEY?: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Every refusal this app can get back, named in terms a Builder can act on.
|
|
40
|
-
*
|
|
41
|
-
* - `notifications_not_configured` — no key bound (e.g. local dev).
|
|
42
|
-
* - `invalid_request` — the send itself was malformed: no audience, an
|
|
43
|
-
* unknown `kind`, a `direct` send with no `recipients` array.
|
|
44
|
-
* - `invalid_recipients` — the list was: empty, over the 200 cap, or an entry
|
|
45
|
-
* naming neither or both of `sub`/`phone`.
|
|
46
|
-
* - `idempotency_key_conflict` — that key was already used with a list of a
|
|
47
|
-
* different length, so its stored outcome cannot be replayed onto this one.
|
|
48
|
-
* - `quota_exceeded` — **nothing was delivered.** Quota is checked up front
|
|
49
|
-
* against the named count, so the call is rejected whole rather than
|
|
50
|
-
* half-sent.
|
|
51
|
-
* - `not_permitted` — this key may not use this route.
|
|
52
|
-
* - `invalid_interaction` — PPLE Today refused the `interaction`; `message`
|
|
53
|
-
* names the first failing path, e.g. `interaction.actions[1].requires[0]`.
|
|
54
|
-
* Nothing was sent.
|
|
55
|
-
* - `interaction_unavailable` — this app cannot send an interaction right now:
|
|
56
|
-
* no callback target yet, or the feature is switched off. Nothing was sent;
|
|
57
|
-
* retrying without `interaction` sends a plain notification.
|
|
58
|
-
* - `notification_not_found` — `closeInteraction` named an id that is not one
|
|
59
|
-
* of this app's notifications (PPLE Today answers the same for all of them).
|
|
60
|
-
* - `send_failed` — anything else.
|
|
61
|
-
*/
|
|
62
|
-
export type NotifyError =
|
|
63
|
-
| "notifications_not_configured"
|
|
64
|
-
| "invalid_request"
|
|
65
|
-
| "invalid_recipients"
|
|
66
|
-
| "idempotency_key_conflict"
|
|
67
|
-
| "quota_exceeded"
|
|
68
|
-
| "not_permitted"
|
|
69
|
-
| "invalid_interaction"
|
|
70
|
-
| "interaction_unavailable"
|
|
71
|
-
| "notification_not_found"
|
|
72
|
-
| "send_failed";
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* What the platform reports alongside a `quota_exceeded` refusal, so an app
|
|
76
|
-
* can back off rather than guess. Denominated in **deliveries**, not calls: a
|
|
77
|
-
* Direct send debits one unit per name it addressed (delivered or not), and a
|
|
78
|
-
* Broadcast debits its audience size.
|
|
79
|
-
*/
|
|
80
|
-
export interface NotifyQuota {
|
|
81
|
-
dailyQuota?: number;
|
|
82
|
-
remaining?: number;
|
|
83
|
-
/** When the budget resets (Asia/Bangkok midnight). */
|
|
84
|
-
resetAt?: string;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* `notificationId` is PPLE Today's id for the send, the one `closeInteraction`
|
|
89
|
-
* takes. A replay of an `idempotencyKey` returns the original id. It is absent
|
|
90
|
-
* only if PPLE Today did not give one (for instance an audience that reached
|
|
91
|
-
* no one).
|
|
92
|
-
*/
|
|
93
|
-
export type NotifyResult =
|
|
94
|
-
| { ok: true; audience: "all"; notificationId?: string }
|
|
95
|
-
| {
|
|
96
|
-
ok: true;
|
|
97
|
-
audience: "direct";
|
|
98
|
-
results: NotificationDelivery[];
|
|
99
|
-
notificationId?: string;
|
|
100
|
-
}
|
|
101
|
-
| {
|
|
102
|
-
ok: false;
|
|
103
|
-
status: number;
|
|
104
|
-
error: NotifyError;
|
|
105
|
-
/** PPLE Today's own words, kept for `invalid_interaction`. */
|
|
106
|
-
message?: string;
|
|
107
|
-
quota?: NotifyQuota;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
export type CloseInteractionResult =
|
|
111
|
-
| { ok: true; closedAt?: string }
|
|
112
|
-
| { ok: false; status: number; error: NotifyError };
|
|
113
|
-
|
|
114
|
-
export async function sendNotification(
|
|
115
|
-
env: NotifyEnv,
|
|
116
|
-
input: Notification,
|
|
117
|
-
fetchImpl: typeof fetch = globalThis.fetch.bind(globalThis),
|
|
118
|
-
): Promise<NotifyResult> {
|
|
119
|
-
if (!env.NOTIFY_API_BASE || !env.NOTIFY_API_KEY) {
|
|
120
|
-
// Not wired up (e.g. local dev without a key). Fail loud, not silent.
|
|
121
|
-
return { ok: false, status: 503, error: "notifications_not_configured" };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const res = await fetchImpl(`${env.NOTIFY_API_BASE}/notifications`, {
|
|
125
|
-
method: "POST",
|
|
126
|
-
headers: {
|
|
127
|
-
authorization: `Bearer ${env.NOTIFY_API_KEY}`,
|
|
128
|
-
"content-type": "application/json",
|
|
129
|
-
},
|
|
130
|
-
body: JSON.stringify({
|
|
131
|
-
// Stated on every send, including a broadcast. There is no shape of this
|
|
132
|
-
// request that leaves the audience to be guessed.
|
|
133
|
-
audience: input.audience,
|
|
134
|
-
content: { header: input.title, message: input.body },
|
|
135
|
-
...(input.linkPath !== undefined ? { linkPath: input.linkPath } : {}),
|
|
136
|
-
...(input.idempotencyKey !== undefined
|
|
137
|
-
? { idempotencyKey: input.idempotencyKey }
|
|
138
|
-
: {}),
|
|
139
|
-
// Passed on unchanged: PPLE Today owns its validation.
|
|
140
|
-
...(input.interaction !== undefined ? { interaction: input.interaction } : {}),
|
|
141
|
-
}),
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
if (!res.ok) {
|
|
145
|
-
const body = await readJson(res);
|
|
146
|
-
const code = codeOf(body);
|
|
147
|
-
const error = interactionErrorFor(code) ?? errorFor(res.status);
|
|
148
|
-
const quota = error === "quota_exceeded" ? quotaOf(body) : undefined;
|
|
149
|
-
const message = error === "invalid_interaction" ? messageOf(body) : undefined;
|
|
150
|
-
return {
|
|
151
|
-
ok: false,
|
|
152
|
-
status: res.status,
|
|
153
|
-
error,
|
|
154
|
-
...(message ? { message } : {}),
|
|
155
|
-
...(quota ? { quota } : {}),
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const body = await readJson(res);
|
|
160
|
-
const id = (body as { notificationId?: unknown } | null)?.notificationId;
|
|
161
|
-
const notificationId = typeof id === "string" ? { notificationId: id } : {};
|
|
162
|
-
if (input.audience.kind === "all") return { ok: true, audience: "all", ...notificationId };
|
|
163
|
-
return {
|
|
164
|
-
ok: true,
|
|
165
|
-
audience: "direct",
|
|
166
|
-
results: zipResults(input.audience.recipients, body),
|
|
167
|
-
...notificationId,
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Stop accepting answers to one of this app's Interactive Notifications.
|
|
173
|
-
* Idempotent: closing again succeeds. Answers already in flight can still
|
|
174
|
-
* arrive after this returns, so an app that needs "first wins" settles it on
|
|
175
|
-
* its own side by `respondedAt`.
|
|
176
|
-
*/
|
|
177
|
-
export async function closeInteraction(
|
|
178
|
-
env: NotifyEnv,
|
|
179
|
-
notificationId: string,
|
|
180
|
-
fetchImpl: typeof fetch = globalThis.fetch.bind(globalThis),
|
|
181
|
-
): Promise<CloseInteractionResult> {
|
|
182
|
-
if (!env.NOTIFY_API_BASE || !env.NOTIFY_API_KEY) {
|
|
183
|
-
return { ok: false, status: 503, error: "notifications_not_configured" };
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
const res = await fetchImpl(
|
|
187
|
-
`${env.NOTIFY_API_BASE}/notifications/${encodeURIComponent(notificationId)}/interaction/close`,
|
|
188
|
-
{ method: "POST", headers: { authorization: `Bearer ${env.NOTIFY_API_KEY}` } },
|
|
189
|
-
);
|
|
190
|
-
|
|
191
|
-
if (!res.ok) {
|
|
192
|
-
const code = codeOf(await readJson(res));
|
|
193
|
-
const error =
|
|
194
|
-
interactionErrorFor(code) ??
|
|
195
|
-
(res.status === 404
|
|
196
|
-
? "notification_not_found"
|
|
197
|
-
: res.status === 401 || res.status === 403
|
|
198
|
-
? "not_permitted"
|
|
199
|
-
: "send_failed");
|
|
200
|
-
return { ok: false, status: res.status, error };
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const closedAt = (await readJson(res) as { closedAt?: unknown } | null)?.closedAt;
|
|
204
|
-
return { ok: true, ...(typeof closedAt === "string" ? { closedAt } : {}) };
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* PPLE Today's own codes for interaction refusals. These share status 400 with
|
|
209
|
-
* recipient refusals, so the code is read before the status is.
|
|
210
|
-
*/
|
|
211
|
-
function interactionErrorFor(code: string): NotifyError | undefined {
|
|
212
|
-
switch (code) {
|
|
213
|
-
case "NOTIFICATION_INVALID_INTERACTION":
|
|
214
|
-
return "invalid_interaction";
|
|
215
|
-
case "NOTIFICATION_INTERACTION_NO_CALLBACK":
|
|
216
|
-
case "INTERACTIVE_NOTIFICATIONS_DISABLED":
|
|
217
|
-
return "interaction_unavailable";
|
|
218
|
-
default:
|
|
219
|
-
return undefined;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/** PPLE Today answers `{ error: { code, message } }`; tolerate a flat body too. */
|
|
224
|
-
function errorObject(body: unknown): Record<string, unknown> {
|
|
225
|
-
if (typeof body !== "object" || body === null) return {};
|
|
226
|
-
const { error } = body as { error?: unknown };
|
|
227
|
-
return typeof error === "object" && error !== null
|
|
228
|
-
? (error as Record<string, unknown>)
|
|
229
|
-
: (body as Record<string, unknown>);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
function codeOf(body: unknown): string {
|
|
233
|
-
const { code } = errorObject(body);
|
|
234
|
-
return typeof code === "string" ? code : "";
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function messageOf(body: unknown): string | undefined {
|
|
238
|
-
const { message } = errorObject(body);
|
|
239
|
-
return typeof message === "string" ? message : undefined;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* The platform splits its refusals: schema-level ones answer 422 (its whole
|
|
244
|
-
* API validates uniformly, and this route does not get to be the exception),
|
|
245
|
-
* recipient-level ones answer 400. Both are refusals — none is a broadcast,
|
|
246
|
-
* none is a silent no-op — but a client that branches on 400 alone is wrong.
|
|
247
|
-
* The status table lives in docs/integration/direct-notifications.md.
|
|
248
|
-
*/
|
|
249
|
-
function errorFor(status: number): NotifyError {
|
|
250
|
-
switch (status) {
|
|
251
|
-
case 422:
|
|
252
|
-
return "invalid_request";
|
|
253
|
-
case 400:
|
|
254
|
-
return "invalid_recipients";
|
|
255
|
-
case 403:
|
|
256
|
-
return "not_permitted";
|
|
257
|
-
case 409:
|
|
258
|
-
return "idempotency_key_conflict";
|
|
259
|
-
case 429:
|
|
260
|
-
return "quota_exceeded";
|
|
261
|
-
default:
|
|
262
|
-
return "send_failed";
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
async function readJson(res: Response): Promise<unknown> {
|
|
267
|
-
return res.json().catch(() => null);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* The three figures the contract says a 429 carries, read from the top level
|
|
272
|
-
* of the refusal body. If the platform ever nests them under an envelope, this
|
|
273
|
-
* is the one place to adjust — the refusal itself is reported either way.
|
|
274
|
-
*/
|
|
275
|
-
function quotaOf(body: unknown): NotifyQuota | undefined {
|
|
276
|
-
if (typeof body !== "object" || body === null) return undefined;
|
|
277
|
-
const { dailyQuota, remaining, resetAt } = body as Record<string, unknown>;
|
|
278
|
-
const quota: NotifyQuota = {
|
|
279
|
-
...(typeof dailyQuota === "number" ? { dailyQuota } : {}),
|
|
280
|
-
...(typeof remaining === "number" ? { remaining } : {}),
|
|
281
|
-
...(typeof resetAt === "string" ? { resetAt } : {}),
|
|
282
|
-
};
|
|
283
|
-
return Object.keys(quota).length > 0 ? quota : undefined;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Pair each recipient this app named with what the platform said about it.
|
|
288
|
-
* The results come back in the order they were named, one per entry, so this
|
|
289
|
-
* pairs them by position and hands back the caller's *own* entry — ready to
|
|
290
|
-
* match onto the list the app holds.
|
|
291
|
-
*
|
|
292
|
-
* Two rules, both in the direction of not lying: a list of a different length
|
|
293
|
-
* is not the answer to this send, so none of it is trusted; and anything not
|
|
294
|
-
* affirmatively reported `delivered` is reported `not_reachable`. See
|
|
295
|
-
* shared/api.ts for what that status may and may not be read as.
|
|
296
|
-
*/
|
|
297
|
-
function zipResults(
|
|
298
|
-
recipients: readonly NotificationRecipient[],
|
|
299
|
-
body: unknown,
|
|
300
|
-
): NotificationDelivery[] {
|
|
301
|
-
const reported = (body as { results?: unknown } | null)?.results;
|
|
302
|
-
const results =
|
|
303
|
-
Array.isArray(reported) && reported.length === recipients.length ? reported : [];
|
|
304
|
-
return recipients.map((recipient, i) => ({
|
|
305
|
-
recipient,
|
|
306
|
-
status:
|
|
307
|
-
(results[i] as { status?: unknown } | undefined)?.status === "delivered"
|
|
308
|
-
? "delivered"
|
|
309
|
-
: "not_reachable",
|
|
310
|
-
}));
|
|
311
|
-
}
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
// The party's email layout and safe HTML building: no imports, so the platform
|
|
2
|
-
// builds its own email (the Receipt, in workers/) from this same file and every
|
|
3
|
-
// email under the party's name looks alike. Apps use it through `./email`.
|
|
4
|
-
//
|
|
5
|
-
// The layout is hand-written table HTML with inline styles, which is what mail
|
|
6
|
-
// apps (Gmail, Outlook, Apple Mail) render alike. Keep `content` simple:
|
|
7
|
-
// paragraphs, links, lists, `<b>`. The logo is a PNG the platform serves.
|
|
8
|
-
|
|
9
|
-
/** HTML that is safe to put in an email as it is: built by `html`, or vouched for with `raw`. */
|
|
10
|
-
export class SafeHtml {
|
|
11
|
-
constructor(readonly value: string) {}
|
|
12
|
-
toString(): string {
|
|
13
|
-
return this.value;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** Text in, the same text as HTML out: `&`, `<`, `>`, `"` and `'` become entities. */
|
|
18
|
-
export function escapeHtml(text: string): string {
|
|
19
|
-
return text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Markup you wrote yourself, inserted as it is. Never pass it anything a user typed. */
|
|
23
|
-
export function raw(trusted: string): SafeHtml {
|
|
24
|
-
return new SafeHtml(trusted);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
type HtmlValue = SafeHtml | string | number | boolean | null | undefined | readonly HtmlValue[];
|
|
28
|
-
|
|
29
|
-
function render(value: HtmlValue): string {
|
|
30
|
-
if (value instanceof SafeHtml) return value.value;
|
|
31
|
-
if (Array.isArray(value)) return value.map(render).join("");
|
|
32
|
-
if (value === null || value === undefined || value === false) return "";
|
|
33
|
-
return escapeHtml(String(value));
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* A tagged template that escapes every inserted value. Nest it freely: an
|
|
38
|
-
* inserted `html` result (or a list of them) goes in as markup, not escaped twice.
|
|
39
|
-
*
|
|
40
|
-
* html`<p>สวัสดี ${user.name}</p>` // name is escaped
|
|
41
|
-
* html`<ul>${items.map((i) => html`<li>${i}</li>`)}</ul>`
|
|
42
|
-
*/
|
|
43
|
-
export function html(strings: TemplateStringsArray, ...values: HtmlValue[]): SafeHtml {
|
|
44
|
-
let out = strings[0] ?? "";
|
|
45
|
-
values.forEach((value, i) => {
|
|
46
|
-
out += render(value) + (strings[i + 1] ?? "");
|
|
47
|
-
});
|
|
48
|
-
return new SafeHtml(out);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** A link a reader may follow: `https:` or `mailto:` only, or this throws. */
|
|
52
|
-
export function safeUrl(url: string): string {
|
|
53
|
-
let parsed: URL;
|
|
54
|
-
try {
|
|
55
|
-
parsed = new URL(url);
|
|
56
|
-
} catch {
|
|
57
|
-
throw new Error(`not a URL: ${url}`);
|
|
58
|
-
}
|
|
59
|
-
if (parsed.protocol !== "https:" && parsed.protocol !== "mailto:") {
|
|
60
|
-
throw new Error(`email links must be https: or mailto:, not ${parsed.protocol}`);
|
|
61
|
-
}
|
|
62
|
-
return parsed.toString();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** The party's contact details, printed in every email's footer. */
|
|
66
|
-
export const PARTY = {
|
|
67
|
-
name: "พรรคประชาชน",
|
|
68
|
-
logoUrl: "https://services.ppleth.ai/email-assets/logo.png",
|
|
69
|
-
logoAlt: "ภาพโลโก้พรรคประชาชน",
|
|
70
|
-
phone: "02-038-5100",
|
|
71
|
-
phoneHref: "tel:+6620385100",
|
|
72
|
-
hours: "จันทร์-ศุกร์ 10:00-18:00 น.",
|
|
73
|
-
email: "office@peoplespartythailand.org",
|
|
74
|
-
line: "@peoplesth",
|
|
75
|
-
lineUrl: "https://line.me/R/ti/p/@peoplesth",
|
|
76
|
-
address: ["พรรคประชาชน สำนักงานใหญ่", "เลขที่ 167 ชั้น 4 ซอย รามคำแหง 42", "แขวงหัวหมาก เขตบางกะปิ", "กรุงเทพมหานคร 10240"],
|
|
77
|
-
} as const;
|
|
78
|
-
|
|
79
|
-
const NAVY = "#0b3a66";
|
|
80
|
-
const ORANGE = "#ff6413";
|
|
81
|
-
const INK = "#1f2937";
|
|
82
|
-
const FONT = "'IBM Plex Sans Thai Looped','Noto Sans Thai','Leelawadee UI',Tahoma,sans-serif";
|
|
83
|
-
|
|
84
|
-
export interface PartyEmailInput {
|
|
85
|
-
/** The big line at the top of the message. */
|
|
86
|
-
heading: string;
|
|
87
|
-
/** The message, built with `html`. */
|
|
88
|
-
content: SafeHtml;
|
|
89
|
-
/** The same message as plain text, for mail apps that show no HTML. */
|
|
90
|
-
text: string;
|
|
91
|
-
/** One call to action. Its URL must be `https:` or `mailto:`. */
|
|
92
|
-
button?: { label: string; url: string };
|
|
93
|
-
/** The grey line mail apps show beside the subject. Defaults to none. */
|
|
94
|
-
preheader?: string;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Dark mode. Mail apps treat it three ways, and the layout is built for all:
|
|
98
|
-
// - Apple Mail, iOS Mail, Outlook for Mac honour `prefers-color-scheme`: the
|
|
99
|
-
// <style> block below swaps the light card for a dark one.
|
|
100
|
-
// - Gmail's apps ignore it and invert colours themselves: a navy block turns
|
|
101
|
-
// light blue and white text turns dark. They leave a `background-image`
|
|
102
|
-
// alone, so the navy header and the orange button are painted twice
|
|
103
|
-
// (`bg()`). Text colours are still inverted, so nothing relies on white text
|
|
104
|
-
// on a dark block: the header carries only the logo (an image, never
|
|
105
|
-
// inverted), and the footer is light with dark text, which Gmail inverts
|
|
106
|
-
// the way it inverts the body.
|
|
107
|
-
// - Clients that do neither show the light layout as it is.
|
|
108
|
-
const DARK_CSS = `
|
|
109
|
-
:root { color-scheme: light dark; supported-color-schemes: light dark; }
|
|
110
|
-
a[x-apple-data-detectors] { color: inherit !important; text-decoration: none !important; }
|
|
111
|
-
@media (prefers-color-scheme: dark) {
|
|
112
|
-
.pe-page { background: #0f141a !important; }
|
|
113
|
-
.pe-card { background: #1a2129 !important; }
|
|
114
|
-
.pe-ink, .pe-ink p, .pe-ink td, .pe-ink li { color: #e6e9ee !important; }
|
|
115
|
-
.pe-heading { color: #ffb48a !important; }
|
|
116
|
-
.pe-ink td { border-color: #2c3540 !important; }
|
|
117
|
-
.pe-foot { background: #11171e !important; border-color: #2c3540 !important; }
|
|
118
|
-
.pe-foot-title { color: #ffb48a !important; }
|
|
119
|
-
.pe-foot-label { color: #8b96a5 !important; }
|
|
120
|
-
.pe-foot-text, .pe-foot-text a { color: #d5dbe3 !important; }
|
|
121
|
-
}`;
|
|
122
|
-
|
|
123
|
-
/** A solid block Gmail's dark mode leaves alone: the colour twice, once as an image. */
|
|
124
|
-
const bg = (color: string) => raw(`background-color:${color};background-image:linear-gradient(${color},${color})`);
|
|
125
|
-
|
|
126
|
-
/** The party layout around your message: `{ html, body }` for `sendEmail`. */
|
|
127
|
-
export function partyEmail(input: PartyEmailInput): { html: string; body: string } {
|
|
128
|
-
const button = input.button
|
|
129
|
-
? html`<table role="presentation" cellpadding="0" cellspacing="0" style="margin:24px 0 8px"><tr><td style="border-radius:8px;${bg(ORANGE)}">
|
|
130
|
-
<a href="${safeUrl(input.button.url)}" style="display:inline-block;padding:12px 24px;font-family:${raw(FONT)};font-size:16px;font-weight:bold;color:#ffffff;text-decoration:none;border-radius:8px">${input.button.label}</a>
|
|
131
|
-
</td></tr></table>`
|
|
132
|
-
: "";
|
|
133
|
-
|
|
134
|
-
const contactRow = (label: string, value: SafeHtml) =>
|
|
135
|
-
html`<tr><td class="pe-foot-label" style="padding:4px 12px 4px 0;font-size:14px;color:#6b7280;white-space:nowrap;vertical-align:top">${label}</td><td class="pe-foot-text" style="padding:4px 0;font-size:15px;color:${INK}">${value}</td></tr>`;
|
|
136
|
-
const link = (href: string, text: string) => html`<a href="${href}" style="color:${INK};text-decoration:none">${text}</a>`;
|
|
137
|
-
// A deliberate, styled link: left bare, Gmail and iOS underline the address in blue themselves.
|
|
138
|
-
const mapsUrl = `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(PARTY.address.join(" "))}`;
|
|
139
|
-
|
|
140
|
-
const page = html`<!doctype html>
|
|
141
|
-
<html lang="th"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
142
|
-
<meta name="color-scheme" content="light dark"><meta name="supported-color-schemes" content="light dark">
|
|
143
|
-
<meta name="format-detection" content="telephone=no,address=no,email=no,date=no">
|
|
144
|
-
<title>${input.heading}</title><style>${raw(DARK_CSS)}</style></head>
|
|
145
|
-
<body class="body pe-page" style="margin:0;padding:0;background:#eef1f5">
|
|
146
|
-
${input.preheader ? html`<div style="display:none;max-height:0;overflow:hidden;opacity:0">${input.preheader}</div>` : ""}
|
|
147
|
-
<table role="presentation" class="pe-page" width="100%" cellpadding="0" cellspacing="0" style="background:#eef1f5"><tr><td align="center" style="padding:24px 12px">
|
|
148
|
-
<table role="presentation" class="pe-card" width="100%" cellpadding="0" cellspacing="0" style="max-width:600px;background:#ffffff;border-radius:12px;overflow:hidden;font-family:${raw(FONT)}">
|
|
149
|
-
<tr><td style="${bg(NAVY)};padding:24px 32px"><img src="${PARTY.logoUrl}" width="176" height="50" alt="${PARTY.logoAlt}" style="display:block;border:0;color:#ffffff;font-size:20px;font-weight:bold"></td></tr>
|
|
150
|
-
<tr><td class="pe-ink" style="padding:32px 32px 24px;color:${INK};font-size:16px;line-height:1.7">
|
|
151
|
-
<h1 class="pe-heading" style="margin:0 0 16px;font-size:24px;line-height:1.4;color:${NAVY}">${input.heading}</h1>
|
|
152
|
-
${input.content}
|
|
153
|
-
${button}
|
|
154
|
-
</td></tr>
|
|
155
|
-
<tr><td class="pe-foot" style="background:#f3f5f8;border-top:1px solid #e3e7ed;padding:28px 32px">
|
|
156
|
-
<div class="pe-foot-title" style="margin:0 0 12px;font-size:20px;font-weight:bold;color:${NAVY}">ติดต่อเรา</div>
|
|
157
|
-
<table role="presentation" cellpadding="0" cellspacing="0">
|
|
158
|
-
${contactRow("โทร", html`${link(PARTY.phoneHref, PARTY.phone)} (${PARTY.hours})`)}
|
|
159
|
-
${contactRow("อีเมล", link(`mailto:${PARTY.email}`, PARTY.email))}
|
|
160
|
-
${contactRow("LINE", link(PARTY.lineUrl, PARTY.line))}
|
|
161
|
-
</table>
|
|
162
|
-
<div class="pe-foot-text" style="margin:16px 0 0;font-size:14px;line-height:1.6;color:#4b5563"><a href="${mapsUrl}" style="color:#4b5563;text-decoration:none">${PARTY.address.map((line, i) => html`${i ? raw("<br>") : ""}${line}`)}</a></div>
|
|
163
|
-
</td></tr>
|
|
164
|
-
</table>
|
|
165
|
-
</td></tr></table>
|
|
166
|
-
</body></html>`;
|
|
167
|
-
|
|
168
|
-
const body = [
|
|
169
|
-
input.heading,
|
|
170
|
-
"",
|
|
171
|
-
input.text,
|
|
172
|
-
...(input.button ? ["", `${input.button.label}: ${safeUrl(input.button.url)}`] : []),
|
|
173
|
-
"",
|
|
174
|
-
"—",
|
|
175
|
-
`ติดต่อ${PARTY.name}`,
|
|
176
|
-
`โทร ${PARTY.phone} (${PARTY.hours})`,
|
|
177
|
-
`อีเมล ${PARTY.email}`,
|
|
178
|
-
`LINE ${PARTY.line}`,
|
|
179
|
-
...PARTY.address,
|
|
180
|
-
].join("\n");
|
|
181
|
-
|
|
182
|
-
return { html: page.value, body };
|
|
183
|
-
}
|