2020-07-23 11:57:44 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-07-22 08:53:29 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-07-23 11:57:44 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
2020-09-22 03:36:45 +03:00
|
|
|
import * as Social from "~/node_common/social";
|
2020-10-27 07:41:42 +03:00
|
|
|
import * as ViewerManager from "~/node_common/managers/viewer";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
import BCrypt from "bcrypt";
|
2020-07-17 13:24:20 +03:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2020-07-22 13:51:40 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
if (!id) {
|
2020-10-27 07:41:42 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_UPDATE", error: true });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const user = await Data.getUserById({
|
|
|
|
id,
|
2020-07-22 08:53:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!user) {
|
2020-10-27 07:41:42 +03:00
|
|
|
return res.status(404).send({ decorator: "SERVER_USER_UPDATE_USER_NOT_FOUND", error: true });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.error) {
|
2020-10-27 07:41:42 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USER_UPDATE_USER_NOT_FOUND", error: true });
|
2020-07-22 08:53:29 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 07:41:42 +03:00
|
|
|
let unsafeResponse;
|
2020-07-22 10:41:29 +03:00
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
if (req.body.username) {
|
|
|
|
const existing = await Data.getUserByUsername({
|
2020-11-13 01:36:20 +03:00
|
|
|
username: req.body.username.toLowerCase(),
|
2020-07-22 13:51:40 +03:00
|
|
|
});
|
|
|
|
|
2020-11-13 01:36:20 +03:00
|
|
|
if (existing && existing.id !== id) {
|
2020-10-27 07:41:42 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_USERNAME_IS_TAKEN", error: true });
|
2020-09-09 20:56:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 13:51:23 +03:00
|
|
|
if (req.body.type === "SAVE_DEFAULT_ARCHIVE_CONFIG") {
|
|
|
|
let b;
|
|
|
|
try {
|
|
|
|
b = await Utilities.getBucketAPIFromUserToken({
|
|
|
|
user,
|
|
|
|
bucketName: "data",
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/pages/api/users/update.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `Utilities.getBucketAPIFromUserToken`,
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(500).send({ decorator: "SERVER_FAILED_TO_GET_BUCKET", error: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const configResponse = await b.buckets.setDefaultArchiveConfig(b.bucketKey, req.body.config);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
Social.sendTextileSlackMessage({
|
|
|
|
file: "/pages/api/users/update.js",
|
|
|
|
user,
|
|
|
|
message: e.message,
|
|
|
|
code: e.code,
|
|
|
|
functionName: `b.buckets.setDefaultArchiveConfig`,
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(500).send({ decorator: "SERVER_DEFAULT_ARCHIVE_CONFIG", error: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 11:57:44 +03:00
|
|
|
if (req.body.type == "CHANGE_PASSWORD") {
|
|
|
|
if (!Validations.password(req.body.password)) {
|
2020-10-27 07:41:42 +03:00
|
|
|
return res.status(500).send({ decorator: "SERVER_INVALID_PASSWORD", error: true });
|
2020-07-23 11:57:44 +03:00
|
|
|
}
|
|
|
|
|
2020-08-11 08:15:39 +03:00
|
|
|
const rounds = Number(Environment.LOCAL_PASSWORD_ROUNDS);
|
|
|
|
const salt = await BCrypt.genSalt(rounds);
|
|
|
|
const hash = await Utilities.encryptPassword(req.body.password, salt);
|
2020-07-23 11:57:44 +03:00
|
|
|
|
2020-10-27 07:41:42 +03:00
|
|
|
unsafeResponse = await Data.updateUserById({
|
2020-07-23 11:57:44 +03:00
|
|
|
id: user.id,
|
|
|
|
salt,
|
2020-08-11 08:15:39 +03:00
|
|
|
password: hash,
|
2020-07-23 11:57:44 +03:00
|
|
|
});
|
2020-11-13 01:36:20 +03:00
|
|
|
} else {
|
|
|
|
unsafeResponse = await Data.updateUserById({
|
|
|
|
id: user.id,
|
|
|
|
username: req.body.username ? req.body.username.toLowerCase() : user.username,
|
|
|
|
data: { ...user.data, ...req.body.data },
|
|
|
|
});
|
2020-07-23 11:57:44 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 01:36:20 +03:00
|
|
|
if (unsafeResponse && !unsafeResponse.error) {
|
2020-10-27 07:41:42 +03:00
|
|
|
ViewerManager.hydratePartialViewer(unsafeResponse);
|
2020-11-13 01:36:20 +03:00
|
|
|
|
|
|
|
if (
|
|
|
|
user.username !== unsafeResponse.username ||
|
|
|
|
user.data.name !== unsafeResponse.data.name ||
|
|
|
|
user.data.photo !== unsafeResponse.data.photo
|
|
|
|
) {
|
|
|
|
SearchManager.updateUser(unsafeResponse, "EDIT");
|
|
|
|
}
|
2020-10-27 07:41:42 +03:00
|
|
|
}
|
|
|
|
|
2020-09-03 03:21:29 +03:00
|
|
|
return res.status(200).send({ decorator: "SERVER_USER_UPDATE" });
|
2020-07-17 13:24:20 +03:00
|
|
|
};
|