@openbrf/plugin-sdk 1.0.0 → 1.4.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.
Files changed (104) hide show
  1. package/README.md +95 -0
  2. package/dist/abort-signal.d.ts +23 -0
  3. package/dist/abort-signal.d.ts.map +1 -0
  4. package/dist/abort-signal.js +2 -0
  5. package/dist/abort-signal.js.map +1 -0
  6. package/dist/action-refusal.d.ts +101 -0
  7. package/dist/action-refusal.d.ts.map +1 -0
  8. package/dist/action-refusal.js +96 -0
  9. package/dist/action-refusal.js.map +1 -0
  10. package/dist/action-schema.d.ts.map +1 -1
  11. package/dist/action-schema.js +4 -1
  12. package/dist/action-schema.js.map +1 -1
  13. package/dist/actions.d.ts +94 -6
  14. package/dist/actions.d.ts.map +1 -1
  15. package/dist/actions.js.map +1 -1
  16. package/dist/blocks.d.ts +310 -0
  17. package/dist/blocks.d.ts.map +1 -0
  18. package/dist/blocks.js +185 -0
  19. package/dist/blocks.js.map +1 -0
  20. package/dist/catalog.d.ts +1092 -41
  21. package/dist/catalog.d.ts.map +1 -1
  22. package/dist/catalog.js +472 -15
  23. package/dist/catalog.js.map +1 -1
  24. package/dist/entitlements.d.ts +117 -0
  25. package/dist/entitlements.d.ts.map +1 -0
  26. package/dist/entitlements.js +62 -0
  27. package/dist/entitlements.js.map +1 -0
  28. package/dist/extensions.d.ts +183 -0
  29. package/dist/extensions.d.ts.map +1 -0
  30. package/dist/extensions.js +67 -0
  31. package/dist/extensions.js.map +1 -0
  32. package/dist/finances.d.ts +294 -0
  33. package/dist/finances.d.ts.map +1 -0
  34. package/dist/finances.js +56 -0
  35. package/dist/finances.js.map +1 -0
  36. package/dist/findings.d.ts +92 -1
  37. package/dist/findings.d.ts.map +1 -1
  38. package/dist/findings.js +91 -0
  39. package/dist/findings.js.map +1 -1
  40. package/dist/host.d.ts +729 -4
  41. package/dist/host.d.ts.map +1 -1
  42. package/dist/host.js +54 -0
  43. package/dist/host.js.map +1 -1
  44. package/dist/imports.d.ts +104 -0
  45. package/dist/imports.d.ts.map +1 -0
  46. package/dist/imports.js +70 -0
  47. package/dist/imports.js.map +1 -0
  48. package/dist/index.d.ts +17 -7
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +15 -6
  51. package/dist/index.js.map +1 -1
  52. package/dist/localized-text.d.ts +18 -0
  53. package/dist/localized-text.d.ts.map +1 -0
  54. package/dist/localized-text.js +17 -0
  55. package/dist/localized-text.js.map +1 -0
  56. package/dist/manifest.d.ts +859 -0
  57. package/dist/manifest.d.ts.map +1 -1
  58. package/dist/manifest.js +957 -12
  59. package/dist/manifest.js.map +1 -1
  60. package/dist/package-check.d.ts.map +1 -1
  61. package/dist/package-check.js +9 -1
  62. package/dist/package-check.js.map +1 -1
  63. package/dist/permissions.d.ts +170 -11
  64. package/dist/permissions.d.ts.map +1 -1
  65. package/dist/permissions.js +210 -2
  66. package/dist/permissions.js.map +1 -1
  67. package/dist/recipients.d.ts +139 -0
  68. package/dist/recipients.d.ts.map +1 -0
  69. package/dist/recipients.js +223 -0
  70. package/dist/recipients.js.map +1 -0
  71. package/dist/records.d.ts +130 -0
  72. package/dist/records.d.ts.map +1 -0
  73. package/dist/records.js +85 -0
  74. package/dist/records.js.map +1 -0
  75. package/dist/require-capability.d.ts +37 -0
  76. package/dist/require-capability.d.ts.map +1 -0
  77. package/dist/require-capability.js +88 -0
  78. package/dist/require-capability.js.map +1 -0
  79. package/dist/settings-schema.d.ts +35 -0
  80. package/dist/settings-schema.d.ts.map +1 -1
  81. package/dist/settings-schema.js +55 -2
  82. package/dist/settings-schema.js.map +1 -1
  83. package/package.json +4 -4
  84. package/src/abort-signal.ts +22 -0
  85. package/src/action-refusal.ts +178 -0
  86. package/src/action-schema.ts +4 -1
  87. package/src/actions.ts +103 -6
  88. package/src/blocks.ts +289 -0
  89. package/src/catalog.ts +630 -72
  90. package/src/entitlements.ts +150 -0
  91. package/src/extensions.ts +212 -0
  92. package/src/finances.ts +317 -0
  93. package/src/findings.ts +94 -0
  94. package/src/host.ts +768 -4
  95. package/src/imports.ts +135 -0
  96. package/src/index.ts +209 -0
  97. package/src/localized-text.ts +19 -0
  98. package/src/manifest.ts +1231 -12
  99. package/src/package-check.ts +15 -0
  100. package/src/permissions.ts +223 -3
  101. package/src/recipients.ts +294 -0
  102. package/src/records.ts +172 -0
  103. package/src/require-capability.ts +106 -0
  104. package/src/settings-schema.ts +64 -2
@@ -0,0 +1,150 @@
1
+ /**
2
+ * What a paid plugin reads about its licence (ADR 0042).
3
+ *
4
+ * The host holds the licences the board entered and verifies them offline,
5
+ * against the keys the plugin's manifest declares and the instance's own id
6
+ * and key, on the association's calendar day. A plugin gets the finding and
7
+ * never the file, and decides for itself what it withholds without an active
8
+ * entitlement: the host loads and serves it either way.
9
+ */
10
+
11
+ /** The version of the licence file format this contract reads. */
12
+ export const LICENCE_FORMAT = "openbrf-licence/1";
13
+
14
+ /** The version of the presentation format `present()` writes. */
15
+ export const LICENCE_PRESENTATION_FORMAT = "openbrf-licence-presentation/1";
16
+
17
+ /** The largest licence file the host accepts, in bytes. */
18
+ export const LICENCE_FILE_MAX_BYTES = 16_384;
19
+
20
+ /** The largest presentation a service has to read, in bytes. */
21
+ export const LICENCE_PRESENTATION_MAX_BYTES = 24_576;
22
+
23
+ /**
24
+ * The finding for the entitlement the manifest names, judged today.
25
+ *
26
+ * Dates are `YYYY-MM-DD` on the association's calendar day, and inclusive: a
27
+ * licence valid until a day is active through the whole of it.
28
+ *
29
+ * - `active`: a licence one of the plugin's keys verified names the
30
+ * entitlement, is bound to this instance, and covers today and this release.
31
+ * - `absent`: no licence the plugin's keys verified names the entitlement.
32
+ * - `expired`: today is after its `validUntil`.
33
+ * - `version-not-covered`: this release was published after its
34
+ * `updatesUntil`. An earlier release stays covered.
35
+ * - `wrong-instance`: it was issued to another instance, by id or by key.
36
+ * - `tampered`: it verified when it was entered and no longer does.
37
+ * - `not-yet-valid`: it was issued after today, which is a wrong clock on
38
+ * one side or the other.
39
+ *
40
+ * Without an active licence the finding is that of the licence entered most
41
+ * recently, so a board that has just entered the wrong file is told about
42
+ * that file.
43
+ */
44
+ export type EntitlementState =
45
+ | {
46
+ status: "active";
47
+ licenceId: string;
48
+ validUntil: string | null;
49
+ updatesUntil: string | null;
50
+ }
51
+ | { status: "absent" }
52
+ | { status: "expired"; licenceId: string; validUntil: string }
53
+ | {
54
+ status: "version-not-covered";
55
+ licenceId: string;
56
+ updatesUntil: string;
57
+ released: string;
58
+ }
59
+ | { status: "wrong-instance"; licenceId: string }
60
+ | { status: "tampered" }
61
+ | { status: "not-yet-valid"; licenceId: string; issuedOn: string };
62
+
63
+ export type EntitlementStatus = EntitlementState["status"];
64
+
65
+ export const ENTITLEMENT_STATUSES = [
66
+ "active",
67
+ "absent",
68
+ "expired",
69
+ "version-not-covered",
70
+ "wrong-instance",
71
+ "tampered",
72
+ "not-yet-valid",
73
+ ] as const satisfies readonly EntitlementStatus[];
74
+
75
+ /**
76
+ * A licensed plugin's view of its entitlement.
77
+ *
78
+ * Read on every call rather than once at boot, so a licence the board enters,
79
+ * one that ends, and the next one taking over all reach a running plugin
80
+ * without a restart.
81
+ */
82
+ export interface PluginEntitlements {
83
+ /** The entitlement the manifest names, judged today. */
84
+ get(): Promise<EntitlementState>;
85
+ /**
86
+ * The active licence with a fresh signature by the instance key, for one of
87
+ * the audiences the manifest declares, as the text to send that service.
88
+ * Null without an active licence.
89
+ *
90
+ * Signed without a network call; sending it is the plugin's. A presentation
91
+ * is good at its audience for five minutes and once, so make one per
92
+ * request rather than keeping one.
93
+ *
94
+ * Throws a RangeError for an audience the manifest does not declare, since
95
+ * that is a defect in the plugin rather than a state of the licence.
96
+ */
97
+ present(audience: string): Promise<string | null>;
98
+ }
99
+
100
+ export interface TestEntitlementsOptions {
101
+ /** What `get()` answers. */
102
+ state: EntitlementState;
103
+ /** The manifest's `licence.audiences`. None by default. */
104
+ audiences?: readonly string[];
105
+ /** What `present()` answers while the state is active. */
106
+ presentation?: (audience: string) => string;
107
+ }
108
+
109
+ /**
110
+ * A stand-in for `host.entitlements` in a plugin's own tests.
111
+ *
112
+ * It answers with the state it was given and keeps the host's two rules about
113
+ * presenting: an undeclared audience is refused, and nothing is presented
114
+ * without an active licence. A plugin that wants to test the service end of a
115
+ * presentation signs licences with a throwaway key generated in the test;
116
+ * nothing here needs one.
117
+ */
118
+ export function createTestEntitlements(
119
+ options: TestEntitlementsOptions,
120
+ ): PluginEntitlements & { state: EntitlementState } {
121
+ const audiences = new Set(options.audiences ?? []);
122
+ const double = {
123
+ state: options.state,
124
+ get: (): Promise<EntitlementState> => Promise.resolve(double.state),
125
+ present: (audience: string): Promise<string | null> => {
126
+ if (!audiences.has(audience)) {
127
+ return Promise.reject(undeclaredAudienceError());
128
+ }
129
+ if (double.state.status !== "active") {
130
+ return Promise.resolve(null);
131
+ }
132
+ return Promise.resolve(
133
+ options.presentation?.(audience) ?? `test-presentation:${audience}`,
134
+ );
135
+ },
136
+ };
137
+ return double;
138
+ }
139
+
140
+ /**
141
+ * The refusal of an audience the manifest does not declare, worded once for
142
+ * the host and its test double. The audience is left out: it is whatever the
143
+ * plugin passed.
144
+ */
145
+ export function undeclaredAudienceError(): RangeError {
146
+ return new RangeError(
147
+ "present() was asked for an audience the plugin's manifest does not " +
148
+ "declare under licence.audiences.",
149
+ );
150
+ }
@@ -0,0 +1,212 @@
1
+ import type { PluginAbortSignal } from "./abort-signal.ts";
2
+ import type { StandardSchemaV1 } from "./standard-schema.ts";
3
+
4
+ /**
5
+ * Plugins that extend plugins (ADR 0030).
6
+ *
7
+ * An extension point (utökningspunkt) is a named slot a plugin declares in its
8
+ * manifest and offers to other plugins; that plugin is the point's host plugin.
9
+ * An addon (utökning) is a plugin that extends another at one of its points,
10
+ * and what it registers there is a contribution (bidrag): a title and a
11
+ * handler. Each is a plugin in every other respect, installed, consented to and
12
+ * switched on by itself, and neither receives the other's permissions.
13
+ *
14
+ * The host carries every call between them. It validates the input against the
15
+ * host plugin's schema before the call and the output after it, copies both as
16
+ * JSON values so that nothing but data crosses, and answers for what the addon
17
+ * did in the result rather than by throwing: a screen that loses one source
18
+ * should go on with the others.
19
+ *
20
+ * Swedish domain terms follow GLOSSARY.md.
21
+ */
22
+
23
+ /**
24
+ * What an extension point id may look like, in the manifest and at the host.
25
+ *
26
+ * Lowercase letters, digits and single hyphens, two to thirty-two characters:
27
+ * the shape a plugin id has, shorter, because it is shown beside one.
28
+ */
29
+ export const EXTENSION_POINT_ID_PATTERN = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
30
+
31
+ /** How many points a plugin may declare, and how many an addon may extend. */
32
+ export const MAX_EXTENSION_POINTS = 8;
33
+
34
+ /** How long a contribution may take when its point names no time of its own. */
35
+ export const EXTENSION_TIMEOUT_MS = 5_000;
36
+
37
+ /** The longest a point may give a contribution. */
38
+ export const EXTENSION_MAX_TIMEOUT_MS = 15_000;
39
+
40
+ /**
41
+ * The longest answer a contribution may give, in characters of its JSON text.
42
+ * A longer one is `output-invalid`, refused before it is parsed or validated.
43
+ */
44
+ export const EXTENSION_MAX_OUTPUT_LENGTH = 1_048_576;
45
+
46
+ /**
47
+ * Why `invoke` answered without a value.
48
+ *
49
+ * Everything an addon can do wrong, and the one state the host plugin cannot
50
+ * see from its own side, so a host plugin can tell a broken addon from one that
51
+ * is switched off without either being thrown at it.
52
+ */
53
+ export const EXTENSION_RESULT_REASONS = [
54
+ /**
55
+ * The addon is not installed, not loaded, switched off, or has registered no
56
+ * contribution at this point.
57
+ */
58
+ "not-serving",
59
+ /**
60
+ * The handler did not answer in time. Its signal was aborted.
61
+ *
62
+ * The time bounds only how long the host waits for the handler's promise.
63
+ * Work the handler does synchronously, and a `toJSON` or getter in its
64
+ * answer, runs on the host's one thread with no limit, as any plugin code
65
+ * does: a handler that loops stalls the instance.
66
+ */
67
+ "timed-out",
68
+ /** The handler threw. What it threw is in the server log, never here. */
69
+ "failed",
70
+ /**
71
+ * The answer does not meet the point's output schema, has no JSON form, or
72
+ * is longer than EXTENSION_MAX_OUTPUT_LENGTH as JSON.
73
+ */
74
+ "output-invalid",
75
+ ] as const;
76
+
77
+ export type ExtensionResultReason = (typeof EXTENSION_RESULT_REASONS)[number];
78
+
79
+ /** What the host plugin hands `define` for one point its manifest declares. */
80
+ export interface ExtensionPointDefinition<Input = unknown, Output = unknown> {
81
+ /**
82
+ * What the host plugin sends a contribution. A strict object at every depth,
83
+ * built with the host's zod, with no record, catchall, `unknown` or `any`
84
+ * anywhere inside it.
85
+ */
86
+ readonly input: StandardSchemaV1<unknown, Input>;
87
+ /**
88
+ * What a contribution must answer. A strict object at every depth, built with
89
+ * the host's zod, with no record, catchall, `unknown` or `any` anywhere inside
90
+ * it: a key the schema does not name is a key an addon could hand over
91
+ * something nobody consented to in.
92
+ */
93
+ readonly output: StandardSchemaV1<unknown, Output>;
94
+ /**
95
+ * How long a contribution may take, in milliseconds.
96
+ * EXTENSION_TIMEOUT_MS by default, at most EXTENSION_MAX_TIMEOUT_MS.
97
+ */
98
+ readonly timeoutMs?: number;
99
+ }
100
+
101
+ /** What the host tells a contribution about the call it is answering. */
102
+ export interface ExtensionContext {
103
+ /** The plugin whose point is being answered. */
104
+ readonly hostPlugin: string;
105
+ readonly point: string;
106
+ /** The point's major version, which is the one the addon named. */
107
+ readonly version: number;
108
+ /** For the logs, so a call can be followed from one plugin to the other. */
109
+ readonly requestId: string;
110
+ readonly now: Date;
111
+ /** Aborted when the call times out or the host plugin gives up on it. */
112
+ readonly signal: PluginAbortSignal;
113
+ }
114
+
115
+ /** What an addon hands `contribute` for one point its manifest extends. */
116
+ export interface ExtensionContributionDefinition<
117
+ Input = unknown,
118
+ Output = unknown,
119
+ > {
120
+ /**
121
+ * A bare key in the addon's own namespace, as an action's is. The host
122
+ * qualifies it as `plugin-<addon>:<key>`.
123
+ */
124
+ readonly titleKey: string;
125
+ readonly descriptionKey?: string;
126
+ /**
127
+ * Answers one call.
128
+ *
129
+ * Runs with the addon's own host object, under the permissions the board
130
+ * consented to for the addon, and with no person behind it: an extension
131
+ * point is a call from one plugin to another, not somebody's request. It
132
+ * must accept input fields it does not know, because a host plugin may add
133
+ * an optional field to a point without raising its version.
134
+ */
135
+ readonly handler: (
136
+ input: Input,
137
+ context: ExtensionContext,
138
+ ) => Promise<Output>;
139
+ }
140
+
141
+ /** One contribution that is serving at a point, as its host plugin sees it. */
142
+ export interface ExtensionContributionSummary {
143
+ /** The addon's plugin id, which `invoke` takes. */
144
+ readonly addon: string;
145
+ /** Qualified into the addon's namespace, `plugin-<addon>:<key>`. */
146
+ readonly titleKey: string;
147
+ readonly descriptionKey: string | null;
148
+ }
149
+
150
+ /** What `invoke` answers: a value, or why there is none. */
151
+ export type ExtensionResult<Output> =
152
+ | { readonly ok: true; readonly value: Output }
153
+ | { readonly ok: false; readonly reason: ExtensionResultReason };
154
+
155
+ /** The plugin and the point an addon contributes to. */
156
+ export interface ExtensionTarget {
157
+ readonly plugin: string;
158
+ readonly point: string;
159
+ }
160
+
161
+ /**
162
+ * Extension points, as a plugin sees them.
163
+ *
164
+ * A host plugin uses `define`, `list` and `invoke`; an addon uses
165
+ * `contribute`. `define` and `contribute` are declarations, callable from the
166
+ * module factory onwards and buffered until the application exists, as
167
+ * `host.actions.register` is. A definition or contribution the host cannot
168
+ * accept - a point or target the manifest does not declare, a schema that is
169
+ * not a strict object built with the host's zod, a second one for the same
170
+ * point - is found when the buffer is flushed and stops the plugin with
171
+ * `extension-refused`, rather than throwing here.
172
+ *
173
+ * `list` and `invoke` keep the ordinary gate and work from `onModuleInit`
174
+ * onwards.
175
+ */
176
+ export interface PluginExtensions {
177
+ /** Host plugin: the schemas of a point its manifest declares. */
178
+ define<Input, Output>(
179
+ point: string,
180
+ definition: ExtensionPointDefinition<Input, Output>,
181
+ ): void;
182
+
183
+ /**
184
+ * Host plugin: the contributions to one of its points that are serving now,
185
+ * in addon id order.
186
+ *
187
+ * Throws for a point the manifest does not declare or the plugin has not
188
+ * defined.
189
+ */
190
+ list(point: string): Promise<readonly ExtensionContributionSummary[]>;
191
+
192
+ /**
193
+ * Host plugin: calls one addon's contribution.
194
+ *
195
+ * Throws for a mistake of the host plugin's own: a point its manifest does
196
+ * not declare or it has not defined, or an input its own schema refuses. An
197
+ * aborted `signal` rejects with the signal's reason. Everything the addon
198
+ * does is answered in the result.
199
+ */
200
+ invoke<Output>(
201
+ point: string,
202
+ addon: string,
203
+ input: unknown,
204
+ options?: { signal?: PluginAbortSignal },
205
+ ): Promise<ExtensionResult<Output>>;
206
+
207
+ /** Addon: the handler for a point its manifest extends. Once per point. */
208
+ contribute<Input, Output>(
209
+ target: ExtensionTarget,
210
+ contribution: ExtensionContributionDefinition<Input, Output>,
211
+ ): void;
212
+ }
@@ -0,0 +1,317 @@
1
+ /**
2
+ * The association's finances, as a plugin may read them, record a delivery
3
+ * against them and hear about them.
4
+ *
5
+ * What is here is the free half of the boundary ROADMAP.md publishes: the
6
+ * notices and charges core already records, read on the same masking rules as
7
+ * the files the board hands over, a receipt that says one of them was
8
+ * delivered, and an event that says one was issued. What carries them out of
9
+ * Open BRF - an accounting system, a bankgiro file, a reminder - is the
10
+ * plugin's own work.
11
+ *
12
+ * Three rules hold on every member here regardless of permission, and they are
13
+ * the address book's (`PluginAddressBook`) read for money:
14
+ *
15
+ * A person with protected personal data is never named. A notice for their
16
+ * household carries its apartment and its amount and withholds every name;
17
+ * a charge on them carries its date and its amount and withholds who, where
18
+ * and why. The row says so with a `withheld` branch rather than an empty
19
+ * string, so a reader cannot take a withheld value for an absent one.
20
+ *
21
+ * A personal identity number is never returned. No row carries one.
22
+ *
23
+ * There is no payment state, because core records none. Nothing here says
24
+ * whether a notice was paid, what is outstanding or what a balance is, and a
25
+ * delivery receipt is not that either: the accounting system settles the
26
+ * debt, and a second answer to whether something was paid is worse than none.
27
+ */
28
+
29
+ /**
30
+ * One delivery of a notice or a charge, as the plugin that made it recorded it.
31
+ *
32
+ * A receipt says that something left the association by a route, at a time,
33
+ * under a reference the receiving system knows it by. It never says that the
34
+ * money arrived, and it is not a status: a notice may carry several receipts,
35
+ * from several plugins, and none of them changes the notice.
36
+ */
37
+ export interface PluginDeliveryReceipt {
38
+ id: string;
39
+ /** The plugin that recorded it. */
40
+ pluginId: string;
41
+ /**
42
+ * The route, in the recording plugin's own word for it: `fortnox`,
43
+ * `autogiro`, `email`. Lowercase letters, digits and hyphens, at most 32.
44
+ */
45
+ channel: string;
46
+ /**
47
+ * What the receiving system calls it, or the statement that it is withheld.
48
+ *
49
+ * Withheld on every receipt another plugin recorded: a plugin reads its own
50
+ * references only. And withheld with the row it is on: on a notice whose
51
+ * holders are withheld and a charge whose party or reason is, by the
52
+ * register as it stands at the read rather than when the receipt was
53
+ * recorded. A reference is the recording plugin's free text, and one written
54
+ * while a household could be named can name it as well as the names can.
55
+ * The receipt itself stays, so a plugin can still tell that the row was
56
+ * delivered.
57
+ */
58
+ reference: PluginDeliveryReference;
59
+ /** When it was delivered, as the plugin stated it. An ISO 8601 instant. */
60
+ deliveredAt: string;
61
+ /** When the receipt was recorded. An ISO 8601 instant. */
62
+ recordedAt: string;
63
+ }
64
+
65
+ /**
66
+ * What the receiving system calls a delivery - a voucher number, a file name, a
67
+ * message id, at most 128 characters - or the statement that it is withheld.
68
+ */
69
+ export type PluginDeliveryReference =
70
+ { state: "visible"; text: string } | { state: "withheld" };
71
+
72
+ /** What a plugin states when it records a delivery. */
73
+ export interface PluginDeliveryReceiptInput {
74
+ target:
75
+ { kind: "feeNotice"; id: string } | { kind: "memberCharge"; id: string };
76
+ channel: string;
77
+ reference: string;
78
+ /** When it was delivered. Now, when left out; never a time still to come. */
79
+ deliveredAt?: Date;
80
+ }
81
+
82
+ /** Who a notice names, or the statement that the names are withheld. */
83
+ export type PluginFeeNoticeHolders =
84
+ | {
85
+ state: "visible";
86
+ /** The tenant-owners' names, in the register's own order. */
87
+ names: string[];
88
+ }
89
+ | { state: "withheld" };
90
+
91
+ /** One apartment's notice in one run (avi). */
92
+ export interface PluginFeeNotice {
93
+ id: string;
94
+ apartment: {
95
+ id: string;
96
+ /** "<street> <number> <apartment number>". Never withheld. */
97
+ label: string;
98
+ number: string;
99
+ };
100
+ /**
101
+ * The tenant-owners holding the apartment when the period closed. Withheld
102
+ * where anybody in the household carries protected personal data or has had
103
+ * processing restricted (GDPR art. 18).
104
+ */
105
+ holders: PluginFeeNoticeHolders;
106
+ /** Kronor with two decimals: "3450.50". What was billed, never a balance. */
107
+ amount: string;
108
+ paymentReference: string;
109
+ deliveries: PluginDeliveryReceipt[];
110
+ }
111
+
112
+ /** A fee notification run (avisering) without its notices. */
113
+ export interface PluginFeeNotificationSummary {
114
+ id: string;
115
+ /** The period billed, inclusive at both ends. "YYYY-MM-DD". */
116
+ from: string;
117
+ to: string;
118
+ /** The due date the board stated. Nothing in core computes from it. */
119
+ dueOn: string;
120
+ /** When the run was issued. An ISO 8601 instant. */
121
+ issuedAt: string;
122
+ /** How many notices the run carries. */
123
+ notices: number;
124
+ /** The sum of what was billed, as kronor with two decimals. */
125
+ total: string;
126
+ }
127
+
128
+ /** A fee notification run with its notices. */
129
+ export interface PluginFeeNotification extends Omit<
130
+ PluginFeeNotificationSummary,
131
+ "notices"
132
+ > {
133
+ notices: PluginFeeNotice[];
134
+ }
135
+
136
+ /** Who a charge is on, or the statement that it is withheld. */
137
+ export type PluginMemberChargeParty =
138
+ | {
139
+ kind: "person";
140
+ state: "visible";
141
+ personId: string;
142
+ name: string;
143
+ /**
144
+ * Where they held a residency on the day of the charge, as the debiting
145
+ * list prints it, or null where they held none.
146
+ */
147
+ apartment: string | null;
148
+ }
149
+ | {
150
+ /**
151
+ * A person the plugin is not told about: one carrying protected personal
152
+ * data, one whose processing is restricted (GDPR art. 18), or one the
153
+ * register no longer holds.
154
+ */
155
+ kind: "person";
156
+ state: "withheld";
157
+ }
158
+ | {
159
+ kind: "apartment";
160
+ /** Null once the apartment has been corrected out of the register. */
161
+ apartmentId: string | null;
162
+ apartment: string | null;
163
+ };
164
+
165
+ /** The board's own words for a charge, or the statement they are withheld. */
166
+ export type PluginMemberChargeReason =
167
+ { state: "visible"; text: string } | { state: "withheld" };
168
+
169
+ /** One charge to a member or an apartment (debitering). */
170
+ export interface PluginMemberCharge {
171
+ id: string;
172
+ /** "YYYY-MM-DD" on the association's own calendar. */
173
+ chargedOn: string;
174
+ chargedTo: PluginMemberChargeParty;
175
+ /** Kronor with two decimals. What was charged, never a balance. */
176
+ amount: string;
177
+ vatTreatment: "EXEMPT" | "RATE";
178
+ /** Whole percent, and null exactly when the treatment is EXEMPT. */
179
+ vatRatePercent: number | null;
180
+ /**
181
+ * Withheld with the party, and on a charge to a household where somebody
182
+ * protected or restricted lived on the day of the charge: what the board
183
+ * wrote about a household can say who lives in it as well as a name can.
184
+ */
185
+ reason: PluginMemberChargeReason;
186
+ /** "YYYY-MM-DD" the basis went to the economic manager, or null. */
187
+ handedToManagerOn: string | null;
188
+ deliveries: PluginDeliveryReceipt[];
189
+ }
190
+
191
+ /**
192
+ * The events the host delivers, by type, with the version it delivers.
193
+ *
194
+ * A version moves when a field is removed or changes meaning; a field added
195
+ * keeps it. The host lists what it delivers in
196
+ * `host.finances.events.supported`, so a plugin built against a newer SDK can
197
+ * tell an event this instance will never send from one that has not happened.
198
+ */
199
+ export const PLUGIN_FINANCE_EVENTS = {
200
+ "feeNotification.issued": 1,
201
+ "memberCharge.recorded": 1,
202
+ "memberCharge.corrected": 1,
203
+ "memberCharge.removed": 1,
204
+ } as const;
205
+
206
+ export type PluginFinanceEventType = keyof typeof PLUGIN_FINANCE_EVENTS;
207
+
208
+ interface PluginFinanceEventBase {
209
+ /** Unique per event, and the same on every redelivery of it. */
210
+ eventId: string;
211
+ /** When the act was committed. An ISO 8601 instant. */
212
+ occurredAt: string;
213
+ }
214
+
215
+ /**
216
+ * Something happened to the association's finances.
217
+ *
218
+ * Identifiers only. An event carries nothing that names a person or states a
219
+ * figure, so it needs no masking of its own: the plugin reads the row through
220
+ * `host.finances`, on the same rules as every other read.
221
+ */
222
+ export type PluginFinanceEvent =
223
+ | (PluginFinanceEventBase & {
224
+ type: "feeNotification.issued";
225
+ version: 1;
226
+ notificationId: string;
227
+ })
228
+ | (PluginFinanceEventBase & {
229
+ type: "memberCharge.recorded" | "memberCharge.corrected";
230
+ version: 1;
231
+ chargeId: string;
232
+ })
233
+ | (PluginFinanceEventBase & {
234
+ type: "memberCharge.removed";
235
+ version: 1;
236
+ /** The charge is gone; only its id is left to say which one it was. */
237
+ chargeId: string;
238
+ });
239
+
240
+ export interface PluginFinanceEvents {
241
+ /** What this host delivers, by type and version. */
242
+ readonly supported: Readonly<Record<string, number>>;
243
+ /**
244
+ * Receives every finance event from now on.
245
+ *
246
+ * Call it once, from `onModuleInit`. Delivery is at least once, from a queue
247
+ * the host keeps for this plugin, and the event is enqueued in the same
248
+ * transaction as the act it reports - so an act that rolled back never
249
+ * produces one and one that committed always does. A handler that throws is
250
+ * retried; one that keeps throwing has the event dropped once the retries
251
+ * are spent. Order is not guaranteed. Ignore a `type` you do not know.
252
+ *
253
+ * Events reach a plugin that is serving and subscribed when the act
254
+ * commits. A plugin switched off, or not yet subscribed, misses them, and
255
+ * reads what it missed with `notifications()` and `charges()` when it starts.
256
+ */
257
+ subscribe(
258
+ handler: (event: PluginFinanceEvent) => Promise<void> | void,
259
+ ): Promise<void>;
260
+ }
261
+
262
+ /**
263
+ * The association's finances, scoped to what the plugin declared.
264
+ *
265
+ * The reads and the events require finances:read; recording a delivery
266
+ * requires finances:recordDelivery.
267
+ */
268
+ export interface PluginFinances {
269
+ /** The fee notification runs, newest period first. */
270
+ notifications(): Promise<PluginFeeNotificationSummary[]>;
271
+ /** One run with its notices, or null where there is no such run. */
272
+ notification(id: string): Promise<PluginFeeNotification | null>;
273
+ /**
274
+ * The charges dated within a period, both ends inclusive, as "YYYY-MM-DD".
275
+ * In date order, and in the order they were entered within a day.
276
+ */
277
+ charges(period: { from: string; to: string }): Promise<PluginMemberCharge[]>;
278
+ /** One charge, or null where there is no such charge. */
279
+ charge(id: string): Promise<PluginMemberCharge | null>;
280
+ /**
281
+ * Records that a notice or a charge was delivered, and returns the receipt.
282
+ *
283
+ * Idempotent: the same target, channel and reference from the same plugin
284
+ * returns the receipt already recorded rather than a second one, so a job
285
+ * retried after a crash cannot record one delivery twice. A reference that
286
+ * holds a Swedish personal identity number is refused.
287
+ *
288
+ * The receipt returned carries the reference visible: it is what the plugin
289
+ * has just stated. A later read withholds it with the row it is on.
290
+ */
291
+ recordDelivery(
292
+ input: PluginDeliveryReceiptInput,
293
+ ): Promise<PluginDeliveryReceipt>;
294
+ events: PluginFinanceEvents;
295
+ }
296
+
297
+ /**
298
+ * A delivery the host would not record, and why.
299
+ *
300
+ * `reason` is stable and meant for the plugin's own code; the message is
301
+ * English for the server log.
302
+ */
303
+ export class PluginDeliveryRefusedError extends Error {
304
+ constructor(
305
+ readonly reason:
306
+ | "target-not-found"
307
+ | "channel-invalid"
308
+ | "reference-invalid"
309
+ | "reference-holds-personal-identity-number"
310
+ | "delivered-at-invalid"
311
+ | "delivered-in-the-future",
312
+ message: string,
313
+ ) {
314
+ super(message);
315
+ this.name = "PluginDeliveryRefusedError";
316
+ }
317
+ }