Files
medusa-store/packages/modules-sdk/src/__tests__/remote-link.spec.ts
Carlos R. L. Rodrigues 4d16acf5f0 feat(link-modules,modules-sdk, utils, types, products) - Remote Link and Link modules (#4695)
What:
- Definition of all Modules links
- `link-modules` package to manage the creation of all pre-defined link or custom ones

```typescript
import { initialize as iniInventory } from "@medusajs/inventory";
import { initialize as iniProduct } from "@medusajs/product";

import {
  initialize as iniLinks,
  runMigrations as migrateLinks
} from "@medusajs/link-modules";

await Promise.all([iniInventory(), iniProduct()]);


await migrateLinks(); // create tables based on previous loaded modules

await iniLinks(); // load link based on previous loaded modules

await iniLinks(undefined, [
  {
    serviceName: "product_custom_translation_service_link",
    isLink: true,
    databaseConfig: {
      tableName: "product_transalations",
    },
    alias: [
      {
        name: "translations",
      },
    ],
    primaryKeys: ["id", "product_id", "translation_id"],
    relationships: [
      {
        serviceName: Modules.PRODUCT,
        primaryKey: "id",
        foreignKey: "product_id",
        alias: "product",
      },
      {
        serviceName: "custom_translation_service",
        primaryKey: "id",
        foreignKey: "translation_id",
        alias: "transalation",
        deleteCascade: true,
      },
    ],
    extends: [
      {
        serviceName: Modules.PRODUCT,
        relationship: {
          serviceName: "product_custom_translation_service_link",
          primaryKey: "product_id",
          foreignKey: "id",
          alias: "translations",
          isList: true,
        },
      },
      {
        serviceName: "custom_translation_service",
        relationship: {
          serviceName: "product_custom_translation_service_link",
          primaryKey: "product_id",
          foreignKey: "id",
          alias: "product_link",
        },
      },
    ],
  },
]); // custom links
```

Remote Link

```typescript
import { RemoteLink, Modules } from "@medusajs/modules-sdk";

// [...] initialize modules and links

const remoteLink = new RemoteLink();

// upsert the relationship
await remoteLink.create({ // one (object) or many (array)
  [Modules.PRODUCT]: {
    variant_id: "var_abc",
  },
  [Modules.INVENTORY]: {
    inventory_item_id: "iitem_abc",
  },
  data: { // optional additional fields
    required_quantity: 5
  }
});

// dismiss (doesn't cascade)
await remoteLink.dismiss({ // one (object) or many (array)
  [Modules.PRODUCT]: {
    variant_id: "var_abc",
  },
  [Modules.INVENTORY]: {
    inventory_item_id: "iitem_abc",
  },
});

// delete
await remoteLink.delete({
  // every key is a module
  [Modules.PRODUCT]: {
    // every key is a linkable field
    variant_id: "var_abc", // single or multiple values
  },
});

// restore
await remoteLink.restore({
  // every key is a module
  [Modules.PRODUCT]: {
    // every key is a linkable field
    variant_id: "var_abc", // single or multiple values
  },
});

```

Co-authored-by: Riqwan Thamir <5105988+riqwan@users.noreply.github.com>
2023-08-30 14:31:32 +00:00

203 lines
5.6 KiB
TypeScript

import { InventoryModule } from "../__mocks__/inventory-module"
import { InventoryStockLocationLink } from "../__mocks__/inventory-stock-location-link"
import { ProductInventoryLinkModule } from "../__mocks__/product-inventory-link"
import { ProductModule } from "../__mocks__/product-module"
import { StockLocationModule } from "../__mocks__/stock-location-module"
import { RemoteLink } from "../remote-link"
const allModules = [
// modules
ProductModule,
InventoryModule,
StockLocationModule,
// links
ProductInventoryLinkModule,
InventoryStockLocationLink,
]
describe("Remote Link", function () {
it("Should get all loaded modules and compose their relationships", async function () {
const remoteLink = new RemoteLink(allModules as any)
const relations = remoteLink.getRelationships()
const prodInventoryLink = relations.get(
"productVariantInventoryInventoryItemLink"
)
const prodModule = relations.get("productService")
const inventoryModule = relations.get("inventoryService")
expect(prodInventoryLink?.get("variant_id")).toEqual(
expect.arrayContaining([
expect.objectContaining({
serviceName: "productService",
primaryKey: "id",
foreignKey: "variant_id",
alias: "variant",
deleteCascade: true,
isPrimary: false,
isForeign: true,
}),
])
)
expect(prodInventoryLink?.get("inventory_item_id")).toEqual(
expect.arrayContaining([
expect.objectContaining({
serviceName: "inventoryService",
primaryKey: "id",
foreignKey: "inventory_item_id",
alias: "inventory",
deleteCascade: true,
isPrimary: false,
isForeign: true,
}),
])
)
expect(prodModule?.get("variant_id")).toEqual(
expect.arrayContaining([
expect.objectContaining({
serviceName: "productVariantInventoryInventoryItemLink",
primaryKey: "variant_id",
foreignKey: "id",
alias: "inventory_items",
isList: true,
isPrimary: true,
isForeign: false,
}),
])
)
expect(inventoryModule?.get("inventory_item_id")).toEqual(
expect.arrayContaining([
expect.objectContaining({
serviceName: "productVariantInventoryInventoryItemLink",
primaryKey: "inventory_item_id",
foreignKey: "id",
alias: "variant_link",
isPrimary: true,
isForeign: false,
}),
])
)
})
it("Should call the correct link module to create relation between 2 keys", async function () {
const remoteLink = new RemoteLink(allModules as any)
await remoteLink.create([
{
productService: {
variant_id: "var_123",
},
inventoryService: {
inventory_item_id: "inv_123",
},
},
{
productService: {
variant_id: "var_abc",
},
inventoryService: {
inventory_item_id: "inv_abc",
},
},
{
inventoryService: {
inventory_level_id: "ilev_123",
},
stockLocationService: {
stock_location_id: "loc_123",
},
},
])
expect(ProductInventoryLinkModule.create).toBeCalledWith([
["var_123", "inv_123"],
["var_abc", "inv_abc"],
])
expect(InventoryStockLocationLink.create).toBeCalledWith([
["ilev_123", "loc_123"],
])
})
it("Should call delete in cascade all the modules involved in the link", async function () {
const remoteLink = new RemoteLink(allModules as any)
ProductInventoryLinkModule.softDelete.mockImplementation(() => {
return {
variant_id: ["var_123"],
inventory_item_id: ["inv_123"],
}
})
ProductModule.softDelete.mockImplementation(() => {
return {
product_id: ["prod_123", "prod_abc"],
variant_id: ["var_123", "var_abc"],
}
})
InventoryModule.softDelete.mockImplementation(() => {
return {
inventory_item_id: ["inv_123"],
inventory_level_id: ["ilev_123"],
}
})
InventoryStockLocationLink.softDelete.mockImplementation(() => {
return {
inventory_level_id: ["ilev_123"],
stock_location_id: ["loc_123"],
}
})
await remoteLink.delete({
productService: {
variant_id: "var_123",
},
})
expect(ProductInventoryLinkModule.softDelete).toBeCalledTimes(2)
expect(ProductModule.softDelete).toBeCalledTimes(1)
expect(InventoryModule.softDelete).toBeCalledTimes(1)
expect(InventoryStockLocationLink.softDelete).toBeCalledTimes(1)
expect(ProductInventoryLinkModule.softDelete).toHaveBeenNthCalledWith(
1,
{ variant_id: ["var_123"] },
{ returnLinkableKeys: ["variant_id", "inventory_item_id"] }
)
expect(ProductInventoryLinkModule.softDelete).toHaveBeenNthCalledWith(
2,
{ variant_id: ["var_abc"] },
{ returnLinkableKeys: ["variant_id", "inventory_item_id"] }
)
expect(ProductModule.softDelete).toBeCalledWith(
{ id: ["var_123"] },
{ returnLinkableKeys: ["product_id", "variant_id"] }
)
expect(InventoryModule.softDelete).toBeCalledWith(
{ id: ["inv_123"] },
{
returnLinkableKeys: [
"inventory_item_id",
"inventory_level_id",
"reservation_item_id",
],
}
)
expect(InventoryStockLocationLink.softDelete).toBeCalledWith(
{
inventory_level_id: ["ilev_123"],
},
{ returnLinkableKeys: ["inventory_level_id", "stock_location_id"] }
)
})
})