diff --git a/.changeset/five-spies-ring.md b/.changeset/five-spies-ring.md new file mode 100644 index 0000000000..b68385b37f --- /dev/null +++ b/.changeset/five-spies-ring.md @@ -0,0 +1,9 @@ +--- +"medusa-test-utils": patch +"create-medusa-app": patch +"@medusajs/core-flows": patch +"@medusajs/types": patch +"@medusajs/utils": patch +--- + +Order transactions diff --git a/integration-tests/modules/__tests__/order/order.spec.ts b/integration-tests/modules/__tests__/order/order.spec.ts index 6c7a07017d..20c250120a 100644 --- a/integration-tests/modules/__tests__/order/order.spec.ts +++ b/integration-tests/modules/__tests__/order/order.spec.ts @@ -148,7 +148,7 @@ medusaIntegrationTestRunner({ tax_total: 0.89, discount_total: 1.1, discount_tax_total: 0.11, - original_total: 70.9, + original_total: 60.9, original_tax_total: 1, item_total: 50, item_subtotal: 50, diff --git a/packages/cli/create-medusa-app/package.json b/packages/cli/create-medusa-app/package.json index cccf473a5e..38c6bb9700 100644 --- a/packages/cli/create-medusa-app/package.json +++ b/packages/cli/create-medusa-app/package.json @@ -33,10 +33,12 @@ "winston": "^3.9.0" }, "devDependencies": { + "@medusajs/types": "^1.11.16", "@types/chalk": "^2.2.0", "@types/commander": "^2.12.2", "@types/configstore": "^6.0.0", "@types/inquirer": "^9.0.3", + "@types/node": "^20.12.11", "@types/node-emoji": "^1.8.2", "@types/pg": "^8.6.6", "@types/uuid": "^9.0.1", diff --git a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts index 556f350b19..b531012a5a 100644 --- a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts +++ b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts @@ -34,7 +34,9 @@ export const completeCartWorkflowId = "complete-cart" */ export const completeCartWorkflow = createWorkflow( completeCartWorkflowId, - (input: WorkflowData): WorkflowResponse => { + ( + input: WorkflowData + ): WorkflowResponse => { const cart = useRemoteQueryStep({ entry_point: "cart", fields: completeCartFields, @@ -44,7 +46,7 @@ export const completeCartWorkflow = createWorkflow( const paymentSessions = validateCartPaymentsStep({ cart }) - authorizePaymentSessionStep({ + const payment = authorizePaymentSessionStep({ // We choose the first payment session, as there will only be one active payment session // This might change in the future. id: paymentSessions[0].id, @@ -94,7 +96,7 @@ export const completeCartWorkflow = createWorkflow( }).config({ name: "final-cart" }) ) - const cartToOrder = transform({ input, cart }, ({ cart }) => { + const cartToOrder = transform({ cart, payment }, ({ cart, payment }) => { const allItems = (cart.items ?? []).map((item) => { return prepareLineItemData({ item, @@ -133,6 +135,15 @@ export const completeCartWorkflow = createWorkflow( .map((adjustment) => adjustment.code) .filter((code) => Boolean) as string[] + const transactions = [ + { + amount: payment.raw_amount ?? payment.amount, + currency_code: payment.currency_code, + reference: "payment", + reference_id: payment.id, + }, + ] + return { region_id: cart.region?.id, customer_id: cart.customer?.id, @@ -147,6 +158,7 @@ export const completeCartWorkflow = createWorkflow( shipping_methods: shippingMethods, metadata: cart.metadata, promo_codes: promoCodes, + transactions, } }) diff --git a/packages/core/core-flows/src/order/steps/add-order-transaction.ts b/packages/core/core-flows/src/order/steps/add-order-transaction.ts new file mode 100644 index 0000000000..28153e59d0 --- /dev/null +++ b/packages/core/core-flows/src/order/steps/add-order-transaction.ts @@ -0,0 +1,27 @@ +import { CreateOrderTransactionDTO } from "@medusajs/types" +import { ModuleRegistrationName } from "@medusajs/utils" +import { StepResponse, createStep } from "@medusajs/workflows-sdk" + +export const addOrderTransactionStepId = "add-order-transaction" +/** + * This step creates an order transaction. + */ +export const addOrderTransactionStep = createStep( + addOrderTransactionStepId, + async (data: CreateOrderTransactionDTO, { container }) => { + const service = container.resolve(ModuleRegistrationName.ORDER) + + const created = await service.addTransactions(data) + + return new StepResponse(created, created.id) + }, + async (id, { container }) => { + if (!id) { + return + } + + const service = container.resolve(ModuleRegistrationName.ORDER) + + await service.deleteTransactions(id) + } +) diff --git a/packages/core/core-flows/src/order/steps/index.ts b/packages/core/core-flows/src/order/steps/index.ts index 16a3575bd6..73387f2c98 100644 --- a/packages/core/core-flows/src/order/steps/index.ts +++ b/packages/core/core-flows/src/order/steps/index.ts @@ -1,3 +1,4 @@ +export * from "./add-order-transaction" export * from "./archive-orders" export * from "./cancel-fulfillment" export * from "./cancel-order-change" diff --git a/packages/core/core-flows/src/payment/workflows/refund-payment.ts b/packages/core/core-flows/src/payment/workflows/refund-payment.ts index 088f0a8182..829917092a 100644 --- a/packages/core/core-flows/src/payment/workflows/refund-payment.ts +++ b/packages/core/core-flows/src/payment/workflows/refund-payment.ts @@ -1,11 +1,14 @@ import { BigNumberInput } from "@medusajs/types" -import { PaymentEvents } from "@medusajs/utils" +import { MathBN, PaymentEvents } from "@medusajs/utils" import { WorkflowData, WorkflowResponse, createWorkflow, + transform, + when, } from "@medusajs/workflows-sdk" -import { emitEventStep } from "../../common" +import { emitEventStep, useRemoteQueryStep } from "../../common" +import { addOrderTransactionStep } from "../../order/steps/add-order-transaction" import { refundPaymentStep } from "../steps/refund-payment" export const refundPaymentWorkflowId = "refund-payment-workflow" @@ -23,6 +26,35 @@ export const refundPaymentWorkflow = createWorkflow( ) => { const payment = refundPaymentStep(input) + const orderPayment = useRemoteQueryStep({ + entry_point: "order_payment_collection", + fields: ["order.id"], + variables: { payment_collection_id: payment.payment_collection_id }, + list: false, + }) + + when({ orderPayment }, ({ orderPayment }) => { + return !!orderPayment?.order?.id + }).then(() => { + const orderTransactionData = transform( + { input, payment, orderPayment }, + ({ input, payment }) => { + return { + order_id: orderPayment.id, + amount: MathBN.mult( + input.amount ?? payment.raw_amount ?? payment.amount, + -1 + ), + currency_code: payment.currency_code, + reference_id: payment.id, + reference: "refund", + } + } + ) + + addOrderTransactionStep(orderTransactionData) + }) + emitEventStep({ eventName: PaymentEvents.REFUNDED, data: { id: payment.id }, diff --git a/packages/core/medusa-test-utils/package.json b/packages/core/medusa-test-utils/package.json index b9d09b52b8..322deb4690 100644 --- a/packages/core/medusa-test-utils/package.json +++ b/packages/core/medusa-test-utils/package.json @@ -35,6 +35,7 @@ "peerDependencies": { "@medusajs/medusa": ">1.19", "@medusajs/modules-sdk": "^1.12.10", + "@mikro-orm/core": "5.9.7", "axios": "^0.28.0", "express": "^4.18.3", "get-port": "^5.1.0", diff --git a/packages/core/types/src/order/mutations.ts b/packages/core/types/src/order/mutations.ts index 94283e4168..9040cbcfd5 100644 --- a/packages/core/types/src/order/mutations.ts +++ b/packages/core/types/src/order/mutations.ts @@ -1151,9 +1151,19 @@ export interface CreateOrderTransactionDTO { order_id: string /** - * The description of the transaction. + * The associated return's ID. */ - description?: string + return_id?: string + + /** + * The associated claim's ID. + */ + claim_id?: string + + /** + * The associated exchange's ID. + */ + exchange_id?: string /** * The data model this transaction references. @@ -1167,17 +1177,6 @@ export interface CreateOrderTransactionDTO { */ reference_id?: string - /** - * The internal note of the transaction. - */ - internal_note?: string | null - - /** - * The user or customer that created this - * transaction. - */ - created_by?: string - /** * The amount of the transaction. */ @@ -1187,11 +1186,6 @@ export interface CreateOrderTransactionDTO { * The currency code of the transaction. */ currency_code: string - - /** - * Holds custom data in key-value pairs. - */ - metadata?: Record | null } /** diff --git a/packages/core/types/src/payment/common.ts b/packages/core/types/src/payment/common.ts index f0eaa7d786..59f85aa856 100644 --- a/packages/core/types/src/payment/common.ts +++ b/packages/core/types/src/payment/common.ts @@ -272,11 +272,21 @@ export interface PaymentDTO { */ amount: BigNumberValue + /** + * The raw amount of the payment. + */ + raw_amount?: BigNumberValue + /** * The authorized amount of the payment. */ authorized_amount?: BigNumberValue + /** + * The raw authorized amount of the payment. + */ + raw_authorized_amount?: BigNumberValue + /** * The ISO 3 character currency code of the payment. */ @@ -337,11 +347,21 @@ export interface PaymentDTO { */ captured_amount?: BigNumberValue + /** + * The sum of the associated captures' raw amounts. + */ + raw_captured_amount?: BigNumberValue + /** * The sum of the associated refunds' amounts. */ refunded_amount?: BigNumberValue + /** + * The sum of the associated refunds' raw amounts. + */ + raw_refunded_amount?: BigNumberValue + /** * The associated captures. * @@ -356,6 +376,11 @@ export interface PaymentDTO { */ refunds?: RefundDTO[] + /** + * The ID of the associated payment collection. + */ + payment_collection_id: string + /** * The associated payment collection. * @@ -553,6 +578,11 @@ export interface PaymentSessionDTO { */ authorized_at?: Date + /** + * The ID of the associated payment collection. + */ + payment_collection_id: string + /** * The payment collection the session is associated with. * diff --git a/packages/core/utils/src/totals/__tests__/totals.ts b/packages/core/utils/src/totals/__tests__/totals.ts index dae434d368..383a5385e1 100644 --- a/packages/core/utils/src/totals/__tests__/totals.ts +++ b/packages/core/utils/src/totals/__tests__/totals.ts @@ -329,7 +329,7 @@ describe("Total calculation", function () { tax_total: 9.512770562770562, discount_total: 17.5, discount_tax_total: 1.65, - original_total: 124.5, + original_total: 109.97619047619048, original_tax_total: 11.067099567099566, item_total: 97.37, item_subtotal: 100.9090909090909, @@ -618,7 +618,7 @@ describe("Total calculation", function () { tax_total: 10.08, discount_total: 24.2, discount_tax_total: 2.42, - original_total: 140.8, + original_total: 115.8, original_tax_total: 12.5, item_total: 85.8, item_subtotal: 100, diff --git a/packages/core/utils/src/totals/cart/index.ts b/packages/core/utils/src/totals/cart/index.ts index e54d455d27..72de3ac6d2 100644 --- a/packages/core/utils/src/totals/cart/index.ts +++ b/packages/core/utils/src/totals/cart/index.ts @@ -191,7 +191,7 @@ export function decorateCartTotals( // TODO: Gift Card calculations const originalTempTotal = MathBN.add( - subtotal, + itemsOriginalSubtotal, shippingOriginalTotal, originalTaxTotal ) diff --git a/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts b/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts index 1297a1f71b..d28418b60f 100644 --- a/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts +++ b/packages/modules/cart/integration-tests/__tests__/services/cart-module/index.spec.ts @@ -2742,7 +2742,7 @@ moduleIntegrationTestRunner({ tax_total: 0, discount_total: 300, discount_tax_total: 0, - original_total: 220, + original_total: 210, original_tax_total: 0, item_total: 200, item_subtotal: 500, @@ -2777,7 +2777,7 @@ moduleIntegrationTestRunner({ precision: 20, }, raw_original_total: { - value: "220", + value: "210", precision: 20, }, raw_original_tax_total: { diff --git a/packages/modules/order/src/services/actions/receive-return.ts b/packages/modules/order/src/services/actions/receive-return.ts index 8fcef5684a..2d77780d2a 100644 --- a/packages/modules/order/src/services/actions/receive-return.ts +++ b/packages/modules/order/src/services/actions/receive-return.ts @@ -50,9 +50,6 @@ function updateReturnItems(returnEntry, items) { const data = items.find((i) => i.details.reference_id === item.item_id) if (!data) return - console.log("ITEM: ", item) - console.log("DATA: ", data) - const receivedQuantity = MathBN.add( item.received_quantity || 0, data.details.quantity diff --git a/yarn.lock b/yarn.lock index 2e43741ae7..5651d2367c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15468,11 +15468,13 @@ __metadata: version: 0.0.0-use.local resolution: "create-medusa-app@workspace:packages/cli/create-medusa-app" dependencies: + "@medusajs/types": ^1.11.16 "@medusajs/utils": ^1.11.9 "@types/chalk": ^2.2.0 "@types/commander": ^2.12.2 "@types/configstore": ^6.0.0 "@types/inquirer": ^9.0.3 + "@types/node": ^20.12.11 "@types/node-emoji": ^1.8.2 "@types/pg": ^8.6.6 "@types/uuid": ^9.0.1 @@ -22754,6 +22756,7 @@ __metadata: peerDependencies: "@medusajs/medusa": ">1.19" "@medusajs/modules-sdk": ^1.12.10 + "@mikro-orm/core": 5.9.7 axios: ^0.28.0 express: ^4.18.3 get-port: ^5.1.0