Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix the global module update #402

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions src/services/module.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,27 @@ const updateModule = async (
): Promise<HydratedDocument<IModule>> => {
// Check if `options.platforms` is in the updateBody
if (updateBody.options && updateBody.options.platforms) {
// Iterate through each platform in the incoming update
for (const newPlatform of updateBody.options.platforms) {
const existingPlatform = module.options?.platforms.find((p) => p.name === newPlatform.name);

// If the platform already exists, update it
if (existingPlatform) {
existingPlatform.metadata = newPlatform.metadata;
} else {
// If the platform does not exist, add new
module.options?.platforms.push(newPlatform);
if (updateBody.options.platforms[0].name == undefined) {
{
const globalOption = module.options?.platforms[0];
console.log(globalOption);
if (globalOption) globalOption.metadata = updateBody.options.platforms[0].metadata;
else module.options?.platforms.push(updateBody.options.platforms[0]);
}
Comment on lines +55 to +61
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 undefined access and improve error handling.

The current implementation has several issues:

  1. Accessing platforms[0] without length check is unsafe
  2. Nested optional chaining with module.options?.platforms[0] could still result in runtime errors
  3. Production code shouldn't contain console.log statements
  4. The special case for undefined platform name isn't well documented

Consider this safer implementation:

-    if (updateBody.options.platforms[0].name == undefined) {
-      {
-        const globalOption = module.options?.platforms[0];
-        console.log(globalOption);
-        if (globalOption) globalOption.metadata = updateBody.options.platforms[0].metadata;
-        else module.options?.platforms.push(updateBody.options.platforms[0]);
-      }
+    const platforms = updateBody.options.platforms;
+    if (!platforms.length) {
+      return module;
+    }
+    
+    // Handle global platform update (when name is undefined)
+    if (!platforms[0].name) {
+      if (!module.options?.platforms?.length) {
+        module.options = {
+          ...module.options,
+          platforms: [platforms[0]]
+        };
+      } else {
+        module.options.platforms[0].metadata = platforms[0].metadata;
+      }
📝 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
if (updateBody.options.platforms[0].name == undefined) {
{
const globalOption = module.options?.platforms[0];
console.log(globalOption);
if (globalOption) globalOption.metadata = updateBody.options.platforms[0].metadata;
else module.options?.platforms.push(updateBody.options.platforms[0]);
}
const platforms = updateBody.options.platforms;
if (!platforms.length) {
return module;
}
// Handle global platform update (when name is undefined)
if (!platforms[0].name) {
if (!module.options?.platforms?.length) {
module.options = {
...module.options,
platforms: [platforms[0]]
};
} else {
module.options.platforms[0].metadata = platforms[0].metadata;
}

} else {
// Iterate through each platform in the incoming update
for (const newPlatform of updateBody.options.platforms) {
const existingPlatform = module.options?.platforms.find((p) => p.name === newPlatform.name);
if (existingPlatform) {
// If the platform already exists, update it
existingPlatform.metadata = newPlatform.metadata;
} else {
// If the platform does not exist, add new
module.options?.platforms.push(newPlatform);
}
Comment on lines +63 to +72
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation and improve error handling for platform updates.

The current implementation lacks proper validation and error handling:

  1. No validation of platform metadata structure
  2. Possible undefined behavior with optional chaining
  3. No error handling for invalid platform data

Consider this improved implementation:

+      if (!module.options?.platforms) {
+        module.options = { platforms: [] };
+      }
+
       // Iterate through each platform in the incoming update
       for (const newPlatform of updateBody.options.platforms) {
+        if (!isValidPlatform(newPlatform)) {
+          throw new Error(`Invalid platform data: ${JSON.stringify(newPlatform)}`);
+        }
+
         const existingPlatform = module.options?.platforms.find((p) => p.name === newPlatform.name);
         if (existingPlatform) {
           // If the platform already exists, update it
           existingPlatform.metadata = newPlatform.metadata;
         } else {
           // If the platform does not exist, add new
-          module.options?.platforms.push(newPlatform);
+          module.options.platforms.push(newPlatform);
         }
       }

Add this validation helper:

interface Platform {
  name?: string;
  metadata: unknown;
}

function isValidPlatform(platform: unknown): platform is Platform {
  return (
    typeof platform === 'object' &&
    platform !== null &&
    ('name' in platform || platform.name === undefined) &&
    'metadata' in platform
  );
}

}
}
}

// Other update operations can be handled here
return await module.save();
};

Expand Down
Loading