Skip to content

update segment service methods for typeorm 1.0 cascading behavior - #3253

Open
bcb37 wants to merge 5 commits into
devfrom
bugfix/3252-typeorm1.0-segment-CRUD
Open

update segment service methods for typeorm 1.0 cascading behavior#3253
bcb37 wants to merge 5 commits into
devfrom
bugfix/3252-typeorm1.0-segment-CRUD

Conversation

@bcb37

@bcb37 bcb37 commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates SegmentService to work around TypeORM 1.0+ cascade behavior changes by avoiding save() with loaded relations and instead managing subSegments relationships via the QueryBuilder relation API.

Changes:

  • Updated list creation to link parent/child segments using relation('subSegments')...add(...) rather than persisting loaded relations.
  • Updated segment upsert pipeline to (a) clear old subSegments relationships explicitly and (b) re-attach new subSegments via the relation API rather than cascading via save().
  • Updated unit tests to reflect the new repository find() existence-check flow and relation-based linking.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
packages/backend/src/api/services/SegmentService.ts Refactors segment/list upsert and relationship management to avoid cascade side effects by using TypeORM relation APIs.
packages/backend/test/unit/services/SegmentService.test.ts Adjusts mocks and expectations for the new find() + relation-API based behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/backend/src/api/services/SegmentService.ts Outdated
Comment thread packages/backend/src/api/services/SegmentService.ts
Comment thread packages/backend/src/api/services/SegmentService.ts Outdated
Comment thread packages/backend/src/api/services/SegmentService.ts Outdated
@bcb37 bcb37 linked an issue Jul 28, 2026 that may be closed by this pull request
bcb37 and others added 4 commits July 28, 2026 16:53
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (5)

packages/backend/test/unit/services/SegmentService.test.ts:1082

  • This test verifies repo.save omits subSegments, but it doesn't verify the new QueryBuilder relation update path (.relation(...).add(...)) ran. Asserting .add(...) was called makes the test cover the behavior change this PR introduces.
      // Verify that repo.save was called WITHOUT subSegments (relations are added via QueryBuilder)
      expect(repo.save).toHaveBeenCalledWith({

packages/backend/test/unit/services/SegmentService.test.ts:1119

  • This test verifies repo.save omits subSegments, but doesn't assert that the relation is applied via QueryBuilder (.add(...)). Adding an assertion here helps ensure missing subsegment IDs are skipped and valid ones still get linked.
      // Verify repo.save was called WITHOUT subSegments (relations are added via QueryBuilder)
      expect(repo.save).toHaveBeenCalledWith({

packages/backend/src/api/services/SegmentService.ts:469

  • In deleteList, the current guard throws List ... not found even when the parent segment itself is missing. This makes the error misleading and harder to debug.
    const parentSegment = await this.getSegmentById(parentSegmentId, logger);
    if (!parentSegment || !parentSegment.subSegments.some((subSegment) => subSegment.id === segmentId)) {
      throw new Error(`List ${segmentId} not found in parent segment ${parentSegmentId}`);
    }

packages/backend/src/api/services/SegmentService.ts:1006

  • The comment says missing subsegment references are skipped “silently”, but the code logs a warning for each missing ID. Update the comment so it matches the actual behavior.
          // Skip unknown subsegment references silently — validation already warned about these
          logger.warn({ message: `SubSegment: ${subSegmentId} not found, skipping reference.` });

packages/backend/test/unit/services/SegmentService.test.ts:1043

  • These updated tests only assert that repo.save is called without subSegments, but they don't assert that the relationship is actually set via the QueryBuilder. Adding an assertion that .add(...) was called will prevent regressions where relations stop being created.

This issue also appears in the following locations of the same file:

  • line 1081
  • line 1118
      // Verify repo.save was called WITHOUT subSegments (relations are added via QueryBuilder)
      expect(repo.save).toHaveBeenCalledWith({

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding subsegments to a segment removes lists from the subsegments

2 participants