-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor : Refactor API endpoints into individual files. fixes #559
- Loading branch information
1 parent
0dceb96
commit a84df6e
Showing
19 changed files
with
591 additions
and
385 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//console.log(`Environment : ${process.env.NODE_ENV}`); | ||
const { | ||
preference, | ||
post | ||
} = require("../db/models"); | ||
|
||
|
||
const { | ||
sendArchiveEmail, | ||
} = require("../controller-email"); | ||
const { encrypt, decrypt } = require("../encryption"); | ||
|
||
|
||
const archiveTweet = async (req, res) => { | ||
console.log("archive POST request"); | ||
try { | ||
const fileName = req.file.key; | ||
const s3URL = req.file.location; | ||
const { url } = req.body; | ||
const user = req.user; | ||
|
||
await post.create({ | ||
userId: user.id, | ||
sourceUrl: url, | ||
permanentUrl: null, | ||
tags: null, | ||
screenshot: fileName, | ||
}); | ||
|
||
const result = await preference.findOne({ | ||
where: { | ||
userId: user.id, | ||
}, | ||
}); | ||
resultPlain = result.get({ plain: true }); | ||
resultPlain = { ...resultPlain, email: decrypt(resultPlain.email) }; | ||
|
||
console.log({ resultPlain }); | ||
|
||
if ( | ||
(result != null && resultPlain.email != undefined) || | ||
resultPlain.email != null | ||
) { | ||
await sendArchiveEmail( | ||
resultPlain.email, | ||
url, | ||
`https://uli-media.tattle.co.in/${fileName}` | ||
); | ||
} | ||
|
||
res.send({ msg: "Tweet Archived" }); | ||
} catch (err) { | ||
res.status(501).send({ msg: "Error archiving tweet" }); | ||
} | ||
}; | ||
|
||
const getArchive = async (req, res) => { | ||
const user = req.user; | ||
const { rows, count } = await post.findAndCountAll({ | ||
where: { | ||
userId: user.id, | ||
}, | ||
limit: 20, | ||
}); | ||
|
||
const archive = rows.map((row) => row.get({ plain: true })); | ||
|
||
res.send({ archive, count }); | ||
}; | ||
|
||
|
||
|
||
module.exports = { getArchive , archiveTweet } ; |
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,19 @@ | ||
//console.log(`Environment : ${process.env.NODE_ENV}`); | ||
|
||
|
||
const { registerAnonymousUser } = require("../controller-auth"); | ||
|
||
|
||
const register = async (req, res) => { | ||
try { | ||
const newUser = await registerAnonymousUser(); | ||
res.send({ user: newUser }); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(501).send(); | ||
} | ||
}; | ||
|
||
|
||
|
||
module.exports = {register} ; |
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,29 @@ | ||
//console.log(`Environment : ${process.env.NODE_ENV}`); | ||
|
||
const { | ||
feedback | ||
} = require("../db/models"); | ||
|
||
|
||
const addFeedback = async (req, res) => { | ||
console.log("POST feedback"); | ||
try { | ||
const data = req.body; | ||
|
||
await feedback.create({ | ||
userId: data.user_id, | ||
tweetText: data.tweet_text, | ||
sentiment: data.tweet_sentiment, | ||
confidence: data.tweet_confidence, | ||
}); | ||
|
||
res.send({ msg: "Feedback Sent" }); | ||
} catch (err) { | ||
//console.log(err); | ||
res.status(501).send({ msg: "Error sending feedback" }); | ||
} | ||
}; | ||
|
||
|
||
|
||
module.exports = {addFeedback} ; |
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,19 @@ | ||
//console.log(`Environment : ${process.env.NODE_ENV}`); | ||
|
||
const { | ||
post | ||
} = require("../db/models"); | ||
|
||
|
||
const getPost = async (req, res) => { | ||
const user = req.user; | ||
const result = post.findAll({ | ||
where: { | ||
userId: user.id, | ||
}, | ||
limit: 20, | ||
}); | ||
res.send({ posts: result.map((res) => res.get({ plain: true })) }); | ||
}; | ||
|
||
module.exports = {getPost} ; |
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,20 @@ | ||
//console.log(`Environment : ${process.env.NODE_ENV}`); | ||
|
||
const { classify } = require("../service-classifier"); | ||
|
||
|
||
const predict = async (req, res) => { | ||
const { text } = req.body; | ||
try { | ||
const label = await classify(text); | ||
res.send(label); | ||
} catch (err) { | ||
console.log("Error : could not classify tweet"); | ||
console.log(err); | ||
res.status(501).send("Could not label this tweet"); | ||
} | ||
}; | ||
|
||
|
||
|
||
module.exports = {predict} ; |
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,57 @@ | ||
//console.log(`Environment : ${process.env.NODE_ENV}`); | ||
|
||
const { | ||
preference | ||
} = require("../db/models"); | ||
|
||
const { encrypt, decrypt } = require("../encryption"); | ||
|
||
|
||
const addPreference = async (req, res) => { | ||
console.log("POST preferences"); | ||
const { id, language, slurList, email } = req.body; | ||
const user = req.user; | ||
const encryptedEmail = email ? encrypt(email) : null; | ||
const result = await preference.upsert({ | ||
id, | ||
userId: user.id, | ||
email: encryptedEmail, | ||
language, | ||
slurList, | ||
}); | ||
|
||
let plainResult = result[0].get({ plain: true }); | ||
console.log({ plainResult }); | ||
res.send({ | ||
...plainResult, | ||
email, | ||
}); | ||
|
||
// res.send({ ...result[0].get({ plain: true }) }); | ||
}; | ||
|
||
|
||
|
||
const getPreference = async (req, res) => { | ||
const user = req.user; | ||
const result = await preference.findOne({ | ||
where: { | ||
userId: user.id, | ||
}, | ||
}); | ||
if (result == null) { | ||
res.status(404).send(); | ||
} else { | ||
let plainResult = result.get({ plain: true }); | ||
// console.log({ plainResult }); | ||
res.send({ | ||
...plainResult, | ||
email: decrypt(plainResult.email), | ||
}); | ||
} | ||
}; | ||
|
||
|
||
|
||
|
||
module.exports = {addPreference , getPreference} ; |
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,20 @@ | ||
//console.log(`Environment : ${process.env.NODE_ENV}`); | ||
|
||
const { resetUser } = require("../controller-auth"); | ||
|
||
|
||
const resetAccount = async (req, res) => { | ||
const { user } = req; | ||
const userId = user.id; | ||
console.log(`reset request for ${userId}`); | ||
try { | ||
await resetUser(userId); | ||
res.send({ message: "Account reset" }); | ||
} catch (err) { | ||
console.log(`Error : unable to reset account`); | ||
console.log(err); | ||
res.status(501).send("Could not reset account"); | ||
} | ||
}; | ||
|
||
module.exports = {resetAccount} ; |
Oops, something went wrong.