add sqlite compatibility + fix whitespace bug collections
This commit is contained in:
@@ -17,14 +17,23 @@ export default async function getUser({
|
||||
id: params.lookupId,
|
||||
username: params.lookupUsername?.toLowerCase(),
|
||||
},
|
||||
include: {
|
||||
whitelistedUsers: {
|
||||
select: {
|
||||
username: true
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!user) return { response: "User not found.", status: 404 };
|
||||
|
||||
const whitelistedUsernames = user.whitelistedUsers?.map(usernames => usernames.username);
|
||||
|
||||
if (
|
||||
!isSelf &&
|
||||
user?.isPrivate &&
|
||||
!user.whitelistedUsers.includes(username.toLowerCase())
|
||||
!whitelistedUsernames.includes(username.toLowerCase())
|
||||
) {
|
||||
return { response: "This profile is private.", status: 401 };
|
||||
}
|
||||
@@ -33,7 +42,7 @@ export default async function getUser({
|
||||
|
||||
const data = isSelf
|
||||
? // If user is requesting its own data
|
||||
lessSensitiveInfo
|
||||
{...lessSensitiveInfo, whitelistedUsers: whitelistedUsernames}
|
||||
: {
|
||||
// If user is requesting someone elses data
|
||||
id: lessSensitiveInfo.id,
|
||||
|
||||
@@ -106,14 +106,56 @@ export default async function updateUser(
|
||||
username: user.username.toLowerCase(),
|
||||
email: user.email?.toLowerCase(),
|
||||
isPrivate: user.isPrivate,
|
||||
whitelistedUsers: user.whitelistedUsers,
|
||||
password:
|
||||
user.newPassword && user.newPassword !== ""
|
||||
? newHashedPassword
|
||||
: undefined,
|
||||
},
|
||||
include: {
|
||||
whitelistedUsers: true
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const { whitelistedUsers, password, ...userInfo } = updatedUser;
|
||||
|
||||
// If user.whitelistedUsers is not provided, we will assume the whitelistedUsers should be removed
|
||||
const newWhitelistedUsernames: string[] = user.whitelistedUsers || [];
|
||||
|
||||
// Get the current whitelisted usernames
|
||||
const currentWhitelistedUsernames: string[] = whitelistedUsers.map((user) => user.username);
|
||||
|
||||
// Find the usernames to be deleted (present in current but not in new)
|
||||
const usernamesToDelete: string[] = currentWhitelistedUsernames.filter(
|
||||
(username) => !newWhitelistedUsernames.includes(username)
|
||||
);
|
||||
|
||||
// Find the usernames to be created (present in new but not in current)
|
||||
const usernamesToCreate: string[] = newWhitelistedUsernames.filter(
|
||||
(username) => !currentWhitelistedUsernames.includes(username) && username.trim() !== ''
|
||||
);
|
||||
|
||||
// Delete whitelistedUsers that are not present in the new list
|
||||
await prisma.whitelistedUser.deleteMany({
|
||||
where: {
|
||||
userId: sessionUser.id,
|
||||
username: {
|
||||
in: usernamesToDelete,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Create new whitelistedUsers that are not in the current list, no create many ;(
|
||||
for (const username of usernamesToCreate) {
|
||||
await prisma.whitelistedUser.create({
|
||||
data: {
|
||||
username,
|
||||
userId: sessionUser.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
||||
const PRICE_ID = process.env.PRICE_ID;
|
||||
|
||||
@@ -125,10 +167,9 @@ export default async function updateUser(
|
||||
user.email as string
|
||||
);
|
||||
|
||||
const { password, ...userInfo } = updatedUser;
|
||||
|
||||
const response: Omit<AccountSettings, "password"> = {
|
||||
...userInfo,
|
||||
whitelistedUsers: newWhitelistedUsernames,
|
||||
profilePic: `/api/avatar/${userInfo.id}?${Date.now()}`,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user