mirror of
https://github.com/mjrode/WhatToWatch.git
synced 2026-05-07 13:09:08 -05:00
Refactor importData to use custom updateOrCreate function, updated all tests.
This commit is contained in:
+192
-3
@@ -1,5 +1,5 @@
|
||||
export default {
|
||||
User: [
|
||||
Users: [
|
||||
{
|
||||
id: 1,
|
||||
firstName: 'Mike',
|
||||
@@ -11,8 +11,197 @@ export default {
|
||||
plexToken: 'testPlexApiToken',
|
||||
sonarrUrl: null,
|
||||
sonarrApiKey: null,
|
||||
createdAt: '2019-09-01 14:36:57.418-05',
|
||||
updatedAt: '2019-09-01 14:37:09.415-05',
|
||||
},
|
||||
{
|
||||
id: 999,
|
||||
firstName: 'Test',
|
||||
lastName: 'User',
|
||||
googleId: '111111111111',
|
||||
email: 'test@email.com',
|
||||
plexUrl: 'https://plex.testlix.com',
|
||||
plexPinId: '11111',
|
||||
plexToken: 'testPlexApiToken',
|
||||
sonarrUrl: null,
|
||||
sonarrApiKey: null,
|
||||
},
|
||||
],
|
||||
|
||||
PlexLibrary: [
|
||||
{
|
||||
id: 1,
|
||||
title: 'Ash vs Evil Dead',
|
||||
type: 'show',
|
||||
views: null,
|
||||
rating_key: 5471,
|
||||
poster_path: null,
|
||||
summary:
|
||||
"Ash has spent the last thirty years avoiding responsibility, maturity, and the terrors of the Evil Dead until a Deadite plague threatens to destroy all of mankind and Ash becomes mankind's only hope. ",
|
||||
rating: 8.7,
|
||||
year: 2015,
|
||||
genre: '[{"tag":"Comedy"},{"tag":"Horror"}]',
|
||||
createdAt: '2019-09-01 18:34:42.659-05',
|
||||
updatedAt: '2019-09-01 18:34:42.796-05',
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: 'Anthony Bourdain: Parts Unknown',
|
||||
type: 'show',
|
||||
views: null,
|
||||
rating_key: 8121,
|
||||
poster_path: null,
|
||||
summary:
|
||||
'Bourdain traveled across the globe to uncover little-known areas of the world and celebrate diverse cultures by exploring food and dining rituals. Known for his curiosity, candor, and acerbic wit, Bourdain took viewers off the beaten path of tourist destinations – including some war-torn parts of the world – and met with a variety of local citizens to offer a window into their lifestyles, and occasionally communed with an internationally lauded chef on his journey. We can all hope to find ourselves to enjoy a life as amazing as Anthony Bourdain did. Rest in peace.',
|
||||
rating: 8.3,
|
||||
year: 2013,
|
||||
genre: null,
|
||||
createdAt: '2019-09-01 18:34:42.659-05',
|
||||
updatedAt: '2019-09-01 18:34:42.784-05',
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
title: 'Atlanta',
|
||||
type: 'show',
|
||||
views: null,
|
||||
rating_key: 6937,
|
||||
poster_path: null,
|
||||
summary:
|
||||
"Two cousins, with different views on art versus commerce, on their way up through the Atlanta rap scene; Earnest 'Earn' Marks, an ambitious college dropout and his estranged cousin, who suddenly becomes a star.",
|
||||
rating: 8.5,
|
||||
year: 2016,
|
||||
genre: '[{"tag":"Comedy"},{"tag":"Drama"}]',
|
||||
createdAt: '2019-09-01 18:34:42.666-05',
|
||||
updatedAt: '2019-09-01 18:34:42.796-05',
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
title: 'Avatar: The Last Airbender',
|
||||
type: 'show',
|
||||
views: null,
|
||||
rating_key: 7060,
|
||||
poster_path: null,
|
||||
summary:
|
||||
'With the Fire Nation on the brink of global domination, a young girl and her brother discover a 12-year old Airbender who reveals himself as the Avatar. Will this irresponsible kid accept his destiny in time to save the world?',
|
||||
rating: 9,
|
||||
year: 2005,
|
||||
genre: '[{"tag":"Action"},{"tag":"Adventure"}]',
|
||||
createdAt: '2019-09-01 18:34:42.666-05',
|
||||
updatedAt: '2019-09-01 18:34:42.801-05',
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
title: '30 Rock',
|
||||
type: 'show',
|
||||
views: null,
|
||||
rating_key: 2529,
|
||||
poster_path: null,
|
||||
summary:
|
||||
"Emmy Award Winner Tina Fey writes, executive produces and stars as Liz Lemon, the head writer of a live variety programme in New York City. Liz's life is turned upside down when brash new network executive Jack Donaghy (Alec Baldwin in his Golden Globe winning role) interferes with her show, bringing the wildly unpredictable Tracy Jordan (Tracy Morgan) into the cast. Now its up to Liz to manage the mayhem and still try to have a life.\r\n",
|
||||
rating: 8.6,
|
||||
year: 2006,
|
||||
genre: '[{"tag":"Comedy"}]',
|
||||
createdAt: '2019-09-01 18:34:42.666-05',
|
||||
updatedAt: '2019-09-01 18:34:42.783-05',
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
title: 'Band of Brothers',
|
||||
type: 'show',
|
||||
views: null,
|
||||
rating_key: 6925,
|
||||
poster_path: null,
|
||||
summary:
|
||||
'The miniseries follows Easy Company, an army unit during World War II, from their initial training at Camp Toccoa to the conclusion of the war. The series is based on the book written by the late Stephen E. Ambrose. \r\n\r\nBand of Brothers is executive produced by Steven Spielberg and Tom Hanks, the series won 6 Emmy Awards. ',
|
||||
rating: 9.4,
|
||||
year: 2001,
|
||||
genre: '[{"tag":"Action"},{"tag":"Adventure"}]',
|
||||
createdAt: '2019-09-01 18:34:42.672-05',
|
||||
updatedAt: '2019-09-01 18:34:42.801-05',
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 7,
|
||||
title: 'Barry',
|
||||
type: 'show',
|
||||
views: null,
|
||||
rating_key: 7026,
|
||||
poster_path: null,
|
||||
summary:
|
||||
'After following his intended target to an acting class, a hitman finds himself intrigued and decides to become an actor and change his life.',
|
||||
rating: 7.8,
|
||||
year: 2018,
|
||||
genre: '[{"tag":"Comedy"},{"tag":"Crime"}]',
|
||||
createdAt: '2019-09-01 18:34:42.672-05',
|
||||
updatedAt: '2019-09-01 18:34:42.801-05',
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 8,
|
||||
title: 'Better Call Saul',
|
||||
type: 'show',
|
||||
views: null,
|
||||
rating_key: 5836,
|
||||
poster_path: null,
|
||||
summary:
|
||||
"See the rise and rise of Jimmy McGill, a small-time lawyer searching for his destiny, but hustling to make ends meet. Working alongside, and often against Jimmy, is ‘fixer’ Mike Erhmantraut. The series tracks Jimmy's evolution into Saul Goodman, the man who puts the word ‘criminal’ in ‘criminal lawyer’.",
|
||||
rating: 8.9,
|
||||
year: 2015,
|
||||
genre: '[{"tag":"Crime"},{"tag":"Drama"}]',
|
||||
createdAt: '2019-09-01 18:34:42.672-05',
|
||||
updatedAt: '2019-09-01 18:34:42.812-05',
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 9,
|
||||
title: 'Big Mouth',
|
||||
type: 'show',
|
||||
views: null,
|
||||
rating_key: 8257,
|
||||
poster_path: null,
|
||||
summary:
|
||||
'Teenage friends find their lives upended by the wonders and horrors of puberty in this edgy comedy from real-life pals Nick Kroll and Andrew Goldberg.',
|
||||
rating: 8.3,
|
||||
year: 2017,
|
||||
genre: '[{"tag":"Animation"},{"tag":"Comedy"}]',
|
||||
createdAt: '2019-09-01 18:34:42.678-05',
|
||||
updatedAt: '2019-09-01 18:34:42.812-05',
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 10,
|
||||
title: 'Big Time in Hollywood, FL',
|
||||
type: 'show before import',
|
||||
views: null,
|
||||
rating_key: 8102,
|
||||
poster_path: null,
|
||||
summary:
|
||||
"Follows two delusional brothers, who are self-proclaimed filmmakers, as they are kicked out of their parent's house and end up on an epic cinematic journey.",
|
||||
rating: 8,
|
||||
year: 2015,
|
||||
genre: '[{"tag":"Comedy"}]',
|
||||
createdAt: '2019-09-01 18:34:42.678-05',
|
||||
updatedAt: '2019-09-01 18:34:42.817-05',
|
||||
UserId: 1,
|
||||
},
|
||||
],
|
||||
PlexSections: [
|
||||
{
|
||||
id: 2,
|
||||
title: 'TV Shows',
|
||||
type: 'show',
|
||||
key: 3,
|
||||
UserId: 999,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
title: 'Movies',
|
||||
type: 'movie',
|
||||
key: 2,
|
||||
UserId: 999,
|
||||
},
|
||||
],
|
||||
|
||||
|
||||
@@ -6,36 +6,28 @@ import MovieDb from 'moviedb-promise';
|
||||
import {Op} from 'sequelize';
|
||||
const mdb = new MovieDb(config.server.movieApiKey);
|
||||
|
||||
const updateOrCreate = (model, where, newItem, beforeCreate) => {
|
||||
const updateOrCreate = async (model, where, newItem, beforeCreate) => {
|
||||
// Try to find record using findOne
|
||||
return model.findOne({where}).then(item => {
|
||||
if (!item) {
|
||||
// Item doesn't exist, so we create it
|
||||
const item = await model.findOne({where});
|
||||
if (!item) {
|
||||
// Item doesn't exist, so we create it
|
||||
|
||||
// Custom promise to add more data to the record
|
||||
// Being saved (optional)
|
||||
Promise.resolve(beforeCreate).then(() =>
|
||||
model
|
||||
.create(newItem, {returning: true, plain: true, raw: true})
|
||||
.then(item => ({item, created: true})),
|
||||
);
|
||||
}
|
||||
const createItem = await model.create(newItem, {
|
||||
returning: true,
|
||||
plain: true,
|
||||
raw: true,
|
||||
});
|
||||
|
||||
return {createItem, created: true};
|
||||
} else {
|
||||
// Item already exists, so we update it
|
||||
return model
|
||||
.update(
|
||||
newItem,
|
||||
{where: where},
|
||||
{returning: true, plain: true, raw: true},
|
||||
)
|
||||
.then(item => ({item, created: false}));
|
||||
});
|
||||
};
|
||||
|
||||
const importSections = async user => {
|
||||
const sections = await plexApi.getSections(user);
|
||||
const dbSections = await createSections(sections, user);
|
||||
return dbSections;
|
||||
const updatedItem = await model.update(
|
||||
newItem,
|
||||
{where: where},
|
||||
{returning: true, plain: true, raw: true},
|
||||
);
|
||||
return {item, created: false};
|
||||
}
|
||||
};
|
||||
|
||||
const importTvPosters = async user => {
|
||||
@@ -60,6 +52,12 @@ const importTvPosters = async user => {
|
||||
}
|
||||
};
|
||||
|
||||
const importSections = async user => {
|
||||
const sections = await plexApi.getSections(user);
|
||||
const dbSections = await createSections(sections, user);
|
||||
return dbSections;
|
||||
};
|
||||
|
||||
const createSections = async (sections, user) => {
|
||||
const updatedSections = await Promise.map(sections, section => {
|
||||
const newSection = {
|
||||
@@ -72,7 +70,6 @@ const createSections = async (sections, user) => {
|
||||
models.PlexSection,
|
||||
{
|
||||
title: section.title,
|
||||
type: section.type,
|
||||
UserId: user.id,
|
||||
},
|
||||
newSection,
|
||||
@@ -85,9 +82,47 @@ const createSections = async (sections, user) => {
|
||||
|
||||
const importLibraries = async user => {
|
||||
const sections = await plexApi.getSections(user);
|
||||
const dbSections = await createSections(sections, user);
|
||||
return Promise.map(sections, section => {
|
||||
return importLibrary(section.key, user);
|
||||
});
|
||||
}).catch(err => console.log('ImportLibraries', err));
|
||||
};
|
||||
|
||||
const importLibrary = async (sectionKey, user) => {
|
||||
const libraryData = await plexApi.getLibraryDataBySection(
|
||||
{
|
||||
sectionKey,
|
||||
},
|
||||
user,
|
||||
);
|
||||
const dbLibraryData = await createLibrary(libraryData, user);
|
||||
return dbLibraryData;
|
||||
};
|
||||
|
||||
const createLibrary = async (libraryData, user) => {
|
||||
const updatedLibrary = await Promise.map(libraryData, sectionLibraryData => {
|
||||
const newSectionLibraryData = {
|
||||
title: sectionLibraryData.title,
|
||||
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),
|
||||
};
|
||||
return updateOrCreate(
|
||||
models.PlexLibrary,
|
||||
{
|
||||
UserId: user.id,
|
||||
title: sectionLibraryData.title,
|
||||
},
|
||||
newSectionLibraryData,
|
||||
);
|
||||
}).catch(err => console.log(err));
|
||||
return updatedLibrary;
|
||||
};
|
||||
|
||||
const importMostWatched = async user => {
|
||||
@@ -107,70 +142,32 @@ const importMostWatchedData = async (sectionKey, user) => {
|
||||
return mostWatchedDbData;
|
||||
};
|
||||
|
||||
const importLibrary = async (sectionKey, user) => {
|
||||
const libraryData = await plexApi.getLibraryDataBySection(
|
||||
{
|
||||
sectionKey,
|
||||
},
|
||||
user,
|
||||
);
|
||||
const dbLibraryData = await createLibrary(libraryData, user);
|
||||
return dbLibraryData;
|
||||
};
|
||||
|
||||
const updateLibrary = (libraryData, user) => {
|
||||
return Promise.map(libraryData, data => {
|
||||
return models.PlexLibrary.upsert(
|
||||
const updateLibrary = async (libraryData, user) => {
|
||||
const updatedLibrary = await Promise.map(libraryData, data => {
|
||||
const newData = {
|
||||
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),
|
||||
};
|
||||
return updateOrCreate(
|
||||
models.PlexLibrary,
|
||||
{
|
||||
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,
|
||||
},
|
||||
title: data.title,
|
||||
},
|
||||
newData,
|
||||
);
|
||||
}).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 {
|
||||
importSections,
|
||||
importLibraries,
|
||||
|
||||
Reference in New Issue
Block a user