diff --git a/latest.dump b/latest.dump deleted file mode 100644 index ed51be5..0000000 Binary files a/latest.dump and /dev/null differ diff --git a/output.sql b/output.sql deleted file mode 100644 index e69de29..0000000 diff --git a/server/services/plex/importData.js b/server/services/plex/importData.js index 5b7f5fc..1ed17bc 100644 --- a/server/services/plex/importData.js +++ b/server/services/plex/importData.js @@ -34,37 +34,29 @@ const importTvPosters = async user => { } }; -const createSections = async (sections, user) => { - try { - return Promise.all( - sections.map(async section => { - const dbSection = await models.PlexSection.findOne({ - where: { - UserId: user.id, - title: section.title, - }, - }); - if (dbSection) { - await dbSection.update({ - title: section.title, - type: section.type, - key: section.key, - UserId: user.id, - }); - } else { - await models.PlexSection.create({ - title: section.title, - type: section.type, - key: section.key, - UserId: user.id, - }); - } - }), +const createSections = (sections, user) => { + console.log('Creating Sections'); + return Promise.map(sections, section => { + return models.PlexSection.upsert( + { + title: section.title, + type: section.type, + key: section.key, + UserId: user.id, + }, + { + where: { + UserId: user.id, + title: section.title, + }, + returning: true, + plain: true, + raw: true, + }, ); - } catch (error) { - console.log(error); - return error; - } + }).catch(err => { + console.log(err); + }); }; const importLibraries = async user => { @@ -87,7 +79,7 @@ const importMostWatched = async user => { const importMostWatchedData = async (sectionKey, user) => { const mostWatchedData = await plexApi.getMostWatched({sectionKey}, user); - const mostWatchedDbData = await updateOrCreateLibrary(mostWatchedData, user); + const mostWatchedDbData = await updateLibrary(mostWatchedData, user); return mostWatchedDbData; }; @@ -98,51 +90,61 @@ const importLibrary = async (sectionKey, user) => { }, user, ); - const dbLibraryData = await updateOrCreateLibrary(libraryData, user); + const dbLibraryData = await createLibrary(libraryData, user); return dbLibraryData; }; -const updateOrCreateLibrary = async (libraryData, user) => { - try { - return Promise.all( - libraryData.map(async data => { - const media = await models.PlexLibrary.findOne({ - where: { - UserId: user.id, - title: sectionLibraryData.title, - }, - }); - if (media) { - await media.update({ - title: data.title, - type: data.type, - views: data.globalViewCount, - rating_key: data.ratingKey, - summary: data.summary, - UserId: user.id, - rating: data.rating, - year: data.year, - genre: JSON.stringify(data.Genre), - }); - } else { - await models.PlexLibrary.create({ - title: data.title, - type: data.type, - views: data.globalViewCount, - rating_key: data.ratingKey, - summary: data.summary, - UserId: user.id, - rating: data.rating, - year: data.year, - genre: JSON.stringify(data.Genre), - }); - } - }), +const updateLibrary = (libraryData, user) => { + return Promise.map(libraryData, data => { + return models.PlexLibrary.upsert( + { + title: data.title, + type: data.type, + views: data.globalViewCount, + rating_key: data.ratingKey, + summary: data.summary, + UserId: user.id, + rating: data.rating, + year: data.year, + genre: JSON.stringify(data.Genre), + }, + { + where: { + UserId: user.id, + title: data.title, + }, + }, ); - } catch (error) { - console.log(error); - return error; - } + }).catch(err => { + console.log(err); + }); +}; + +const createLibrary = (libraryData, user) => { + const userId = user.id; + return Promise.map(libraryData, sectionLibraryData => { + return models.PlexLibrary.upsert( + { + title: sectionLibraryData.title, + UserId: userId, + type: sectionLibraryData.type, + views: sectionLibraryData.views, + rating_key: sectionLibraryData.ratingKey, + meta_data_path: sectionLibraryData.key, + UserId: user.id, + summary: sectionLibraryData.summary, + rating: sectionLibraryData.rating, + year: sectionLibraryData.year, + genre: JSON.stringify(sectionLibraryData.Genre), + }, + { + where: { + UserId: user.id, + title: sectionLibraryData.title, + }, + }, + ); + }).catch(err => console.log(err)); }; export default {