mirror of
https://github.com/mjrode/WhatToWatch.git
synced 2026-05-06 04:29:12 -05:00
Use Promise.map for importing most watched and add tests.
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user