Simplified how we fetch and update requests that have landed in plex (#146)
This commit is contained in:
		| @@ -1,6 +1,6 @@ | ||||
| import Plex from "../src/plex/plexRepository"; | ||||
| import establishedDatabase from "../src/database/database"; | ||||
| import Configuration from "../src/config/configuration"; | ||||
| import Plex from "../src/plex/plex.js"; | ||||
| import establishedDatabase from "../src/database/database.js"; | ||||
| import Configuration from "../src/config/configuration.js"; | ||||
|  | ||||
| const configuration = Configuration.getInstance(); | ||||
| const plex = new Plex( | ||||
| @@ -13,32 +13,29 @@ const queries = { | ||||
|   saveNewStatus: `UPDATE requests SET status = ? WHERE id IS ? and type IS ?` | ||||
| }; | ||||
|  | ||||
| const getByStatus = () => | ||||
| const getRequestsNotYetInPlex = () => | ||||
|   establishedDatabase.all(queries.getRequestsNotYetInPlex); | ||||
|  | ||||
| const commitNewStatus = (status, id, type, title) => { | ||||
|   console.log(type, title, "updated to:", status); | ||||
| async function getNewRequestMatchesInPlex() { | ||||
|   const requests = await getRequestsNotYetInPlex(); | ||||
|   const exists = await Promise.all( | ||||
|     requests.map(request => plex.existsInPlex(request)) | ||||
|   ); | ||||
|  | ||||
|   return requests.filter(() => exists.shift()); | ||||
| } | ||||
|  | ||||
| function commitNewStatus(status, id, type, title) { | ||||
|   console.log(`${type} ${title} updated to: ${status}`); | ||||
|   return establishedDatabase.run(queries.saveNewStatus, [status, id, type]); | ||||
| }; | ||||
| } | ||||
|  | ||||
| const getNewRequestMatchesInPlex = async () => { | ||||
|   const requests = await getByStatus(); | ||||
|  | ||||
|   return Promise.all(requests.map(plex.inPlex)) | ||||
|     .catch(error => | ||||
|       console.log("error from checking plex for existance:", error) | ||||
|     ) | ||||
|     .then(matchedRequests => | ||||
|       matchedRequests.filter(request => request.matchedInPlex) | ||||
|     ); | ||||
| }; | ||||
|  | ||||
| const updateMatchInDb = (match, status) => { | ||||
| function updateMatchInDb(match, status) { | ||||
|   return commitNewStatus(status, match.id, match.type, match.title); | ||||
| }; | ||||
| } | ||||
|  | ||||
| getNewRequestMatchesInPlex() | ||||
|   .then(newMatches => | ||||
|     Promise.all(newMatches.map(match => updateMatchInDb(match, "downloaded"))) | ||||
|   ) | ||||
|   .then(() => process.exit(0)); | ||||
|   .finally(() => process.exit(0)); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user