forked from pryv/open-pryv.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSize.ts
More file actions
46 lines (41 loc) · 1.31 KB
/
Size.ts
File metadata and controls
46 lines (41 loc) · 1.31 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
/**
* @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);
const { getUsersRepository, UserRepositoryOptions, User } = require('business/src/users/index.ts');
const { getMall } = require('mall');
class Size {
/**
* Computes and updates storage size for the given user.
*
*/
async computeForUser (user: any) {
const mall = await getMall();
const storageInfo = await mall.getUserStorageInfos(user.id);
let dbDocuments = 0;
let attachedFiles = 0;
for (const [_key, entry] of Object.entries(storageInfo) as Array<[string, any]>) {
if (entry.streams?.count) dbDocuments += entry.streams?.count;
if (entry.events?.count) dbDocuments += entry.events?.count;
if (entry.files?.sizeKb) attachedFiles += entry.files?.sizeKb;
}
// reconstruct previous system
const storageUsed = {
dbDocuments,
attachedFiles
};
const userObject = new User(user);
const usersRepository = await getUsersRepository();
await usersRepository.updateOne(
userObject,
storageUsed,
UserRepositoryOptions.SYSTEM_USER_ACCESS_ID
);
return storageInfo;
}
}
export { Size };