@@ -486,188 +486,7 @@ export class GenericController {
486486 console . log ( "Database connection closed successfully" ) ;
487487 }
488488
489- // static async createFullPaper(data: any) {
490- // console.log("Processing full paper creation:", data);
491-
492- // // Extract and validate main paper data
493- // const paperData = { ...data };
494- // delete paperData.authors;
495- // delete paperData.parts;
496- // delete paperData.tids;
497- // delete paperData.sees;
498- // delete paperData.dds;
499-
500- // // Find or Create Paper
501- // const [paper, paperCreated] =
502- // await models.Paper.findOrCreate({
503- // where: { name: paperData.name, year: paperData.year },
504- // defaults: paperData,
505- // })as [Paper, boolean];
506-
507- // console.log(
508- // `Paper ${paperCreated ? "created" : "found"}:`,
509- // paper.get({ plain: true }),
510- // );
511-
512- // // Helper function to find or create entities dynamically
513- // async function findOrCreateEntity(modelName: string, items: any[]) {
514- // if (!items || !Array.isArray(items)) return [];
515-
516- // return await Promise.all(
517- // items.map(async (item) => {
518- // const searchCriteria = { ...item };
519- // delete searchCriteria.id;
520- // delete searchCriteria.createdAt;
521- // delete searchCriteria.updatedAt;
522-
523- // const [entity] = await models[modelName].findOrCreate({
524- // where: searchCriteria,
525- // defaults: item,
526- // });
527-
528- // return entity;
529- // }),
530- // );
531- // }
532-
533- // // Find or Create Authors
534- // const authors = await findOrCreateEntity("Author", data.authors) as Author[];
535- // if (authors.length) await paper.addAuthors(authors);
536-
537- // // Find or Create Parts
538- // const parts = await findOrCreateEntity("Part", data.parts) as Part[];
539- // if (parts.length) await paper.addParts(parts);
540-
541- // // Find or Create TIDs and Link to Paper & Parts
542- // const tids = await findOrCreateEntity("Tid", data.tids) as Tid[];
543- // for (const tid of tids) {
544- // await tid.setPaper(paper);
545- // const linkedPart = parts.find((p) => p.name === tid.name);
546- // if (linkedPart) await tid.setPart(linkedPart);
547- // }
548-
549- // // Find or Create SEEs and Link to Paper & Parts
550- // const sees = await findOrCreateEntity("See", data.sees) as See[];
551- // for (const see of sees) {
552- // await see.setPaper(paper);
553- // const linkedPart = parts.find((p) => p.name === see.name);
554- // if (linkedPart) await see.setPart(linkedPart);
555- // }
556-
557- // // Find or Create DDs and Link to Paper & Parts
558- // const dds = await findOrCreateEntity("Dd", data.dds) as Dd[];
559- // for (const dd of dds) {
560- // await dd.setPaper(paper);
561- // const linkedPart = parts.find((p) => p.name === dd.name);
562- // if (linkedPart) await dd.setPart(linkedPart);
563- // }
564-
565- // console.log("Paper and all related entities processed successfully.");
566-
567- // return {
568- // paper: paper.get({ plain: true }),
569- // authors: authors.map((a) => a.get({ plain: true })),
570- // parts: parts.map((p) => p.get({ plain: true })),
571- // tids: tids.map((t) => t.get({ plain: true })),
572- // sees: sees.map((s) => s.get({ plain: true })),
573- // dds: dds.map((d) => d.get({ plain: true })),
574- // };
575- // }
576-
577- // static async createFullPaper(data: any) {
578- // console.log("Processing full paper creation:", data);
579-
580- // // Extract and validate main paper data
581- // const paperData = { ...data };
582- // delete paperData.authors;
583- // delete paperData.parts;
584-
585- // // Find or Create Paper
586- // const [paper, paperCreated] = (await models.Paper.findOrCreate({
587- // where: { name: paperData.name, year: paperData.year },
588- // defaults: paperData,
589- // })) as [Paper, boolean];
590-
591- // console.log(
592- // `Paper ${paperCreated ? "created" : "found"}:`,
593- // paper.get({ plain: true }),
594- // );
595-
596- // async function findOrCreateEntity(modelName: string, items: any[]) {
597- // if (!items || !Array.isArray(items)) return [];
598-
599- // const results = [];
600- // for (const item of items) {
601- // const searchCriteria = { ...item };
602- // delete searchCriteria.id;
603- // delete searchCriteria.createdAt;
604- // delete searchCriteria.updatedAt;
605-
606- // const [entity] = await models[modelName].findOrCreate({
607- // where: searchCriteria,
608- // defaults: item,
609- // });
610-
611- // results.push(entity);
612- // }
613-
614- // return results;
615- // }
616-
617- // // Find or Create Authors and Associate with Paper
618- // const authors = (await findOrCreateEntity(
619- // "Author",
620- // data.authors,
621- // )) as Author[];
622- // if (authors.length) await paper.addAuthors(authors);
623-
624- // // Find or Create Parts and Associate with Paper
625- // const parts = (await findOrCreateEntity("Part", data.parts)) as Part[];
626- // if (parts.length) await paper.addParts(parts);
627-
628- // // Process tids, sees, and dds inside each part
629- // for (const partData of data.parts) {
630- // // Find the corresponding Part entity
631- // const part = parts.find((p) => p.name === partData.name);
632- // if (!part) continue;
633-
634- // // Process TIDs
635- // if (partData.tids && partData.tids.length > 0) {
636- // const tids = (await findOrCreateEntity("Tid", partData.tids)) as Tid[];
637- // for (const tid of tids) {
638- // await tid.setPaper(paper); // Associate with paper
639- // await tid.setPart(part); // Associate with part
640- // }
641- // }
642-
643- // // Process SEEs
644- // if (partData.sees && partData.sees.length > 0) {
645- // const sees = (await findOrCreateEntity("See", partData.sees)) as See[];
646- // for (const see of sees) {
647- // await see.setPaper(paper); // Associate with paper
648- // await see.setPart(part); // Associate with part
649- // }
650- // }
651-
652- // // Process DDs
653- // if (partData.dds && partData.dds.length > 0) {
654- // const dds = (await findOrCreateEntity("Dd", partData.dds)) as Dd[];
655- // for (const dd of dds) {
656- // await dd.setPaper(paper); // Associate with paper
657- // await dd.setPart(part); // Associate with part
658- // }
659- // }
660- // }
661-
662- // console.log("Paper and all related entities processed successfully.");
663-
664- // return {
665- // paper: paper.get({ plain: true }),
666- // authors: authors.map((a) => a.get({ plain: true })),
667- // parts: parts.map((p) => p.get({ plain: true })),
668- // };
669- // }
670-
489+ /** Create full paper as well as related instance by giving full object */
671490 static async createFullPaper ( data : any ) {
672491 console . log ( "Processing full paper creation:" , data ) ;
673492
@@ -758,9 +577,23 @@ export class GenericController {
758577 console . log ( "Paper and all related entities processed successfully." ) ;
759578
760579 return {
761- paper : paper . get ( { plain : true } ) ,
580+ id : paper . id ,
581+ name : paper . name ,
582+ year : paper . year ,
583+ createdAt : paper . createdAt ,
584+ updatedAt : paper . updatedAt ,
762585 authors : authors . map ( ( a ) => a . get ( { plain : true } ) ) ,
763- parts : parts . map ( ( p ) => p . get ( { plain : true } ) ) ,
586+ parts : await Promise . all (
587+ parts . map ( async ( p ) => {
588+ return models . Part . findByPk ( p . getDataValue ( "id" ) , {
589+ include : [
590+ { model : models . Tid } ,
591+ { model : models . See } ,
592+ { model : models . Dd } ,
593+ ] ,
594+ } ) . then ( ( part ) => part ?. get ( { plain : true } ) ) ;
595+ } ) ,
596+ ) ,
764597 } ;
765598 }
766599}
0 commit comments