insite-users-client 1.0.26
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/README.md +13 -0
- package/lib/UsersSubscriptionGroup.js +57 -0
- package/lib/index.js +1 -0
- package/lib/orgs.js +72 -0
- package/lib/roles.js +20 -0
- package/lib/user.js +36 -0
- package/lib/users.js +83 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Eugene Nesvetaev
|
|
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/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { SubscriptionGroup } from "insite-subscriptions-client";
|
|
2
|
+
import { handleExtendedOrgs, handleOrgs, handleOrgsBeforeSubscribe } from "./orgs";
|
|
3
|
+
import { handleRoles } from "./roles";
|
|
4
|
+
import { handleUser } from "./user";
|
|
5
|
+
import { handleExtendedUsers, handleUsers } from "./users";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export class UsersSubscriptionGroup extends SubscriptionGroup {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
super([
|
|
11
|
+
[ "orgs", "map", handleOrgs, handleOrgsBeforeSubscribe ],
|
|
12
|
+
[ "users", "map", handleUsers ],
|
|
13
|
+
[ "user", [ true ], handleUser ]
|
|
14
|
+
], options);
|
|
15
|
+
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
isExtended = false;
|
|
19
|
+
|
|
20
|
+
extend() {
|
|
21
|
+
|
|
22
|
+
if (this.isExtended === false) {
|
|
23
|
+
this.isExtended = null;
|
|
24
|
+
|
|
25
|
+
return this.attach([
|
|
26
|
+
[ "roles", "map", handleRoles ],
|
|
27
|
+
[ "extendedUsers", "map", "users.extended", handleExtendedUsers, true ],
|
|
28
|
+
[ "extendedOrgs", "map", "orgs.extended", handleExtendedOrgs, true ]
|
|
29
|
+
]).then(() => {
|
|
30
|
+
|
|
31
|
+
this.isExtended = true;
|
|
32
|
+
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
unextend() {
|
|
39
|
+
|
|
40
|
+
if (this.isExtended) {
|
|
41
|
+
this.isExtended = false;
|
|
42
|
+
|
|
43
|
+
this.detach([ "roles", "extendedUsers", "extendedOrgs" ]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
static handleValues({ orgs, users, user }, options) {
|
|
50
|
+
return new SubscriptionGroup([
|
|
51
|
+
[ "orgs", orgs, handleOrgs, handleOrgsBeforeSubscribe ],
|
|
52
|
+
[ "users", users, handleUsers ],
|
|
53
|
+
[ "user", user, handleUser ]
|
|
54
|
+
], options);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./UsersSubscriptionGroup";
|
package/lib/orgs.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export function handleOrgsBeforeSubscribe(orgs) {
|
|
2
|
+
|
|
3
|
+
orgs.null = {
|
|
4
|
+
_id: null,
|
|
5
|
+
title: "",
|
|
6
|
+
isOrg: true,
|
|
7
|
+
users: Object.assign(new Set(), { sorted: [] })
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** @this UsersSubscriptionGroup */
|
|
13
|
+
export function handleOrgs(updated) {
|
|
14
|
+
if (updated)
|
|
15
|
+
for (const org of updated.added) {
|
|
16
|
+
org.isOrg = true;
|
|
17
|
+
|
|
18
|
+
org.users = Object.assign(new Set(), { sorted: [] });
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
const nullOrgUsers = this.values.orgs.null.users;
|
|
22
|
+
nullOrgUsers.clear();
|
|
23
|
+
nullOrgUsers.sorted.length = 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @this UsersSubscriptionGroup */
|
|
29
|
+
export function handleExtendedOrgs(updated) {
|
|
30
|
+
|
|
31
|
+
const { orgs } = this.values;
|
|
32
|
+
|
|
33
|
+
if (updated) {
|
|
34
|
+
const { users, extendedOrgs } = this.values;
|
|
35
|
+
|
|
36
|
+
for (const orgDoc of updated) {
|
|
37
|
+
const org = orgs.get(orgDoc._id);
|
|
38
|
+
|
|
39
|
+
org.owners = orgDoc.owners.map(_id => users.get(_id) || orgs.get(_id));
|
|
40
|
+
org.slaveOrgs = orgs.getAll(orgDoc.slaveOrgs);
|
|
41
|
+
org.note = orgDoc.note;
|
|
42
|
+
org._l = 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const sortedHierarchically = [];
|
|
46
|
+
|
|
47
|
+
for (const orgDoc of extendedOrgs.sorted) {
|
|
48
|
+
const org = orgs.get(orgDoc._id);
|
|
49
|
+
|
|
50
|
+
for (const slaveOrg of org.slaveOrgs)
|
|
51
|
+
slaveOrg._l = org._l + 1;
|
|
52
|
+
|
|
53
|
+
sortedHierarchically.push(org);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
orgs.sortedHierarchically = sortedHierarchically;
|
|
57
|
+
|
|
58
|
+
if (this.isLoaded) {
|
|
59
|
+
this.items.users.emitUpdate();
|
|
60
|
+
this.items.orgs.emitUpdate(updated);
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
for (const org of orgs.sorted) {
|
|
64
|
+
delete org.owners;
|
|
65
|
+
delete org.slaveOrgs;
|
|
66
|
+
delete org.note;
|
|
67
|
+
delete org._l;
|
|
68
|
+
}
|
|
69
|
+
delete orgs.sortedHierarchically;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
}
|
package/lib/roles.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** @this UsersSubscriptionGroup */
|
|
2
|
+
export function handleRoles(updated) {
|
|
3
|
+
if (updated) {
|
|
4
|
+
const { roles } = this.values;
|
|
5
|
+
|
|
6
|
+
for (const role of updated) {
|
|
7
|
+
role.ownInvolves = roles.getAll(role.ownInvolves);
|
|
8
|
+
role.involves = roles.getAll(role.involves);
|
|
9
|
+
role._l = 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
for (const role of roles.sorted)
|
|
13
|
+
for (const involvedRole of role.ownInvolves)
|
|
14
|
+
involvedRole._l = role._l + 1;
|
|
15
|
+
|
|
16
|
+
if (this.isLoaded)
|
|
17
|
+
this.items.users.emitUpdate(updated);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
}
|
package/lib/user.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** @this UsersSubscriptionGroup */
|
|
2
|
+
export function handleUser(user) {
|
|
3
|
+
if (user) {
|
|
4
|
+
|
|
5
|
+
const slavesSnapshot = user.slaveIds.join(",");
|
|
6
|
+
|
|
7
|
+
if (user.slavesSnapshot !== slavesSnapshot) {
|
|
8
|
+
user.slavesSnapshot = slavesSnapshot;
|
|
9
|
+
|
|
10
|
+
const slaves = [];
|
|
11
|
+
const slaveUsers = [];
|
|
12
|
+
const slaveOrgs = [];
|
|
13
|
+
|
|
14
|
+
const { users, orgs } = this.values;
|
|
15
|
+
|
|
16
|
+
for (const _id of user.slaveIds) {
|
|
17
|
+
let slave = users.get(_id);
|
|
18
|
+
if (slave) {
|
|
19
|
+
slaves.push(slave);
|
|
20
|
+
slaveUsers.push(slave);
|
|
21
|
+
} else {
|
|
22
|
+
slave = orgs.get(_id);
|
|
23
|
+
if (slave) {
|
|
24
|
+
slaves.push(slave);
|
|
25
|
+
slaveOrgs.push(slave);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
user.slaves = slaves;
|
|
31
|
+
user.slaveUsers = slaveUsers;
|
|
32
|
+
user.slaveOrgs = slaveOrgs;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
}
|
package/lib/users.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** @this UsersSubscriptionGroup */
|
|
2
|
+
export function handleUsers(updated) {
|
|
3
|
+
if (updated) {
|
|
4
|
+
const { users, user: currentUser, orgs } = this.values;
|
|
5
|
+
|
|
6
|
+
if (users.get(currentUser._id) !== currentUser) {
|
|
7
|
+
users.set(currentUser._id, currentUser);
|
|
8
|
+
users.sorted[users.sorted.findIndex(user => user._id === currentUser._id)] = currentUser;
|
|
9
|
+
}
|
|
10
|
+
const updatedUserIndex = updated.findIndex(user => user._id === currentUser._id);
|
|
11
|
+
|
|
12
|
+
if (~updatedUserIndex)
|
|
13
|
+
updated[updatedUserIndex] = currentUser;
|
|
14
|
+
|
|
15
|
+
const addedUserIndex = updated.added.findIndex(user => user._id === currentUser._id);
|
|
16
|
+
|
|
17
|
+
if (~addedUserIndex)
|
|
18
|
+
updated.added[updatedUserIndex] = currentUser;
|
|
19
|
+
|
|
20
|
+
for (const user of updated.added)
|
|
21
|
+
user.isUser = true;
|
|
22
|
+
|
|
23
|
+
let shouldUpdateUser;
|
|
24
|
+
const orgsToSortUsers = new Set();
|
|
25
|
+
|
|
26
|
+
for (const user of updated) {
|
|
27
|
+
const org = orgs.get(user.orgId) ?? orgs.null;
|
|
28
|
+
|
|
29
|
+
if (user.org !== org) {
|
|
30
|
+
if (user.org) {
|
|
31
|
+
user.org.users.delete(user);
|
|
32
|
+
user.org.users.sorted.remove(user);
|
|
33
|
+
}
|
|
34
|
+
user.org = org;
|
|
35
|
+
user.org.users.add(user);
|
|
36
|
+
user.org.users.sorted.push(user);
|
|
37
|
+
|
|
38
|
+
orgsToSortUsers.add(user.org);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (user === currentUser)
|
|
42
|
+
shouldUpdateUser = true;
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for (const org of orgsToSortUsers)
|
|
47
|
+
org.users.sorted.sort(users.sortCompareFunction);
|
|
48
|
+
|
|
49
|
+
if (updated.deleted.length)
|
|
50
|
+
for (const org of orgs.sorted)
|
|
51
|
+
for (const user of org.users)
|
|
52
|
+
if (!users.has(user._id)) {
|
|
53
|
+
org.users.delete(user);
|
|
54
|
+
org.users.sorted.remove(user);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (this.isLoaded) {
|
|
58
|
+
if (shouldUpdateUser)
|
|
59
|
+
this.items.user.emitUpdate();
|
|
60
|
+
this.items.orgs.emitUpdate();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** @this UsersSubscriptionGroup */
|
|
67
|
+
export function handleExtendedUsers(updated) {
|
|
68
|
+
|
|
69
|
+
const { users } = this.values;
|
|
70
|
+
|
|
71
|
+
if (updated) {
|
|
72
|
+
const { roles } = this.values;
|
|
73
|
+
|
|
74
|
+
for (const userDoc of updated) {
|
|
75
|
+
const user = users.get(userDoc._id);
|
|
76
|
+
|
|
77
|
+
user.roles = roles.getAll(userDoc.roleIds);
|
|
78
|
+
}
|
|
79
|
+
} else
|
|
80
|
+
for (const user of users.sorted)
|
|
81
|
+
delete user.roles;
|
|
82
|
+
|
|
83
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "insite-users-client",
|
|
3
|
+
"version": "1.0.26",
|
|
4
|
+
"description": "inSite users client side",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"module": "./lib/index.js",
|
|
8
|
+
"packageManager": "yarn@1.22.22",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/nesvet/insite-users-client.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"insite",
|
|
18
|
+
"users",
|
|
19
|
+
"client"
|
|
20
|
+
],
|
|
21
|
+
"author": "Eugene Nesvetaev <dev@nesvet.dev> (https://nesvet.dev/)",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"insite-subscriptions-client": "^1"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@nesvet/eslint-config": "^1"
|
|
28
|
+
}
|
|
29
|
+
}
|