47 Commits

Author SHA1 Message Date
Oli Juhl
5f807ee657 chore: Ensure isolated order deletion (#14405)
## Summary

Ensure that address deletion during order deletion is handled correctly with respect to cascading deletes. Currently, when deleting an order that does not have a shipping or billing address, we incorrectly attempt to delete all order addresses. This happens because `undefined` address IDs are not handled properly during deletion.

More specifically, when deleting an order without addresses, the following method is called with these arguments:
```ts
await deleteOrderAddresses([undefined, undefined])
```
This triggers deletion of all order addresses.

To make matters worse, because we have a cascade delete defined from order addresses to orders, the same call also deletes all associated orders.

The root cause is the following filtering logic:
```ts
const orderAddressIds = orders
      .map((order) => [order.shipping_address_id, order.billing_address_id])
      .flat(1)
```

For orders without addresses, this produces `[undefined, undefined]` as input filter, which our delete methods interpret as:
```ts
await delete({ '$or': [ { id: undefined }, { id: undefined } ] })
```

This effectively translates to “delete all addresses.”

In this PR, we make two changes to prevent this going forward:
1. Filter out undefined address IDs before attempting deletion
2. Remove the cascade delete from address to order, as this is an aggressive constraint

The latter change is open for discussion, but cascading deletes from a child entity to a parent is slightly off IMO. Let me know your thoughts.

## Checklist

Please ensure the following before requesting a review:

- [x] I have added a **changeset** for this PR
    - Every non-breaking change should be marked as a **patch**
    - To add a changeset, run `yarn changeset` and follow the prompts
- [x] The changes are covered by relevant **tests**
- [x] I have verified the code works as intended locally
- [ ] I have linked the related issue(s) if applicable

---

## Additional Context

Add any additional context, related issues, or references that might help the reviewer understand this PR.


---

> [!NOTE]
> Strengthens order deletion to avoid unintended cascades and validates behavior with tests.
> 
> - Update FK constraints in migration to `ON DELETE SET NULL` for `order.shipping_address_id` and `order.billing_address_id` (was `CASCADE`)
> - In `order-module-service.ts` `deleteOrders`, filter falsy address IDs and conditionally batch-delete `order_address`/`order_change` via `promiseAll`
> - Add integration tests `integration-tests/__tests__/delete-order.spec.ts` covering deletion of orders and associated entities, deleting orders without addresses (no cross-order impact), and address deletion setting `NULL` on the order
> - Add changeset marking `@medusajs/order` patch
> 
> <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 8e4ab59af407ec865f73fbf286ec93e710915c8e. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup>
2026-01-02 10:16:29 +00:00
Oli Juhl
78842af1c3 fix: Compute "virtual" adjustments for order previews (#13306)
* wip

* add wip

* wip

* reuse action

* finish first draft

* fix tests

* cleanup

* Only compute adjustments when necessary

* Create hot-carrots-look.md

* address comments

* minor tweaks

* fix pay col

* fix test

* wip

* Dwip

* wip

* fix: adjustment typo

* fix: import

* fix: workflow imports

* wip: update test

* feat: upsert versioned  adjustments when previewing order

* fix: revert unique codes change

* fix: order spec test with versioning

* wip: save

* feat: make adjustments work for preview and confirm flow, wip base repo filtering of older version adjustments

* fix: missing populate where

* wip: populate where loading versioned adjustments

* fix: filter out older adjustment versions

* temp: comment adjustments in repo

* test: add adjustment if no version

* wip: configure populate where in order base repository

* fix: rm manual filtering

* fix: revert base repo changes

* fix: revert

* fix: use order item version instead of order version

* fix: rm only in test

* fix: update case spec

* fix: remove sceanrio, wip test with draft promotion

* feat: test correct adjustments when disabling promotion

* feat: complex test case

* feat: test consecutive order edits

* feat: 2 promotions test case with a fixed promo

* feat: migrate existing order line item adjustments to order items latest version

* feat: update dep after merge

* wip: load adjustments separatley

* feat: adjustments collections

* fix: spread result, handle related entity case

* fix: update lock

* feat: make sure version is loaded, refactor, handle related entity case

* fix: check fields

* feat: loading adjustments for list and count

* fix: correct items version field

* fix: rm empty array

* fix: wip order modules spec

* fix: order module specs

* feat: preinit items adjustments

* fix: rm only

* fix: rm only

* chore: cleanup

* fix: migration files

* fix: dont change formatting

* fix: core package build

* chore: more cleanup

* fix: item update util

* fix: duplicate import

* fix: refresh adjustments for exchanges (#13992)

* wip: exchange adjustments

* feat: test - receive items

* feat: finish test case

* fix: casing

* fix(draft-orders, core-flows, orders) refresh adjustments for draft orders (#14025)

* wip: draft orders adjustments refresh

* feat: rewrite to use REPLACE action + test

* fix: rm only

* feat: cleanup old REPLACE actions

* feat: cleanup adjustemnts when 0 promotions

* wip: canceling draft order

* fix: make version arg optional

* fix: restore promotion links

* feat: test reverting on cancelation

* fix: address comments in tests

* wip: fix summary on preview

* fix: get pending diff on preview summary from total

* fix: revert pending diff change

---------

Co-authored-by: fPolic <mainacc.polic@gmail.com>
Co-authored-by: Frane Polić <16856471+fPolic@users.noreply.github.com>
2025-11-25 10:41:14 +01:00
Adrien de Peretti
6746fecd72 chore(order): custom display id (#14024)
* chore(order): Accept a custom display generator option

* chore(order): Accept a custom display generator option

* wip

* wip

* finalize

* Create tricky-olives-battle.md

* fields

* changeset

* update integration tests

* update migrations

* fix changeset
2025-11-18 10:17:46 +01:00
William Bouchard
fe4e7481a9 feat(order,dashboard): version order credit lines (#13766)
* feat(): version order credit lines

* undo last change

* adjust where

* remove date on ui

* Create five-donuts-obey.md

* add test

* nit comment

* woops
2025-10-22 10:26:05 +02:00
Carlos R. L. Rodrigues
9563ee446f fix(utils,core-flows): subtotal calculation and returns location (#13497)
* fix(utils,core-flows): subtotal calculation and returns location

* changeset

* fix test

* var

* rm extra field from test

* fix original total

* fix partial refunds and pending difference

* fix test

* fix test

* test

* extract to util

* original total and update payment when receive return

* original_subtotal

* default fields

* test

* calculate pending difference

* revert claims test

* pending difference

* creadit line fix

* if
2025-09-18 17:50:40 +02:00
Adrien de Peretti
823a5c75ff chore(): order module index and some impl details (#13462)
* order module fixes

* order module fixes

* fixes

* revert big number

* Create wet-wombats-rule.md
2025-09-11 09:02:38 +02:00
Adrien de Peretti
2f70f13351 fix(): Order constraint and receive return (#12889)
**What**
- Fix missing `ON DELETE CASCADE` constraint on order credit lines
- Fix `receiveReturn` miss usage
- Make all order integration tests to run and rename them all to `*.spec.ts`
- Fix package.json typo
2025-07-04 12:59:50 +00:00
Carlos R. L. Rodrigues
0c957350a6 fix(order): calculate taxes on order edit flows (#11518)
* fix(order): calcualte taxes on order edit flows

* merge summary

* tests

* fix pending difference

* comments

* claim test

* revert method
2025-02-21 15:27:40 +01:00
Carlos R. L. Rodrigues
cc73802ab3 chore(order): dml (#10292)
* ../../core/types/src/dml/index.ts

* ../../core/types/src/dml/index.ts

* fix: relationships mapping

* handle nullable foreign keys types

* handle nullable foreign keys types

* handle nullable foreign keys types

* continue to update product category repository

* fix all product category repositories issues

* fix product category service types

* fix product module service types

* fix product module service types

* fix repository template type

* refactor: use a singleton DMLToMikroORM factory instance

Since the MikroORM MetadataStorage is global, we will also have to turn DML
to MikroORM entities conversion use a global bucket as well

* refactor: update product module to use DML in tests

* wip: tests

* WIP product linkable fixes

* continue type fixing and start test fixing

* test: fix more tests

* fix repository

* fix pivot table computaion + fix mikro orm repository

* fix many to many management and configuration

* fix many to many management and configuration

* fix many to many management and configuration

* update product tag relation configuration

* Introduce experimental dml hooks to fix some issues with categories

* more fixes

* fix product tests

* add missing id prefixes

* fix product category handle management

* test: fix more failing tests

* test: make it all green

* test: fix breaking tests

* fix: build issues

* fix: build issues

* fix: more breaking tests

* refactor: fix issues after merge

* refactor: fix issues after merge

* refactor: surpress types error

* test: fix DML failing tests

* improve many to many inference + tests

* Wip fix columns from product entity

* remove product model before create hook and manage handle validation and transformation at the service level

* test: fix breaking unit tests

* fix: product module service to not update handle on product update

* fix define link and joiner config

* test: fix joiner config test

* test: fix joiner config test

* fix joiner config primary keys

* Fix joiner config builder

* Fix joiner config builder

* test: remove only modifier from test

* refactor: remove hooks usage from product collection

* refactor: remove hooks usage from product-option

* refactor: remove hooks usage for computing category handle

* refactor: remove hooks usage from productCategory model

* refactor: remove hooks from DML

* refactor: remove cruft

* order dml

* cleanup

* re add foerign key indexes

* wip

* chore: remove unused types

* wip

* changes

* rm raw

* autoincrement

* wip

* rel

* refactor: cleanup

* migration and models configuration adjustments

* cleanup

* number searchable

* fix random ordering

* fix

* test: fix product-category tests

* test: update breaking DML tests

* test: array assertion to not care about ordering

* fix: temporarily apply id ordering for products

* types

* wip

* WIP type improvements

* update order models

* partially fix types temporarely

* rel

* fix: recursive type issue

* improve type inference breaks

* improve type inference breaks

* update models

* rm nullable

* default value

* repository

* update default value handling

* fix unit tests

* WIP

* toMikroORM

* fix relations

* cascades

* fix

* experimental dml hooks

* rm migration

* serial

* nullable autoincrement

* fix model

* model changes

* fix one to one DML

* order test

* fix addresses

* fix unit tests

* Re align dml entity name inference

* update model table name config

* update model table name config

* revert

* update return relation

* WIP

* hasOne

* models

* fix

* model

* initial commit

* cart service

* order module

* utils unit test

* index engine

* changeset

* merge

* fix hasOne with fk

* update

* free text filter per entity

* tests

* prod category

* property string many to many

* fix big number

* link modules migration set names

* merge

* shipping option rules

* serializer

* unit test

* fix test mikro orm init

* fix test mikro orm init

* Maintain merge object properties

* fix test mikro orm init

* prevent unit test from connecting to db

* wip

* fix test

* fix test

* link test

* schema

* models

* auto increment

* hook

* model hooks

* order

* wip

* orm version

* request return field

* fix return configuration on order model

* workflows

* core flows

* unit test

* test

* base repo

* test

* base repo

* test fix

* inventory move

* locking inventory

* test

* free text fix

* rm timeout mock

* migrate fulfillment values

* v6.4.3

* cleanup

* link-modules update sql

* revert test

* remove fake timers

---------

Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
Co-authored-by: Harminder Virk <virk.officials@gmail.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
2025-01-21 08:04:47 -05:00
Oli Juhl
505768dd9f fix: Product type tax overrides (#9951)
* fix: Make product type tax override work

* fix: Make product type tax override work
2024-11-06 20:33:40 +01:00
Carlos R. L. Rodrigues
2a98be6b65 feat(medusa): ordem items endpoint (#9646) 2024-10-18 05:59:24 -03:00
Harminder Virk
68560787e5 breaking: rename package names to be consistent and under @medusajs scope (#9580) 2024-10-16 22:28:09 +05:30
Adrien de Peretti
1d8939df3a chore(): Allow to register modules through array (#9522) 2024-10-11 15:17:00 +02:00
Adrien de Peretti
e096feb7d5 chore: Update modules deps (#9286) 2024-09-26 11:14:35 +05:30
Adrien de Peretti
90d530565b chore(): Remove default limit from the build query (#9257)
* chore(): Remove default limit from the build query

* rm take: null

* fix tests

* fix tests

* fix db usage

* fix typo

* rm unsused template arg

* fixes

* fixes

* fixes

* fixes

* fixes

* fixes

* fixes
2024-09-24 16:06:45 +02:00
Riqwan Thamir
3e97a64b21 feat(core-flows,medusa,utils,types): adds delivered_quantity to order (#9130)
what:

- adds delivered_quantity to order


https://github.com/user-attachments/assets/709b1727-08ed-4a88-ae29-38f13540e301
2024-09-16 09:59:01 +00:00
Carlos R. L. Rodrigues
950cf9af79 chore: remove container registration name (#9137) 2024-09-16 06:22:24 -03:00
Carlos R. L. Rodrigues
fdd0543011 chore: joiner config entity property (#9084) 2024-09-11 06:25:25 -03:00
Riqwan Thamir
afd0921326 fix(utils,medusa,order,cart): fix totals when promotions are included (#9014)
* fix(utils): fix totals when promotions are included

* chore: update totals calc

* chore: ignore taxes when taxable amount is 0

* chore: use subtotals everywhere

* chore: fix shipping totals + tests
2024-09-10 10:59:22 +02:00
Adrien de Peretti
2c5e72d141 feat: Application types generation from project GQL schema's (#8995) 2024-09-06 15:15:32 +05:30
Carlos R. L. Rodrigues
6cfe9bd874 fix(orchestration): remote joiner alias conflict (#8844) 2024-08-28 11:34:22 -03:00
Carlos R. L. Rodrigues
be942ff15c fix(order): update order summary when transactions change (#8633) 2024-08-16 19:40:44 +00:00
Carlos R. L. Rodrigues
91f07e1a59 chore(order): item update quantity (#8513) 2024-08-08 14:12:16 -03:00
Carlos R. L. Rodrigues
42c80e4264 Chore/order claims 2 (#8312) 2024-07-29 10:15:35 -03:00
Carlos R. L. Rodrigues
7a5349c0ae chore(core-flows): adjust inventory when return is received (#8222) 2024-07-23 13:36:46 -03:00
Adrien de Peretti
566300d54b fix: normalize path before consuming it while loading models (#8194)
* fix: normalize path before consuming it while loading models

* Make it so that models derived from the service if present

* fix linkable tests that now match what has been passed to the medusa service if there is no custom joiner config

* fix linkable tests that now match what has been passed to the medusa service if there is no custom joiner config

* fix linkable tests that now match what has been passed to the medusa service if there is no custom joiner config
2024-07-21 21:21:05 +02:00
Carlos R. L. Rodrigues
535832b692 chore(medusa, core-flows): receive item return request (#8172)
What:
* `POST /admin/returns/:id/receive-items`
* `POST /admin/returns/:id/receive-items/:action_id`
* `DELETE /admin/returns/:id/receive-items/:action_id`
* `POST /admin/returns/:id/dismiss-items`
* `POST /admin/returns/:id/dismiss-items/:action_id`
* `DELETE /admin/returns/:id/dismiss-items/:action_id`
* `POST /admin/returns/:id/receive/confirm`

CLOSES: CC-190, CC-191, CC-192, CC-193, CC-195, CC-196, CC-197

* Inventory management isn't included yet. Will add it in a next PR
2024-07-18 14:46:34 +00:00
Carlos R. L. Rodrigues
d4fe2daa57 fix(core-flow): request item return reason (#8152) 2024-07-17 09:35:33 +01:00
Carlos R. L. Rodrigues
5813216c88 core(core-flows, medusa): remove request item return (#8146) 2024-07-16 11:23:34 -03:00
Carlos R. L. Rodrigues
7123f9ff63 feat(core-flows,medusa,order): remove return shipping (#8137)
What:
* `DELETE /admin/returs/:id/shipping_methods/:action_id`

FIXES: CC-187
2024-07-16 10:27:16 +00:00
Riqwan Thamir
bec2dcd667 feat(core-flows, order): add workflow to create change order actions (#8056)
what:

- adds workflow to create change order actions
2024-07-10 10:35:03 +00:00
Carlos R. L. Rodrigues
2b2e2fbb3d chore(order): preview order change (#8025)
What:
 - new method `previewOrderChange`
   - Calculate all the actions related to an order change.
   - Return the preview of the final Order, with all the calculated values.
   - Associate actions with items and shipping_methods they modified.

FIXES: CORE-2509
2024-07-09 14:45:55 +00:00
Riqwan Thamir
b6fd82e31e feat(order,core-flows): added order change create workflow (#8033)
what:

- adds anorder change create workflow
- remove order change service, brings validation to entry point service
2024-07-09 12:12:05 +00:00
Adrien de Peretti
b368251ca3 chore: Migrate modules to use the Module util (#7964)
**What**
Apply the `Module` util to each module to handle the export and provide the linkable configuration object.

- Most joiner config that does not have any special config are removed/cleaned up in favor of the autogenerated one or simpler one
- linkable are now available in all modules
- cleaned up the dependencies of the modules

FIXES CORE-2410
2024-07-05 13:00:40 +00:00
Carlos R. L. Rodrigues
56394fe0d4 fix(order): order list payment and fulfillment status (#7934) 2024-07-04 06:37:25 -03:00
Carlos R. L. Rodrigues
07715e6b50 chore(order): cancel return (#7881) 2024-07-02 06:52:58 -03:00
Adrien de Peretti
937a632eb6 chore: make module loaders DML aware and auto generate joiner config (#7781)
* chore: make module loaders DML aware and auto generate joiner config

* fixes and cleanup

* improve dml entity check

* add unit tests on load resources

* cleanup deps

* cleanup deps

* cleanup Modules

* finalise

* fix modules-sdk jest

* fix modules-sdk jest

* fix import

* fix import
2024-06-20 18:18:07 +02:00
Adrien de Peretti
48963f55ef Chore/rm main entity concept (#7709)
**What**
Update the `MedusaService` class, factory and types to remove the concept of main modules. The idea being that all method will be explicitly named and suffixes to represent the object you are trying to manipulate.
This pr also includes various fixes in different modules

Co-authored-by: Stevche Radevski <4820812+sradevski@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
2024-06-19 13:02:16 +00:00
Carlos R. L. Rodrigues
cfa983001b feat(order): create claim and exchange (#7734) 2024-06-18 08:08:16 -03:00
Carlos R. L. Rodrigues
37426939da Feat(order): post purchase support (#7666) 2024-06-10 18:44:51 -03:00
Carlos R. L. Rodrigues
122186a78d chore(order): cancel order (#7586) 2024-06-03 12:31:33 -03:00
Carlos R. L. Rodrigues
af0140d317 feat(order): cancel fulfillment (#7573) 2024-06-02 09:33:24 -03:00
Carlos R. L. Rodrigues
62ef82f497 feat(order): fulfillment workflow (#7385)
FIXES: CORE-2162 CORE-2167 CORE-2041
2024-05-27 12:49:33 +00:00
Carlos R. L. Rodrigues
f57dc98c7a chore(order): Paid Total and Refunded Total (#7373) 2024-05-20 15:48:57 -03:00
Carlos R. L. Rodrigues
6c94d0205c Feat(order): return reason and transaction methods (#7303) 2024-05-13 16:15:25 +00:00
Oli Juhl
cf9605fe6f feat(order): Add display_id to order table (#7242)
Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
2024-05-05 19:43:13 +02:00
Adrien de Peretti
4eae25e1ef chore(): Reorganize modules (#7210)
**What**
Move all modules to the modules directory
2024-05-02 15:33:34 +00:00