2023-05-20 14:25:00 -05:00
|
|
|
import { prisma } from "@/lib/api/db";
|
|
|
|
import { AccountSettings } from "@/types/global";
|
2023-05-22 15:29:24 -05:00
|
|
|
import bcrypt from "bcrypt";
|
2023-07-01 09:11:39 -05:00
|
|
|
import removeFile from "@/lib/api/storage/removeFile";
|
|
|
|
import createFile from "@/lib/api/storage/createFile";
|
2023-05-20 14:25:00 -05:00
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
export default async function updateUser(
|
|
|
|
user: AccountSettings,
|
|
|
|
userId: number
|
|
|
|
) {
|
2023-07-12 13:26:34 -05:00
|
|
|
if (!user.username || !user.email)
|
|
|
|
return {
|
|
|
|
response: "Username/Email invalid.",
|
|
|
|
status: 400,
|
|
|
|
};
|
2023-06-09 17:31:14 -05:00
|
|
|
|
|
|
|
// Avatar Settings
|
|
|
|
|
2023-05-22 07:20:48 -05:00
|
|
|
const profilePic = user.profilePic;
|
|
|
|
|
2023-06-08 08:39:22 -05:00
|
|
|
if (profilePic.startsWith("data:image/jpeg;base64")) {
|
|
|
|
if (user.profilePic.length < 1572864) {
|
2023-05-22 07:20:48 -05:00
|
|
|
try {
|
|
|
|
const base64Data = profilePic.replace(/^data:image\/jpeg;base64,/, "");
|
|
|
|
|
2023-07-01 09:11:39 -05:00
|
|
|
await createFile({
|
|
|
|
filePath: `uploads/avatar/${userId}.jpg`,
|
|
|
|
data: base64Data,
|
|
|
|
isBase64: true,
|
2023-05-22 07:20:48 -05:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.log("Error saving image:", err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("A file larger than 1.5MB was uploaded.");
|
2023-06-09 17:31:14 -05:00
|
|
|
return {
|
|
|
|
response: "A file larger than 1.5MB was uploaded.",
|
|
|
|
status: 400,
|
|
|
|
};
|
2023-05-22 07:20:48 -05:00
|
|
|
}
|
2023-06-08 08:39:22 -05:00
|
|
|
} else if (profilePic == "") {
|
2023-07-01 09:11:39 -05:00
|
|
|
removeFile({ filePath: `uploads/avatar/${userId}.jpg` });
|
2023-05-22 07:20:48 -05:00
|
|
|
}
|
2023-05-20 14:25:00 -05:00
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
// Other settings
|
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
const saltRounds = 10;
|
|
|
|
const newHashedPassword = bcrypt.hashSync(user.newPassword || "", saltRounds);
|
|
|
|
|
2023-05-20 14:25:00 -05:00
|
|
|
const updatedUser = await prisma.user.update({
|
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
name: user.name,
|
2023-07-08 05:35:43 -05:00
|
|
|
username: user.username.toLowerCase(),
|
2023-07-12 13:26:34 -05:00
|
|
|
email: user.email?.toLowerCase(),
|
2023-05-22 23:08:16 -05:00
|
|
|
isPrivate: user.isPrivate,
|
2023-05-20 14:25:00 -05:00
|
|
|
whitelistedUsers: user.whitelistedUsers,
|
2023-07-12 13:26:34 -05:00
|
|
|
password:
|
|
|
|
user.newPassword && user.newPassword !== ""
|
|
|
|
? newHashedPassword
|
|
|
|
: undefined,
|
2023-05-20 14:25:00 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-05-27 14:05:07 -05:00
|
|
|
const { password, ...userInfo } = updatedUser;
|
2023-05-22 07:20:48 -05:00
|
|
|
|
2023-06-08 08:39:22 -05:00
|
|
|
const response: Omit<AccountSettings, "password"> = {
|
|
|
|
...userInfo,
|
|
|
|
profilePic: `/api/avatar/${userInfo.id}?${Date.now()}`,
|
|
|
|
};
|
|
|
|
|
|
|
|
return { response, status: 200 };
|
2023-05-20 14:25:00 -05:00
|
|
|
}
|