Skip to content

Commit ef0afc8

Browse files
committed
Update creation of tids,sees, and dds
1 parent 28faccf commit ef0afc8

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

server/src/generic-controller.ts

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

server/src/models.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ class Paper extends Model<
6060
declare addTid: HasManyAddAssociationMixin<Tid, number>;
6161
declare addTids: HasManyAddAssociationsMixin<Tid, number>;
6262
declare setTids: HasManyAddAssociationsMixin<Tid, number>;
63+
declare removeTids: HasManyAddAssociationsMixin<Tid, number>;
6364
declare addSee: HasManyAddAssociationMixin<See, number>;
6465
declare addSees: HasManyAddAssociationsMixin<See, number>;
6566
declare setSees: HasManyAddAssociationsMixin<See, number>;
67+
declare removeSees: HasManyAddAssociationsMixin<See, number>;
6668
declare addDd: HasManyAddAssociationMixin<Dd, number>;
6769
declare addDds: HasManyAddAssociationsMixin<Dd, number>;
6870
declare setDds: HasManyAddAssociationsMixin<Dd, number>;
71+
declare removeDds: HasManyAddAssociationsMixin<Dd, number>;
6972
}
7073

7174
Paper.init(
@@ -100,12 +103,15 @@ class Part extends Model<InferAttributes<Part>, InferCreationAttributes<Part>> {
100103
declare addTid: HasManyAddAssociationMixin<Tid, number>;
101104
declare addTids: HasManyAddAssociationsMixin<Tid, number>;
102105
declare setTids: HasManyAddAssociationsMixin<Tid, number>;
106+
declare removeTids: HasManyAddAssociationsMixin<Tid, number>;
103107
declare addSee: HasManyAddAssociationMixin<See, number>;
104108
declare addSees: HasManyAddAssociationsMixin<See, number>;
105109
declare setSees: HasManyAddAssociationsMixin<See, number>;
110+
declare removeSees: HasManyAddAssociationsMixin<See, number>;
106111
declare addDd: HasManyAddAssociationMixin<Dd, number>;
107112
declare addDds: HasManyAddAssociationsMixin<Dd, number>;
108113
declare setDds: HasManyAddAssociationsMixin<Dd, number>;
114+
declare removeDds: HasManyAddAssociationsMixin<Dd, number>;
109115
}
110116

111117
Part.init(

0 commit comments

Comments
 (0)