Skip to content

refactor: remove redundant code in NPC helpers and event sorting#710

Open
kamilwronka wants to merge 1 commit intodevelopfrom
refactor/small-cleanup-redundant-code
Open

refactor: remove redundant code in NPC helpers and event sorting#710
kamilwronka wants to merge 1 commit intodevelopfrom
refactor/small-cleanup-redundant-code

Conversation

@kamilwronka
Copy link
Copy Markdown
Contributor

@kamilwronka kamilwronka commented Apr 8, 2026

Summary

  • Remove redundant property overrides in composeNpcFromGame — the spread ...npc already includes nick, prof, wt, lvl, type from GameNpc, so re-assigning them with identical values was dead code
  • Remove unnecessary optional chaining on getTime method in find-active-event-heroes-by-npc.ts — changed createdAt?.getTime?.() to createdAt?.getTime() since if createdAt is a Date, getTime is guaranteed to exist

Safety

  • Both changes are behavior-preserving: no logic changes, only removal of redundancy
  • All existing tests pass (599 API tests, 224 game-client tests)
  • Lint and format checks pass on changed files

Files touched

  • apps/game-client/src/hooks/game-events/helpers/npc.helpers.ts
  • apps/api/src/events/utils/find-active-event-heroes-by-npc.ts

Residual risks

  • None — changes are trivially safe

Test plan

  • Pre-commit hooks pass (lint, format, tests)
  • All unit tests pass in both affected packages

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Fixed event start time calculation for proper event sorting
    • Corrected NPC data field handling in the game client
  • Refactor

    • Improved error handling consistency across Discord integration
    • Streamlined guild lookup and retrieval operations
    • Optimized message emission flow for better responsiveness

- Remove redundant property overrides in composeNpcFromGame where nick,
  prof, wt, lvl, type were re-assigned from npc after already being
  spread via ...npc
- Remove unnecessary optional chaining on getTime method call in
  find-active-event-heroes-by-npc.ts (createdAt?.getTime?.() to
  createdAt?.getTime() — if createdAt exists, getTime is guaranteed)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 8, 2026

📝 Walkthrough

Walkthrough

Five separate refactoring and cleanup changes across chat, Discord, event, guild, and game client services. Includes removing an await statement, extracting duplicated error-checking logic, fixing optional-chaining syntax, consolidating guild-lookup methods, and removing unused field copies from NPC composition.

Changes

Cohort / File(s) Summary
Chat Service Async Flow
apps/api/src/chat/chat.service.ts
Removed await from emitMessage(msg) invocation, causing sendMessage to return immediately after Redis operations without waiting for AMQP publish completion.
Discord Service Error Handling
apps/api/src/discord/discord.service.ts
Added private isNotFoundStatus helper method and replaced two inline 404-error checks with calls to it in getGuildMember error handling.
Event Utils Syntax Fix
apps/api/src/events/utils/find-active-event-heroes-by-npc.ts
Updated optional-chaining syntax in sort comparator from createdAt?.getTime?.() to createdAt?.getTime() for event start-time extraction.
Guild Service Refactoring
apps/api/src/guilds/guilds.service.ts
Renamed variable comparedGuilds to allGuildsInDiscord; inlined getGuildByIdInternal logic directly into getGuildById and removed the helper method.
NPC Composition Field Removal
apps/game-client/src/hooks/game-events/helpers/npc.helpers.ts
Removed field copying for nick, prof, wt, lvl, and type from input NPC in composeNpcFromGame; other field assignments remain unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 A rabbit's ode to cleaner code
Awaits removed, the flow runs free,
Four hundred fours now unified,
Methods inlined, simplicity earned,
Fields pruned clean—less is more today! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary refactoring changes: removing redundant code in NPC helpers and event sorting utilities.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/small-cleanup-redundant-code

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.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying lootlog-landing with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2f998cb
Status: ✅  Deploy successful!
Preview URL: https://1093ba6a.lootlog-landing.pages.dev
Branch Preview URL: https://refactor-small-cleanup-redun.lootlog-landing.pages.dev

View logs

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
lootlog-docs 2f998cb Commit Preview URL

Branch Preview URL
Apr 08 2026, 05:13 PM

Copy link
Copy Markdown

@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

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/api/src/guilds/guilds.service.ts`:
- Around line 107-112: The current check uses allGuildsInDiscord =
guilds.every(g => discordGuildIds.includes(g.id)) which is always true because
guilds were fetched with id: { in: discordGuildIds }; instead change the
predicate to verify that every id in discordGuildIds exists in the fetched
guilds (e.g., discordGuildIds.every(id => guilds.some(g => g.id === id)) or
compare sets/lengths) before deciding to call
this.discordService.clearUserGuildIdsCache(userId); update both occurrences (the
one using allGuildsInDiscord and the call to clearUserGuildIdsCache) so the
cache is invalidated when there are missing guild IDs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0dfb7333-488d-4aba-9451-111eef24c23c

📥 Commits

Reviewing files that changed from the base of the PR and between f8c52e1 and 2f998cb.

📒 Files selected for processing (5)
  • apps/api/src/chat/chat.service.ts
  • apps/api/src/discord/discord.service.ts
  • apps/api/src/events/utils/find-active-event-heroes-by-npc.ts
  • apps/api/src/guilds/guilds.service.ts
  • apps/game-client/src/hooks/game-events/helpers/npc.helpers.ts
💤 Files with no reviewable changes (1)
  • apps/game-client/src/hooks/game-events/helpers/npc.helpers.ts

Comment on lines +107 to 112
const allGuildsInDiscord = guilds.every((guild) => {
return discordGuildIds.includes(guild.id);
});

if (!comparedGuilds) {
if (!allGuildsInDiscord) {
await this.discordService.clearUserGuildIdsCache(userId);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Cache invalidation condition is effectively always true and never triggers.

At Line 107 and Line 501, allGuildsInDiscord is derived from guilds.every((guild) => discordGuildIds.includes(guild.id)), but guilds was already fetched with id: { in: discordGuildIds }. That makes the predicate trivially true, so Lines 111/505 won’t clear stale cache entries.

Suggested fix (apply in both locations)
-const allGuildsInDiscord = guilds.every((guild) => {
-  return discordGuildIds.includes(guild.id);
-});
-
-if (!allGuildsInDiscord) {
+const dbGuildIds = new Set(guilds.map((guild) => guild.id));
+const hasGuildIdsMissingInDb = discordGuildIds.some(
+  (discordGuildId) => !dbGuildIds.has(discordGuildId),
+);
+
+if (hasGuildIdsMissingInDb) {
   await this.discordService.clearUserGuildIdsCache(userId);
 }

Also applies to: 501-506

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/api/src/guilds/guilds.service.ts` around lines 107 - 112, The current
check uses allGuildsInDiscord = guilds.every(g =>
discordGuildIds.includes(g.id)) which is always true because guilds were fetched
with id: { in: discordGuildIds }; instead change the predicate to verify that
every id in discordGuildIds exists in the fetched guilds (e.g.,
discordGuildIds.every(id => guilds.some(g => g.id === id)) or compare
sets/lengths) before deciding to call
this.discordService.clearUserGuildIdsCache(userId); update both occurrences (the
one using allGuildsInDiscord and the call to clearUserGuildIdsCache) so the
cache is invalidated when there are missing guild IDs.

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