Skip to content

Commit

Permalink
for mozilla#1412: postResolveBreach updates
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Jan 21, 2020
1 parent 61f9d31 commit 7d07089
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
37 changes: 36 additions & 1 deletion controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,44 @@ async function postRemoveFxm(req, res) {
res.redirect("/");
}

function _updateResolvedBreaches(options) {
const { resolvedBreaches, affectedEmail, isResolved, recencyIndexNumber } = options;
// TODO: clarify the logic here. maybe change the endpoint to PUT /breach-resolution
// with the new resolution value ?
debugger;
if (isResolved === "false") {
return Array.isArray(resolvedBreaches[affectedEmail]) ? resolvedBreaches[affectedEmail].push(recencyIndexNumber) : Object.assign({[affectedEmail]: [recencyIndexNumber]});
}
return resolvedBreaches[affectedEmail].filter( el => el !== recencyIndexNumber );
}

// Placeholder -- WIP
async function postResolveBreach(req, res) {
const user = req.user;
debugger;
const sessionUser = req.user;
const { affectedEmail, recencyIndex, isResolved } = req.body;
const recencyIndexNumber = Number(recencyIndex);
const affectedEmailIsSubscriberRecord = sessionUser.primary_email === affectedEmail;
const affectedEmailInEmailAddresses = sessionUser.email_addresses.filter( ea => {
ea.email === affectedEmail;
});

if (!affectedEmailIsSubscriberRecord && !affectedEmailInEmailAddresses) {
return res.json("Error: affectedEmail is not valid for this subscriber");
}

const resolvedBreaches = getResolvedBreachesForEmail(sessionUser, affectedEmail);
const updatedResolvedBreaches = _updateResolvedBreaches(
{ resolvedBreaches, affectedEmail, isResolved, recencyIndexNumber }
);

const updatedSubscriber = await DB.setBreachesResolved(
{ user: sessionUser, updatedResolvedBreaches }
);
req.session.user = updatedSubscriber;
return res.json("Breach marked as resolved.");
}
/*
// Currently we're sending { affectedEmail, recencyIndex, isResolved, passwordsExposed } in req.body
// Not sure if we need all of these or need to send other/additional values?
Expand Down Expand Up @@ -415,6 +449,7 @@ async function postResolveBreach(req, res) {
res.json(localizedModalStrings);
}
*/

async function postUnsubscribe(req, res) {
const { token, emailHash } = req.body;
Expand Down
23 changes: 8 additions & 15 deletions db/DB.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,21 +325,14 @@ const DB = {
return updatedSubscriber;
},

async setBreachResolved(options) {
const {subscriber, emailAddressId, recencyIndex} = options;
if (emailAddressId) {
// TODO: SELECT email from email_addresses where ea.id
// TODO: if (!subscriber.breaches_resolved[email].includes(recencyIndex) {
// subscriber.breaches_resolved[email].push(recencyIndex);
// UPDATE subscriber SET breaches_resolved = subscriber.breaches_resolved
// }
// return updatedSubscriber;
}
// TODO: if (!subscriber.breaches_resolved[subscriber.primary_email].includes(recencyIndex) {
// subscriber.breaches_resolved[subscriber.primary_email].push(recencyIndex);
// UPDATE subscriber SET breaches_resolved = subscriber.breaches_resolved
// }
// return updatedSubscriber;
async setBreachesResolved(options) {
const { user, updatedResolvedBreaches } = options;
await knex("subscribers")
.where("id", user.id)
.update({
breaches_resolved: updatedResolvedBreaches,
});
return this.getSubscriberByEmail(user.primary_email);
},

async removeSubscriber(subscriber) {
Expand Down
3 changes: 1 addition & 2 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { asyncMiddleware, requireSessionUser } = require("../middleware");
const {
add, verify, logout,
getDashboard, getPreferences, getBreachStats,
removeEmail, resendEmail, updateCommunicationOptions, resolveBreach,
removeEmail, resendEmail, updateCommunicationOptions,
getUnsubscribe, postUnsubscribe, getRemoveFxm, postRemoveFxm, postResolveBreach,
} = require("../controllers/user");

Expand All @@ -23,7 +23,6 @@ router.get("/dashboard", csrfProtection, requireSessionUser, asyncMiddleware(get
router.get("/preferences", csrfProtection, requireSessionUser, asyncMiddleware(getPreferences));
router.use("/breach-stats", bearerToken());
router.get("/breach-stats", urlEncodedParser, asyncMiddleware(getBreachStats));
router.post("/breach-resolution", urlEncodedParser, csrfProtection, requireSessionUser, asyncMiddleware(resolveBreach));
router.get("/logout", logout);
router.post("/email", urlEncodedParser, csrfProtection, requireSessionUser, asyncMiddleware(add));
router.post("/remove-email", urlEncodedParser, csrfProtection, requireSessionUser, asyncMiddleware(removeEmail));
Expand Down

0 comments on commit 7d07089

Please sign in to comment.