Use Promise.map for importing most watched and add tests.

This commit is contained in:
mike.rode
2019-02-27 19:21:32 -06:00
parent 735be2e0c4
commit 6b747477ae
2 changed files with 46 additions and 24 deletions
+22 -24
View File
@@ -34,10 +34,10 @@ const importLibraries = async () => {
});
};
const importMostWatched = async req => {
const mostWatched = await plexApi.getMostWatched(req);
mostWatched.forEach(async libraryData => {
await updateLibrary([libraryData]);
const importMostWatched = async type => {
const mostWatchedRecords = await plexApi.getMostWatched(type);
return Promise.map(mostWatchedRecords, mostWatchedRecord => {
return updateLibrary([mostWatchedRecord]);
});
};
@@ -50,27 +50,25 @@ const importLibrary = async sectionId => {
};
const updateLibrary = libraryData => {
return Promise.try(() => {
libraryData.forEach(data => {
models.PlexLibrary.update(
{
return Promise.map(libraryData, data => {
return models.PlexLibrary.update(
{
title: data.title,
type: data.type,
views: data.globalViewCount,
rating_key: data.ratingKey,
metadata_path: data.key,
summary: data.summary,
rating: data.rating,
year: data.year,
genre: JSON.stringify(data.Genre),
},
{
where: {
title: data.title,
type: data.type,
views: data.globalViewCount,
rating_key: data.ratingKey,
metadata_path: data.key,
summary: data.summary,
rating: data.rating,
year: data.year,
genre: JSON.stringify(data.Genre),
},
{
where: {
title: data.title,
},
},
);
});
},
);
}).catch(err => {
console.log(err);
});
@@ -78,7 +76,7 @@ const updateLibrary = libraryData => {
const createLibrary = libraryData => {
return Promise.map(libraryData, sectionLibraryData => {
models.PlexLibrary.upsert(
return models.PlexLibrary.upsert(
{
title: sectionLibraryData.title,
type: sectionLibraryData.type,
@@ -55,4 +55,28 @@ describe('ImportData', () => {
librarySecondRequest.should.be.length(56);
});
});
describe('GET /plex/import/most-watched', () => {
it('should find and store libraries in the database', async () => {
nocks.plexSections();
nocks.plexLibrary();
await importData.importLibraries();
const library = await models.PlexLibrary.findAll();
library.should.be.length(56);
nocks.mostWatched();
const req = { query: { type: 2 } };
await importData.importMostWatched(req);
const libraryMostWatched = await models.PlexLibrary.findAll();
const newGirl = libraryMostWatched.filter(
data => data.dataValues.title === 'New Girl',
);
newGirl[0].dataValues.views.should.eq(74);
newGirl[0].dataValues.metadata_path.should.eq(
'/library/metadata/5485/children',
);
libraryMostWatched.should.be.length(56);
});
});
});