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

feat(medusa,types): added store apis for products #7144

Merged
merged 15 commits into from Apr 29, 2024
Merged

Conversation

riqwan
Copy link
Contributor

@riqwan riqwan commented Apr 24, 2024

what:

  • adds products list api
  • adds products retrieve api

RESOLVES CORE-2016

Copy link

vercel bot commented Apr 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
api-reference 🔄 Building (Inspect) Visit Preview Apr 29, 2024 3:00pm
medusa-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 29, 2024 3:00pm
2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
docs-ui ⬜️ Ignored (Inspect) Visit Preview Apr 29, 2024 3:00pm
medusa-docs ⬜️ Ignored (Inspect) Visit Preview Apr 29, 2024 3:00pm

Copy link

changeset-bot bot commented Apr 24, 2024

🦋 Changeset detected

Latest commit: 765f346

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@medusajs/medusa Patch
@medusajs/types Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -0,0 +1,332 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
Copy link
Member

Choose a reason for hiding this comment

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

@riqwan I know you wrote a lot of tests here, but does it make sense to actually make the integration-tests/api tests work instead? If you feel like it will take a lot of time I'm fine with doing it later on, but we really need to try and clean up the tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

2 points:

  • I'd like these tests to be run on the CI, which it won't with the api tests.
  • I don't get why we do that. Wouldn't it just be better to move over the test cases to modules and make it work for v2? We're not really writing more tests for v1, so we wouldn't be missing out much. We do the same everywhere else - v2 routes, v2 dashboard, why not the same here?

Copy link
Member

Choose a reason for hiding this comment

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

@riqwan The idea is that, unless we intended a breaking change, the test scenarios in api are still valid and should work regardless if it is v1 or v2. Eg. for the product endpoints, we have very few breaking changes, and that gives us some confidence that we are compliant with v1, except for the intentional breaking changes.

But if you feel like that is not a valid point, then we should discuss and settle on a single approach, as currently we have a split between the two

packages/medusa/src/api-v2/store/products/helpers.ts Outdated Show resolved Hide resolved
packages/medusa/src/api-v2/store/products/validators.ts Outdated Show resolved Hide resolved
Copy link
Member

@sradevski sradevski left a comment

Choose a reason for hiding this comment

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

Looks good!

req: MedusaRequest<StoreGetProductsParamsType>,
res: MedusaResponse
) => {
const context = isPresent(req.pricingContext)
Copy link
Member

Choose a reason for hiding this comment

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

Nothing to do now, I am just thinking if we can make the context-passing and calculations setup a bit nicer. Just talking out loud here 😄

method: "ALL",
matcher: "/store/products*",
middlewares: [
authenticate("store", ["session", "bearer"], {
Copy link
Member

Choose a reason for hiding this comment

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

Again, for another time - I think having a global middleware that just populates the context will be much better than relying on authenticate, as this middleware ideally should do actual authentication (and the allowUnauthenticated flag should not exist).

Comment on lines +10 to +25
const context = isPresent(req.pricingContext)
? {
"variants.calculated_price": { context: req.pricingContext },
}
: undefined

const product = await refetchProduct(
{
id: req.params.id,
context,
},
req.scope,
req.remoteQueryConfig.fields
)

res.json({ product })
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const context = isPresent(req.pricingContext)
? {
"variants.calculated_price": { context: req.pricingContext },
}
: undefined
const product = await refetchProduct(
{
id: req.params.id,
context,
},
req.scope,
req.remoteQueryConfig.fields
)
res.json({ product })
const refetchProductParams: object = {
id: req.params.id,
}
if (isPresent(req.pricingContext)) {
config.context = {
"variants.calculated_price": { context: req.pricingContext },
}
}
const product = await refetchProduct(
refetchProductParams,
req.scope,
req.remoteQueryConfig.fields
)
res.json({ product })

Copy link
Member

Choose a reason for hiding this comment

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

wdyt? It is only a suggestion

const categoriesFilter = isPresent(filters.category_id)
? {
categories: {
...filters.category_id,
Copy link
Member

@adrien2p adrien2p Apr 29, 2024

Choose a reason for hiding this comment

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

suggestion: I believe the spread here will create an issue, it will assign the index as the key and the id as the value as it seems to be an array spreaded into an object

]

export const retrieveProductQueryConfig = {
defaults: defaultStoreProductFields,
Copy link
Member

Choose a reason for hiding this comment

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

q: do we need to limit what is accessible by the store, I am thinking if someone add more link or properties or something, the store could request for some admin data? This is more of a global question :)

@kodiakhq kodiakhq bot merged commit 11517f0 into develop Apr 29, 2024
24 checks passed
@kodiakhq kodiakhq bot deleted the feat/products-api branch April 29, 2024 15:14
Comment on lines +10 to +19
if (
!req.remoteQueryConfig.fields.some((field) =>
field.startsWith("variants.calculated_price")
)
) {
delete req.filterableFields.region_id
delete req.filterableFields.currency_code

return next()
}
Copy link
Member

Choose a reason for hiding this comment

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

Q: here, If I pass the region id and/or currency but I forget about the relation, I will still get a result but without the context filtering, it can make it hard to debug, what if we throw an invalid data here instead to say that it is not possible to filter the with the context without this relation, or alternatively we infer the context filtering if region id or currency code is present since they are only used in that context.

wdyt?

"currency",
{ code: query.currency_code },
req.scope,
["code"]
Copy link
Member

@adrien2p adrien2p Apr 29, 2024

Choose a reason for hiding this comment

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

Q: isn't the code here the same as the one provided? Are we doing that to check if the entity exists?

}

// If a region or currency_code is not present in the context, we will not be able to calculate prices
if (!isPresent(pricingContext)) {
Copy link
Member

@adrien2p adrien2p Apr 29, 2024

Choose a reason for hiding this comment

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

suggestion: shouldn't we check for the currency and or region? Could it be possible that none of them are present but another key is? I think a check on region or currency would be more future proof. Wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants