Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/database/postgres/user-tracking.ts
1503 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { callback2 } from "@cocalc/util/async-utils";
7
import { expire_time, is_valid_uuid_string } from "@cocalc/util/misc";
8
import { PostgreSQL } from "./types";
9
10
export async function record_user_tracking(
11
db: PostgreSQL,
12
account_id: string,
13
event: string,
14
value: { [key: string]: any }
15
): Promise<void> {
16
/* Right now this function is called from outside typescript
17
(e.g., api from user), so we have to do extra type checking.
18
Also, the input is uuid's, which typescript can't check. */
19
if (!is_valid_uuid_string(account_id)) {
20
throw Error("invalid account_id");
21
}
22
if (event == null || event.length == 0) {
23
throw Error("evt must be specified");
24
}
25
if (event.length > 80) {
26
throw Error("event must have length at most 80");
27
}
28
if (value == null) {
29
throw Error("value must be specified");
30
}
31
32
await callback2(db._query, {
33
query: "INSERT INTO user_tracking",
34
values: {
35
"account_id :: UUID": account_id,
36
"time :: TIMESTAMP": "NOW()",
37
"event :: TEXT": event,
38
"value :: JSONB": value,
39
"expire :: TIMESTAMP": expire_time(30 * 24 * 60 * 60),
40
},
41
});
42
}
43
44