@aria-framework/customer 0.0.0-stage → 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.
@@ -0,0 +1,176 @@
1
+ /**
2
+ * THE TABLES OF SUPPORT ACCESS, AS DATA (db-worker 0.31.0 `defineTables`). The app's own generator writes them into the
3
+ * app's own migration pair (`aria-tenancy new --definitions customer_access`), which the app commits and never edits;
4
+ * the app spreads `accessTenancy()` into its `defineTenancy`; and `checkAccessSchema` / `assertAccessSchema` compare the
5
+ * database with this set (at boot, or in the app's live test).
6
+ *
7
+ * VERSION 2 NAMES. None of the first app's five table names (support_access, support_defaults, support_grants,
8
+ * support_requests, support_managed) is used, so an app holding those can create these and drop those in one migration.
9
+ *
10
+ * customer_access_settings CUSTOMER one row per (customer, partner, kind) the customer has SET; no row = the
11
+ * starting value. Unique (customer_id, partner_id, kind).
12
+ * customer_access_defaults PARTNER a partner's starting value per kind for its new customers; append-only, one
13
+ * row per save. No customer column and no key into customers (a partner table
14
+ * keys only into partner tables: db-worker refuses the other).
15
+ * customer_access_grants CUSTOMER "Let them in now": one live grant per customer (partial unique index); no kind.
16
+ * customer_access_requests CUSTOMER a helper asking to come in: one waiting request per helper per customer; no kind.
17
+ * customer_access_managed CUSTOMER the partner's "we manage this customer" mark, one per customer; no kind.
18
+ *
19
+ * Every CUSTOMER table is keyed (customer_id, partner_id) → customers (id, partner_id) ON DELETE CASCADE ON UPDATE
20
+ * CASCADE by the generator. A user is a plain column with no key (user_id, set_by_user): a key into the principal table is
21
+ * attribution, and the format refuses it.
22
+ *
23
+ * WHAT THE SET NEEDS OF THE APP'S OWN TABLES (the anchors; the schema check verifies each):
24
+ * customers id TEXT, partner_id, name TEXT, status TEXT ('active' is active), created_at TEXT (UTC
25
+ * 'YYYY-MM-DD HH:MM:SS', stamped by the database: the received-time rule compares it, as text, with
26
+ * customer_access_defaults.created_at, stamped by the same clock); unique (id, partner_id)
27
+ * customer_contacts id TEXT, customer_id TEXT, partner_id, name TEXT; unique (id, partner_id)
28
+ */
29
+
30
+ 'use strict';
31
+
32
+ const { defineTables, tenancyFragment, checkDefinedSchema, assertDefinedSchema } = require('@aria-framework/db-worker');
33
+
34
+ const LEVEL = "level IN ('look', 'change')";
35
+ const LEVEL_RULE = "(mode = 'open' AND level IS NOT NULL) OR (mode <> 'open' AND level IS NULL)";
36
+ const name200 = (c) => `length(${c}) BETWEEN 1 AND 200`;
37
+ const KIND = { name: 'kind', type: 'TEXT', notNull: true, check: 'length(kind) BETWEEN 1 AND 40' };
38
+ const USER = { postgres: 'BIGINT', sqlite: 'INTEGER' };
39
+
40
+ const RAW = () => ({
41
+ name: 'customer_access',
42
+ version: '1.0.0',
43
+ comment: [
44
+ 'SUPPORT ACCESS, version 2 of the model (@aria-framework/customer, part access): who of the customer\'s partner may',
45
+ 'come in to help, per kind of data, as the customer set it. No helper tier: the partner is the one helper.',
46
+ 'customer_access_settings the customer\'s setting per kind; no row = the starting value (held in code)',
47
+ 'customer_access_defaults a partner\'s starting value per kind for its NEW customers, append-only',
48
+ 'customer_access_grants let them in now: one live grant per customer, for every kind not set to Never',
49
+ 'customer_access_requests a helper asking to come in: one waiting request per helper per customer',
50
+ 'customer_access_managed the partner\'s "we manage this customer" mark',
51
+ 'Times are UTC \'YYYY-MM-DD HH:MM:SS\'.'
52
+ ],
53
+ anchors: {
54
+ customers: {
55
+ tenancy: { customer: 'id' },
56
+ columns: [
57
+ { name: 'id', type: 'TEXT', notNull: true },
58
+ { name: 'partner_id', type: 'PARTNER_ID', notNull: true },
59
+ { name: 'name', type: 'TEXT', notNull: true },
60
+ { name: 'status', type: 'TEXT', notNull: true },
61
+ { name: 'created_at', type: 'TEXT', notNull: true }
62
+ ],
63
+ unique: [['id', 'partner_id']]
64
+ },
65
+ customer_contacts: {
66
+ tenancy: { customer: 'customer_id' },
67
+ columns: [
68
+ { name: 'id', type: 'TEXT', notNull: true },
69
+ { name: 'customer_id', type: 'TEXT', notNull: true },
70
+ { name: 'partner_id', type: 'PARTNER_ID', notNull: true },
71
+ { name: 'name', type: 'TEXT', notNull: true }
72
+ ],
73
+ unique: [['id', 'partner_id']]
74
+ }
75
+ },
76
+ tables: [
77
+ {
78
+ name: 'customer_access_settings',
79
+ description: "a customer's own choice, per kind of data, of whether its partner may come in to help",
80
+ tenancy: { customer: 'customer_id' },
81
+ columns: [
82
+ KIND,
83
+ { name: 'mode', type: 'TEXT', notNull: true, check: "mode IN ('open', 'ask', 'never')" },
84
+ { name: 'level', type: 'TEXT', check: LEVEL },
85
+ { name: 'set_by_name', type: 'TEXT', notNull: true, check: name200('set_by_name') },
86
+ { name: 'set_by_contact', type: 'TEXT' },
87
+ { name: 'updated_at', type: 'TEXT', notNull: true, default: { utcNow: true } }
88
+ ],
89
+ constraints: [{ name: 'ck_customer_access_settings_level', check: LEVEL_RULE }]
90
+ },
91
+ {
92
+ name: 'customer_access_defaults',
93
+ description: "a partner's starting support access per kind of data for its new customers, one row per save",
94
+ tenancy: 'PARTNER',
95
+ columns: [
96
+ KIND,
97
+ { name: 'mode', type: 'TEXT', notNull: true, check: "mode IN ('open', 'ask')" },
98
+ { name: 'level', type: 'TEXT', check: LEVEL },
99
+ { name: 'set_by_name', type: 'TEXT', notNull: true, check: name200('set_by_name') },
100
+ { name: 'set_by_user', type: USER }
101
+ ],
102
+ constraints: [{ name: 'ck_customer_access_defaults_level', check: LEVEL_RULE }]
103
+ },
104
+ {
105
+ name: 'customer_access_grants',
106
+ description: 'a customer letting its partner in for a while, into every kind not set to Never, and how it ended',
107
+ tenancy: { customer: 'customer_id' },
108
+ columns: [
109
+ { name: 'level', type: 'TEXT', notNull: true, check: LEVEL },
110
+ { name: 'ends_at', type: 'TEXT' },
111
+ { name: 'given_by_name', type: 'TEXT', notNull: true, check: name200('given_by_name') },
112
+ { name: 'given_by_contact', type: 'TEXT' },
113
+ { name: 'request_id', type: 'BIGINT' },
114
+ { name: 'ended_at', type: 'TEXT' },
115
+ { name: 'ended_how', type: 'TEXT', check: "ended_how IN ('ended', 'lapsed', 'replaced')" },
116
+ { name: 'ended_by_name', type: 'TEXT', check: 'length(ended_by_name) <= 200' }
117
+ ],
118
+ constraints: [{ name: 'ck_customer_access_grants_ended', check: '(ended_at IS NULL AND ended_how IS NULL) OR (ended_at IS NOT NULL AND ended_how IS NOT NULL)' }]
119
+ },
120
+ {
121
+ name: 'customer_access_requests',
122
+ description: "a helper asking a customer to be let in, and the customer's answer (the first answer settles it)",
123
+ tenancy: { customer: 'customer_id' },
124
+ columns: [
125
+ { name: 'user_id', type: 'BIGINT', notNull: true },
126
+ { name: 'user_partner_id', type: 'BIGINT', notNull: true },
127
+ { name: 'user_name', type: 'TEXT', notNull: true, check: name200('user_name') },
128
+ { name: 'company', type: 'TEXT', notNull: true, check: name200('company') },
129
+ { name: 'reason', type: 'TEXT', notNull: true, check: 'length(reason) BETWEEN 1 AND 300' },
130
+ { name: 'duration', type: 'TEXT', notNull: true, check: "duration IN ('1h', '24h', '7d')" },
131
+ { name: 'level', type: 'TEXT', notNull: true, check: LEVEL },
132
+ { name: 'state', type: 'TEXT', notNull: true, default: { text: 'waiting' }, check: "state IN ('waiting', 'allowed', 'declined', 'lapsed', 'cancelled')" },
133
+ { name: 'expires_at', type: 'TEXT', notNull: true },
134
+ { name: 'answered_at', type: 'TEXT' },
135
+ { name: 'answered_by_name', type: 'TEXT', check: 'length(answered_by_name) <= 200' },
136
+ { name: 'answered_by_contact', type: 'TEXT' },
137
+ { name: 'granted_level', type: 'TEXT', check: "granted_level IN ('look', 'change')" },
138
+ { name: 'grant_id', type: 'BIGINT' }
139
+ ]
140
+ },
141
+ {
142
+ name: 'customer_access_managed',
143
+ description: "a partner's standing 'we manage this customer' mark (its staff come in without a reason while the customer lets them)",
144
+ tenancy: { customer: 'customer_id' },
145
+ columns: [
146
+ { name: 'set_by_name', type: 'TEXT', notNull: true, check: name200('set_by_name') },
147
+ { name: 'set_by_user', type: USER }
148
+ ]
149
+ }
150
+ ],
151
+ indexes: [
152
+ { name: 'uq_customer_access_settings_kind', table: 'customer_access_settings', unique: true, columns: ['customer_id', 'partner_id', 'kind'] },
153
+ { name: 'idx_customer_access_defaults_kind', table: 'customer_access_defaults', columns: ['partner_id', 'kind', 'created_at'] },
154
+ { name: 'uq_customer_access_grants_live', table: 'customer_access_grants', unique: true, columns: ['customer_id', 'partner_id'], where: 'ended_at IS NULL' },
155
+ { name: 'idx_customer_access_grants_open', table: 'customer_access_grants', columns: ['ended_at', 'ends_at'] },
156
+ { name: 'uq_customer_access_requests_waiting', table: 'customer_access_requests', unique: true, columns: ['customer_id', 'partner_id', 'user_id'], where: "state = 'waiting'" },
157
+ { name: 'idx_customer_access_requests_state', table: 'customer_access_requests', columns: ['state', 'expires_at'] },
158
+ { name: 'uq_customer_access_managed_customer', table: 'customer_access_managed', unique: true, columns: ['customer_id', 'partner_id'] }
159
+ ]
160
+ });
161
+
162
+ /** The set, defined and frozen. */
163
+ const accessTables = defineTables(RAW());
164
+
165
+ /** What the app spreads into its defineTenancy: `defineTenancy({ ..., ...accessTenancy() })`. */
166
+ const accessTenancy = () => tenancyFragment(accessTables);
167
+
168
+ /** Compare the database with the set: `{ problems, compared }` (db-worker checkDefinedSchema). */
169
+ const checkAccessSchema = (driver, { partnerIdType } = {}) => checkDefinedSchema(driver, [accessTables], { partnerIdType });
170
+ /** The same, throwing TABLE_DEFINITIONS_MISMATCH naming every problem: what the app calls when it starts. */
171
+ const assertAccessSchema = (driver, { partnerIdType } = {}) => assertDefinedSchema(driver, [accessTables], { partnerIdType });
172
+
173
+ /** The table names, for an app's own lists. */
174
+ const ACCESS_TABLES = Object.freeze(accessTables.tables.map((t) => t.name));
175
+
176
+ module.exports = { accessTables, accessTenancy, checkAccessSchema, assertAccessSchema, ACCESS_TABLES, RAW_ACCESS_TABLES: RAW };
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
2
  "name": "@aria-framework/customer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Aria App Framework — customer module. What makes an app customer-driven, as factories and data an app passes its own lists to. 0.1, part access: the customer's support-access setting per kind of data (version 2 of the model: no helper tier), grants and requests with their state machines, the partner's starting value and managed mark, the sweep, the audit action names, and the five tables as db-worker table definitions with their schema check. No permission name, route prefix or app word lives here.",
5
+ "license": "UNLICENSED",
6
+ "private": false,
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "main": "index.js",
11
+ "files": [
12
+ "index.js",
13
+ "lib/",
14
+ "README.md",
15
+ "CHANGELOG.md"
16
+ ],
17
+ "scripts": {
18
+ "test": "node test/run.js",
19
+ "prepublishOnly": "node ../../test/packaging.js customer"
20
+ },
21
+ "peerDependencies": {
22
+ "@aria-framework/db-worker": ">=0.31.0"
23
+ },
24
+ "devDependencies": {
25
+ "@electric-sql/pglite": "^0.5.8"
26
+ }
27
+ }