forked from pryv/open-pryv.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovisioning.ts
More file actions
126 lines (116 loc) · 4.14 KB
/
provisioning.ts
File metadata and controls
126 lines (116 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @license
* Copyright (C) Pryv https://pryv.com
* This file is part of Pryv.io and released under BSD-Clause-3 License
* Refer to LICENSE file
*/
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
/**
* CMC plugin — auto-provisioning of reserved parent streams on user creation.
*
* The five reserved parents (`:_cmc:`, `:_cmc:inbox`, `:_cmc:apps`,
* `:_cmc:_internal`, `:_cmc:_internal:retries`) must auto-exist on every
* user account so that:
* - User code can `streams.create({parentId: ':_cmc:apps'})` for
* organizational app roots.
* - Plugin code can `events.create` into `:_cmc:_internal:retries`
* for the retry queue.
*
* Per-app sub-trees (`:_cmc:apps:<app-code>:chats:*` /
* `:_cmc:apps:<app-code>:<...>:collectors:*`) are NOT pre-provisioned —
* the plugin creates them on demand at acceptance time, nested under
* whichever stream the trigger event was written to. Same for the
* per-capability streams under `:_cmc:_internal:offer:*` /
* `:_cmc:_internal:responses:*`.
*
* This is called from the user-creation flow in business/src/users/
* repository.ts. The streams are created via the mall directly,
* bypassing the api-server middleware chain (otherwise the reserved-root
* hook would reject creation of its own parents).
*
* Idempotent: catches "already exists" errors so re-provisioning is safe
* (e.g. running on existing users for backfill).
*/
const C = require('./constants.ts');
type Mall = {
streams: {
create (userId: string, params: any): Promise<any>;
getOne?: (userId: string, params: any) => Promise<any>;
};
};
type ProvisionLogger = {
debug: (msg: string, ...rest: any[]) => void;
warn: (msg: string, ...rest: any[]) => void;
};
/**
* Tree of reserved parents, ordered so children's parents always exist
* by the time the child is created.
*/
const RESERVED_TREE: Array<{ id: string; parentId: string | null; name: string }> = [
{ id: C.NS, parentId: null, name: 'CMC' },
{ id: C.NS_INBOX, parentId: C.NS, name: 'CMC inbox' },
{ id: C.NS_APPS, parentId: C.NS, name: 'CMC apps' },
{ id: C.NS_INTERNAL, parentId: C.NS, name: 'CMC plugin-internal' },
{ id: C.NS_INTERNAL_RETRIES, parentId: C.NS_INTERNAL, name: 'CMC retry queue' },
];
function isAlreadyExistsError (err: any): boolean {
if (err == null) return false;
const msg = String(err.message || err);
if (msg.includes('already exists') || msg.includes('item-already-exists')) return true;
if (err.id === 'item-already-exists') return true;
if (err.data?.id === 'item-already-exists') return true;
// mall throws APIError with id; be tolerant of multiple shapes.
return false;
}
/**
* Create the seven CMC reserved parent streams for the given user.
* Each create is wrapped to ignore "already exists" so the function
* is idempotent. Returns the list of stream-ids that were newly created
* (does not include ids that were already present).
*/
async function provisionUserStreams (params: {
mall: Mall;
userId: string;
accessId?: string;
logger?: ProvisionLogger;
}): Promise<string[]> {
const { mall, userId, accessId, logger } = params;
const created: string[] = [];
for (const stream of RESERVED_TREE) {
const payload: any = {
id: stream.id,
parentId: stream.parentId,
name: stream.name,
clientData: {
cmc: { kind: 'reserved-parent', autoProvisioned: true },
},
};
if (accessId != null) {
payload.createdBy = accessId;
payload.modifiedBy = accessId;
}
try {
await mall.streams.create(userId, payload);
created.push(stream.id);
logger?.debug('cmc: provisioned reserved parent stream', { userId, streamId: stream.id });
} catch (err: any) {
if (isAlreadyExistsError(err)) {
logger?.debug('cmc: reserved parent already present', { userId, streamId: stream.id });
continue;
}
logger?.warn('cmc: failed to provision reserved parent stream', {
userId,
streamId: stream.id,
error: err?.message || err,
});
throw err;
}
}
return created;
}
export {
RESERVED_TREE,
provisionUserStreams,
isAlreadyExistsError,
};