Normalize to double quotes across all files with Prettier

This commit is contained in:
X4Lo committed 2026-06-21 22:28:08 +01:00
1 parent 73bff72cec
commit a5659cf4c8
190 files changed
+9523 -8859

No files matched your search

+33 -35
View File
@@ -5,14 +5,12 @@ const Entry = require("../models/Entry");
const Genre = require("../models/Genre");
const Media = require("../models/Media");
async function initializeData() {
initializeGenres();
//markEntriesRequireCreditsSync();
// migrateAccuracyField();
}
// const collections = await Collection.findAll({
// where: {
// posterPath: { [Sequelize.Op.notLike]: '/%' }
@@ -36,14 +34,14 @@ async function initializeData() {
async function markEntriesRequireCreditsSync() {
try {
const [affectedCount] = await Entry.update(
{ entry_sync_status: 'RequireCreditsSync' },
{ where: { entry_sync_status: null } }
{ entry_sync_status: "RequireCreditsSync" },
{ where: { entry_sync_status: null } },
);
if (affectedCount > 0) {
console.log(`Marked ${affectedCount} entries as RequireCreditsSync`);
}
} catch (error) {
console.error('Error marking entries for credits sync:', error);
console.error("Error marking entries for credits sync:", error);
}
}
@@ -51,33 +49,33 @@ async function initializeGenres() {
try {
// * data to insert:
const initialGenres = [
{ id: 10759, name: 'Action & Adventure' },
{ id: 16, name: 'Animation' },
{ id: 35, name: 'Comedy' },
{ id: 80, name: 'Crime' },
{ id: 99, name: 'Documentary' },
{ id: 18, name: 'Drama' },
{ id: 10751, name: 'Family' },
{ id: 10762, name: 'Kids' },
{ id: 9648, name: 'Mystery' },
{ id: 10763, name: 'News' },
{ id: 10764, name: 'Reality' },
{ id: 10765, name: 'Sci-Fi & Fantasy' },
{ id: 10766, name: 'Soap' },
{ id: 10767, name: 'Talk' },
{ id: 10768, name: 'War & Politics' },
{ id: 37, name: 'Western' },
{ id: 28, name: 'Action' },
{ id: 12, name: 'Adventure' },
{ id: 14, name: 'Fantasy' },
{ id: 36, name: 'History' },
{ id: 27, name: 'Horror' },
{ id: 10402, name: 'Music' },
{ id: 10749, name: 'Romance' },
{ id: 878, name: 'Science Fiction' },
{ id: 10770, name: 'TV Movie' },
{ id: 53, name: 'Thriller' },
{ id: 10752, name: 'War' }
{ id: 10759, name: "Action & Adventure" },
{ id: 16, name: "Animation" },
{ id: 35, name: "Comedy" },
{ id: 80, name: "Crime" },
{ id: 99, name: "Documentary" },
{ id: 18, name: "Drama" },
{ id: 10751, name: "Family" },
{ id: 10762, name: "Kids" },
{ id: 9648, name: "Mystery" },
{ id: 10763, name: "News" },
{ id: 10764, name: "Reality" },
{ id: 10765, name: "Sci-Fi & Fantasy" },
{ id: 10766, name: "Soap" },
{ id: 10767, name: "Talk" },
{ id: 10768, name: "War & Politics" },
{ id: 37, name: "Western" },
{ id: 28, name: "Action" },
{ id: 12, name: "Adventure" },
{ id: 14, name: "Fantasy" },
{ id: 36, name: "History" },
{ id: 27, name: "Horror" },
{ id: 10402, name: "Music" },
{ id: 10749, name: "Romance" },
{ id: 878, name: "Science Fiction" },
{ id: 10770, name: "TV Movie" },
{ id: 53, name: "Thriller" },
{ id: 10752, name: "War" },
];
// * Check if the data already exists
@@ -85,7 +83,7 @@ async function initializeGenres() {
// ? If data already exists, return
if (existingGenres.length == initialGenres.length) {
console.log('Genres already initialized');
console.log("Genres already initialized");
return;
}
@@ -94,7 +92,7 @@ async function initializeGenres() {
console.log(`${initialGenres.length} Genres initialized successfully`);
} catch (error) {
console.error('Error initializing data:', error);
console.error("Error initializing data:", error);
}
}
@@ -102,7 +100,7 @@ module.exports = {
initializeData,
initializeGenres,
markEntriesRequireCreditsSync,
}
};
// async function migrateAccuracyField() {
// try {
+9 -9
View File
@@ -1,14 +1,14 @@
const { Sequelize } = require('sequelize');
const fs = require('fs');
const { Sequelize } = require("sequelize");
const fs = require("fs");
const logStream = fs.createWriteStream('sequelize.log', { flags: 'a' });
const logStream = fs.createWriteStream("sequelize.log", { flags: "a" });
const sequelize = new Sequelize('Showzy', 'root', 'password', {
host: 'localhost',
dialect: 'mysql',
const sequelize = new Sequelize("Showzy", "root", "password", {
host: "localhost",
dialect: "mysql",
logging: (msg) => {
logStream.write(msg + '\n');
}
logStream.write(msg + "\n");
},
});
module.exports = sequelize;
module.exports = sequelize;
+131 -106
View File
@@ -1,22 +1,21 @@
// Import models
const Franchise = require('../models/Franchise');
const Entry = require('../models/Entry')
const EntryId = require('../models/EntryId')
const EntryRating = require('../models/EntryRating')
const EntrySeen = require('../models/EntrySeen');
const Collection = require('../models/Collection');
const UserCollectionEntry = require('../models/UserCollectionEntry');
const Season = require('../models/Season');
const Episode = require('../models/Episode');
const EpisodeSeen = require('../models/EpisodeSeen');
const Genre = require('../models/Genre');
const EntryGenre = require('../models/EntryGenre');
const Media = require('../models/Media');
const Network = require('../models/Network');
const EntryNetwork = require('../models/EntryNetwork');
const People = require('../models/People');
const Credit = require('../models/Credit');
const Franchise = require("../models/Franchise");
const Entry = require("../models/Entry");
const EntryId = require("../models/EntryId");
const EntryRating = require("../models/EntryRating");
const EntrySeen = require("../models/EntrySeen");
const Collection = require("../models/Collection");
const UserCollectionEntry = require("../models/UserCollectionEntry");
const Season = require("../models/Season");
const Episode = require("../models/Episode");
const EpisodeSeen = require("../models/EpisodeSeen");
const Genre = require("../models/Genre");
const EntryGenre = require("../models/EntryGenre");
const Media = require("../models/Media");
const Network = require("../models/Network");
const EntryNetwork = require("../models/EntryNetwork");
const People = require("../models/People");
const Credit = require("../models/Credit");
// ? one to one
// A hasOne B = A > B
@@ -25,197 +24,223 @@ const Credit = require('../models/Credit');
// A hasMany B = A > B
// A belongsTo B = A < B
// * ENTRY & EntryId - ONE TO ONE
Entry.hasOne(EntryId, {
foreignKey: 'entry_id', as: 'entryId', onDelete: 'CASCADE',
foreignKey: "entry_id",
as: "entryId",
onDelete: "CASCADE",
});
EntryId.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
EntryId.belongsTo(Entry, { foreignKey: "entry_id", as: "entry" });
// * ENTRY & EntryRating - ONE TO ONE
Entry.hasOne(EntryRating, {
foreignKey: 'entry_id', as: 'entryRating', onDelete: 'CASCADE',
foreignKey: "entry_id",
as: "entryRating",
onDelete: "CASCADE",
});
EntryRating.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
EntryRating.belongsTo(Entry, { foreignKey: "entry_id", as: "entry" });
// * ENTRY & ENTRY_SEEN - ONE TO MANY
Entry.hasMany(EntrySeen, {
foreignKey: 'entry_id', as: 'entrySeen', onDelete: 'CASCADE',
foreignKey: "entry_id",
as: "entrySeen",
onDelete: "CASCADE",
});
EntrySeen.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
EntrySeen.belongsTo(Entry, { foreignKey: "entry_id", as: "entry" });
// * ENTRY & LIST - MANY TO MANY
Entry.belongsToMany(Collection, {
through: UserCollectionEntry,
foreignKey: 'entry_id',
otherKey: 'user_collection_id',
onDelete: 'CASCADE',
as: 'collections',
foreignKey: "entry_id",
otherKey: "user_collection_id",
onDelete: "CASCADE",
as: "collections",
});
Collection.belongsToMany(Entry, {
through: UserCollectionEntry,
foreignKey: 'user_collection_id',
otherKey: 'entry_id',
onDelete: 'CASCADE',
as: 'entries',
foreignKey: "user_collection_id",
otherKey: "entry_id",
onDelete: "CASCADE",
as: "entries",
});
Entry.hasMany(UserCollectionEntry, { foreignKey: 'entry_id', as: 'userCollectionEntries' });
UserCollectionEntry.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
Collection.hasMany(UserCollectionEntry, { foreignKey: 'user_collection_id', as: 'userCollectionEntries' });
UserCollectionEntry.belongsTo(Collection, { foreignKey: 'user_collection_id', as: 'userCollection' });
Entry.hasMany(UserCollectionEntry, {
foreignKey: "entry_id",
as: "userCollectionEntries",
});
UserCollectionEntry.belongsTo(Entry, { foreignKey: "entry_id", as: "entry" });
Collection.hasMany(UserCollectionEntry, {
foreignKey: "user_collection_id",
as: "userCollectionEntries",
});
UserCollectionEntry.belongsTo(Collection, {
foreignKey: "user_collection_id",
as: "userCollection",
});
// * ENTRY & GENRE - MANY TO MANY
Entry.belongsToMany(Genre, {
through: EntryGenre,
foreignKey: 'entry_id',
otherKey: 'genre_id',
onDelete: 'CASCADE',
as: 'genres',
foreignKey: "entry_id",
otherKey: "genre_id",
onDelete: "CASCADE",
as: "genres",
});
Genre.belongsToMany(Entry, {
through: EntryGenre,
foreignKey: 'genre_id',
otherKey: 'entry_id',
onDelete: 'CASCADE',
as: 'entries',
foreignKey: "genre_id",
otherKey: "entry_id",
onDelete: "CASCADE",
as: "entries",
});
Entry.hasMany(EntryGenre, { foreignKey: 'entry_id', as: 'entryGenres' });
EntryGenre.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
Genre.hasMany(EntryGenre, { foreignKey: 'genre_id', as: 'entryGenres' });
EntryGenre.belongsTo(Genre, { foreignKey: 'genre_id', as: 'genre' });
Entry.hasMany(EntryGenre, { foreignKey: "entry_id", as: "entryGenres" });
EntryGenre.belongsTo(Entry, { foreignKey: "entry_id", as: "entry" });
Genre.hasMany(EntryGenre, { foreignKey: "genre_id", as: "entryGenres" });
EntryGenre.belongsTo(Genre, { foreignKey: "genre_id", as: "genre" });
// * ENTRY & NETWORK - MANY TO MANY
Entry.belongsToMany(Network, {
through: EntryNetwork,
foreignKey: 'entry_id',
otherKey: 'network_id',
onDelete: 'CASCADE',
as: 'networks',
foreignKey: "entry_id",
otherKey: "network_id",
onDelete: "CASCADE",
as: "networks",
});
Network.belongsToMany(Entry, {
through: EntryNetwork,
foreignKey: 'network_id',
otherKey: 'entry_id',
onDelete: 'CASCADE',
as: 'entries',
foreignKey: "network_id",
otherKey: "entry_id",
onDelete: "CASCADE",
as: "entries",
});
// ! MEDIA
// * MEDIA & ENTRY - MANY TO ONE
Media.belongsTo(Entry, {
foreignKey: 'mediaableId',
as: 'entry',
foreignKey: "mediaableId",
as: "entry",
constraints: false,
where: {
mediaable: 'entry'
}
mediaable: "entry",
},
});
Entry.hasMany(Media, {
foreignKey: 'mediaableId',
foreignKey: "mediaableId",
constraints: false,
as: 'medias',
scope: { mediaable: 'entry' }
as: "medias",
scope: { mediaable: "entry" },
});
// * MEDIA & People - MANY TO ONE
Media.belongsTo(People, {
foreignKey: 'mediaableId',
as: 'people',
foreignKey: "mediaableId",
as: "people",
constraints: false,
where: {
mediaable: 'people'
}
mediaable: "people",
},
});
People.hasMany(Media, {
foreignKey: 'mediaableId',
foreignKey: "mediaableId",
constraints: false,
as: 'medias',
scope: { mediaable: 'people' }
as: "medias",
scope: { mediaable: "people" },
});
// * MEDIA & Collection - MANY TO ONE
Media.belongsTo(Collection, {
foreignKey: 'mediaableId',
as: 'collection',
foreignKey: "mediaableId",
as: "collection",
constraints: false,
where: {
mediaable: 'collection'
}
mediaable: "collection",
},
});
Collection.hasMany(Media, {
foreignKey: 'mediaableId',
foreignKey: "mediaableId",
constraints: false,
as: 'medias',
scope: { mediaable: 'collection' }
as: "medias",
scope: { mediaable: "collection" },
});
// * MEDIA & Season - MANY TO ONE
Media.belongsTo(Season, {
foreignKey: 'mediaableId',
as: 'season',
foreignKey: "mediaableId",
as: "season",
constraints: false,
where: {
mediaable: 'season'
}
mediaable: "season",
},
});
Season.hasMany(Media, {
foreignKey: 'mediaableId',
foreignKey: "mediaableId",
constraints: false,
as: 'medias',
scope: { mediaable: 'season' }
as: "medias",
scope: { mediaable: "season" },
});
// * MEDIA & Episode - MANY TO ONE
Media.belongsTo(Episode, {
foreignKey: 'mediaableId',
as: 'episode',
foreignKey: "mediaableId",
as: "episode",
constraints: false,
where: {
mediaable: 'episode'
}
mediaable: "episode",
},
});
Episode.hasMany(Media, {
foreignKey: 'mediaableId',
foreignKey: "mediaableId",
constraints: false,
as: 'medias',
scope: { mediaable: 'episode' }
as: "medias",
scope: { mediaable: "episode" },
});
// ! MEDIA END
// * ENTRY & SEASON - ONE TO MANY
Entry.hasMany(Season, { foreignKey: 'entry_id', as: 'seasons', onDelete: 'CASCADE', });
Season.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
Entry.hasMany(Season, {
foreignKey: "entry_id",
as: "seasons",
onDelete: "CASCADE",
});
Season.belongsTo(Entry, { foreignKey: "entry_id", as: "entry" });
// * SEASON & EPISODE - ONE TO MANY
Season.hasMany(Episode, { foreignKey: 'season_id', onDelete: 'CASCADE', });
Episode.belongsTo(Season, { foreignKey: 'season_id' });
Season.hasMany(Episode, { foreignKey: "season_id", onDelete: "CASCADE" });
Episode.belongsTo(Season, { foreignKey: "season_id" });
// * EPISODE & EPISODE_SEEN - ONE TO MANY
Episode.hasMany(EpisodeSeen, { foreignKey: 'episode_id', onDelete: 'CASCADE', });
EpisodeSeen.belongsTo(Episode, { foreignKey: 'episode_id' });
Episode.hasMany(EpisodeSeen, { foreignKey: "episode_id", onDelete: "CASCADE" });
EpisodeSeen.belongsTo(Episode, { foreignKey: "episode_id" });
// * ENTRY & EPISODE_SEEN - ONE TO MANY
Entry.hasMany(EpisodeSeen, { foreignKey: 'entry_id', onDelete: 'CASCADE', });
EpisodeSeen.belongsTo(Entry, { foreignKey: 'entry_id' });
Entry.hasMany(EpisodeSeen, { foreignKey: "entry_id", onDelete: "CASCADE" });
EpisodeSeen.belongsTo(Entry, { foreignKey: "entry_id" });
// * ----
Franchise.hasMany(Entry, { foreignKey: 'franchise_id' });
Entry.belongsTo(Franchise, { foreignKey: 'franchise_id' });
Franchise.hasMany(Entry, { foreignKey: "franchise_id" });
Entry.belongsTo(Franchise, { foreignKey: "franchise_id" });
// * ENTRY & Credit - ONE TO MANY
Entry.hasMany(Credit, { foreignKey: 'entry_id', as: 'credits', onDelete: 'CASCADE', });
Credit.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
Entry.hasMany(Credit, {
foreignKey: "entry_id",
as: "credits",
onDelete: "CASCADE",
});
Credit.belongsTo(Entry, { foreignKey: "entry_id", as: "entry" });
// * PEOPLE & CAST - ONE TO MANY
People.hasMany(Credit, { foreignKey: 'people_id', as: 'roles', onDelete: 'CASCADE', });
Credit.belongsTo(People, { foreignKey: 'people_id', as: 'person' });
People.hasMany(Credit, {
foreignKey: "people_id",
as: "roles",
onDelete: "CASCADE",
});
Credit.belongsTo(People, { foreignKey: "people_id", as: "person" });
+132 -98
View File
@@ -1,17 +1,17 @@
// Import models
const Franchise = require('../models/Franchise');
const Entry = require('../models/Entry')
const EntryId = require('../models/EntryId')
const EntryRating = require('../models/EntryRating')
const EntrySeen = require('../models/EntrySeen');
const UserCollection = require('../models/Collection');
const UserCollectionEntry = require('../models/UserCollectionEntry');
const Season = require('../models/Season');
const Episode = require('../models/Episode');
const EpisodeSeen = require('../models/EpisodeSeen');
const Genre = require('../models/Genre');
const EntryGenre = require('../models/EntryGenre');
const sequelize = require('./database');
const Franchise = require("../models/Franchise");
const Entry = require("../models/Entry");
const EntryId = require("../models/EntryId");
const EntryRating = require("../models/EntryRating");
const EntrySeen = require("../models/EntrySeen");
const UserCollection = require("../models/Collection");
const UserCollectionEntry = require("../models/UserCollectionEntry");
const Season = require("../models/Season");
const Episode = require("../models/Episode");
const EpisodeSeen = require("../models/EpisodeSeen");
const Genre = require("../models/Genre");
const EntryGenre = require("../models/EntryGenre");
const sequelize = require("./database");
// ! Hooks
// ? user collections
@@ -19,22 +19,27 @@ const sequelize = require('./database');
UserCollectionEntry.afterCreate(async (instance, options) => {
try {
// ? mimicking a change in UserCollection to trigger updatedAt property update for the UserCollection
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
const userCollections = await UserCollection.findByPk(
instance.user_collection_id,
);
await userCollections.update({
name: sequelize.literal('name')
name: sequelize.literal("name"),
});
const userCollectionEntriesCount = await UserCollectionEntry.count({
where: {
user_collection_id: instance.user_collection_id
}
})
user_collection_id: instance.user_collection_id,
},
});
// ? updating newly create UserCollectionEntry order
await instance.update({
order: userCollectionEntriesCount
})
order: userCollectionEntriesCount,
});
} catch (error) {
console.log(`Sequelize Hook Error: UserCollectionEntry.afterCreate: `, error)
console.log(
`Sequelize Hook Error: UserCollectionEntry.afterCreate: `,
error,
);
}
});
@@ -42,53 +47,64 @@ UserCollectionEntry.afterCreate(async (instance, options) => {
UserCollectionEntry.afterUpdate(async (instance, options) => {
try {
// ? mimicking a change in UserCollection to trigger updatedAt property update for the UserCollection
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
const userCollections = await UserCollection.findByPk(
instance.user_collection_id,
);
await userCollections.update({
name: sequelize.literal('name')
name: sequelize.literal("name"),
});
// ? updating other entries order
const collectionEntries = await UserCollectionEntry.findAll({
where: {
user_collection_id: instance.user_collection_id
user_collection_id: instance.user_collection_id,
},
order: [['order', 'ASC']]
})
order: [["order", "ASC"]],
});
// * changing the order of all entries that are now after the updated entry
// * changing the order of all entries that are now after the updated entry
let startOrder = instance.order + 1;
for (let i = 0; i < collectionEntries.length; i++) {
if (collectionEntries[i].order >= instance.order && collectionEntries[i].entry_id != instance.entry_id) {
await collectionEntries[i].update({ order: startOrder }, { hooks: false })
if (
collectionEntries[i].order >= instance.order &&
collectionEntries[i].entry_id != instance.entry_id
) {
await collectionEntries[i].update(
{ order: startOrder },
{ hooks: false },
);
startOrder++;
}
}
} catch (error) {
console.log(`Sequelize Hook Error: UserCollectionEntry.afterUpdate: `, error)
console.log(
`Sequelize Hook Error: UserCollectionEntry.afterUpdate: `,
error,
);
}
});
// * entry remove
UserCollectionEntry.afterDestroy(async (instance, options) => {
try {
const userCollection = await UserCollection.findByPk(instance.user_collection_id);
const userCollection = await UserCollection.findByPk(
instance.user_collection_id,
);
const entry = await Entry.findByPk(instance.entry_id);
if (!entry || !userCollection) return;
// ? mimicking a change to trigger updatedAt property update
let updates = {
name: sequelize.literal('name')
}
name: sequelize.literal("name"),
};
const collectionsEntries = await UserCollectionEntry.findAll({
where: {
user_collection_id: instance.user_collection_id
user_collection_id: instance.user_collection_id,
},
include: [
{ model: Entry, as: 'entry' },
]
})
include: [{ model: Entry, as: "entry" }],
});
// ? updating collection poster
// ? remove the poster if the collection is empty now
@@ -106,16 +122,19 @@ UserCollectionEntry.afterDestroy(async (instance, options) => {
if (instance.order != collectionsEntries.length) {
for (let i = 0; i < collectionsEntries.length; i++) {
if (collectionsEntries[i].order > instance.order) {
collectionsEntries[i].update({ order: order }, { hooks: false })
collectionsEntries[i].update({ order: order }, { hooks: false });
order++;
}
}
}
// ? updating the collection updatedAt and poster maybe
await userCollection.update(updates)
await userCollection.update(updates);
} catch (error) {
console.log(`Sequelize Hook Error: UserCollectionEntry.afterDestroy: `, error)
console.log(
`Sequelize Hook Error: UserCollectionEntry.afterDestroy: `,
error,
);
}
});
@@ -128,22 +147,28 @@ EntrySeen.afterCreate(async (instance, options) => {
// this will ensure that the entry is updated anyways to trigger the updatedAt property
let update = {
watchlist: false
watchlist: false,
};
// ? comparing the instance.date_seen with the entry date_first_seen &
// ? date_last_seen to check if they should be replaced
if (!entry.date_first_seen || instance.date_seen < new Date(entry.date_first_seen)) {
if (
!entry.date_first_seen ||
instance.date_seen < new Date(entry.date_first_seen)
) {
update.date_first_seen = instance.date_seen;
}
if (!entry.date_last_seen || instance.date_seen > new Date(entry.date_last_seen)) {
if (
!entry.date_last_seen ||
instance.date_seen > new Date(entry.date_last_seen)
) {
update.date_last_seen = instance.date_seen;
}
// * updating the entry
await entry.update(update);
} catch (error) {
console.log(`Sequelize Hook Error: EntrySeen.afterCreate: `, error)
console.log(`Sequelize Hook Error: EntrySeen.afterCreate: `, error);
}
});
@@ -158,11 +183,9 @@ EntrySeen.afterUpdate(async (instance, options) => {
// ? and the last one to: date_last_seen
const entrySeens = await EntrySeen.findAll({
where: {
entry_id: instance.entry_id
entry_id: instance.entry_id,
},
order: [
['date_seen', 'ASC']
]
order: [["date_seen", "ASC"]],
});
let update = {};
@@ -172,7 +195,7 @@ EntrySeen.afterUpdate(async (instance, options) => {
// * updating the entry
await entry.update(update);
} catch (error) {
console.log(`Sequelize Hook Error: EntrySeen.afterUpdate: `, error)
console.log(`Sequelize Hook Error: EntrySeen.afterUpdate: `, error);
}
});
@@ -187,11 +210,9 @@ EntrySeen.afterDestroy(async (instance, options) => {
// ? and the last one to: date_last_seen
const entrySeens = await EntrySeen.findAll({
where: {
entry_id: instance.entry_id
entry_id: instance.entry_id,
},
order: [
['date_seen', 'ASC']
]
order: [["date_seen", "ASC"]],
});
let update = {};
@@ -209,7 +230,7 @@ EntrySeen.afterDestroy(async (instance, options) => {
// * updating the entry
await entry.update(update);
} catch (error) {
console.log(`Sequelize Hook Error: EntrySeen.afterUpdate: `, error)
console.log(`Sequelize Hook Error: EntrySeen.afterUpdate: `, error);
}
});
@@ -224,15 +245,21 @@ EpisodeSeen.afterCreate(async (instance, options) => {
// this will ensure that the entry is updated anyways to trigger the updatedAt property
let update = {
// title: sequelize.literal('title')
watchlist: false
watchlist: false,
};
// ? comparing the instance.date_seen with the entry date_first_seen &
// ? date_last_seen to check if they should be replaced
if (!entry.date_first_seen || instance.date_seen < new Date(entry.date_first_seen)) {
if (
!entry.date_first_seen ||
instance.date_seen < new Date(entry.date_first_seen)
) {
update.date_first_seen = instance.date_seen;
}
if (!entry.date_last_seen || instance.date_seen > new Date(entry.date_last_seen)) {
if (
!entry.date_last_seen ||
instance.date_seen > new Date(entry.date_last_seen)
) {
update.date_last_seen = instance.date_seen;
}
@@ -245,22 +272,28 @@ EpisodeSeen.afterCreate(async (instance, options) => {
// this will ensure that the episode is updated anyways to trigger the updatedAt property
let episodeUpdate = {
name: sequelize.literal('name')
name: sequelize.literal("name"),
};
// ? comparing the instance.date_seen with the entry date_first_seen &
// ? date_last_seen to check if they should be replaced
if (!episode.date_first_seen || instance.date_seen < new Date(episode.date_first_seen)) {
if (
!episode.date_first_seen ||
instance.date_seen < new Date(episode.date_first_seen)
) {
episodeUpdate.date_first_seen = instance.date_seen;
}
if (!episode.date_last_seen || instance.date_seen > new Date(episode.date_last_seen)) {
if (
!episode.date_last_seen ||
instance.date_seen > new Date(episode.date_last_seen)
) {
episodeUpdate.date_last_seen = instance.date_seen;
}
// * updating the episode
await episode.update(episodeUpdate);
} catch (error) {
console.log(`Sequelize Hook Error: EpisodeSeen.afterCreate: `, error)
console.log(`Sequelize Hook Error: EpisodeSeen.afterCreate: `, error);
}
});
@@ -275,22 +308,24 @@ EpisodeSeen.afterUpdate(async (instance, options) => {
// ? for the current entry
EpisodeSeen.findAll({
attributes: [
[sequelize.fn('min', sequelize.col('date_seen')), 'minValue'],
[sequelize.fn('max', sequelize.col('date_seen')), 'maxValue']
[sequelize.fn("min", sequelize.col("date_seen")), "minValue"],
[sequelize.fn("max", sequelize.col("date_seen")), "maxValue"],
],
where: {
entry_id: instance.entry_id
}
}).then(result => {
let update = {};
entry_id: instance.entry_id,
},
})
.then((result) => {
let update = {};
update.date_first_seen = result[0].dataValues.minValue;
update.date_last_seen = result[0].dataValues.maxValue;
update.date_first_seen = result[0].dataValues.minValue;
update.date_last_seen = result[0].dataValues.maxValue;
entry.update(update);
}).catch(err => {
console.error('Error:', err);
});
entry.update(update);
})
.catch((err) => {
console.error("Error:", err);
});
// ! updating the episode
// ? getting the episode that we just created the episode-seen for
@@ -300,22 +335,24 @@ EpisodeSeen.afterUpdate(async (instance, options) => {
// ? for the current episode
EpisodeSeen.findAll({
attributes: [
[sequelize.fn('min', sequelize.col('date_seen')), 'minValue'],
[sequelize.fn('max', sequelize.col('date_seen')), 'maxValue']
[sequelize.fn("min", sequelize.col("date_seen")), "minValue"],
[sequelize.fn("max", sequelize.col("date_seen")), "maxValue"],
],
where: {
episode_id: instance.episode_id
}
}).then(result => {
let update = {};
update.date_first_seen = result[0].dataValues.minValue;
update.date_last_seen = result[0].dataValues.maxValue;
episode.update(update);
}).catch(err => {
console.error('Error:', err);
});
episode_id: instance.episode_id,
},
})
.then((result) => {
let update = {};
update.date_first_seen = result[0].dataValues.minValue;
update.date_last_seen = result[0].dataValues.maxValue;
episode.update(update);
})
.catch((err) => {
console.error("Error:", err);
});
} catch (error) {
console.log(`Sequelize Hook Error: EpisodeSeen.afterUpdate: `, error)
console.log(`Sequelize Hook Error: EpisodeSeen.afterUpdate: `, error);
}
});
@@ -331,11 +368,9 @@ EpisodeSeen.afterDestroy(async (instance, options) => {
// ? and the last one to: date_last_seen
const episodeSeens = await EpisodeSeen.findAll({
where: {
entry_id: instance.entry_id
entry_id: instance.entry_id,
},
order: [
['date_seen', 'ASC']
]
order: [["date_seen", "ASC"]],
});
let update = {};
@@ -362,11 +397,9 @@ EpisodeSeen.afterDestroy(async (instance, options) => {
// ? and the last one to: date_last_seen
const episodeSeens2 = await EpisodeSeen.findAll({
where: {
episode_id: instance.episode_id
episode_id: instance.episode_id,
},
order: [
['date_seen', 'ASC']
]
order: [["date_seen", "ASC"]],
});
let episodeUpdate = {};
@@ -378,12 +411,13 @@ EpisodeSeen.afterDestroy(async (instance, options) => {
episodeUpdate.date_last_seen = null;
} else {
episodeUpdate.date_first_seen = episodeSeens2[0].date_seen;
episodeUpdate.date_last_seen = episodeSeens2[episodeSeens2.length - 1].date_seen;
episodeUpdate.date_last_seen =
episodeSeens2[episodeSeens2.length - 1].date_seen;
}
// * updating the episode
await episode.update(episodeUpdate);
} catch (error) {
console.log(`Sequelize Hook Error: EpisodeSeen.afterUpdate: `, error)
console.log(`Sequelize Hook Error: EpisodeSeen.afterUpdate: `, error);
}
});
});