import getAccountId from "lib/account/get-account";
import getPool from "@cocalc/database/pool";
import getParams from "lib/api/get-params";
export default async function handle(req, res) {
const account_id = await getAccountId(req);
if (account_id == null) {
res.json({ files: [] });
return;
}
let { interval } = getParams(req);
try {
const files = await fileAccess({ account_id, interval });
res.json({ files });
} catch (err) {
res.json({ error: `${err.message}` });
}
}
interface Access {
project_id: string;
title: string;
path: string;
}
async function fileAccess({ account_id, interval }): Promise<Access[]> {
const pool = getPool("medium");
const { rows } = await pool.query(
"SELECT DISTINCT file_access_log.filename AS path, file_access_log.project_id AS project_id, projects.title AS title FROM file_access_log, projects WHERE file_access_log.project_id=projects.project_id AND file_access_log.time >= NOW() - $2::interval AND file_access_log.account_id=$1 ORDER BY title,path",
[account_id, interval ? interval : "1 day"]
);
return rows;
}