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