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: inaccurate purchaseTime #279

Merged
merged 1 commit into from
Oct 13, 2024

Conversation

songlim327
Copy link
Contributor

@songlim327 songlim327 commented Oct 11, 2024

What type of PR is this?

(REQUIRED)

  • bug

What this PR does / why we need it:

(REQUIRED)

The purchaseTime value from the FormDatePicker returns a Date object when the form is submitted. This object only contains the date without any time details (hours, minutes, or seconds). For example, in GMT+8, it displays as "Fri Oct 11 2024 00:00:00 GMT+0800 (Malaysia Time)".

However, when this date is sent to the update API, the payload is converted to a string using JSON.stringify, the Date object is automatically converted to UTC (GMT+0). This causes the original date to shift, resulting in "2024-10-10T16:00:00.000Z" (which reflects an 8-hour difference due to the timezone offset)

This PR resolves the issue by applying the appropriate timezone offset to the purchaseTime before passing it to JSON.stringify, ensuring the correct date and time are retained.

Which issue(s) this PR fixes:

(REQUIRED)

Fixes bug #125

Summary by CodeRabbit

  • New Features
    • Introduced a "Purchase Date" field for item updates, allowing users to input the purchase time directly.
    • Enhanced item update functionality to capture and send purchase time to the backend.

Copy link
Contributor

coderabbitai bot commented Oct 11, 2024

Walkthrough

The changes in the edit.vue file introduce a new variable named purchaseTime and modify the saveItem function to incorporate this variable. The purchaseTime is initialized to null and later assigned a value based on item.value.purchaseTime, adjusted for timezone. The payload object for API updates now includes purchaseTime. Additionally, a new field for "Purchase Date" is added to the purchaseFields array, enhancing the component's ability to capture and manage item purchase details.

Changes

File Path Change Summary
frontend/pages/item/[id]/index/edit.vue - Added variable purchaseTime initialized to null in saveItem function.
- Updated saveItem to handle purchaseTime with timezone adjustment.
- Modified purchaseFields to include a new field for "Purchase Date" referencing purchaseTime.
- Updated template section to allow user input for the purchase date.

Security Recommendations

  • Ensure that the input for the new "Purchase Date" field is validated to prevent injection attacks.
  • Implement proper error handling for the API call to manage potential failures gracefully.

🎉 In the code we weave, a date to retrieve,
A purchase time now set, for users to believe.
With fields that expand, and functions that blend,
Our app grows in grace, on this we depend!
🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c9ed50a and be6e8d9.

📒 Files selected for processing (1)
  • frontend/pages/item/[id]/index/edit.vue (2 hunks)
🧰 Additional context used
🔇 Additional comments (4)
frontend/pages/item/[id]/index/edit.vue (4)

83-83: LGTM! Improves code readability.

The added empty line enhances code readability by separating logical blocks.


Line range hint 1-1: LGTM! Consider adding input validation for the new Purchase Date field.

The addition of the "Purchase Date" field to purchaseFields is consistent with the PR objectives and allows direct input of the purchase date.

To ensure proper input validation, let's check for existing date validation mechanisms:

#!/bin/bash
# Search for date validation in Vue components
rg "validate.*date" --type vue

If no suitable validation is found, consider implementing date input validation to prevent invalid dates from being processed.


92-92: LGTM! Consider data validation before transmission.

The addition of purchaseTime to the payload ensures the adjusted time is sent to the API, addressing the PR objectives.

For security, let's verify that the purchaseTime is properly validated before transmission:

#!/bin/bash
# Search for purchaseTime validation
rg "purchaseTime.*validate" --type vue

If no validation is found, consider adding a validation step before including purchaseTime in the payload to prevent potential injection attacks or data corruption.


70-80: LGTM! Consider adding a comment for clarity.

The changes address the timezone issue mentioned in the PR objectives. The use of getTimezoneOffset() correctly adjusts the time.

Consider adding a comment explaining the timezone adjustment:

+ // Adjust for timezone to ensure correct date representation
purchaseTime = new Date(item.value.purchaseTime.getTime() - item.value.purchaseTime.getTimezoneOffset() * 60000);

To ensure this change doesn't introduce any unintended side effects, let's verify its usage:

✅ Verification successful

Security Recommendation: Ensure that all date manipulations, especially those involving timezone adjustments, are handled securely to prevent potential vulnerabilities related to date parsing and storage.


Verified! The purchaseTime handling is consistently and correctly implemented across all relevant Vue files, ensuring proper timezone adjustments.

  • Security Recommendation: Ensure that all date manipulations, especially those involving timezone adjustments, are handled securely to prevent potential vulnerabilities related to date parsing and storage.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other occurrences of purchaseTime handling
rg "purchaseTime" --type vue

Length of output: 220


Script:

#!/bin/bash
# Search for other occurrences of purchaseTime handling in Vue files
rg "purchaseTime" --glob "*.vue" -A 2

Length of output: 1581


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

if (item.value.purchasePrice) {
purchasePrice = item.value.purchasePrice;
}
if (item.value.soldPrice) {
soldPrice = item.value.soldPrice;
}
if (item.value.purchaseTime) {
purchaseTime = new Date(item.value.purchaseTime.getTime() - item.value.purchaseTime.getTimezoneOffset() * 60000);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is a minute being tacked on at the end of this math?

Copy link
Collaborator

Choose a reason for hiding this comment

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

gettime returns number of milliseconds whereas gettimezoneoffset returns number of minutes, so multiplying the timezone offset by 60000 converts it to milliseconds
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

Copy link
Contributor

@tankerkiller125 tankerkiller125 Oct 13, 2024

Choose a reason for hiding this comment

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

OK this makes more sense now, but also... What in the hell where the people who designed and implemented JavaScript drinking when they put this crap together? Both of those functions come from the same type for crying out loud!

@tankerkiller125 tankerkiller125 merged commit a1e6685 into sysadminsmedia:main Oct 13, 2024
6 of 8 checks passed
@songlim327 songlim327 deleted the fix-125 branch October 13, 2024 10:31
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.

3 participants