🔧 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 }, source: { type: String, default: null },
markEntireVolumeWanted: Boolean, markEntireVolumeWanted: Boolean,
issues: { issues: {
type: [ type: [
{ {
_id: false, // Disable automatic ObjectId creation for each issue
id: Number, id: Number,
url: String, url: String,
image: { type: Array, default: [] }, image: { type: Array, default: [] },
}, },
], ],
default: null, // Set default to null for issues default: null,
}, },
volume: { volume: {
type: { 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, name: String,
}, },
default: null, // Set default to null for volume default: null,
}, },
}); },
{ _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) {
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
); );
switch (ctx.params.importType) { if (existingVolume) {
case "new": // Check for existing issues
console.log(comicMetadata); let existingIssues = [];
return await Comic.create(comicMetadata); if (wanted.issues && wanted.issues.length > 0) {
case "update": existingIssues = wanted.issues.filter(
return await Comic.findOneAndUpdate( (issue: any) =>
{ existingVolume.wanted.issues.some(
"acquisition.directconnect.downloads.bundleId": (existing: any) =>
ctx.params.bundleId, 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,
}, },
comicMetadata, $addToSet: {},
};
if (wanted.issues && wanted.issues.length > 0) {
update.$addToSet["wanted.issues"] = {
$each: wanted.issues,
};
}
const updatedDocument =
await Comic.findOneAndUpdate(
condition,
update,
{ {
upsert: true, upsert: true,
new: true, new: true,
setDefaultsOnInsert: true,
overwrite: false,
} }
); );
default:
return 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
); );
} }