-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from icssc/33-feat-email-subscription-for-cert…
…ain-keywords-or-item-type feat: Email subscription for certain keywords or item type
- Loading branch information
Showing
10 changed files
with
216 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from "express"; | ||
import sendEmail from "../util/sendEmail"; | ||
|
||
|
||
const emailRouter = express.Router(); | ||
|
||
emailRouter.get("/", async (req, res) => { | ||
sendEmail("[email protected]", "test email", "email test"); | ||
res.json("email sent"); | ||
}); | ||
|
||
export default emailRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import express from "express"; | ||
import client from "../server/db.js"; | ||
import { searchesTable } from "../config/db-config.js"; | ||
|
||
const searchRouter = express.Router(); | ||
|
||
// Add a search keyword and/or email | ||
searchRouter.post("/", async (req, res) => { | ||
try { | ||
const { keyword, email } = req.body; | ||
|
||
// search for emails subscribed to certain keyword | ||
const search = await client.query( | ||
`SELECT emails | ||
FROM ${searchesTable} | ||
WHERE keyword = $1`, | ||
[keyword]); | ||
|
||
if (search.rows.length == 1){ | ||
// subscribe user to keyword notification if they aren't already subscribed to it | ||
if (!search.rows[0].emails.includes(email)){ | ||
const item = await client.query( | ||
`UPDATE ${searchesTable} | ||
SET emails = array_append(emails, $1) | ||
WHERE keyword = $2 | ||
RETURNING *`, | ||
[email, keyword]); | ||
res.json(item.rows[0]); | ||
console.log("updated subscribers of", keyword); | ||
} | ||
else { | ||
res.json("email already subscribed to keyword"); | ||
} | ||
} else { // keyword doesn't exist in table yet, add new row | ||
const item = await client.query( | ||
`INSERT INTO ${searchesTable} (keyword, emails) VALUES($1, $2) RETURNING *`, | ||
[keyword, [email]] | ||
); | ||
console.log("inserted new row"); | ||
res.json(item.rows[0]); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}); | ||
|
||
// Remove user's keyword subscription | ||
searchRouter.delete("/", async (req, res) => { | ||
try { | ||
const { keyword, email } = req.body; | ||
|
||
const updatedSubscription = await client.query( | ||
`UPDATE ${searchesTable} | ||
SET emails = array_remove(emails, $1) | ||
WHERE keyword = $2 | ||
RETURNING *`, | ||
[email, keyword] | ||
); | ||
|
||
if (updatedSubscription.rowCount === 0) { | ||
return res.status(404).json({message: "keyword not found"}); | ||
} | ||
|
||
res.json(updatedSubscription.rows[0]); | ||
} catch (error) { | ||
console.error(error.message); | ||
res.status(500).send("Server error"); | ||
} | ||
}); | ||
|
||
// Find all keywords that user is subscribed to | ||
searchRouter.get("/:email", async (req, res) => { | ||
try { | ||
const { email } = req.params; | ||
|
||
const keywords = await client.query( | ||
`SELECT keyword | ||
FROM ${searchesTable} | ||
WHERE $1 = ANY(emails);`, | ||
[email] | ||
); | ||
|
||
const keywordList = keywords.rows.map(row => row.keyword); | ||
res.json(keywordList); | ||
} catch (error) { | ||
console.error(error.message); | ||
res.status(500).send("Server error"); | ||
} | ||
}); | ||
|
||
export default searchRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import AWS from 'aws-sdk'; | ||
|
||
const SES_CONFIG = { | ||
accessKeyId: process.env.AWS_SES_ACCESS_KEY, | ||
secretAccessKey: process.env.AWS_SES_SECRET_ACCESS_KEY, | ||
region: process.env.AWS_SES_REGION, | ||
}; | ||
|
||
const AWS_SES = new AWS.SES(SES_CONFIG); | ||
|
||
const sendEmail = async (email, subject, message) => { | ||
const params = { | ||
Destination: { | ||
ToAddresses: [email], | ||
}, | ||
Message: { | ||
Body: { | ||
Html: { | ||
Charset: "UTF-8", | ||
Data: message, | ||
}, | ||
}, | ||
Subject: { | ||
Charset: "UTF-8", | ||
Data: subject, | ||
}, | ||
}, | ||
Source: email, | ||
}; | ||
|
||
try { | ||
await AWS_SES.sendEmail(params).promise(); | ||
console.log(`Email sent to ${email}`); | ||
} catch (error) { | ||
console.log("Error sending email", error); | ||
} | ||
} | ||
|
||
export default sendEmail; |
Oops, something went wrong.