@@ -660,6 +660,8 @@ export class GenericController {
660660 if ( data . parts ) {
661661 const parts = ( await this . manageEntities ( "Part" , data . parts ) ) as Part [ ] ;
662662 append ? await paper . addParts ( parts ) : await paper . setParts ( parts ) ;
663+ console . log ( "gotty here" ) ;
664+ console . log ( data . parts ) ;
663665 await this . handleTestsForParts ( parts , paper , data . parts , append ) ;
664666 }
665667 }
@@ -674,42 +676,107 @@ export class GenericController {
674676 append : boolean ,
675677 ) {
676678 for ( const partData of partsData ) {
677- const part = parts . find ( ( p ) => p . name === partData . name ) ;
679+ let part ;
680+ if ( partData . name ) {
681+ part = parts . find ( ( p ) => p . name === partData . name ) ;
682+ } else {
683+ part = parts . find ( ( p ) => p . id === partData . id ) ;
684+ }
678685 if ( ! part ) continue ;
679686
680687 console . log ( `Processing part: ${ partData . name } ` ) ;
681688
682689 // Handle tids
683690 if ( Array . isArray ( partData . tids ) ) {
691+ console . log ( "Processing tids for part:" , partData . id ) ;
692+
684693 const tids = ( await this . manageEntities (
685694 "Tid" ,
686695 partData . tids ,
687696 true ,
688697 ) ) as Tid [ ] ;
689- append ? await part . addTids ( tids ) : await part . setTids ( tids ) ;
690- append ? await paper . addTids ( tids ) : await paper . setTids ( tids ) ;
698+
699+ if ( append ) {
700+ await part . addTids ( tids ) ;
701+ await paper . addTids ( tids ) ;
702+ } else {
703+ // Find only the tids that belong to this specific Paper-Part combo
704+ const existingTids = await Tid . findAll ( {
705+ where : { partId : part . id , paperId : paper . id } , // Ensure we only target the current Paper-Part
706+ } ) ;
707+
708+ // Remove only these specific tids
709+ await part . removeTids ( existingTids ) ;
710+ await paper . removeTids ( existingTids ) ;
711+
712+ // If new tids are provided, add them back
713+ if ( tids . length > 0 ) {
714+ await part . addTids ( tids ) ;
715+ await paper . addTids ( tids ) ;
716+ }
717+ }
691718 }
692719
693720 // Handle sees
694721 if ( Array . isArray ( partData . sees ) ) {
722+ console . log ( "Processing sees for part:" , partData . id ) ;
723+
695724 const sees = ( await this . manageEntities (
696725 "See" ,
697726 partData . sees ,
698727 true ,
699728 ) ) as See [ ] ;
700- append ? await part . addSees ( sees ) : await part . setSees ( sees ) ;
701- append ? await paper . addSees ( sees ) : await paper . setSees ( sees ) ;
729+
730+ if ( append ) {
731+ await part . addSees ( sees ) ;
732+ await paper . addSees ( sees ) ;
733+ } else {
734+ // Find only the sees that belong to this specific Paper-Part combo
735+ const existingSees = await See . findAll ( {
736+ where : { partId : part . id , paperId : paper . id } , // Ensure we only target the current Paper-Part
737+ } ) ;
738+
739+ // Remove only these specific sees
740+ await part . removeSees ( existingSees ) ;
741+ await paper . removeSees ( existingSees ) ;
742+
743+ // If new sees are provided, add them back
744+ if ( sees . length > 0 ) {
745+ await part . addSees ( sees ) ;
746+ await paper . addSees ( sees ) ;
747+ }
748+ }
702749 }
703750
704751 // Handle dds
705752 if ( Array . isArray ( partData . dds ) ) {
753+ console . log ( "Processing dds for part:" , partData . id ) ;
754+
706755 const dds = ( await this . manageEntities (
707756 "Dd" ,
708757 partData . dds ,
709758 true ,
710759 ) ) as Dd [ ] ;
711- append ? await part . addDds ( dds ) : await part . setDds ( dds ) ;
712- append ? await paper . addDds ( dds ) : await paper . setDds ( dds ) ;
760+
761+ if ( append ) {
762+ await part . addDds ( dds ) ;
763+ await paper . addDds ( dds ) ;
764+ } else {
765+ // Find only the dds that belong to this specific Paper-Part combo
766+ const existingDds = await Dd . findAll ( {
767+ where : { partId : part . id , paperId : paper . id } , // Ensure we only target the current Paper-Part
768+ } ) ;
769+
770+ // Remove only these specific dds
771+ await part . removeDds ( existingDds ) ;
772+ await paper . removeDds ( existingDds ) ;
773+
774+ // If new dds are provided, add them back
775+ if ( dds . length > 0 ) {
776+ await part . addDds ( dds ) ;
777+ await paper . addDds ( dds ) ;
778+ }
779+ }
713780 }
714781 }
715782 }
0 commit comments