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/next/pages/api/v2/accounts/set-name.ts
Views: 687
1
/*
2
Set account {user/first/last} name.
3
*/
4
5
import userQuery from "@cocalc/database/user-query";
6
import userIsInGroup from "@cocalc/server/accounts/is-in-group";
7
8
import getAccountId from "lib/account/get-account";
9
import getParams from "lib/api/get-params";
10
11
import { apiRoute, apiRouteOperation } from "lib/api";
12
import { SuccessStatus } from "lib/api/status";
13
import {
14
SetAccountNameInputSchema,
15
SetAccountNameOutputSchema,
16
} from "lib/api/schema/accounts/set-name";
17
18
async function handle(req, res) {
19
try {
20
await get(req);
21
res.json(SuccessStatus);
22
} catch (err) {
23
res.json({ error: `${err.message ? err.message : err}` });
24
return;
25
}
26
}
27
28
async function get(req) {
29
const client_account_id = await getAccountId(req);
30
31
if (client_account_id == null) {
32
throw Error("Must be signed in to edit account name.");
33
}
34
35
const { username, first_name, last_name, account_id } = getParams(req);
36
37
// This user MUST be an admin:
38
if (account_id && !(await userIsInGroup(client_account_id, "admin"))) {
39
throw Error(
40
"The `account_id` field may only be specified by account administrators.",
41
);
42
}
43
44
return userQuery({
45
account_id: account_id || client_account_id,
46
query: {
47
accounts: {
48
// Any provided values must be non-empty in order for userQuery to SET values
49
// instead of fetching them.
50
//
51
...(username && { name: username }),
52
...(first_name && { first_name }),
53
...(last_name && { last_name }),
54
},
55
},
56
});
57
}
58
59
export default apiRoute({
60
setName: apiRouteOperation({
61
method: "POST",
62
openApiOperation: {
63
tags: ["Accounts", "Admin"],
64
},
65
})
66
.input({
67
contentType: "application/json",
68
body: SetAccountNameInputSchema,
69
})
70
.outputs([
71
{
72
status: 200,
73
contentType: "application/json",
74
body: SetAccountNameOutputSchema,
75
},
76
])
77
.handler(handle),
78
});
79
80