Skip to content

Refactored Adapter API#1335

Open
Banibrata-Manna wants to merge 4 commits intohotwax:mainfrom
Banibrata-Manna:refactor-adapter-apis
Open

Refactored Adapter API#1335
Banibrata-Manna wants to merge 4 commits intohotwax:mainfrom
Banibrata-Manna:refactor-adapter-apis

Conversation

@Banibrata-Manna
Copy link
Copy Markdown
Contributor

  • Refactored Adapter API
  • Moved Adapter APIs to suitables store actions
  • Removed client usages from adpater APIs

Related Issues

#1334

Short Description and Why It's Useful

Screenshots of Visual Changes before/after (If There Are Any)

Contribution and Currently Important Rules Acceptance

Moved Adapter APIs to suitables store actions
Removed client usages from adpater APIs
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @Banibrata-Manna, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the application's API interaction layer by eliminating a monolithic adapter file. The core purpose is to improve modularity and maintainability by relocating API logic directly into the Pinia stores responsible for managing the relevant data. This change streamlines how different parts of the application fetch and update data, making the codebase more organized and easier to understand.

Highlights

  • Adapter API Removal: The central src/adapter/index.ts file, which previously housed a multitude of API-related functions, has been completely removed. This signifies a major architectural shift towards decentralizing API calls.
  • API Logic Migration to Pinia Stores: All API functions previously residing in the adapter file have been migrated into their respective Pinia stores. Specifically, functions related to user profiles, preferences, security groups, and time zones are now part of src/stores/userProfileStore.ts, while functions concerning facilities, product stores, and product identification preferences have been moved to src/stores/productStore.ts.
  • Simplified API Calls and Client Usage Removal: The direct usage of the client utility for API calls, which often required explicit token and baseURL parameters, has been removed. API calls within the stores now primarily use the api service, suggesting that authentication and base URL handling are now managed more implicitly or internally by the api service itself.
  • Streamlined Store Interactions: Calls to getProfile, getFacilityPreference, getEComStorePreference, and setFacilityPreference from authStore and productStore no longer require passing token, baseURL, or userId as arguments. These parameters are now handled internally by the refactored store methods, simplifying their usage.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@Banibrata-Manna
Copy link
Copy Markdown
Contributor Author

@gemini-code-assist

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request refactors API-related utility functions by removing the adapter/index.ts file and migrating its contents into methods within the productStore.ts and userProfileStore.ts Pinia stores. This change centralizes API logic, eliminating the need to explicitly pass authentication tokens and base URLs to these functions. Specifically, authStore.ts and productStore.ts were updated to call these new store methods directly, and StorePermissions.vue now uses userProfileStore methods for security group operations. Review comments highlight a flaw in the fetchFacilities logic where facility IDs from party and group queries are incorrectly handled, leading to potential overwrites and incorrect rejections. Additionally, the getProfile method in userProfileStore.ts needs to use the hasError utility for robust error checking, and the getUserPreference method in the same store requires a try-catch block or a safe parsing utility for JSON.parse to prevent unhandled exceptions.

Comment thread src/stores/productStore.ts Outdated
Comment on lines +372 to +453
async fetchFacilities(partyId: string, facilityGroupId: string, isAdminUser: boolean, payload: any): Promise <any> {
let facilityIds: Array<string> = [];
let filters: any = {};
let resp = {} as any

// Fetch the facilities associated with party
if(partyId) {
try {
resp = await this.fetchFacilitiesByParty(partyId)

facilityIds = resp.map((facility: any) => facility.facilityId);
if (!facilityIds.length) {
return Promise.reject({
code: 'error',
message: 'Failed to fetch user facilities',
serverResponse: resp.data
})
}
} catch(error) {
return Promise.reject({
code: 'error',
message: 'Failed to fetch user facilities',
serverResponse: error
})
}
}

// Fetch the facilities associated with group
if(facilityGroupId) {
try {
resp = await this.fetchFacilitiesByGroup(facilityGroupId, filters)

facilityIds = resp.map((facility: any) => facility.facilityId);
if (!facilityIds.length) {
return Promise.reject({
code: 'error',
message: 'Failed to fetch user facilities',
serverResponse: resp.data
})
}
} catch(error) {
return Promise.reject({
code: 'error',
message: 'Failed to fetch user facilities',
serverResponse: error
})
}
}

if(facilityIds.length) {
filters = {
facilityId: facilityIds.join(","),
facilityId_op: "in",
pageSize: facilityIds.length
}
}

const params = {
url: "oms/facilities",
method: "GET",
params: {
pageSize: 500,
...payload,
...filters
}
}

let facilities: Array<any> = []

try {
resp = await api(params);
facilities = resp.data
} catch(error) {
return Promise.reject({
code: "error",
message: "Failed to fetch facilities",
serverResponse: error
})
}

return Promise.resolve(facilities)
},
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.

high

The logic in fetchFacilities has a flaw. When both partyId and facilityGroupId are provided, the facilityIds from the party are overwritten by those from the group. Additionally, the check for empty results (if (!facilityIds.length)) inside the if(facilityGroupId) block can lead to an incorrect rejection if the party query returned facilities but the group query did not.

I recommend refactoring this function to correctly accumulate facility IDs from both sources, remove duplicates, and then perform a single check for whether any facilities were found before proceeding.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed facilityGroup support since we don't need it in this app, It was common code om oms-api.

Comment on lines +178 to +180
if (!resp) {
throw 'Error getting user profile'
}
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.

high

The error check if (!resp) is not sufficient. The hasError utility function should be used here to check for application-level errors returned in the response body with a 200 status code. Without it, the application might proceed with an invalid user profile.

Suggested change
if (!resp) {
throw 'Error getting user profile'
}
if (!resp || hasError(resp)) {
throw 'Error getting user profile';
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hasError is used for OFBiz api calls, since the user profile comes from Maarg any error in api call will automaticlly be thrown.

Comment thread src/stores/userProfileStore.ts Outdated
let resp = {} as any;
try {
resp = await api(params);
return Promise.resolve(resp.data[0]?.preferenceValue ? JSON.parse(resp.data[0]?.preferenceValue).toString() : "")
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.

high

The JSON.parse call is not wrapped in a try-catch block. If resp.data[0]?.preferenceValue is not a valid JSON string, this will cause an unhandled exception. The original adapter/index.ts file had a jsonParse utility function to handle this safely. A similar safe parsing mechanism should be used here.

Example of safe parsing:

let parsedValue;
try {
  parsedValue = JSON.parse(value);
} catch (e) {
  parsedValue = value;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, Added a function in utils

@gemini-code-assist
Copy link
Copy Markdown
Contributor

@Banibrata-Manna Thank you for the ping! I'm now proceeding with a detailed code review of your pull request. I will post my findings shortly.

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.

2 participants