@12-apps/tenant 0.0.0-stage → 1.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/ADOPTING.md +195 -0
- package/README.md +71 -2
- package/package.json +77 -4
- package/prisma/migrations/20261126120000_tenant_closings_and_exports/migration.sql +56 -0
- package/prisma/tenant.prisma +88 -0
- package/scripts/sync-tenant-schema.mjs +73 -0
- package/src/config.ts +314 -0
- package/src/core.ts +65 -0
- package/src/hono/index.ts +17 -0
- package/src/index.ts +45 -0
- package/src/manifest/index.ts +19 -0
- package/src/port.ts +151 -0
- package/src/react/index.ts +28 -0
- package/src/server/index.ts +94 -0
- package/src/tree/index.ts +147 -0
- package/src/tree/path.ts +103 -0
- package/src/validate.ts +223 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The configuration every `@12-apps/tenant` factory takes, and every later
|
|
3
|
+
* slice extends.
|
|
4
|
+
*
|
|
5
|
+
* The package knows only "tenant". What a tenant IS (a shop, a hospital, a
|
|
6
|
+
* clinic), what its statuses are called, which ids the host's clients and
|
|
7
|
+
* audit trail already read, and every sentence a person sees are the host's,
|
|
8
|
+
* and arrive here. Nothing below has a default: a missing piece fails at
|
|
9
|
+
* startup (`validate.ts`), it never silently adopts another host's values.
|
|
10
|
+
*
|
|
11
|
+
* Every id and sentence the lifecycle, the purge and the export will need is
|
|
12
|
+
* required NOW, in 1.0: a required piece added later would break every host's
|
|
13
|
+
* config, so the set is complete from the first release.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The host's full status set for its tenant row.
|
|
18
|
+
*
|
|
19
|
+
* `all` is every value the host's row may hold, the closing three included.
|
|
20
|
+
* The package acts ONLY on `active`, `closing`, `closed` and the statuses in
|
|
21
|
+
* `closableFrom`, and never assumes there are no others: a tenant in any other
|
|
22
|
+
* status (a pending one, say) is left alone. A close records the status it
|
|
23
|
+
* started from (`TenantClosing.priorStatus`) so a reopen can restore it.
|
|
24
|
+
*/
|
|
25
|
+
export interface TenantStatusSet<S extends string = string> {
|
|
26
|
+
/** Every status the host's tenant row may hold. Non-empty, no duplicates. */
|
|
27
|
+
all: readonly S[];
|
|
28
|
+
/** The tenant is live. */
|
|
29
|
+
active: S;
|
|
30
|
+
/** A close is scheduled: read-only, the purge is pending, it can reopen. */
|
|
31
|
+
closing: S;
|
|
32
|
+
/** The purge ran. The row stays, for good. */
|
|
33
|
+
closed: S;
|
|
34
|
+
/**
|
|
35
|
+
* The statuses a close may start from: `active`, and any other the host
|
|
36
|
+
* allows (a platform may close a suspended tenant). Non-empty, each listed
|
|
37
|
+
* in `all`, never `closing` or `closed`.
|
|
38
|
+
*/
|
|
39
|
+
closableFrom: readonly S[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The refusal codes an API answers when the lifecycle refuses a request.
|
|
44
|
+
* Clients read these, so the host keeps whatever values its clients already
|
|
45
|
+
* understand. Two refusals may not share a code.
|
|
46
|
+
*/
|
|
47
|
+
export interface TenantRefusalCodes {
|
|
48
|
+
/** The tenant is closing: someone without the close permission is kept out. */
|
|
49
|
+
closing: string;
|
|
50
|
+
/** The tenant is closed: nothing is served any more. */
|
|
51
|
+
closed: string;
|
|
52
|
+
/** A write at a closing tenant by someone who may only read it. */
|
|
53
|
+
readOnly: string;
|
|
54
|
+
/** A public request (a checkout, say) reaching a tenant that closed. */
|
|
55
|
+
gone: string;
|
|
56
|
+
/** The close's reason is too short. */
|
|
57
|
+
reasonTooShort: string;
|
|
58
|
+
/** The name typed to confirm the close is not the tenant's. */
|
|
59
|
+
nameMismatch: string;
|
|
60
|
+
/** Closing a tenant that is already closing or closed. */
|
|
61
|
+
alreadyClosing: string;
|
|
62
|
+
/** Closing a tenant whose status is not in `closableFrom`. */
|
|
63
|
+
notActive: string;
|
|
64
|
+
/** Something still blocks the close (the host's blockers). */
|
|
65
|
+
closingBlocked: string;
|
|
66
|
+
/** Reopening after the purge: nothing is left to restore. */
|
|
67
|
+
purged: string;
|
|
68
|
+
/** Reopening a tenant that is not closing. */
|
|
69
|
+
notClosing: string;
|
|
70
|
+
/** A data export asked by an account with no person behind it: nobody to mail the link to. */
|
|
71
|
+
exportNeedsPerson: string;
|
|
72
|
+
/** The export's link names no export of this tenant, or one not built yet. */
|
|
73
|
+
exportNotFound: string;
|
|
74
|
+
/** The export's link is past its expiry. */
|
|
75
|
+
exportExpired: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The audit action ids the package records. Each is the host's own id. */
|
|
79
|
+
export interface TenantAuditActionIds {
|
|
80
|
+
/** A close was confirmed and scheduled. */
|
|
81
|
+
closeScheduled: string;
|
|
82
|
+
/** A pending close was undone. */
|
|
83
|
+
closeReopened: string;
|
|
84
|
+
/** The purge ran. */
|
|
85
|
+
closePurged: string;
|
|
86
|
+
/** The host's after-close hook failed; the sweep retries it. */
|
|
87
|
+
afterCloseFailed: string;
|
|
88
|
+
/** A data export was requested. */
|
|
89
|
+
exportRequested: string;
|
|
90
|
+
/** A data export was downloaded. */
|
|
91
|
+
exportDownloaded: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The notification type ids the package raises. Each is the host's own id. */
|
|
95
|
+
export interface TenantNotificationTypeIds {
|
|
96
|
+
/** A close was scheduled. */
|
|
97
|
+
closingScheduled: string;
|
|
98
|
+
/** The purge is a week away. */
|
|
99
|
+
closingWeek: string;
|
|
100
|
+
/** The purge is a day away. */
|
|
101
|
+
closingDay: string;
|
|
102
|
+
/** A pending close was undone. */
|
|
103
|
+
reopened: string;
|
|
104
|
+
/** A data export finished: ready to download, or failed (the payload says which). */
|
|
105
|
+
exportReady: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The permission ids that gate the package's acts. Each is the host's own id. */
|
|
109
|
+
export interface TenantPermissionIds {
|
|
110
|
+
/** Close and reopen the tenant, and take its data export. */
|
|
111
|
+
close: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Every id the package uses. The package never makes one up. */
|
|
115
|
+
export interface TenantIds {
|
|
116
|
+
refusals: TenantRefusalCodes;
|
|
117
|
+
audit: TenantAuditActionIds;
|
|
118
|
+
notifications: TenantNotificationTypeIds;
|
|
119
|
+
permissions: TenantPermissionIds;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Every sentence the package shows or sends, by key: what a copy pack holds.
|
|
124
|
+
*
|
|
125
|
+
* WHOLE SENTENCES, never a noun to slot in: the tenant's noun changes the rest
|
|
126
|
+
* of the sentence in gendered languages ("sua clínica será encerrada" / "seu
|
|
127
|
+
* hospital será encerrado"), so only the host can write it. A sentence that
|
|
128
|
+
* carries a value is a FUNCTION the host writes, so it also owns plurals
|
|
129
|
+
* (`count`) and word order. `name` is the tenant's name; a `day` is already
|
|
130
|
+
* formatted for the pack's locale, in the tenant's time zone.
|
|
131
|
+
*
|
|
132
|
+
* `text` keys are non-empty strings; `sentence` keys are functions.
|
|
133
|
+
*/
|
|
134
|
+
export const TENANT_COPY_SHAPE = {
|
|
135
|
+
// Notices, in the inbox and by e-mail (the title is the e-mail's subject).
|
|
136
|
+
noticeScheduledTitle: 'sentence',
|
|
137
|
+
noticeScheduledBody: 'sentence',
|
|
138
|
+
noticeWeekTitle: 'sentence',
|
|
139
|
+
noticeWeekBody: 'sentence',
|
|
140
|
+
noticeDayTitle: 'sentence',
|
|
141
|
+
noticeDayBody: 'sentence',
|
|
142
|
+
noticeReopenedTitle: 'sentence',
|
|
143
|
+
noticeReopenedBody: 'sentence',
|
|
144
|
+
noticeExportReadyTitle: 'sentence',
|
|
145
|
+
noticeExportReadyBody: 'sentence',
|
|
146
|
+
noticeExportFailedTitle: 'sentence',
|
|
147
|
+
noticeExportFailedBody: 'sentence',
|
|
148
|
+
// Refusals, as a person reads them: one per refusal code.
|
|
149
|
+
refusalClosing: 'text',
|
|
150
|
+
refusalClosed: 'text',
|
|
151
|
+
refusalReadOnly: 'text',
|
|
152
|
+
refusalGone: 'text',
|
|
153
|
+
refusalReasonTooShort: 'text',
|
|
154
|
+
refusalNameMismatch: 'text',
|
|
155
|
+
refusalAlreadyClosing: 'text',
|
|
156
|
+
refusalNotActive: 'text',
|
|
157
|
+
refusalBlocked: 'sentence',
|
|
158
|
+
refusalPurged: 'text',
|
|
159
|
+
refusalNotClosing: 'text',
|
|
160
|
+
refusalExportNeedsPerson: 'text',
|
|
161
|
+
refusalExportNotFound: 'text',
|
|
162
|
+
refusalExportExpired: 'text',
|
|
163
|
+
// The close page: its three steps (blockers, data, close) and the popup.
|
|
164
|
+
pendingTitle: 'text',
|
|
165
|
+
pendingNone: 'text',
|
|
166
|
+
blocker: 'sentence',
|
|
167
|
+
shortcut: 'sentence',
|
|
168
|
+
dataTitle: 'text',
|
|
169
|
+
dataTitlePlain: 'text',
|
|
170
|
+
dataNote: 'text',
|
|
171
|
+
dataAction: 'text',
|
|
172
|
+
dataPreparing: 'text',
|
|
173
|
+
dataOffline: 'text',
|
|
174
|
+
closeTitle: 'text',
|
|
175
|
+
closeExplain: 'text',
|
|
176
|
+
closeAction: 'text',
|
|
177
|
+
missing: 'sentence',
|
|
178
|
+
dialogTitle: 'text',
|
|
179
|
+
dialogBody: 'sentence',
|
|
180
|
+
dialogUndo: 'text',
|
|
181
|
+
nameLabel: 'text',
|
|
182
|
+
nameHint: 'sentence',
|
|
183
|
+
nameMismatch: 'text',
|
|
184
|
+
reasonLabel: 'text',
|
|
185
|
+
reasonPlaceholder: 'text',
|
|
186
|
+
cancel: 'text',
|
|
187
|
+
actFailed: 'text',
|
|
188
|
+
loadFailed: 'text',
|
|
189
|
+
retry: 'text',
|
|
190
|
+
// The close page while closing, and once closed.
|
|
191
|
+
closingTitle: 'text',
|
|
192
|
+
closingBody: 'sentence',
|
|
193
|
+
reopen: 'text',
|
|
194
|
+
reopened: 'text',
|
|
195
|
+
reopenNote: 'text',
|
|
196
|
+
closedTitle: 'text',
|
|
197
|
+
closedBody: 'text',
|
|
198
|
+
// The banner every screen shows to whoever holds the close permission.
|
|
199
|
+
bannerTitle: 'text',
|
|
200
|
+
bannerBody: 'sentence',
|
|
201
|
+
bannerReadOnly: 'text',
|
|
202
|
+
// The refusal screen of a member a closing or closed tenant keeps out.
|
|
203
|
+
refusedClosingTitle: 'text',
|
|
204
|
+
refusedClosingBody: 'text',
|
|
205
|
+
refusedClosedTitle: 'text',
|
|
206
|
+
refusedClosedBody: 'text',
|
|
207
|
+
switchTenant: 'text',
|
|
208
|
+
signOut: 'text',
|
|
209
|
+
seeClosing: 'text',
|
|
210
|
+
// The platform's view of a closing tenant.
|
|
211
|
+
platformCancelClosing: 'text',
|
|
212
|
+
platformClosingNote: 'sentence',
|
|
213
|
+
} as const satisfies Record<string, 'text' | 'sentence'>;
|
|
214
|
+
|
|
215
|
+
export type TenantCopyKey = keyof typeof TENANT_COPY_SHAPE;
|
|
216
|
+
|
|
217
|
+
/** One locale's full copy pack: every key, typed. */
|
|
218
|
+
export interface TenantCopyPack {
|
|
219
|
+
readonly noticeScheduledTitle: (name: string) => string;
|
|
220
|
+
readonly noticeScheduledBody: (name: string, purgeDay: string) => string;
|
|
221
|
+
readonly noticeWeekTitle: (name: string) => string;
|
|
222
|
+
readonly noticeWeekBody: (name: string, purgeDay: string) => string;
|
|
223
|
+
readonly noticeDayTitle: (name: string) => string;
|
|
224
|
+
readonly noticeDayBody: (name: string) => string;
|
|
225
|
+
readonly noticeReopenedTitle: (name: string) => string;
|
|
226
|
+
readonly noticeReopenedBody: (name: string) => string;
|
|
227
|
+
readonly noticeExportReadyTitle: (name: string) => string;
|
|
228
|
+
readonly noticeExportReadyBody: (name: string, expiresDay: string) => string;
|
|
229
|
+
readonly noticeExportFailedTitle: (name: string) => string;
|
|
230
|
+
readonly noticeExportFailedBody: (name: string) => string;
|
|
231
|
+
readonly refusalClosing: string;
|
|
232
|
+
readonly refusalClosed: string;
|
|
233
|
+
readonly refusalReadOnly: string;
|
|
234
|
+
readonly refusalGone: string;
|
|
235
|
+
readonly refusalReasonTooShort: string;
|
|
236
|
+
readonly refusalNameMismatch: string;
|
|
237
|
+
readonly refusalAlreadyClosing: string;
|
|
238
|
+
readonly refusalNotActive: string;
|
|
239
|
+
/** `count` items still block the close. */
|
|
240
|
+
readonly refusalBlocked: (count: number) => string;
|
|
241
|
+
readonly refusalPurged: string;
|
|
242
|
+
readonly refusalNotClosing: string;
|
|
243
|
+
readonly refusalExportNeedsPerson: string;
|
|
244
|
+
readonly refusalExportNotFound: string;
|
|
245
|
+
readonly refusalExportExpired: string;
|
|
246
|
+
readonly pendingTitle: string;
|
|
247
|
+
readonly pendingNone: string;
|
|
248
|
+
/** One line per kind of blocker; `kind` is one of the host's own blocker kinds. */
|
|
249
|
+
readonly blocker: (kind: string, count: number) => string;
|
|
250
|
+
/** The shortcut that resolves a blocker of that kind. */
|
|
251
|
+
readonly shortcut: (kind: string) => string;
|
|
252
|
+
readonly dataTitle: string;
|
|
253
|
+
readonly dataTitlePlain: string;
|
|
254
|
+
readonly dataNote: string;
|
|
255
|
+
readonly dataAction: string;
|
|
256
|
+
readonly dataPreparing: string;
|
|
257
|
+
readonly dataOffline: string;
|
|
258
|
+
readonly closeTitle: string;
|
|
259
|
+
readonly closeExplain: string;
|
|
260
|
+
readonly closeAction: string;
|
|
261
|
+
/** `count` blockers are left. */
|
|
262
|
+
readonly missing: (count: number) => string;
|
|
263
|
+
readonly dialogTitle: string;
|
|
264
|
+
readonly dialogBody: (name: string) => string;
|
|
265
|
+
readonly dialogUndo: string;
|
|
266
|
+
readonly nameLabel: string;
|
|
267
|
+
readonly nameHint: (name: string) => string;
|
|
268
|
+
readonly nameMismatch: string;
|
|
269
|
+
readonly reasonLabel: string;
|
|
270
|
+
readonly reasonPlaceholder: string;
|
|
271
|
+
readonly cancel: string;
|
|
272
|
+
readonly actFailed: string;
|
|
273
|
+
readonly loadFailed: string;
|
|
274
|
+
readonly retry: string;
|
|
275
|
+
readonly closingTitle: string;
|
|
276
|
+
readonly closingBody: (sinceDay: string, purgeDay: string) => string;
|
|
277
|
+
readonly reopen: string;
|
|
278
|
+
readonly reopened: string;
|
|
279
|
+
readonly reopenNote: string;
|
|
280
|
+
readonly closedTitle: string;
|
|
281
|
+
readonly closedBody: string;
|
|
282
|
+
readonly bannerTitle: string;
|
|
283
|
+
readonly bannerBody: (sinceDay: string, purgeDay: string) => string;
|
|
284
|
+
readonly bannerReadOnly: string;
|
|
285
|
+
readonly refusedClosingTitle: string;
|
|
286
|
+
readonly refusedClosingBody: string;
|
|
287
|
+
readonly refusedClosedTitle: string;
|
|
288
|
+
readonly refusedClosedBody: string;
|
|
289
|
+
readonly switchTenant: string;
|
|
290
|
+
readonly signOut: string;
|
|
291
|
+
readonly seeClosing: string;
|
|
292
|
+
readonly platformCancelClosing: string;
|
|
293
|
+
readonly platformClosingNote: (closedDay: string, purgeDay: string) => string;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** The keys of a copy pack, in a fixed order. */
|
|
297
|
+
export const TENANT_COPY_KEYS = Object.keys(TENANT_COPY_SHAPE) as readonly TenantCopyKey[];
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The copy packs by locale tag (`pt-BR`, `en-US`). No pack ships with the
|
|
301
|
+
* package. `defaultLocale` names the pack a request with no matching locale
|
|
302
|
+
* reads, and must itself be one of the packs.
|
|
303
|
+
*/
|
|
304
|
+
export interface TenantCopyConfig {
|
|
305
|
+
locales: Readonly<Record<string, TenantCopyPack>>;
|
|
306
|
+
defaultLocale: string;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** What both halves (`createApiTenant`, `createWebTenant`) are configured with. */
|
|
310
|
+
export interface TenantCoreConfig<S extends string = string> {
|
|
311
|
+
statuses: TenantStatusSet<S>;
|
|
312
|
+
ids: TenantIds;
|
|
313
|
+
copy: TenantCopyConfig;
|
|
314
|
+
}
|
package/src/core.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pure readings both halves answer from a validated config, and the
|
|
3
|
+
* deep freeze both apply to it.
|
|
4
|
+
*/
|
|
5
|
+
import type { TenantCopyPack, TenantCoreConfig, TenantRefusalCodes } from './config';
|
|
6
|
+
|
|
7
|
+
/** Which refusal a code is, by the host's own codes. */
|
|
8
|
+
export type TenantRefusalKind = keyof TenantRefusalCodes;
|
|
9
|
+
|
|
10
|
+
/** `T` with every nested object and array read-only; functions are left as they are. */
|
|
11
|
+
export type DeepReadonly<T> = T extends (...args: never[]) => unknown
|
|
12
|
+
? T
|
|
13
|
+
: T extends readonly (infer E)[]
|
|
14
|
+
? readonly DeepReadonly<E>[]
|
|
15
|
+
: T extends object
|
|
16
|
+
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
|
|
17
|
+
: T;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A frozen COPY of the plain data in `value`: objects and arrays are copied
|
|
21
|
+
* and frozen at every depth, functions are kept as they are. The host's own
|
|
22
|
+
* objects are never frozen in place, and a change it makes to them after
|
|
23
|
+
* startup does not reach the validated config.
|
|
24
|
+
*/
|
|
25
|
+
export function frozenCopy<T>(value: T): DeepReadonly<T> {
|
|
26
|
+
if (Array.isArray(value)) return Object.freeze(value.map((item: unknown) => frozenCopy(item))) as DeepReadonly<T>;
|
|
27
|
+
if (typeof value !== 'object' || value === null) return value as DeepReadonly<T>;
|
|
28
|
+
const copy = Object.fromEntries(
|
|
29
|
+
Object.entries(value).map(([key, child]: [string, unknown]) => [key, frozenCopy(child)]),
|
|
30
|
+
);
|
|
31
|
+
return Object.freeze(copy) as DeepReadonly<T>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** What both factories hand back on top of their own half. */
|
|
35
|
+
export interface TenantReadings<S extends string> {
|
|
36
|
+
/** True only for the statuses the package acts on: active, closing, closed and `closableFrom`. */
|
|
37
|
+
isLifecycleStatus(status: string): status is S;
|
|
38
|
+
/** True when a close may start from `status` (`statuses.closableFrom`). */
|
|
39
|
+
isClosableFrom(status: string): status is S;
|
|
40
|
+
/** The copy pack for a locale tag, or the default locale's when none matches. */
|
|
41
|
+
copyFor(locale: string | null | undefined): TenantCopyPack;
|
|
42
|
+
/** Which refusal a code from the API is, or null when it is none of the package's. */
|
|
43
|
+
refusalKind(code: string): TenantRefusalKind | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createReadings<S extends string>(
|
|
47
|
+
config: DeepReadonly<TenantCoreConfig<S>>,
|
|
48
|
+
): TenantReadings<S> {
|
|
49
|
+
const { statuses } = config;
|
|
50
|
+
const closable = new Set<string>(statuses.closableFrom);
|
|
51
|
+
const lifecycle = new Set<string>([statuses.active, statuses.closing, statuses.closed, ...closable]);
|
|
52
|
+
const byCode = new Map<string, TenantRefusalKind>(
|
|
53
|
+
(Object.entries(config.ids.refusals) as [TenantRefusalKind, string][]).map(([kind, code]) => [code, kind]),
|
|
54
|
+
);
|
|
55
|
+
const { locales, defaultLocale } = config.copy;
|
|
56
|
+
return {
|
|
57
|
+
isLifecycleStatus: (status: string): status is S => lifecycle.has(status),
|
|
58
|
+
isClosableFrom: (status: string): status is S => closable.has(status),
|
|
59
|
+
copyFor: (locale) =>
|
|
60
|
+
(locale !== null && locale !== undefined && Object.hasOwn(locales, locale)
|
|
61
|
+
? locales[locale]
|
|
62
|
+
: locales[defaultLocale]) as TenantCopyPack,
|
|
63
|
+
refusalKind: (code) => byCode.get(code) ?? null,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@12-apps/tenant/hono` — mounts `createApiTenant`'s descriptors on a Hono app.
|
|
3
|
+
*
|
|
4
|
+
* Hono is an optional peer and is only TYPE-imported here: the adapter calls
|
|
5
|
+
* `app.on(method, path, handler)` on whatever app the host passes.
|
|
6
|
+
*/
|
|
7
|
+
import type { Hono } from 'hono';
|
|
8
|
+
|
|
9
|
+
import type { ApiTenant } from '../server';
|
|
10
|
+
|
|
11
|
+
/** Mount every route of `tenant` on `app`, in order. Returns `app`. */
|
|
12
|
+
export function mountTenant<A extends Pick<Hono, 'on'>>(app: A, tenant: Pick<ApiTenant, 'routes'>): A {
|
|
13
|
+
for (const route of tenant.routes) {
|
|
14
|
+
app.on(route.method, route.path, (context) => route.handler(context.req.raw));
|
|
15
|
+
}
|
|
16
|
+
return app;
|
|
17
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@12-apps/tenant` — the shared, runtime-free half: the config types every
|
|
3
|
+
* factory takes, the tenant port's shape, and the startup validation.
|
|
4
|
+
*
|
|
5
|
+
* Backend: `@12-apps/tenant/server` (`createApiTenant`). Hono mount:
|
|
6
|
+
* `@12-apps/tenant/hono`. Frontend: `@12-apps/tenant/react`
|
|
7
|
+
* (`createWebTenant`). Adoption contract: ADOPTING.md.
|
|
8
|
+
*/
|
|
9
|
+
export {
|
|
10
|
+
TENANT_COPY_KEYS,
|
|
11
|
+
TENANT_COPY_SHAPE,
|
|
12
|
+
type TenantAuditActionIds,
|
|
13
|
+
type TenantCopyConfig,
|
|
14
|
+
type TenantCopyKey,
|
|
15
|
+
type TenantCopyPack,
|
|
16
|
+
type TenantCoreConfig,
|
|
17
|
+
type TenantIds,
|
|
18
|
+
type TenantNotificationTypeIds,
|
|
19
|
+
type TenantPermissionIds,
|
|
20
|
+
type TenantRefusalCodes,
|
|
21
|
+
type TenantStatusSet,
|
|
22
|
+
} from './config';
|
|
23
|
+
export {
|
|
24
|
+
TENANT_PORT_METHODS,
|
|
25
|
+
TENANT_TREE_PORT_METHODS,
|
|
26
|
+
type TenantPort,
|
|
27
|
+
type TenantRecord,
|
|
28
|
+
type TenantTreeMove,
|
|
29
|
+
type TenantTreeNode,
|
|
30
|
+
} from './port';
|
|
31
|
+
export {
|
|
32
|
+
TenantTreeError,
|
|
33
|
+
childPath,
|
|
34
|
+
createTenantTree,
|
|
35
|
+
depthOf,
|
|
36
|
+
isWithin,
|
|
37
|
+
parsePath,
|
|
38
|
+
rootPath,
|
|
39
|
+
validateNode,
|
|
40
|
+
type TenantTree,
|
|
41
|
+
type TenantTreeErrorCode,
|
|
42
|
+
type TenantTreePort,
|
|
43
|
+
} from './tree';
|
|
44
|
+
export { TenantConfigError, validateCoreConfig, validatePort, validateTreePort } from './validate';
|
|
45
|
+
export type { DeepReadonly, TenantReadings, TenantRefusalKind } from './core';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@12-apps/tenant/manifest` — the SHARED wiring manifest.
|
|
3
|
+
*
|
|
4
|
+
* - `db` — the partial names no host table (the tenant id is an opaque
|
|
5
|
+
* string, no foreign key), so it composes into any host's schema folder.
|
|
6
|
+
*
|
|
7
|
+
* Every other capability is declined for now, each with its argument in
|
|
8
|
+
* `.wiring-conformance.json`: the routes, jobs, mail, notifications,
|
|
9
|
+
* permissions and screens arrive with the slices that add them.
|
|
10
|
+
*
|
|
11
|
+
* `@12-apps/wiring` is a TYPE-ONLY dependency here.
|
|
12
|
+
*/
|
|
13
|
+
import type { PackageManifest } from '@12-apps/wiring';
|
|
14
|
+
|
|
15
|
+
export const tenantManifest = {
|
|
16
|
+
name: '@12-apps/tenant',
|
|
17
|
+
contract: 1,
|
|
18
|
+
db: { partial: 'prisma/tenant.prisma', migrations: 'prisma/migrations' },
|
|
19
|
+
} as const satisfies PackageManifest;
|
package/src/port.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tenant port: the ONLY way the package reaches the host's tenant row.
|
|
3
|
+
*
|
|
4
|
+
* The tenant row, its slugs and its domains stay the host's. The package never
|
|
5
|
+
* maps them (Prisma refuses two models on one table, and a shared partial has
|
|
6
|
+
* no foreign key into a host table), so it asks the host through these five
|
|
7
|
+
* functions instead. Every one is required. (The tenant tree has a port of its
|
|
8
|
+
* own, `TenantTreePort`, optional as a whole.)
|
|
9
|
+
*
|
|
10
|
+
* TRANSACTIONS. Every lifecycle step changes the tenant row and the package's
|
|
11
|
+
* own rows together: a close flips the status, recounts the blockers (one
|
|
12
|
+
* found rolls the flip back), replaces the closing row and writes the audit
|
|
13
|
+
* line, all in ONE transaction the package opens through the host. So every
|
|
14
|
+
* write takes the caller's transaction `tx` and must run on it, never on a
|
|
15
|
+
* client of its own; `byId` takes it too, so a refusal reads the status the
|
|
16
|
+
* transaction sees.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** What the package needs to know about one tenant. */
|
|
20
|
+
export interface TenantRecord<S extends string = string> {
|
|
21
|
+
/** The host's tenant id, opaque to the package. */
|
|
22
|
+
id: string;
|
|
23
|
+
/** The tenant's CURRENT slug (never a former one the host keeps). */
|
|
24
|
+
slug: string;
|
|
25
|
+
/** The tenant row's current status: any value of the host's status set. */
|
|
26
|
+
status: S;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* One tenant's place in the tenant tree, as the host stores it.
|
|
31
|
+
*
|
|
32
|
+
* The tree is ownership only (a company above its units, a unit above its
|
|
33
|
+
* venues), of any depth. `path` is the materialized path the package computes
|
|
34
|
+
* (`/<root id>/…/<own id>/`), so no tree read recurses.
|
|
35
|
+
*/
|
|
36
|
+
export interface TenantTreeNode {
|
|
37
|
+
/** The host's tenant id. */
|
|
38
|
+
id: string;
|
|
39
|
+
/** The tenant directly above, or null for a standalone tenant (a root). */
|
|
40
|
+
parentId: string | null;
|
|
41
|
+
/** `/<root id>/…/<own id>/`; a root's is `/<own id>/`. */
|
|
42
|
+
path: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A subtree move, planned and validated by the package. The host first locks
|
|
47
|
+
* the subtree and the new parent (ADOPTING.md, "The tenant tree (opt-in)"), then
|
|
48
|
+
* rewrites in ONE statement: every path starting with `fromPath` has that
|
|
49
|
+
* prefix replaced by `toPath`, and the row `id` is re-pointed at `parentId`.
|
|
50
|
+
*/
|
|
51
|
+
export interface TenantTreeMove {
|
|
52
|
+
/** The tenant that moves, everything below it moving with it. */
|
|
53
|
+
id: string;
|
|
54
|
+
/** Its new parent, or null to make it a root. */
|
|
55
|
+
parentId: string | null;
|
|
56
|
+
/**
|
|
57
|
+
* The new parent's path as the package read it (null with no parent). The
|
|
58
|
+
* host applies the move only while the parent's path still is this.
|
|
59
|
+
*/
|
|
60
|
+
parentPath: string | null;
|
|
61
|
+
/** The moving tenant's path before the move: the prefix replaced. */
|
|
62
|
+
fromPath: string;
|
|
63
|
+
/** Its path after the move: the replacement prefix. */
|
|
64
|
+
toPath: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* `Tx` is the host's transaction client. The package never opens one itself:
|
|
69
|
+
* it hands back whatever the host gave it, so the host's own writes and the
|
|
70
|
+
* package's land in the same transaction.
|
|
71
|
+
*/
|
|
72
|
+
export interface TenantPort<S extends string = string, Tx = unknown> {
|
|
73
|
+
/**
|
|
74
|
+
* The tenant whose CURRENT slug is `slug`, or null when none is. A former
|
|
75
|
+
* slug the host still keeps for redirects does not resolve here: kept slugs
|
|
76
|
+
* are the address slice's, through a port of their own.
|
|
77
|
+
*/
|
|
78
|
+
bySlug(slug: string): Promise<TenantRecord<S> | null>;
|
|
79
|
+
/**
|
|
80
|
+
* The tenant with this id, or null when none exists. Read on `tx` when one
|
|
81
|
+
* is given, so a step inside a transaction sees that transaction's row.
|
|
82
|
+
*/
|
|
83
|
+
byId(id: string, tx?: Tx): Promise<TenantRecord<S> | null>;
|
|
84
|
+
/**
|
|
85
|
+
* Compare-and-set the tenant's status, ON `tx`: move it to `to` ONLY while
|
|
86
|
+
* it is still `from` (`UPDATE … SET status = to WHERE id = id AND status =
|
|
87
|
+
* from`). Resolves true when the row moved, false when it was not in `from`
|
|
88
|
+
* (another request got there first). The package relies on that answer to
|
|
89
|
+
* make every lifecycle step happen once, and on `tx` to roll the move back
|
|
90
|
+
* when a later step of the same transaction refuses.
|
|
91
|
+
*/
|
|
92
|
+
setStatusIf(id: string, from: S, to: S, tx: Tx): Promise<boolean>;
|
|
93
|
+
/**
|
|
94
|
+
* Erase the tenant row's own profile (name, contact, address, whatever the
|
|
95
|
+
* host keeps on it) ON `tx`, inside the purge's transaction. The row itself
|
|
96
|
+
* stays.
|
|
97
|
+
*/
|
|
98
|
+
clearProfile(tx: Tx, id: string): Promise<void>;
|
|
99
|
+
/** The tenant's IANA time zone (`America/Sao_Paulo`): reminders and purge dates are computed in it. */
|
|
100
|
+
timeZone(id: string): Promise<string>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The port's members, in a fixed order: what startup validation checks. */
|
|
104
|
+
export const TENANT_PORT_METHODS = [
|
|
105
|
+
'bySlug',
|
|
106
|
+
'byId',
|
|
107
|
+
'setStatusIf',
|
|
108
|
+
'clearProfile',
|
|
109
|
+
'timeZone',
|
|
110
|
+
] as const satisfies readonly (keyof TenantPort)[];
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The tenant TREE's port: OPTIONAL as a whole, and every member required when
|
|
114
|
+
* it is given (`createApiTenant`'s `tree`). A host with no tree passes none and
|
|
115
|
+
* gets no `tenant.tree`; a host written against a release before the tree
|
|
116
|
+
* boots unchanged. Same transaction rule as the tenant port: the one write
|
|
117
|
+
* takes the caller's `tx`, required; the reads take it optionally.
|
|
118
|
+
*/
|
|
119
|
+
export interface TenantTreePort<Tx = unknown> {
|
|
120
|
+
/**
|
|
121
|
+
* One tenant's node, ONE read. Null when none exists. Read inside
|
|
122
|
+
* `tx` when one is given, so a move's plan sees what its write will.
|
|
123
|
+
*/
|
|
124
|
+
treeNode(id: string, tx?: Tx): Promise<TenantTreeNode | null>;
|
|
125
|
+
/**
|
|
126
|
+
* The tenant and every tenant above it, in ONE read (the ids are
|
|
127
|
+
* the segments of its own path). Any order; empty when the tenant is unknown.
|
|
128
|
+
*/
|
|
129
|
+
treeAncestors(id: string, tx?: Tx): Promise<TenantTreeNode[]>;
|
|
130
|
+
/**
|
|
131
|
+
* The tenant and every tenant below it at any depth, in ONE
|
|
132
|
+
* indexed prefix read on the path. Any order; empty when the tenant is unknown.
|
|
133
|
+
*/
|
|
134
|
+
treeDescendants(id: string, tx?: Tx): Promise<TenantTreeNode[]>;
|
|
135
|
+
/**
|
|
136
|
+
* Apply a planned move inside the caller's transaction (required: a write
|
|
137
|
+
* never runs outside it): lock the subtree and the new parent, then ONE
|
|
138
|
+
* rewrite statement (ADOPTING.md, "The tenant tree (opt-in)"). Answers the rows
|
|
139
|
+
* written. 0 means the tree changed since the plan (the subtree is no longer
|
|
140
|
+
* at `fromPath`, or the parent no longer at `parentPath`): nothing written.
|
|
141
|
+
*/
|
|
142
|
+
moveSubtree(move: TenantTreeMove, tx: Tx): Promise<number>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The tree port's members, in a fixed order: what startup validation checks when a tree is given. */
|
|
146
|
+
export const TENANT_TREE_PORT_METHODS = [
|
|
147
|
+
'treeNode',
|
|
148
|
+
'treeAncestors',
|
|
149
|
+
'treeDescendants',
|
|
150
|
+
'moveSubtree',
|
|
151
|
+
] as const satisfies readonly (keyof TenantTreePort)[];
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@12-apps/tenant/react` — the frontend factory.
|
|
3
|
+
*
|
|
4
|
+
* `createWebTenant` takes the half of the config a browser needs (the status
|
|
5
|
+
* set, the ids, the copy packs) and validates it at startup exactly as the
|
|
6
|
+
* backend does. The tenant port is the server's and never reaches a browser.
|
|
7
|
+
* This slice ships the readings a screen needs (which pack to show, which
|
|
8
|
+
* refusal an answer is); the screens arrive with the lifecycle slice.
|
|
9
|
+
*/
|
|
10
|
+
import type { TenantCoreConfig } from '../config';
|
|
11
|
+
import { createReadings, frozenCopy, type DeepReadonly, type TenantReadings } from '../core';
|
|
12
|
+
import { validateCoreConfig } from '../validate';
|
|
13
|
+
|
|
14
|
+
export { TenantConfigError } from '../validate';
|
|
15
|
+
|
|
16
|
+
/** Everything `createWebTenant` needs. Nothing is optional. */
|
|
17
|
+
export type TenantWebConfig<S extends string = string> = TenantCoreConfig<S>;
|
|
18
|
+
|
|
19
|
+
export interface WebTenant<S extends string = string> extends TenantReadings<S> {
|
|
20
|
+
/** The validated config, a deep-frozen copy. */
|
|
21
|
+
config: DeepReadonly<TenantWebConfig<S>>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createWebTenant<S extends string>(config: TenantWebConfig<S>): WebTenant<S> {
|
|
25
|
+
validateCoreConfig(config);
|
|
26
|
+
const data = frozenCopy({ statuses: config.statuses, ids: config.ids, copy: config.copy });
|
|
27
|
+
return { ...createReadings(data), config: data };
|
|
28
|
+
}
|