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(oas): declare x-expanded-relations - Store #3482

Merged
merged 10 commits into from
Mar 16, 2023
6 changes: 6 additions & 0 deletions .changeset/loud-pillows-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/client-types": patch
"@medusajs/medusa": patch
---

feat(oas): declare x-expanded-relations - Store

This file was deleted.

21 changes: 12 additions & 9 deletions integration-tests/api/__tests__/store/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,18 @@ describe("/store/auth", () => {

expect(response.status).toEqual(200)
expect(response.data.customer.password_hash).toEqual(undefined)
expect(response.data.customer).toMatchSnapshot({
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
first_name: "test",
last_name: "testesen",
phone: null,
email: "[email protected]",
})
expect(response.data.customer).toEqual(
expect.objectContaining({
id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
first_name: "test",
last_name: "testesen",
phone: null,
email: "[email protected]",
shipping_addresses: expect.arrayContaining([]),
})
)
})

describe("Store session management", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ describe("/store/payment-collections", () => {
})
)

expect(response.status).toEqual(207)
expect(response.status).toEqual(200)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EntityManager } from "typeorm"
import AuthService from "../../../../services/auth"
import CustomerService from "../../../../services/customer"
import { validator } from "../../../../utils/validator"
import { defaultRelations } from "."

/**
* @oas [post] /store/auth
Expand Down Expand Up @@ -91,7 +92,7 @@ export default async (req, res) => {

const customerService: CustomerService = req.scope.resolve("customerService")
const customer = await customerService.retrieve(result.customer?.id || "", {
relations: ["orders", "orders.items"],
relations: defaultRelations,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will also include shipping_addresses in order to harmonize the responses shaped with get-session.

})

res.json({ customer })
Expand Down
3 changes: 2 additions & 1 deletion packages/medusa/src/api/routes/store/auth/get-session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import CustomerService from "../../../../services/customer"
import { defaultRelations } from "."

/**
* @oas [get] /store/auth
Expand Down Expand Up @@ -52,7 +53,7 @@ export default async (req, res) => {
const customerService: CustomerService = req.scope.resolve("customerService")

const customer = await customerService.retrieve(req.user.customer_id, {
relations: ["shipping_addresses", "orders", "orders.items"],
relations: defaultRelations,
})

res.json({ customer })
Expand Down
8 changes: 8 additions & 0 deletions packages/medusa/src/api/routes/store/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ export default (app) => {
return app
}

export const defaultRelations = ["orders", "orders.items", "shipping_addresses"]

/**
* @schema StoreAuthRes
* type: object
* x-expanded-relations:
* field: customer
* relations:
* - orders
* - orders.items
* - shipping_addresses
* required:
* - customer
* properties:
Expand Down
21 changes: 15 additions & 6 deletions packages/medusa/src/api/routes/store/carts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,24 @@ export const defaultStoreCartRelations = [
* - region.payment_providers
* - shipping_address
* - shipping_methods
* - shipping_methods.shipping_option
* implicit:
* - items.tax_lines
* - items.variant.product
* eager:
* - region.fulfillment_providers
* - region.payment_providers
* - region.tax_rates
* - shipping_methods.shipping_option
* - shipping_methods.tax_lines
* implicit:
* - items
* - items.variant
* - items.variant.product
* - items.tax_lines
* - items.adjustments
* - gift_cards
* - discounts
* - discounts.rule
* - shipping_methods
* - shipping_methods.tax_lines
* - shipping_address
* - region
* - region.tax_rates
Comment on lines +178 to +191
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Relations from decorateTotals subprocess.

private getTotalsRelations(config: FindConfig<Cart>): string[] {
const relationSet = new Set(config.relations)
relationSet.add("items")
relationSet.add("items.variant")
relationSet.add("items.variant.product")
relationSet.add("items.tax_lines")
relationSet.add("items.adjustments")
relationSet.add("gift_cards")
relationSet.add("discounts")
relationSet.add("discounts.rule")
relationSet.add("shipping_methods")
relationSet.add("shipping_methods.tax_lines")
relationSet.add("shipping_address")
relationSet.add("region")
relationSet.add("region.tax_rates")
return Array.from(relationSet.values())
}

* totals:
* - discount_total
* - gift_card_tax_total
Expand Down
3 changes: 2 additions & 1 deletion packages/medusa/src/api/routes/store/collections/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ export default (app) => {
return app
}

export const defaultStoreCollectionRelations = ["products"]
export const defaultStoreCollectionRelations = []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

defaultStoreCollectionRelations is not used and is not passed to services in controllers. To avoid confusion, product has been moves to allowedFields. This change should have no effect.

export const allowedFields = [
"id",
"title",
"handle",
"products",
"metadata",
"created_at",
"updated_at",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import { Type } from "class-transformer"
* - (query) offset=0 {integer} The number of collections to skip before starting to collect the collections set
* - (query) limit=10 {integer} The number of collections to return
* - in: query
* name: handle
* style: form
* explode: false
* description: Filter by the collection handle
* schema:
* type: array
* items:
* type: string
Comment on lines +16 to +23
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missing OAS for query param.

export class StoreGetCollectionsParams {
@IsOptional()
@IsArray()
handle?: string[]

* - in: query
* name: created_at
* description: Date comparison for when resulting collections were created.
* schema:
Expand Down
99 changes: 99 additions & 0 deletions packages/medusa/src/api/routes/store/customers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export const allowedStoreCustomersFields = [
/**
* @schema StoreCustomersRes
* type: object
* x-expanded-relations:
* field: customer
* relations:
* - billing_address
* - shipping_addresses
Comment on lines +117 to +121
Copy link
Contributor Author

Choose a reason for hiding this comment

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

export const defaultStoreCustomersRelations = [
"shipping_addresses",
"billing_address",
]

* required:
* - customer
* properties:
Expand All @@ -124,9 +129,103 @@ export type StoreCustomersRes = {
customer: Omit<Customer, "password_hash">
}

/**
* @schema StoreCustomersResetPasswordRes
Copy link
Contributor Author

Choose a reason for hiding this comment

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

customer model in reset password response will not include expanded-relations from StoreCustomersRes. Creating a separate response for reset password endpoint.

* type: object
* required:
* - customer
* properties:
* customer:
* $ref: "#/components/schemas/Customer"
*/
export type StoreCustomersResetPasswordRes = {
customer: Omit<Customer, "password_hash">
}

/**
* @schema StoreCustomersListOrdersRes
* type: object
* x-expanded-relations:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exact match with StoreOrdersRes.

* @schema StoreOrdersRes
* type: object
* required:
* - order
* x-expanded-relations:
* field: order
* relations:
* - customer
* - discounts
* - discounts.rule
* - fulfillments
* - fulfillments.tracking_links
* - items
* - items.variant
* - payments
* - region
* - shipping_address
* - shipping_methods
* eager:
* - fulfillments.items
* - region.fulfillment_providers
* - region.payment_providers
* - shipping_methods.shipping_option
* implicit:
* - claims
* - claims.additional_items
* - claims.additional_items.adjustments
* - claims.additional_items.refundable
* - claims.additional_items.tax_lines
* - discounts
* - discounts.rule
* - gift_card_transactions
* - gift_card_transactions.gift_card
* - gift_cards
* - items
* - items.adjustments
* - items.refundable
* - items.tax_lines
* - items.variant
* - items.variant.product
* - refunds
* - region
* - shipping_methods
* - shipping_methods.tax_lines
* - swaps
* - swaps.additional_items
* - swaps.additional_items.adjustments
* - swaps.additional_items.refundable
* - swaps.additional_items.tax_lines
* totals:
* - discount_total
* - gift_card_tax_total
* - gift_card_total
* - paid_total
* - refundable_amount
* - refunded_total
* - shipping_total
* - subtotal
* - tax_total
* - total
* - claims.additional_items.discount_total
* - claims.additional_items.gift_card_total
* - claims.additional_items.original_tax_total
* - claims.additional_items.original_total
* - claims.additional_items.refundable
* - claims.additional_items.subtotal
* - claims.additional_items.tax_total
* - claims.additional_items.total
* - items.discount_total
* - items.gift_card_total
* - items.original_tax_total
* - items.original_total
* - items.refundable
* - items.subtotal
* - items.tax_total
* - items.total
* - swaps.additional_items.discount_total
* - swaps.additional_items.gift_card_total
* - swaps.additional_items.original_tax_total
* - swaps.additional_items.original_total
* - swaps.additional_items.refundable
* - swaps.additional_items.subtotal
* - swaps.additional_items.tax_total
* - swaps.additional_items.total

* field: orders
* relations:
* - customer
* - discounts
* - discounts.rule
* - fulfillments
* - fulfillments.tracking_links
* - items
* - items.variant
* - payments
* - region
* - shipping_address
* - shipping_methods
* eager:
* - region.fulfillment_providers
* - region.payment_providers
* - shipping_methods.shipping_option
* implicit:
* - claims
* - claims.additional_items
* - claims.additional_items.adjustments
* - claims.additional_items.refundable
* - claims.additional_items.tax_lines
* - customer
* - discounts
* - discounts.rule
* - gift_card_transactions
* - gift_card_transactions.gift_card
* - gift_cards
* - items
* - items.adjustments
* - items.refundable
* - items.tax_lines
* - items.variant
* - items.variant.product
* - refunds
* - region
* - shipping_address
* - shipping_methods
* - shipping_methods.tax_lines
* - swaps
* - swaps.additional_items
* - swaps.additional_items.adjustments
* - swaps.additional_items.refundable
* - swaps.additional_items.tax_lines
* totals:
* - discount_total
* - gift_card_tax_total
* - gift_card_total
* - paid_total
* - refundable_amount
* - refunded_total
* - shipping_total
* - subtotal
* - tax_total
* - total
* - claims.additional_items.discount_total
* - claims.additional_items.gift_card_total
* - claims.additional_items.original_tax_total
* - claims.additional_items.original_total
* - claims.additional_items.refundable
* - claims.additional_items.subtotal
* - claims.additional_items.tax_total
* - claims.additional_items.total
* - items.discount_total
* - items.gift_card_total
* - items.original_tax_total
* - items.original_total
* - items.refundable
* - items.subtotal
* - items.tax_total
* - items.total
* - swaps.additional_items.discount_total
* - swaps.additional_items.gift_card_total
* - swaps.additional_items.original_tax_total
* - swaps.additional_items.original_total
* - swaps.additional_items.refundable
* - swaps.additional_items.subtotal
* - swaps.additional_items.tax_total
* - swaps.additional_items.total
* required:
* - orders
* - count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { EntityManager } from "typeorm"
* content:
* application/json:
* schema:
* $ref: "#/components/schemas/StoreCustomersRes"
* $ref: "#/components/schemas/StoreCustomersResetPasswordRes"
* "400":
* $ref: "#/components/responses/400_error"
* "401":
Expand Down
35 changes: 35 additions & 0 deletions packages/medusa/src/api/routes/store/order-edits/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,41 @@ export default (app) => {
/**
* @schema StoreOrderEditsRes
* type: object
* x-expanded-relations:
* field: order_edit
* relations:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

export const defaultOrderEditRelations: string[] = [
"changes",
"changes.line_item",
"changes.line_item.variant",
"changes.original_line_item",
"changes.original_line_item.variant",
"items",
"items.variant",
"items.adjustments",
"items.tax_lines",
"payment_collection",
]

* - changes
* - changes.line_item
* - changes.line_item.variant
* - changes.original_line_item
* - changes.original_line_item.variant
* - items
* - items.adjustments
* - items.tax_lines
* - items.variant
* - payment_collection
* implicit:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

OrderEdit decorateTotals subprocess

async decorateTotals(orderEdit: OrderEdit): Promise<OrderEdit> {
const { order_id, items } = await this.retrieve(orderEdit.id, {
select: ["id", "order_id", "items"],
relations: [
"items",
"items.tax_lines",
"items.adjustments",
"items.variant",
],
})

* - items
* - items.tax_lines
* - items.adjustments
* - items.variant
* totals:
* - difference_due
* - discount_total
* - gift_card_tax_total
* - gift_card_total
* - shipping_total
* - subtotal
* - tax_total
* - total
* - items.discount_total
* - items.gift_card_total
* - items.original_tax_total
* - items.original_total
* - items.refundable
* - items.subtotal
* - items.tax_total
* - items.total
* required:
* - order_edit
* properties:
Expand Down
Loading