replaced email with username

This commit is contained in:
Daniel
2023-07-08 14:05:43 +03:30
parent 3993615071
commit 97ca682c0a
32 changed files with 283 additions and 117 deletions
@@ -16,7 +16,7 @@ export default async function getCollection(userId: number) {
include: {
user: {
select: {
email: true,
username: true,
name: true,
},
},
@@ -42,7 +42,7 @@ export default async function postCollection(
color: collection.color,
members: {
create: collection.members.map((e) => ({
user: { connect: { email: e.user.email.toLowerCase() } },
user: { connect: { username: e.user.username.toLowerCase() } },
canCreate: e.canCreate,
canUpdate: e.canUpdate,
canDelete: e.canDelete,
@@ -57,7 +57,7 @@ export default async function postCollection(
include: {
user: {
select: {
email: true,
username: true,
name: true,
},
},
@@ -43,7 +43,7 @@ export default async function updateCollection(
isPublic: collection.isPublic,
members: {
create: collection.members.map((e) => ({
user: { connect: { email: e.user.email.toLowerCase() } },
user: { connect: { username: e.user.username.toLowerCase() } },
canCreate: e.canCreate,
canUpdate: e.canUpdate,
canDelete: e.canDelete,
@@ -58,7 +58,7 @@ export default async function updateCollection(
include: {
user: {
select: {
email: true,
username: true,
name: true,
id: true,
},
+5 -5
View File
@@ -1,13 +1,13 @@
import { prisma } from "@/lib/api/db";
export default async function getUser(
lookupEmail: string,
lookupUsername: string,
isSelf: boolean,
userEmail: string
username: string
) {
const user = await prisma.user.findUnique({
where: {
email: lookupEmail.toLowerCase(),
username: lookupUsername.toLowerCase(),
},
});
@@ -16,7 +16,7 @@ export default async function getUser(
if (
!isSelf &&
user?.isPrivate &&
!user.whitelistedUsers.includes(userEmail.toLowerCase())
!user.whitelistedUsers.includes(username.toLowerCase())
) {
return { response: "This profile is private.", status: 401 };
}
@@ -30,7 +30,7 @@ export default async function getUser(
// If user is requesting someone elses data
id: unsensitiveInfo.id,
name: unsensitiveInfo.name,
email: unsensitiveInfo.email,
username: unsensitiveInfo.username,
};
return { response: data || null, status: 200 };
+1 -1
View File
@@ -72,7 +72,7 @@ export default async function updateUser(
},
data: {
name: user.name,
email: user.email.toLowerCase(),
username: user.username.toLowerCase(),
isPrivate: user.isPrivate,
whitelistedUsers: user.whitelistedUsers,
},
+12 -12
View File
@@ -1,32 +1,32 @@
import { CollectionIncludingMembersAndLinkCount, Member } from "@/types/global";
import getPublicUserDataByEmail from "./getPublicUserDataByEmail";
import getPublicUserDataByUsername from "./getPublicUserDataByUsername";
import { toast } from "react-hot-toast";
const addMemberToCollection = async (
ownerEmail: string,
memberEmail: string,
ownerUsername: string,
memberUsername: string,
collection: CollectionIncludingMembersAndLinkCount,
setMember: (newMember: Member) => null | undefined
) => {
const checkIfMemberAlreadyExists = collection.members.find((e) => {
const email = e.user.email.toLowerCase();
return email === memberEmail.toLowerCase();
const username = e.user.username.toLowerCase();
return username === memberUsername.toLowerCase();
});
if (
// no duplicate members
!checkIfMemberAlreadyExists &&
// member can't be empty
memberEmail.trim() !== "" &&
memberUsername.trim() !== "" &&
// member can't be the owner
memberEmail.trim() !== ownerEmail
memberUsername.trim() !== ownerUsername
) {
// Lookup, get data/err, list ...
const user = await getPublicUserDataByEmail(
memberEmail.trim().toLowerCase()
const user = await getPublicUserDataByUsername(
memberUsername.trim().toLowerCase()
);
if (user.email) {
if (user.username) {
setMember({
collectionId: collection.id,
userId: user.id,
@@ -35,12 +35,12 @@ const addMemberToCollection = async (
canDelete: false,
user: {
name: user.name,
email: user.email,
username: user.username,
},
});
}
} else if (checkIfMemberAlreadyExists) toast.error("User already exists.");
else if (memberEmail.trim() === ownerEmail)
else if (memberUsername.trim() === ownerUsername)
toast.error("You are already the collection owner.");
};
@@ -1,8 +1,8 @@
import { toast } from "react-hot-toast";
export default async function getPublicUserDataByEmail(email: string) {
export default async function getPublicUserDataByEmail(username: string) {
const response = await fetch(
`/api/routes/users?email=${email.toLowerCase()}`
`/api/routes/users?username=${username.toLowerCase()}`
);
const data = await response.json();