SpaceNinjaServer/src/controllers/custom/renameAccountController.ts
Sainan cf3007b744
Some checks failed
Build / build (22) (push) Waiting to run
Build Docker image / docker (push) Waiting to run
Build / build (18) (push) Has been cancelled
Build / build (20) (push) Has been cancelled
chore: update config when admin changes their name (#1298)
Reviewed-on: #1298
2025-03-23 09:06:08 -07:00

29 lines
1.0 KiB
TypeScript

import { RequestHandler } from "express";
import { getAccountForRequest, isAdministrator, isNameTaken } from "@/src/services/loginService";
import { config, saveConfig } from "@/src/services/configService";
export const renameAccountController: RequestHandler = async (req, res) => {
const account = await getAccountForRequest(req);
if (typeof req.query.newname == "string") {
if (await isNameTaken(req.query.newname)) {
res.status(409).json("Name already in use");
} else {
if (isAdministrator(account)) {
for (let i = 0; i != config.administratorNames!.length; ++i) {
if (config.administratorNames![i] == account.DisplayName) {
config.administratorNames![i] = req.query.newname;
}
}
await saveConfig();
}
account.DisplayName = req.query.newname;
await account.save();
res.end();
}
} else {
res.status(400).end();
}
};