@nu-art/ts-focused-object-backend 0.400.5

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/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './modules/ModuleBE_FocusedObject.js';
2
+ export * from './modules/module-pack.js';
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './modules/ModuleBE_FocusedObject.js';
2
+ export * from './modules/module-pack.js';
@@ -0,0 +1,18 @@
1
+ import { Module } from '@nu-art/ts-common';
2
+ import { OnPreLogout } from '@nu-art/user-account-backend';
3
+ type Config = {};
4
+ export declare class ModuleBE_FocusedObject_Class extends Module<Config> implements OnPreLogout {
5
+ __onPreLogout: () => Promise<void>;
6
+ protected init(): void;
7
+ private updateFocusData;
8
+ private onAccountLogOut;
9
+ private getFocusMap;
10
+ private setFocusMap;
11
+ private cleanTTL;
12
+ private clearFocusForTabId;
13
+ private setNewFocusData;
14
+ private clearFocusForDeviceId;
15
+ private processNodes;
16
+ }
17
+ export declare const ModuleBE_FocusedObject: ModuleBE_FocusedObject_Class;
18
+ export {};
@@ -0,0 +1,136 @@
1
+ import { _keys, cloneObj, compare, currentTimeMillis, flatArray, Module } from '@nu-art/ts-common';
2
+ import { addRoutes, createBodyServerApi } from '@nu-art/thunderstorm-backend';
3
+ import { Header_DeviceId, Header_TabId, MemKey_AccountId } from '@nu-art/user-account-backend';
4
+ import { ModuleBE_Firebase } from '@nu-art/firebase-backend';
5
+ import { ApiDef_FocusedObject, } from '@nu-art/ts-focused-object-shared';
6
+ import { DefaultTTL_FocusedObject, getRelationalPath } from '@nu-art/ts-focused-object-shared/consts';
7
+ export class ModuleBE_FocusedObject_Class extends Module {
8
+ __onPreLogout = async () => {
9
+ console.log('USER LOG OUT');
10
+ await this.onAccountLogOut();
11
+ };
12
+ init() {
13
+ addRoutes([
14
+ createBodyServerApi(ApiDef_FocusedObject._v1.update, this.updateFocusData)
15
+ ]);
16
+ }
17
+ // ######################## Callbacks ########################
18
+ updateFocusData = async (request) => {
19
+ const tabId = Header_TabId.get();
20
+ const oldFocusMap = await this.getFocusMap();
21
+ const newFocusMap = cloneObj(oldFocusMap);
22
+ //Make changes to newFocusMap
23
+ this.cleanTTL(newFocusMap);
24
+ this.clearFocusForTabId(newFocusMap, tabId);
25
+ this.setNewFocusData(newFocusMap, request.focusedEntities);
26
+ //Return if there are no changes
27
+ if (compare(oldFocusMap, newFocusMap))
28
+ return;
29
+ await this.setFocusMap(newFocusMap);
30
+ };
31
+ onAccountLogOut = async () => {
32
+ const deviceId = Header_DeviceId.get();
33
+ const oldFocusMap = await this.getFocusMap();
34
+ const newFocusMap = cloneObj(oldFocusMap);
35
+ this.clearFocusForDeviceId(newFocusMap, deviceId);
36
+ //Return if there are no changes
37
+ if (compare(oldFocusMap, newFocusMap))
38
+ return;
39
+ await this.setFocusMap(newFocusMap);
40
+ };
41
+ // ######################## Logic ########################
42
+ getFocusMap = async () => {
43
+ const ref = ModuleBE_Firebase.createAdminSession().getDatabase().ref(getRelationalPath());
44
+ return await ref.get({});
45
+ };
46
+ setFocusMap = async (map) => {
47
+ const ref = ModuleBE_Firebase.createAdminSession().getDatabase().ref(getRelationalPath());
48
+ return await ref.patch(map);
49
+ };
50
+ cleanTTL = (map) => {
51
+ const now = currentTimeMillis();
52
+ return this.processNodes(map, 'tabId', (dbKey, itemId, accountId, deviceId, tabId) => {
53
+ if (map[dbKey][itemId][accountId][deviceId][tabId] + DefaultTTL_FocusedObject >= now)
54
+ return false;
55
+ delete map[dbKey][itemId][accountId][deviceId][tabId];
56
+ return true;
57
+ });
58
+ };
59
+ clearFocusForTabId = (map, _tabId) => {
60
+ return this.processNodes(map, 'tabId', (dbKey, itemId, accountId, deviceId, tabId) => {
61
+ if (_tabId !== tabId)
62
+ return false;
63
+ delete map[dbKey][itemId][accountId][deviceId][tabId];
64
+ // //No more entries under this deviceId
65
+ // if (!_keys(map[dbKey][itemId][accountId][deviceId]).length)
66
+ // delete map[dbKey][itemId][accountId][deviceId];
67
+ //
68
+ // //No more entries under this accountId
69
+ // if (!_keys(map[dbKey][itemId][accountId]).length)
70
+ // delete map[dbKey][itemId][accountId];
71
+ //
72
+ // //If no more entries under this itemId
73
+ // if (_keys(map[dbKey][itemId]).length)
74
+ // delete map[dbKey][itemId];
75
+ //
76
+ // //If no more entries under this dbKey
77
+ // if (_keys(map[dbKey]).length)
78
+ // delete map[dbKey];
79
+ return true;
80
+ });
81
+ };
82
+ setNewFocusData = (map, focusEntities) => {
83
+ const now = currentTimeMillis();
84
+ const tabId = Header_TabId.get();
85
+ const accountId = MemKey_AccountId.get();
86
+ const deviceId = Header_DeviceId.get();
87
+ focusEntities.forEach(focusEntity => {
88
+ map[focusEntity.dbKey] ??= {};
89
+ map[focusEntity.dbKey][focusEntity.itemId] ??= {};
90
+ map[focusEntity.dbKey][focusEntity.itemId][accountId] ??= {};
91
+ map[focusEntity.dbKey][focusEntity.itemId][accountId][deviceId] ??= {};
92
+ map[focusEntity.dbKey][focusEntity.itemId][accountId][deviceId][tabId] = now;
93
+ });
94
+ };
95
+ clearFocusForDeviceId = (map, _deviceId) => {
96
+ this.processNodes(map, 'deviceId', (dbKey, itemId, accountId, deviceId) => {
97
+ if (deviceId !== _deviceId)
98
+ return false;
99
+ delete map[dbKey][itemId][accountId][deviceId];
100
+ // //No more entries under this accountId
101
+ // if (!_keys(map[dbKey][itemId][accountId]).length)
102
+ // delete map[dbKey][itemId][accountId];
103
+ //
104
+ // //If no more entries under this itemId
105
+ // if (_keys(map[dbKey][itemId]).length)
106
+ // delete map[dbKey][itemId];
107
+ //
108
+ // //If no more entries under this dbKey
109
+ // if (_keys(map[dbKey]).length)
110
+ // delete map[dbKey];
111
+ return true;
112
+ });
113
+ };
114
+ // ######################## Logic - Process Nodes ########################
115
+ processNodes = (map, resolution, processor) => {
116
+ return flatArray(_keys(map).map(dbKey => {
117
+ if (resolution === 'dbKey')
118
+ return processor(dbKey);
119
+ return _keys(map[dbKey]).map(itemId => {
120
+ if (resolution === 'itemId')
121
+ return processor(dbKey, itemId);
122
+ return _keys(map[dbKey][itemId]).map(accountId => {
123
+ if (resolution === 'accountId')
124
+ return processor(dbKey, itemId, accountId);
125
+ return _keys(map[dbKey][itemId][accountId]).map(deviceId => {
126
+ if (resolution === 'deviceId')
127
+ return processor(dbKey, itemId, accountId, deviceId);
128
+ return _keys(map[dbKey][itemId][accountId][deviceId]).map(tabId => //For every tabId
129
+ processor(dbKey, itemId, accountId, deviceId, tabId));
130
+ });
131
+ });
132
+ });
133
+ })).some(i => i);
134
+ };
135
+ }
136
+ export const ModuleBE_FocusedObject = new ModuleBE_FocusedObject_Class();
@@ -0,0 +1,3 @@
1
+ import { Module } from '@nu-art/ts-common';
2
+ export declare const ModulePack_FocusedObjectBE: Module[];
3
+ export declare const ModulePackBE_FocusedObject: Module<any, any, import("@nu-art/ts-common").Validator<any> | import("@nu-art/ts-common").TypeValidator<any>>[];
@@ -0,0 +1,5 @@
1
+ import { ModuleBE_FocusedObject } from './ModuleBE_FocusedObject.js';
2
+ export const ModulePack_FocusedObjectBE = [
3
+ ModuleBE_FocusedObject
4
+ ];
5
+ export const ModulePackBE_FocusedObject = ModulePack_FocusedObjectBE;
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@nu-art/ts-focused-object-backend",
3
+ "version": "0.400.5",
4
+ "description": "ts-focused-object - Express & Typescript based backend framework Backend",
5
+ "keywords": [
6
+ "TacB0sS",
7
+ "infra",
8
+ "nu-art",
9
+ "thunderstorm",
10
+ "typescript",
11
+ "ts-focused-object"
12
+ ],
13
+ "homepage": "https://github.com/nu-art-js/thunderstorm",
14
+ "bugs": {
15
+ "url": "https://github.com/nu-art-js/thunderstorm/issues"
16
+ },
17
+ "publishConfig": {
18
+ "directory": "dist",
19
+ "linkDirectory": true
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+ssh://git@github.com:nu-art-js/thunderstorm.git"
24
+ },
25
+ "license": "Apache-2.0",
26
+ "author": "TacB0sS",
27
+ "files": [
28
+ "**/*"
29
+ ],
30
+ "scripts": {
31
+ "build": "tsc"
32
+ },
33
+ "dependencies": {
34
+ "@nu-art/ts-focused-object-shared": "0.400.5",
35
+ "@nu-art/firebase-backend": "0.400.5",
36
+ "@nu-art/firebase-shared": "0.400.5",
37
+ "@nu-art/thunderstorm-backend": "0.400.5",
38
+ "@nu-art/thunderstorm-shared": "0.400.5",
39
+ "@nu-art/ts-common": "0.400.5",
40
+ "@nu-art/user-account-backend": "0.400.5",
41
+ "@nu-art/user-account-shared": "0.400.5",
42
+ "firebase": "^11.9.0",
43
+ "firebase-admin": "13.4.0",
44
+ "firebase-functions": "6.3.2",
45
+ "react": "^18.0.0"
46
+ },
47
+ "devDependencies": {
48
+ "@types/react": "^18.0.0",
49
+ "@types/chai": "^4.3.4",
50
+ "@types/mocha": "^10.0.1"
51
+ },
52
+ "unitConfig": {
53
+ "type": "typescript-lib"
54
+ },
55
+ "type": "module",
56
+ "exports": {
57
+ ".": {
58
+ "types": "./index.d.ts",
59
+ "import": "./index.js"
60
+ },
61
+ "./*": {
62
+ "types": "./*.d.ts",
63
+ "import": "./*.js"
64
+ }
65
+ }
66
+ }