@deverjak/tenantkit-plugin-sms 0.1.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/LICENSE +21 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +94 -0
- package/dist/src/port.d.ts +37 -0
- package/dist/src/port.js +19 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chytré Digital
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/** Per-tenant SMS settings (doc 09 §6.5). Provider CREDENTIALS live in the vault, NEVER here (doc 09 §1, §6.5). */
|
|
3
|
+
export declare const SmsSettings: z.ZodObject<{
|
|
4
|
+
provider: z.ZodDefault<z.ZodEnum<{
|
|
5
|
+
twilio: "twilio";
|
|
6
|
+
smsbrana: "smsbrana";
|
|
7
|
+
}>>;
|
|
8
|
+
senderId: z.ZodString;
|
|
9
|
+
reminderHours: z.ZodDefault<z.ZodNumber>;
|
|
10
|
+
events: z.ZodObject<{
|
|
11
|
+
reminder: z.ZodDefault<z.ZodBoolean>;
|
|
12
|
+
creditIssued: z.ZodDefault<z.ZodBoolean>;
|
|
13
|
+
applicationApproved: z.ZodDefault<z.ZodBoolean>;
|
|
14
|
+
sessionCancelled: z.ZodDefault<z.ZodBoolean>;
|
|
15
|
+
}, z.core.$strip>;
|
|
16
|
+
quietHours: z.ZodOptional<z.ZodObject<{
|
|
17
|
+
start: z.ZodString;
|
|
18
|
+
end: z.ZodString;
|
|
19
|
+
tz: z.ZodString;
|
|
20
|
+
}, z.core.$strip>>;
|
|
21
|
+
monthlyCapMinor: z.ZodOptional<z.ZodNumber>;
|
|
22
|
+
}, z.core.$strip>;
|
|
23
|
+
export type SmsSettingsValues = z.infer<typeof SmsSettings>;
|
|
24
|
+
declare const _default: import("@deverjak/tenantkit-kernel").Plugin;
|
|
25
|
+
export default _default;
|
|
26
|
+
export { type SmsProvider } from './src/port';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realizes docs/09-plugins-and-subscriptions.md §6 + docs/02-reservation-core.md §12 — the SMS plugin.
|
|
3
|
+
*
|
|
4
|
+
* `plugin:sms` is `pro`-tier. It sends transactional SMS for the same events the email layer handles but over a
|
|
5
|
+
* different channel, behind a PROVIDER PORT so the concrete gateway (Twilio, SMSbrana) is swappable. It touches
|
|
6
|
+
* the system only through the five seams (doc 09 §1): its own `sms.*` schema, namespaced routes, the event
|
|
7
|
+
* subscriptions below, UI slots (none here), and a Zod settings schema. It NEVER alters core/public tables.
|
|
8
|
+
*
|
|
9
|
+
* Build order: ships AFTER `payments` (doc 09 §9) since it depends on the scheduler + `notification_preferences`.
|
|
10
|
+
*/
|
|
11
|
+
import { definePlugin } from '@deverjak/tenantkit-kernel';
|
|
12
|
+
import { z } from 'zod';
|
|
13
|
+
import { StubSmsProvider } from './src/port';
|
|
14
|
+
/** Per-tenant SMS settings (doc 09 §6.5). Provider CREDENTIALS live in the vault, NEVER here (doc 09 §1, §6.5). */
|
|
15
|
+
export const SmsSettings = z.object({
|
|
16
|
+
provider: z.enum(['twilio', 'smsbrana']).default('smsbrana'),
|
|
17
|
+
senderId: z.string().max(11), // alphanumeric sender, where the provider allows
|
|
18
|
+
reminderHours: z.number().int().default(24),
|
|
19
|
+
events: z.object({
|
|
20
|
+
reminder: z.boolean().default(true),
|
|
21
|
+
creditIssued: z.boolean().default(false),
|
|
22
|
+
applicationApproved: z.boolean().default(true),
|
|
23
|
+
sessionCancelled: z.boolean().default(true),
|
|
24
|
+
}),
|
|
25
|
+
quietHours: z.object({ start: z.string(), end: z.string(), tz: z.string() }).optional(),
|
|
26
|
+
monthlyCapMinor: z.number().int().optional(),
|
|
27
|
+
});
|
|
28
|
+
/** The bundled template pack seeded on enable (doc 09 §2.3, §6.4): one row per (key, locale). */
|
|
29
|
+
const TEMPLATE_PACK = [
|
|
30
|
+
{ key: 'session_reminder', locale: 'cs', body: 'Připomínka: lekce {{course}} zítra v {{time}}.' },
|
|
31
|
+
{ key: 'session_reminder', locale: 'en', body: 'Reminder: {{course}} lesson tomorrow at {{time}}.' },
|
|
32
|
+
{ key: 'credit_issued', locale: 'cs', body: 'Máte novou omluvenku, platí do {{expiresAt}}.' },
|
|
33
|
+
{ key: 'credit_issued', locale: 'en', body: 'You have a new makeup credit, valid until {{expiresAt}}.' },
|
|
34
|
+
];
|
|
35
|
+
/** Resolve the configured provider adapter from settings (doc 09 §6.1). Real adapters read creds from the vault. */
|
|
36
|
+
function selectProvider(settings) {
|
|
37
|
+
switch (settings.provider) {
|
|
38
|
+
// case 'twilio': return new TwilioProvider(...)
|
|
39
|
+
// case 'smsbrana': return new SmsbranaProvider(...)
|
|
40
|
+
default:
|
|
41
|
+
return new StubSmsProvider(); // mockup default — logs instead of dialing a gateway
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export default definePlugin({
|
|
45
|
+
id: 'sms',
|
|
46
|
+
name: { cs: 'SMS notifikace', en: 'SMS notifications' },
|
|
47
|
+
requiresTier: 'pro', // entitlement gate (doc 09 §3.2) — `plugin:sms` is pro-only
|
|
48
|
+
dbSchema: 'sms', // owns the sms.* schema; migrations ship with this package (doc 09 §1)
|
|
49
|
+
settingsSchema: SmsSettings,
|
|
50
|
+
/**
|
|
51
|
+
* Idempotent provisioning (doc 09 §2.3): seed default `sms.templates` so a freshly enabled tenant already has
|
|
52
|
+
* Czech + English reminder copy. PORTS REFACTOR (docs/14): the lifecycle ctx is vendor-neutral — `ctx.db` is a
|
|
53
|
+
* `ScopedDb` fenced to the plugin's OWN `sms.*` schema (doc 09 §7), not a Supabase client. Portable code calls
|
|
54
|
+
* a SECURITY DEFINER RPC; the on-conflict upsert lives inside `sms.seed_templates`, so enabling twice is a no-op.
|
|
55
|
+
*/
|
|
56
|
+
async onEnable(ctx) {
|
|
57
|
+
await ctx.db.rpc('seed_templates', { tenant_id: ctx.tenantId, templates: TEMPLATE_PACK });
|
|
58
|
+
},
|
|
59
|
+
/**
|
|
60
|
+
* Event subscriptions (doc 09 §6.2). Each handler reads core data read-only and writes only `sms.*` (a queued
|
|
61
|
+
* `sms.messages` row the dispatcher later hands to the provider). Consent / quiet-hours / cost accounting
|
|
62
|
+
* (doc 09 §6.3) are applied by the sender before the actual `provider.send`.
|
|
63
|
+
*/
|
|
64
|
+
events: {
|
|
65
|
+
/** N hours before a lesson — "Připomínka: lekce zítra v 17:00…" (doc 09 §6.2). */
|
|
66
|
+
'session.reminder_due': async (event) => {
|
|
67
|
+
await queueFromEvent(event, 'session_reminder');
|
|
68
|
+
},
|
|
69
|
+
/** On excuse → credit — "Máte novou omluvenku, platí do …" (doc 09 §5.1, §6.2; opt-in default off). */
|
|
70
|
+
'credit.issued': async (event) => {
|
|
71
|
+
await queueFromEvent(event, 'credit_issued');
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
// routes: mounted under /api/plugins/sms/* (e.g. a test-send + a costs report). Each is auto-wrapped with
|
|
75
|
+
// assertPluginEnabled (doc 09 §4) so an un-entitled tenant never reaches plugin code.
|
|
76
|
+
routes: {
|
|
77
|
+
'test-send': {
|
|
78
|
+
POST: async () => Response.json({ ok: true, note: 'mockup: would enqueue a test SMS via the configured provider' }),
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
/**
|
|
83
|
+
* Shared queue step: turn a core event into a queued `sms.messages` row (status='queued'). This is where a real
|
|
84
|
+
* impl resolves recipients + locale (guardian profile → tenant default, doc 09 §6.4), checks opt-in + quiet
|
|
85
|
+
* hours, renders the `{{var}}` template, and lets the dispatcher call `selectProvider(settings).send(...)`.
|
|
86
|
+
* Sketched as a structural stub so the subscription shape is concrete without wiring the dispatcher here.
|
|
87
|
+
*/
|
|
88
|
+
async function queueFromEvent(_event, _templateKey) {
|
|
89
|
+
// await admin.from('messages').insert({ tenant_id, to_phone, template, locale, body, status: 'queued' })
|
|
90
|
+
// void selectProvider(settings) // chosen at send time from per-tenant settings
|
|
91
|
+
void selectProvider;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
export {} from './src/port';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realizes docs/09-plugins-and-subscriptions.md §6.1 — the SMS provider PORT (infrastructure boundary, doc 01 §3).
|
|
3
|
+
*
|
|
4
|
+
* The application layer depends on `SmsProvider`, never on Twilio/SMSbrana directly — so a tenant on SMSbrana
|
|
5
|
+
* and a tenant on Twilio run identical code (the same lesson the four Supabase factories teach: parameterize the
|
|
6
|
+
* vendor). Concrete adapters (TwilioProvider, SmsbranaProvider) implement this and are selected by
|
|
7
|
+
* `settings.provider`; their credentials come from the vault (doc 09 §6.5), never from `plugin_settings`.
|
|
8
|
+
*/
|
|
9
|
+
/** E.164 phone string, e.g. '+420777123456'. Branded loosely; validated at the edge. */
|
|
10
|
+
export type E164 = string;
|
|
11
|
+
export interface SmsProvider {
|
|
12
|
+
/** Send one message. Returns the provider reference + (optional) cost in minor units for cost accounting. */
|
|
13
|
+
send(msg: {
|
|
14
|
+
to: E164;
|
|
15
|
+
body: string;
|
|
16
|
+
senderId?: string;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
ref: string;
|
|
19
|
+
costMinor?: number;
|
|
20
|
+
}>;
|
|
21
|
+
name: 'twilio' | 'smsbrana';
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A stub provider for the mockup / local / tests: it never dials a gateway, it just logs and returns a fake ref.
|
|
25
|
+
* Real deployments register `TwilioProvider` / `SmsbranaProvider` instead (selected in index.ts `selectProvider`).
|
|
26
|
+
*/
|
|
27
|
+
export declare class StubSmsProvider implements SmsProvider {
|
|
28
|
+
readonly name = "smsbrana";
|
|
29
|
+
send(msg: {
|
|
30
|
+
to: E164;
|
|
31
|
+
body: string;
|
|
32
|
+
senderId?: string;
|
|
33
|
+
}): Promise<{
|
|
34
|
+
ref: string;
|
|
35
|
+
costMinor?: number;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
package/dist/src/port.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realizes docs/09-plugins-and-subscriptions.md §6.1 — the SMS provider PORT (infrastructure boundary, doc 01 §3).
|
|
3
|
+
*
|
|
4
|
+
* The application layer depends on `SmsProvider`, never on Twilio/SMSbrana directly — so a tenant on SMSbrana
|
|
5
|
+
* and a tenant on Twilio run identical code (the same lesson the four Supabase factories teach: parameterize the
|
|
6
|
+
* vendor). Concrete adapters (TwilioProvider, SmsbranaProvider) implement this and are selected by
|
|
7
|
+
* `settings.provider`; their credentials come from the vault (doc 09 §6.5), never from `plugin_settings`.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* A stub provider for the mockup / local / tests: it never dials a gateway, it just logs and returns a fake ref.
|
|
11
|
+
* Real deployments register `TwilioProvider` / `SmsbranaProvider` instead (selected in index.ts `selectProvider`).
|
|
12
|
+
*/
|
|
13
|
+
export class StubSmsProvider {
|
|
14
|
+
name = 'smsbrana';
|
|
15
|
+
async send(msg) {
|
|
16
|
+
// console.debug('[sms:stub] →', msg.to, msg.body) — wired to the app logger in a real build (doc 01 §9)
|
|
17
|
+
return { ref: `stub-${Date.now()}`, costMinor: 0 };
|
|
18
|
+
}
|
|
19
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@deverjak/tenantkit-plugin-sms",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Reference SMS plugin for tenantkit, behind a swappable SmsProvider port (Twilio / SMSbrana). requiresTier: 'pro'. Touches the system only through the five plugin seams.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"zod": "^4.0.0",
|
|
21
|
+
"@deverjak/tenantkit-kernel": "0.2.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"typescript": "^5.9.3"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"build": "tsc -p tsconfig.build.json"
|
|
29
|
+
}
|
|
30
|
+
}
|