CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/account/licenses/util.ts
Views: 687
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 { Map } from "immutable";
7
8
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
9
import { webapp_client } from "@cocalc/frontend/webapp-client";
10
import { field_cmp, cmp_Date } from "@cocalc/util/misc";
11
import { SiteLicense } from "@cocalc/util/types/site-licenses";
12
13
type FunctionType = () => Promise<SiteLicense[]>;
14
15
export const getManagedLicenses: FunctionType = reuseInFlight(async () => {
16
const v = (
17
await webapp_client.async_query({
18
query: {
19
manager_site_licenses: [
20
{
21
id: null,
22
title: null,
23
description: null,
24
info: null,
25
expires: null,
26
activates: null,
27
created: null,
28
last_used: null,
29
managers: null,
30
upgrades: null,
31
quota: null,
32
run_limit: null,
33
apply_limit: null,
34
},
35
],
36
},
37
})
38
).query.manager_site_licenses;
39
// Sort by created with newest first
40
return v.sort((a, b) => cmp_Date(b.created, a.created));
41
});
42
43
// Return list of id's of projects that have at least one license applied to
44
// them. The license may or may not be valid, in use, etc.
45
export function projects_with_licenses(
46
project_map: undefined | Map<string, any>,
47
): { last_edited?: Date; project_id: string; num_licenses: number }[] {
48
if (project_map == null) return [];
49
const v: {
50
last_edited?: Date;
51
project_id: string;
52
num_licenses: number;
53
}[] = [];
54
for (const y of project_map) {
55
const [project_id, project] = y;
56
const num_licenses = project.get("site_license")?.size;
57
if (num_licenses > 0) {
58
v.push({
59
last_edited: project.get("last_edited"),
60
project_id,
61
num_licenses,
62
});
63
}
64
}
65
v.sort(field_cmp("last_edited"));
66
v.reverse();
67
return v;
68
}
69
70