Skip to content

Commit

Permalink
commented out email + keyword search in items for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
NwinNwin committed Jan 21, 2025
2 parents 9fa1e82 + b2fbaad commit ab6babe
Show file tree
Hide file tree
Showing 20 changed files with 641 additions and 534 deletions.
112 changes: 67 additions & 45 deletions packages/functions/src/routes/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,29 @@ itemsRouter.post("/", async (req, res) => {
ishelped,
} = req.body;

if (!isPositionWithinBounds(location[0], location[1])) {
res.json("ITEM OUT OF BOUNDS (UCI ONLY)");
// Validate location
if (!location || !Array.isArray(location) || location.length !== 2) {
return res.status(400).json({
error: "Invalid location. Please provide a valid latitude and longitude.",
});
}

const item = await client.query(
`INSERT INTO ${itemsTable} (name, description, type, islost, location, date, itemdate, email, image, isresolved, ishelped) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *`,
const [latitude, longitude] = location;

// Check if the location values are numbers and within bounds
if (
typeof latitude !== "number" ||
typeof longitude !== "number" ||
!isPositionWithinBounds(latitude, longitude)
) {
return res.status(400).json({
error: "ITEM OUT OF BOUNDS (UCI ONLY)",
});
}

// Add item to the database
await client.query(
`INSERT INTO ${itemsTable} (name, description, type, islost, location, date, itemdate, email, image, isresolved, ishelped) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`,
[
name,
description,
Expand All @@ -48,61 +65,66 @@ itemsRouter.post("/", async (req, res) => {
]
);

// query to get user emails subscribed to relevant keywords
const subscribers = await client.query(
`SELECT emails
FROM ${searchesTable}
WHERE keyword IN ($1, $2, $3);`,
[name, description, type]
);

// add emails to set to remove duplicates
const emailSet = new Set();
subscribers.rows.forEach(row => {
row.emails.forEach(email => {
uniqueEmails.add(email);
});
});

const uniqueEmails = Array.from(emailSet);
// // query to get user emails subscribed to relevant keywords
// const subscribers = await client.query(
// `SELECT emails
// FROM ${searchesTable}
// WHERE keyword IN ($1, $2, $3);`,
// [name, description, type]
// );

// // add emails to set to remove duplicates
// const emailSet = new Set();
// subscribers.rows.forEach(row => {
// row.emails.forEach(email => {
// uniqueEmails.add(email);
// });
// });

// const uniqueEmails = Array.from(emailSet);
// console.log(uniqueEmails);

res.json(item.rows[0]); // send the response immediately after adding the item
let contentString = "";
// res.json(item.rows[0]); // send the response immediately after adding the item
// let contentString = "";

// COMMENT OUT FOR TESTING PURPOSES
if (process.env.NODE_ENV === "production") {
function sendDelayedEmail(index) {
if (index >= uniqueEmails.length) return;
// // COMMENT OUT FOR TESTING PURPOSES
// if (process.env.NODE_ENV === "production") {
// function sendDelayedEmail(index) {
// if (index >= uniqueEmails.length) return;

let email = uniqueEmails[index].email;
contentString += `A new item, ${name}, is added to ZotnFound!`;
// let email = uniqueEmails[index].email;
// contentString += `A new item, ${name}, is added to ZotnFound!`;

const dynamicContent = {
content: contentString,
image: image,
url: `https://zotnfound.com/${item.rows[0].id}`,
};
// const dynamicContent = {
// content: contentString,
// image: image,
// url: `https://zotnfound.com/${item.rows[0].id}`,
// };

// const customizedTemplate = template
// .replace("{{content}}", dynamicContent.content)
// .replace("{{image}}", dynamicContent.image)
// .replace("{{url}}", dynamicContent.url);
// // const customizedTemplate = template
// // .replace("{{content}}", dynamicContent.content)
// // .replace("{{image}}", dynamicContent.image)
// // .replace("{{url}}", dynamicContent.url);

// sendEmail(email, "A nearby item was added.", customizedTemplate);
// // sendEmail(email, "A nearby item was added.", customizedTemplate);

contentString = "";
console.log("sent " + email);
setTimeout(() => sendDelayedEmail(index + 1), 500); // recursive call to iterate through all user emails
}
// contentString = "";
// console.log("sent " + email);
// setTimeout(() => sendDelayedEmail(index + 1), 500); // recursive call to iterate through all user emails
// }

sendDelayedEmail(0);
}
// sendDelayedEmail(0);
// }
// Send a success response
res.status(200).json({ message: "Item was added successfully." });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
});



itemsRouter.get("/", async (req, res) => {
try {
// Retrieve the User-Email header
Expand Down
12 changes: 11 additions & 1 deletion packages/functions/src/routes/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import client from "../server/db.js";
const leaderboardRouter = express.Router();
// const middleware = require("../middleware/index.js");

// add a user to leaderboard
leaderboardRouter.post("/", async (req, res) => {
try {
const { email, points } = req.body; // Get email and points from request body
Expand All @@ -13,6 +12,17 @@ leaderboardRouter.post("/", async (req, res) => {
return res.status(400).send("Email and points are required");
}

// Check if the email already exists in the database
const existingUser = await client.query(
`SELECT * FROM ${leaderboardTable} WHERE email = $1`,
[email]
);

if (existingUser.rows.length > 0) {
return res.status(400).send("Email already exists in the leaderboard");
}

// Insert the new email and points into the leaderboard
await client.query(
`INSERT INTO ${leaderboardTable} (email, points) VALUES ($1, $2)`,
[email, points]
Expand Down
1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@testing-library/user-event": "^13.5.0",
"@vitejs/plugin-react": "^4.2.1",
"axios": "^1.4.0",
"browser-image-compression": "^2.0.2",
"dotenv": "^16.1.4",
"firebase": "^9.23.0",
"firebase-auth": "^0.1.2",
Expand Down
5 changes: 4 additions & 1 deletion packages/web/src/assets/logos/others_black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions packages/web/src/assets/logos/others_white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/web/src/components/AboutPage/AboutPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function AboutPage() {
});

getLeaderboardCount().then((leaderboardData) => {
setLeaderboardCount(leaderboardData.data);
setLeaderboardCount(leaderboardData.data + 500);
});
}, []);

Expand Down
10 changes: 8 additions & 2 deletions packages/web/src/components/AboutPage/StatCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ const AboutSection = () => (
Origin of ZotnFound
</Text>
<Text>
Many people are constantly losing their belongings, whether that be their phones, keys, or water bottles. This is especially true for UCI students on the UCI subreddit, where there are countless posts being created about lost and found items. Due to this problem, we decided to take matters into our own hands and created an Instagram account to help lost items return back to their original owners. We have so far helped over 10 people and gained over 300+ followers.
Many people are constantly losing their belongings, whether that be
their phones, keys, or water bottles. This is especially true for UCI
students on the UCI subreddit, where there are countless posts being
created about lost and found items. Due to this problem, we decided to
take matters into our own hands and created an Instagram account to help
lost items return back to their original owners. We have so far helped
over 10 people and gained over 300+ followers.
</Text>
</Box>
<Image
Expand All @@ -128,4 +134,4 @@ const Footer = () => (
>
<Text>&copy; 2023 ZotnFound. All rights reserved.</Text>
</Flex>
);
);
Loading

0 comments on commit ab6babe

Please sign in to comment.