🔧 Modified Comic model

This commit is contained in:
2024-04-16 22:41:33 -05:00
parent 94cb95f4bf
commit d7f3d3a7cf
2 changed files with 119 additions and 53 deletions

View File

@@ -55,29 +55,34 @@ const DirectConnectBundleSchema = mongoose.Schema({
size: String, size: String,
type: {}, type: {},
}); });
const wantedSchema = new mongoose.Schema({ const wantedSchema = mongoose.Schema(
source: { type: String, default: null }, {
markEntireVolumeWanted: Boolean, source: { type: String, default: null },
issues: { markEntireVolumeWanted: Boolean,
type: [ 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, id: Number,
url: String, url: String,
image: { type: Array, default: [] }, image: { type: Array, default: [] },
name: String,
}, },
], default: null,
default: null, // Set default to null for issues
},
volume: {
type: {
id: Number,
url: String,
image: { type: Array, default: [] },
name: String,
}, },
default: null, // Set default to null for volume
}, },
}); { _id: false }
); // Disable automatic ObjectId creation for the wanted object itself
const ComicSchema = mongoose.Schema( const ComicSchema = mongoose.Schema(
{ {
@@ -93,18 +98,12 @@ const ComicSchema = mongoose.Schema(
}, },
sourcedMetadata: { sourcedMetadata: {
comicInfo: { type: mongoose.Schema.Types.Mixed, default: {} }, comicInfo: { type: mongoose.Schema.Types.Mixed, default: {} },
comicvine: { comicvine: { type: mongoose.Schema.Types.Mixed, default: {} }, // Set as a freeform object
type: Object,
es_indexed: true,
default: {},
},
shortboxed: {},
locg: { locg: {
type: LOCGSchema, type: LOCGSchema,
es_indexed: true, es_indexed: true,
default: {}, default: {},
}, },
gcd: {},
}, },
rawFileDetails: { rawFileDetails: {
type: RawFileDetailsSchema, type: RawFileDetailsSchema,

View File

@@ -248,10 +248,7 @@ export default class ImportService extends Service {
payload: { payload: {
_id?: string; _id?: string;
sourcedMetadata: { sourcedMetadata: {
comicvine?: { comicvine?: any;
volume: { api_detail_url: string };
volumeInformation: {};
};
locg?: {}; locg?: {};
}; };
inferredMetadata: { inferredMetadata: {
@@ -262,7 +259,7 @@ export default class ImportService extends Service {
}; };
wanted: { wanted: {
issues: []; issues: [];
volume: {}; volume: { id: number };
source: string; source: string;
markEntireVolumeWanted: Boolean; markEntireVolumeWanted: Boolean;
}; };
@@ -275,36 +272,106 @@ export default class ImportService extends Service {
}> }>
) { ) {
try { try {
let volumeDetails; console.log(
const comicMetadata = ctx.params.payload; JSON.stringify(ctx.params.payload, null, 4)
);
const { payload } = ctx.params;
const { wanted } = payload;
console.log("Saving to Mongo..."); console.log("Saving to Mongo...");
console.log(
`Import type: [${ctx.params.importType}]` let condition = {};
); if (wanted.volume && wanted.volume.id) {
switch (ctx.params.importType) { condition["wanted.volume.id"] =
case "new": wanted.volume.id;
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;
} }
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) { } catch (error) {
console.log(error); console.log(error);
throw new Errors.MoleculerError( throw new Errors.MoleculerError(
"Import failed.", "Operation failed.",
500 500
); );
} }