Fix: Plex errors (#141)

* inPlex function returns promise for more consistent return types

* plexUserId should default to null on falsey
This commit is contained in:
2022-08-25 17:28:29 +02:00
committed by GitHub
parent 93d394c2fe
commit 696f9f989c
4 changed files with 9 additions and 10 deletions

View File

@@ -16,11 +16,6 @@ const queries = {
const getByStatus = () => const getByStatus = () =>
establishedDatabase.all(queries.getRequestsNotYetInPlex); establishedDatabase.all(queries.getRequestsNotYetInPlex);
const checkIfRequestExistInPlex = async request => {
request.existsInPlex = await plex.existsInPlex(request);
return request;
};
const commitNewStatus = (status, id, type, title) => { const commitNewStatus = (status, id, type, title) => {
console.log(type, title, "updated to:", status); console.log(type, title, "updated to:", status);
return establishedDatabase.run(queries.saveNewStatus, [status, id, type]); return establishedDatabase.run(queries.saveNewStatus, [status, id, type]);
@@ -29,12 +24,12 @@ const commitNewStatus = (status, id, type, title) => {
const getNewRequestMatchesInPlex = async () => { const getNewRequestMatchesInPlex = async () => {
const requests = await getByStatus(); const requests = await getByStatus();
return Promise.all(requests.map(checkIfRequestExistInPlex)) return Promise.all(requests.map(plex.inPlex))
.catch(error => .catch(error =>
console.log("error from checking plex for existance:", error) console.log("error from checking plex for existance:", error)
) )
.then(matchedRequests => .then(matchedRequests =>
matchedRequests.filter(request => request.existsInPlex) matchedRequests.filter(request => request.matchedInPlex)
); );
}; };

View File

@@ -41,9 +41,12 @@ class PlexRepository {
inPlex(_tmdbResult) { inPlex(_tmdbResult) {
const tmdbResult = { ..._tmdbResult }; const tmdbResult = { ..._tmdbResult };
this.search(tmdbResult.title)
return this.search(tmdbResult.title)
.then(plexResult => addAttributeIfTmdbInPlex(tmdbResult, plexResult)) .then(plexResult => addAttributeIfTmdbInPlex(tmdbResult, plexResult))
.catch(() => { .catch(() => {
// log("some error here::", error);
/** /**
* If something crashes with search from this function it probably * If something crashes with search from this function it probably
* fine to set the `matchedInPlex` attribute to false and return * fine to set the `matchedInPlex` attribute to false and return

View File

@@ -14,7 +14,7 @@ function readRequestController(req, res) {
requestRepository requestRepository
.lookup(mediaId, type) .lookup(mediaId, type)
.then(movies => { .then(movies => {
res.send(movies); res.send(movies || {});
}) })
.catch(error => { .catch(error => {
res.status(404).send({ success: false, message: error.message }); res.status(404).send({ success: false, message: error.message });

View File

@@ -18,7 +18,8 @@ const mustHaveAccountLinkedToPlex = (req, res, next) => {
req.loggedInUser.username req.loggedInUser.username
) )
.then(row => { .then(row => {
const plexUserId = row.plex_userid; const plexUserId = row?.plex_userid || null;
if (plexUserId === null) { if (plexUserId === null) {
return res.status(403).send({ return res.status(403).send({
success: false, success: false,