Skip to content

Commit dd28d78

Browse files
committed
fix(V2): exclude cross-ref FKs from join table ownership resolution
Join tables (project_methodology, stakeholder_projects, unit_label) reference entities owned by different organizations. The ownership guard was following all parent FKs, collecting non-home org UIDs from the referenced side, and blocking legitimate deletes/cascade deletes. Only follow the owning-side FK for ownership resolution on these tables while leaving non-join table behavior unchanged.
1 parent c0dce25 commit dd28d78

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

src/models/v2/staging-v2.model.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ class StagingV2 extends Model {
158158
};
159159
}
160160

161+
static getJoinTableOwnershipFks() {
162+
return {
163+
project_methodology: new Set(['cadTrustProjectId']),
164+
stakeholder_projects: new Set(['cadTrustProjectId']),
165+
unit_label: new Set(['cadTrustUnitId']),
166+
};
167+
}
168+
169+
static getExcludedFieldsForTable(table) {
170+
const ownershipFks = StagingV2.getJoinTableOwnershipFks()[table];
171+
if (!ownershipFks) return new Set();
172+
173+
return new Set(
174+
StagingV2.getOwnershipParentModels()
175+
.map(([fieldName]) => fieldName)
176+
.filter((f) => !ownershipFks.has(f)),
177+
);
178+
}
179+
161180
static getOwnershipParentModels() {
162181
return [
163182
['cadTrustProjectId', ProjectV2],
@@ -311,8 +330,11 @@ class StagingV2 extends Model {
311330
: null;
312331

313332
if (existingRecord) {
333+
const tableExcludedFields = StagingV2.getExcludedFieldsForTable(values.table);
314334
await StagingV2.assertOwnerOrgUidsAreHome(
315-
await StagingV2.collectOwnerOrgUids(existingRecord, options),
335+
await StagingV2.collectOwnerOrgUids(
336+
existingRecord, options, new Set(), 0, false, [], tableExcludedFields,
337+
),
316338
values.table,
317339
true,
318340
);
@@ -332,6 +354,8 @@ class StagingV2 extends Model {
332354
);
333355
}
334356

357+
const updateExcluded = StagingV2.getExcludedFieldsForTable(values.table);
358+
updateExcluded.add(primaryKeyApiField);
335359
await StagingV2.assertOwnerOrgUidsAreHome(
336360
await StagingV2.collectOwnerOrgUids(
337361
recordData,
@@ -340,7 +364,7 @@ class StagingV2 extends Model {
340364
0,
341365
true,
342366
unresolvedFields,
343-
new Set([primaryKeyApiField]),
367+
updateExcluded,
344368
),
345369
values.table,
346370
payloadHasOwnershipFields,

0 commit comments

Comments
 (0)