Skip to content

Conversation

ankita-p17
Copy link
Contributor

@ankita-p17 ankita-p17 commented Oct 15, 2025

This is linked to the reported bug #1485

Fetched only relevant data from table to optimize the performance

Summary by CodeRabbit

  • Bug Fixes
    • Reuses the latest available invitation, improving reliability and reducing inconsistencies.
  • Refactor
    • Streamlined invitation lookup to return a single, deterministic result.
    • Simplified logic by removing unnecessary array handling.
  • Chores
    • Reduced log verbosity by moving certain messages to debug level.

Copy link

coderabbitai bot commented Oct 15, 2025

Walkthrough

Repository method getInvitationDidByOrgId now returns a single agent_invitations record via findFirst with ordering, instead of an array via findMany. Service createConnectionInvitation aligns to consume a single object, simplifying variable naming and logging to debug. Other calls are reformatted without behavioral changes.

Changes

Cohort / File(s) Summary
Repository: single-record invitation fetch
apps/connection/src/connection.repository.ts
Changed getInvitationDidByOrgId return type from Promise<agent_invitations[]> to Promise<agent_invitations>; implementation uses findFirst with orderBy instead of findMany. Reformatted getConnectionRecords and deleteMany calls; no functional change there. Minor whitespace/comment tweaks.
Service: align to single invitation object
apps/connection/src/connection.service.ts
Updated createConnectionInvitation to handle a single agent_invitations object (no array handling). Renamed legacyinvitationDid → legacyInvitationDid. Simplified logic to directly read data.invitationDid. Adjusted logs to debug.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant Service as ConnectionService
  participant Repo as ConnectionRepository
  participant DB as Database

  Client->>Service: createConnectionInvitation(orgId, ...)
  Service->>Repo: getInvitationDidByOrgId(orgId)
  Repo->>DB: findFirst(agent_invitations where orgId) + orderBy
  DB-->>Repo: agent_invitations (single record)
  Repo-->>Service: agent_invitations
  Service->>Service: Resolve connectionInvitationDid (invitationDid || legacyInvitationDid)
  Service-->>Client: Return invitation details
  note over Service: Logging reduced to debug
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A whisk of ears, a hop through code,
I sniffed one invite on a single road.
No baskets of many, just first we find—
A tidy trail, with logs aligned.
Thump, thump—merge day cheer! 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly identifies the primary change—minimizing fetched data during connection request creation—which aligns with the PR’s objective to optimize performance by retrieving only the first invitation record rather than an array of records. It is concise, clear, and directly tied to the key modifications in the repository and service layers.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/slow-connection

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ankita-p17 ankita-p17 changed the title fix: Minimize data while getting connection fix: Minimize data while creating connection request Oct 15, 2025
Copy link

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/connection/src/connection.repository.ts (1)

376-387: Unify getInvitationDidByOrgId to return only invitationDid with null-safety

  • In apps/connection/src/connection.repository.ts, apps/verification/src/repositories/verification.repository.ts and apps/issuance/src/issuance.repository.ts: change the method to
    async getInvitationDidByOrgId(orgId: string): Promise<{ invitationDid: string } | null> {
      return this.prisma.agent_invitations.findFirst({
        where: { orgId },
        select: { invitationDid: true },
        orderBy: { createDateTime: 'desc' }  // verify: newest vs oldest
      });
    }
  • Update all callers to handle a null return before dereferencing.
  • Confirm with the business whether ordering should be 'desc' (newest) or 'asc' (oldest).
🧹 Nitpick comments (1)
apps/connection/src/connection.service.ts (1)

538-541: Use template strings for debug logs.

Second arg is treated as Logger context, not value. Interpolate values instead.

-      this.logger.debug('connectionInvitationDid:', connectionInvitationDid);
+      this.logger.debug(`connectionInvitationDid: ${connectionInvitationDid}`);
-
-      this.logger.debug(`logoUrl:::, ${organisation.logoUrl}`);
+      this.logger.debug(`logoUrl: ${organisation.logoUrl}`);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c712ea6 and 5e474cd.

📒 Files selected for processing (2)
  • apps/connection/src/connection.repository.ts (2 hunks)
  • apps/connection/src/connection.service.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
apps/connection/src/connection.repository.ts (2)
apps/connection/src/connection.service.ts (2)
  • getConnectionRecords (103-112)
  • deleteConnectionRecords (662-703)
apps/connection/src/connection.controller.ts (1)
  • deleteConnectionRecords (100-103)
🔇 Additional comments (1)
apps/connection/src/connection.repository.ts (1)

348-360: Good optimization: selecting only required fields.

Limiting fields in findMany aligns with the PR goal and reduces payload/IO. LGTM.

Comment on lines +531 to 537
let legacyInvitationDid;
if (IsReuseConnection) {
const data: agent_invitations[] = await this.connectionRepository.getInvitationDidByOrgId(orgId);
if (data && 0 < data.length) {
const [firstElement] = data;
legacyinvitationDid = firstElement?.invitationDid ?? undefined;

this.logger.log('legacyinvitationDid:', legacyinvitationDid);
}
const data: agent_invitations = await this.connectionRepository.getInvitationDidByOrgId(orgId);
legacyInvitationDid = data.invitationDid ?? undefined;
}
const connectionInvitationDid = invitationDid ? invitationDid : legacyinvitationDid;
const connectionInvitationDid = invitationDid ? invitationDid : legacyInvitationDid;

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Fix potential runtime crash when no invitation exists.

getInvitationDidByOrgId can return null; dereferencing data.invitationDid will throw. Use null-safe access and avoid forcing the type.

Apply:

-      let legacyInvitationDid;
-      if (IsReuseConnection) {
-        const data: agent_invitations = await this.connectionRepository.getInvitationDidByOrgId(orgId);
-        legacyInvitationDid = data.invitationDid ?? undefined;
-      }
-      const connectionInvitationDid = invitationDid ? invitationDid : legacyInvitationDid;
+      let legacyInvitationDid: string | undefined;
+      if (IsReuseConnection) {
+        const data = await this.connectionRepository.getInvitationDidByOrgId(orgId);
+        legacyInvitationDid = data?.invitationDid ?? undefined;
+      }
+      const connectionInvitationDid = invitationDid ?? legacyInvitationDid;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let legacyInvitationDid;
if (IsReuseConnection) {
const data: agent_invitations[] = await this.connectionRepository.getInvitationDidByOrgId(orgId);
if (data && 0 < data.length) {
const [firstElement] = data;
legacyinvitationDid = firstElement?.invitationDid ?? undefined;
this.logger.log('legacyinvitationDid:', legacyinvitationDid);
}
const data: agent_invitations = await this.connectionRepository.getInvitationDidByOrgId(orgId);
legacyInvitationDid = data.invitationDid ?? undefined;
}
const connectionInvitationDid = invitationDid ? invitationDid : legacyinvitationDid;
const connectionInvitationDid = invitationDid ? invitationDid : legacyInvitationDid;
let legacyInvitationDid: string | undefined;
if (IsReuseConnection) {
const data = await this.connectionRepository.getInvitationDidByOrgId(orgId);
legacyInvitationDid = data?.invitationDid ?? undefined;
}
const connectionInvitationDid = invitationDid ?? legacyInvitationDid;
🤖 Prompt for AI Agents
In apps/connection/src/connection.service.ts around lines 531 to 537, the call
to this.connectionRepository.getInvitationDidByOrgId(orgId) can return null so
dereferencing data.invitationDid may throw; update the code to check that the
returned value is not null (or use optional chaining) before accessing
invitationDid and avoid forcing the type—assign legacyInvitationDid =
data?.invitationDid ?? undefined (or explicitly handle the null return by
setting legacyInvitationDid = undefined when data is null) so the subsequent
connectionInvitationDid computation is safe.

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.

1 participant