From d7f3d3a7cfac29b51bf5fa1b3a177f43d98b9d26 Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Tue, 16 Apr 2024 22:41:33 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Modified=20Comic=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/comic.model.ts | 47 +++++++------- services/library.service.ts | 125 +++++++++++++++++++++++++++--------- 2 files changed, 119 insertions(+), 53 deletions(-) diff --git a/models/comic.model.ts b/models/comic.model.ts index 94725f6..e5a5da9 100644 --- a/models/comic.model.ts +++ b/models/comic.model.ts @@ -55,29 +55,34 @@ const DirectConnectBundleSchema = mongoose.Schema({ size: String, type: {}, }); -const wantedSchema = new mongoose.Schema({ - source: { type: String, default: null }, - markEntireVolumeWanted: Boolean, - issues: { - type: [ - { +const wantedSchema = mongoose.Schema( + { + source: { type: String, default: null }, + markEntireVolumeWanted: Boolean, + issues: { + type: [ + { + _id: false, // Disable automatic ObjectId creation for each issue + id: Number, + url: String, + image: { type: Array, default: [] }, + }, + ], + default: null, + }, + volume: { + type: { + _id: false, // Disable automatic ObjectId creation for volume id: Number, url: String, image: { type: Array, default: [] }, + name: String, }, - ], - default: null, // Set default to null for issues - }, - volume: { - type: { - id: Number, - url: String, - image: { type: Array, default: [] }, - name: String, + default: null, }, - default: null, // Set default to null for volume }, -}); + { _id: false } +); // Disable automatic ObjectId creation for the wanted object itself const ComicSchema = mongoose.Schema( { @@ -93,18 +98,12 @@ const ComicSchema = mongoose.Schema( }, sourcedMetadata: { comicInfo: { type: mongoose.Schema.Types.Mixed, default: {} }, - comicvine: { - type: Object, - es_indexed: true, - default: {}, - }, - shortboxed: {}, + comicvine: { type: mongoose.Schema.Types.Mixed, default: {} }, // Set as a freeform object locg: { type: LOCGSchema, es_indexed: true, default: {}, }, - gcd: {}, }, rawFileDetails: { type: RawFileDetailsSchema, diff --git a/services/library.service.ts b/services/library.service.ts index 9f8d937..1199cd7 100644 --- a/services/library.service.ts +++ b/services/library.service.ts @@ -248,10 +248,7 @@ export default class ImportService extends Service { payload: { _id?: string; sourcedMetadata: { - comicvine?: { - volume: { api_detail_url: string }; - volumeInformation: {}; - }; + comicvine?: any; locg?: {}; }; inferredMetadata: { @@ -262,7 +259,7 @@ export default class ImportService extends Service { }; wanted: { issues: []; - volume: {}; + volume: { id: number }; source: string; markEntireVolumeWanted: Boolean; }; @@ -275,36 +272,106 @@ export default class ImportService extends Service { }> ) { try { - let volumeDetails; - const comicMetadata = ctx.params.payload; + console.log( + JSON.stringify(ctx.params.payload, null, 4) + ); + const { payload } = ctx.params; + const { wanted } = payload; console.log("Saving to Mongo..."); - console.log( - `Import type: [${ctx.params.importType}]` - ); - switch (ctx.params.importType) { - case "new": - console.log(comicMetadata); - return await Comic.create(comicMetadata); - case "update": - return await Comic.findOneAndUpdate( - { - "acquisition.directconnect.downloads.bundleId": - ctx.params.bundleId, - }, - comicMetadata, - { - upsert: true, - new: true, - } - ); - default: - return false; + + let condition = {}; + if (wanted.volume && wanted.volume.id) { + condition["wanted.volume.id"] = + wanted.volume.id; } + + if (Object.keys(condition).length === 0) { + console.log("No valid identifier for upsert."); + return { + success: false, + message: + "No valid volume identifier provided.", + }; + } + + // Check if the volume or issues already exist + const existingVolume = await Comic.findOne( + condition + ); + if (existingVolume) { + // Check for existing issues + let existingIssues = []; + if (wanted.issues && wanted.issues.length > 0) { + existingIssues = wanted.issues.filter( + (issue: any) => + existingVolume.wanted.issues.some( + (existing: any) => + existing.id === issue.id + ) + ); + } + + if (existingIssues.length > 0) { + return { + success: false, + message: `Issue(s) with ID(s) ${existingIssues + .map((i) => i.id) + .join(", ")} already exist.`, + }; + } + + return { + success: false, + message: + "Volume with this ID already exists.", + }; + } + + // Perform the upsert operation if no existing volume or issues were found + let update = { + $set: { + "wanted.source": payload.wanted.source, + "wanted.markEntireVolumeWanted": + payload.wanted.markEntireVolumeWanted, + "wanted.volume": payload.wanted.volume, + "sourcedMetadata.comicvine": + payload.sourcedMetadata.comicvine, + }, + $addToSet: {}, + }; + + if (wanted.issues && wanted.issues.length > 0) { + update.$addToSet["wanted.issues"] = { + $each: wanted.issues, + }; + } + + const updatedDocument = + await Comic.findOneAndUpdate( + condition, + update, + { + upsert: true, + new: true, + setDefaultsOnInsert: true, + overwrite: false, + } + ); + + console.log( + "Document upserted with new issues:", + updatedDocument + ); + return { + success: true, + message: "Document updated successfully.", + data: updatedDocument, + }; } catch (error) { console.log(error); throw new Errors.MoleculerError( - "Import failed.", + "Operation failed.", 500 ); }