Skip to content

Commit

Permalink
Updated for need for bbf
Browse files Browse the repository at this point in the history
  • Loading branch information
wqyeo committed Mar 19, 2024
1 parent 387f987 commit 559a618
Show file tree
Hide file tree
Showing 27 changed files with 93 additions and 93 deletions.
4 changes: 2 additions & 2 deletions SpyGamersApi/src/controllers/accounts/changeUsername.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const changeUsername = async (request: FastifyRequest, reply: FastifyRepl
const { auth_token, new_username } = request.body as { auth_token: string; new_username: string; };
const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

const existingUser = await prisma.account.findFirst({
Expand All @@ -19,7 +19,7 @@ export const changeUsername = async (request: FastifyRequest, reply: FastifyRepl
});

if (existingUser != null) {
return reply.status(406).send({ status: "USERNAME_TAKEN" });
return reply.status(200).send({ status: "USERNAME_TAKEN" });
}

await prisma.account.update({
Expand Down
2 changes: 1 addition & 1 deletion SpyGamersApi/src/controllers/accounts/checkAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const checkAuth = async (request: FastifyRequest, reply: FastifyReply) =>
const { auth_token } = request.body as { auth_token: string; }
const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

const result = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const getLatestConversation = async (request: FastifyRequest, reply: Fast
// Find the account associated with the provided auth token
const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
reply.status(401).send({ status: "BAD_AUTH" });
reply.status(200).send({ status: "BAD_AUTH" });
return;
}

Expand Down
4 changes: 2 additions & 2 deletions SpyGamersApi/src/controllers/accounts/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const login = async (request: FastifyRequest, reply: FastifyReply) => {
// Verify the password
const match = await bcrypt.compare(password, account.password);
if (!match) {
return reply.status(401).send({ status: "PASSWORD_INVALID" });
return reply.status(200).send({ status: "PASSWORD_INVALID" });
}

const sessionToken = randomBytes(32).toString('hex');
Expand All @@ -39,7 +39,7 @@ export const login = async (request: FastifyRequest, reply: FastifyReply) => {
reply.status(201).send({ status: "SUCCESS", session_token: sessionToken, account_id: account.id, timezone_code: account.timezone_code });
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2025") {
return reply.status(404).send({ status: `USERNAME_INVALID` });
return reply.status(200).send({ status: `USERNAME_INVALID` });
}

reply.status(500).send({ status: "FAILURE" });
Expand Down
12 changes: 6 additions & 6 deletions SpyGamersApi/src/controllers/accounts/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ export const register = async (request: FastifyRequest, reply: FastifyReply) =>
const { username, email, password } = request.body as { username: string; email: string; password: string };

if (password.length < 4) {
return reply.status(400).send({ status: "PASSWORD_TOO_SHORT" });
return reply.status(200).send({ status: "PASSWORD_TOO_SHORT" });
}

if (password.length > 16) {
return reply.status(400).send({ status: "PASSWORD_TOO_LONG" });
return reply.status(200).send({ status: "PASSWORD_TOO_LONG" });
}

if (username.length > 128) {
return reply.status(400).send({ status: "USERNAME_TOO_LONG" });
return reply.status(200).send({ status: "USERNAME_TOO_LONG" });
}

if (!regexValidateEmail(email)) {
return reply.status(400).send({ status: "INVALID_EMAIL" });
return reply.status(200).send({ status: "INVALID_EMAIL" });
}

if (email.length > 256) {
return reply.status(400).send({ status: "EMAIL_TOO_LONG" });
return reply.status(200).send({ status: "EMAIL_TOO_LONG" });
}

// TODO: SMTP to verify email?
Expand All @@ -56,7 +56,7 @@ export const register = async (request: FastifyRequest, reply: FastifyReply) =>

const isUniqueConstraintViolation = error.code === "P2002";
if (isUniqueConstraintViolation) {
return reply.status(400).send({ status: `${error.meta?.target}_TAKEN`.toUpperCase() });
return reply.status(200).send({ status: `${error.meta?.target}_TAKEN`.toUpperCase() });
}
}

Expand Down
2 changes: 1 addition & 1 deletion SpyGamersApi/src/controllers/accounts/setTimezone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const setTimezone = async (request: FastifyRequest, reply: FastifyReply)

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

await prisma.account.update({
Expand Down
14 changes: 7 additions & 7 deletions SpyGamersApi/src/controllers/groups/addMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const addMember = async (request: FastifyRequest, reply: FastifyReply) =>

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

// Check if group exists
Expand All @@ -22,7 +22,7 @@ export const addMember = async (request: FastifyRequest, reply: FastifyReply) =>
});

if (!groupExists){
return reply.status(406).send({ status: "GROUP_NOT_EXISTS" });
return reply.status(200).send({ status: "GROUP_NOT_EXISTS" });
}

// Check if the account is an admin member of the specified group
Expand All @@ -34,11 +34,11 @@ export const addMember = async (request: FastifyRequest, reply: FastifyReply) =>
});

if (!isMember) {
return reply.status(406).send({ status: "NOT_GROUP_MEMBER" });
return reply.status(200).send({ status: "NOT_GROUP_MEMBER" });
}

if (!isMember.is_admin) {
return reply.status(406).send({ status: "NOT_GROUP_ADMIN" });
return reply.status(200).send({ status: "NOT_GROUP_ADMIN" });
}

// Check if target is already a member:
Expand All @@ -50,13 +50,13 @@ export const addMember = async (request: FastifyRequest, reply: FastifyReply) =>
});

if (targetIsMember) {
return reply.status(406).send({ status: "EXISTING_MEMBER"})
return reply.status(200).send({ status: "EXISTING_MEMBER"})
}

// Check if target and user is friends
const friendship = await searchFriendship(prisma, account.id, target_user_id);
if (friendship == undefined || !friendship.request_accepted) {
return reply.status(406).send({ status: "NOT_FRIENDS" })
return reply.status(200).send({ status: "NOT_FRIENDS" })
}

// Add into group...
Expand All @@ -68,7 +68,7 @@ export const addMember = async (request: FastifyRequest, reply: FastifyReply) =>
}
})

reply.status(200).send({ status: "SUCCESS" });
reply.status(201).send({ status: "SUCCESS" });
} catch (error) {
console.error("Error:", error);
reply.status(500).send({ status: "FAILURE" });
Expand Down
10 changes: 5 additions & 5 deletions SpyGamersApi/src/controllers/groups/changeInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const changeInformation = async (request: FastifyRequest, reply: FastifyR

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

// Check if group exists
Expand All @@ -28,7 +28,7 @@ export const changeInformation = async (request: FastifyRequest, reply: FastifyR
});

if (!groupExists){
return reply.status(406).send({ status: "GROUP_NOT_EXISTS" });
return reply.status(200).send({ status: "GROUP_NOT_EXISTS" });
}

// Check if the account is an admin member of the specified group
Expand All @@ -40,15 +40,15 @@ export const changeInformation = async (request: FastifyRequest, reply: FastifyR
});

if (!isMember) {
return reply.status(406).send({ status: "NOT_GROUP_MEMBER" });
return reply.status(200).send({ status: "NOT_GROUP_MEMBER" });
}

if (!isMember.is_admin) {
return reply.status(406).send({ status: "NOT_GROUP_ADMIN" });
return reply.status(200).send({ status: "NOT_GROUP_ADMIN" });
}

if (!description && !group_name && !public_status) {
return reply.status(201).send({ status: "NOTHING_CHANGED" });
return reply.status(200).send({ status: "NOTHING_CHANGED" });
}

if (!description || isStringEmptyOrWhitespace(description)) {
Expand Down
6 changes: 3 additions & 3 deletions SpyGamersApi/src/controllers/groups/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export const createGroup = async (request: FastifyRequest, reply: FastifyReply)

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (account == undefined) {
reply.status(401).send({ status: "BAD_AUTH" });
reply.status(200).send({ status: "BAD_AUTH" });
}

if (isStringEmptyOrWhitespace(group_name)) {
reply.status(406).send({ status: "EMPTY_GROUP_NAME" });
reply.status(200).send({ status: "EMPTY_GROUP_NAME" });
}

if (group_name.length > 128) {
reply.status(406).send({ status: "NAME_TOO_LONG" });
reply.status(200).send({ status: "NAME_TOO_LONG" });
}

// Create the new group
Expand Down
8 changes: 4 additions & 4 deletions SpyGamersApi/src/controllers/groups/deleteMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const deleteGroupMessage = async (request: FastifyRequest, reply: Fastify

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "FAILURE" });
return reply.status(200).send({ status: "FAILURE" });
}

const targetMessage = await prisma.groupMessage.findFirst({
Expand All @@ -23,15 +23,15 @@ export const deleteGroupMessage = async (request: FastifyRequest, reply: Fastify
});

if (targetMessage == null) {
return reply.status(406).send({ status: "MESSAGE_NOT_FOUND" });
return reply.status(200).send({ status: "MESSAGE_NOT_FOUND" });
}

if (targetMessage.sender_id != account.id) {
return reply.status(406).send({ status: "NOT_MESSAGE_AUTHOR" });
return reply.status(200).send({ status: "NOT_MESSAGE_AUTHOR" });
}

if (targetMessage.is_deleted) {
return reply.status(406).send({ status: "ALREADY_DELETED" });
return reply.status(200).send({ status: "ALREADY_DELETED" });
}

await prisma.groupMessage.update({
Expand Down
8 changes: 4 additions & 4 deletions SpyGamersApi/src/controllers/groups/editMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export const editGroupMessage = async (request: FastifyRequest, reply: FastifyRe

try {
if (isStringEmptyOrWhitespace(new_content)){
return reply.status(406).send({ status: "EMPTY_CONTENT" });
return reply.status(200).send({ status: "EMPTY_CONTENT" });
}

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

const targetMessage = await prisma.groupMessage.findFirst({
Expand All @@ -29,11 +29,11 @@ export const editGroupMessage = async (request: FastifyRequest, reply: FastifyRe
});

if (targetMessage == null) {
return reply.status(406).send({ status: "MESSAGE_NOT_FOUND" });
return reply.status(200).send({ status: "MESSAGE_NOT_FOUND" });
}

if (targetMessage.sender_id != account.id) {
return reply.status(406).send({ status: "NOT_MESSAGE_AUTHOR" });
return reply.status(200).send({ status: "NOT_MESSAGE_AUTHOR" });
}

await prisma.groupMessage.update({
Expand Down
2 changes: 1 addition & 1 deletion SpyGamersApi/src/controllers/groups/getAccountGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const getAccountGroups = async (request: FastifyRequest, reply: FastifyRe

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

let groups;
Expand Down
6 changes: 3 additions & 3 deletions SpyGamersApi/src/controllers/groups/getMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const getGroupMembers = async (request: FastifyRequest, reply: FastifyRep

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

// Check if group exists
Expand All @@ -21,7 +21,7 @@ export const getGroupMembers = async (request: FastifyRequest, reply: FastifyRep
});

if (!groupExists){
return reply.status(406).send({ status: "GROUP_NOT_EXISTS" });
return reply.status(200).send({ status: "GROUP_NOT_EXISTS" });
}

// Check if the account is a member of the specified group
Expand All @@ -33,7 +33,7 @@ export const getGroupMembers = async (request: FastifyRequest, reply: FastifyRep
});

if (!isMember) {
return reply.status(406).send({ status: "NOT_GROUP_MEMBER" });
return reply.status(200).send({ status: "NOT_GROUP_MEMBER" });
}

// Fetch all accounts that are members of the specified group
Expand Down
6 changes: 3 additions & 3 deletions SpyGamersApi/src/controllers/groups/getMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const getGroupMessages = async (request: FastifyRequest, reply: FastifyRe

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

// Check if group exists
Expand All @@ -22,7 +22,7 @@ export const getGroupMessages = async (request: FastifyRequest, reply: FastifyRe
});

if (!groupExists){
return reply.status(406).send({ status: "GROUP_NOT_EXISTS" });
return reply.status(200).send({ status: "GROUP_NOT_EXISTS" });
}

// Check if the account is a member of the specified group
Expand All @@ -34,7 +34,7 @@ export const getGroupMessages = async (request: FastifyRequest, reply: FastifyRe
});

if (!isMember) {
return reply.status(406).send({ status: "NOT_GROUP_MEMBER" });
return reply.status(200).send({ status: "NOT_GROUP_MEMBER" });
}

// Find the latest messages of the group
Expand Down
10 changes: 5 additions & 5 deletions SpyGamersApi/src/controllers/groups/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const joinGroup = async (request: FastifyRequest, reply: FastifyReply) =>

const account = await tryFindAccountBySessionToken(auth_token, prisma);
if (!account) {
return reply.status(401).send({ status: "BAD_AUTH" });
return reply.status(200).send({ status: "BAD_AUTH" });
}

// Check if group exists
Expand All @@ -21,7 +21,7 @@ export const joinGroup = async (request: FastifyRequest, reply: FastifyReply) =>
});

if (!group){
return reply.status(406).send({ status: "GROUP_NOT_EXISTS" });
return reply.status(200).send({ status: "GROUP_NOT_EXISTS" });
}
// Check if target is already a member:
const targetIsMember = await prisma.groupMember.findFirst({
Expand All @@ -32,11 +32,11 @@ export const joinGroup = async (request: FastifyRequest, reply: FastifyReply) =>
});

if (targetIsMember) {
return reply.status(406).send({ status: "EXISTING_MEMBER"})
return reply.status(420006).send({ status: "EXISTING_MEMBER"})
}

if (!group.is_public) {
return reply.status(406).send({ status: "GROUP_NOT_PUBLIC" });
return reply.status(200).send({ status: "GROUP_NOT_PUBLIC" });
}

// Add into group...
Expand All @@ -48,7 +48,7 @@ export const joinGroup = async (request: FastifyRequest, reply: FastifyReply) =>
}
})

reply.status(200).send({ status: "SUCCESS" });
reply.status(201).send({ status: "SUCCESS" });
} catch (error) {
console.error("Error:", error);
reply.status(500).send({ status: "FAILURE" });
Expand Down
Loading

0 comments on commit 559a618

Please sign in to comment.