main
| 1 | import { |
| 2 | addShippingMethodToCartWorkflow, |
| 3 | addToCartWorkflow, |
| 4 | createCartCreditLinesWorkflow, |
| 5 | createCartWorkflow, |
| 6 | createPaymentCollectionForCartWorkflow, |
| 7 | deleteCartCreditLinesWorkflow, |
| 8 | deleteLineItemsStepId, |
| 9 | deleteLineItemsWorkflow, |
| 10 | findOrCreateCustomerStepId, |
| 11 | listShippingOptionsForCartWithPricingWorkflow, |
| 12 | listShippingOptionsForCartWorkflow, |
| 13 | refreshPaymentCollectionForCartWorkflow, |
| 14 | updateCartWorkflow, |
| 15 | updateLineItemInCartWorkflow, |
| 16 | updateLineItemsStepId, |
| 17 | updatePaymentCollectionStepId, |
| 18 | updateTaxLinesWorkflow, |
| 19 | } from "@medusajs/core-flows" |
| 20 | import { StepResponse } from "@medusajs/framework/workflows-sdk" |
| 21 | import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
| 22 | import { |
| 23 | ICartModuleService, |
| 24 | ICustomerModuleService, |
| 25 | IFulfillmentModuleService, |
| 26 | IInventoryService, |
| 27 | IPaymentModuleService, |
| 28 | IPricingModuleService, |
| 29 | IProductModuleService, |
| 30 | IRegionModuleService, |
| 31 | ISalesChannelModuleService, |
| 32 | IStockLocationService, |
| 33 | } from "@medusajs/types" |
| 34 | import { |
| 35 | ContainerRegistrationKeys, |
| 36 | Modules, |
| 37 | PriceListStatus, |
| 38 | PriceListType, |
| 39 | ProductStatus, |
| 40 | RuleOperator, |
| 41 | } from "@medusajs/utils" |
| 42 | import { |
| 43 | adminHeaders, |
| 44 | createAdminUser, |
| 45 | generatePublishableKey, |
| 46 | generateStoreHeaders, |
| 47 | } from "../../../../helpers/create-admin-user" |
| 48 | import { seedStorefrontDefaults } from "../../../../helpers/seed-storefront-defaults" |
| 49 | import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer" |
| 50 | import { normalizeBigNumbers } from "@medusajs/test-utils" |
| 51 | |
| 52 | jest.setTimeout(200000) |
| 53 | |
| 54 | const env = {} |
| 55 | |
| 56 | medusaIntegrationTestRunner({ |
| 57 | env, |
| 58 | testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
| 59 | describe("Carts workflows", () => { |
| 60 | let appContainer |
| 61 | let cartModuleService: ICartModuleService |
| 62 | let regionModuleService: IRegionModuleService |
| 63 | let scModuleService: ISalesChannelModuleService |
| 64 | let customerModule: ICustomerModuleService |
| 65 | let productModule: IProductModuleService |
| 66 | let pricingModule: IPricingModuleService |
| 67 | let paymentModule: IPaymentModuleService |
| 68 | let stockLocationModule: IStockLocationService |
| 69 | let inventoryModule: IInventoryService |
| 70 | let fulfillmentModule: IFulfillmentModuleService |
| 71 | let remoteLink |
| 72 | let remoteQuery |
| 73 | let query |
| 74 | let storeHeaders |
| 75 | let salesChannel |
| 76 | let defaultRegion |
| 77 | let customer |
| 78 | let storeHeadersWithCustomer |
| 79 | let setPricingContextHook: any |
| 80 | let setShippingOptionsContextHook: any |
| 81 | |
| 82 | beforeAll(async () => { |
| 83 | appContainer = getContainer() |
| 84 | cartModuleService = appContainer.resolve(Modules.CART) |
| 85 | regionModuleService = appContainer.resolve(Modules.REGION) |
| 86 | scModuleService = appContainer.resolve(Modules.SALES_CHANNEL) |
| 87 | customerModule = appContainer.resolve(Modules.CUSTOMER) |
| 88 | productModule = appContainer.resolve(Modules.PRODUCT) |
| 89 | pricingModule = appContainer.resolve(Modules.PRICING) |
| 90 | paymentModule = appContainer.resolve(Modules.PAYMENT) |
| 91 | fulfillmentModule = appContainer.resolve(Modules.FULFILLMENT) |
| 92 | inventoryModule = appContainer.resolve(Modules.INVENTORY) |
| 93 | stockLocationModule = appContainer.resolve(Modules.STOCK_LOCATION) |
| 94 | remoteLink = appContainer.resolve(ContainerRegistrationKeys.REMOTE_LINK) |
| 95 | remoteQuery = appContainer.resolve( |
| 96 | ContainerRegistrationKeys.REMOTE_QUERY |
| 97 | ) |
| 98 | query = appContainer.resolve(ContainerRegistrationKeys.QUERY) |
| 99 | |
| 100 | createCartWorkflow.hooks.setPricingContext( |
| 101 | (input) => { |
| 102 | if (setPricingContextHook) { |
| 103 | return setPricingContextHook(input) |
| 104 | } |
| 105 | }, |
| 106 | () => {} |
| 107 | ) |
| 108 | addToCartWorkflow.hooks.setPricingContext( |
| 109 | (input) => { |
| 110 | if (setPricingContextHook) { |
| 111 | return setPricingContextHook(input) |
| 112 | } |
| 113 | }, |
| 114 | () => {} |
| 115 | ) |
| 116 | listShippingOptionsForCartWorkflow.hooks.setPricingContext( |
| 117 | (input) => { |
| 118 | if (setPricingContextHook) { |
| 119 | return setPricingContextHook(input) |
| 120 | } |
| 121 | }, |
| 122 | () => {} |
| 123 | ) |
| 124 | listShippingOptionsForCartWorkflow.hooks.setShippingOptionsContext( |
| 125 | (input) => { |
| 126 | if (setShippingOptionsContextHook) { |
| 127 | return setShippingOptionsContextHook(input) |
| 128 | } |
| 129 | }, |
| 130 | () => {} |
| 131 | ) |
| 132 | listShippingOptionsForCartWithPricingWorkflow.hooks.setShippingOptionsContext( |
| 133 | (input) => { |
| 134 | if (setShippingOptionsContextHook) { |
| 135 | return setShippingOptionsContextHook(input) |
| 136 | } |
| 137 | }, |
| 138 | () => {} |
| 139 | ) |
| 140 | }) |
| 141 | |
| 142 | beforeAll(async () => { |
| 143 | const publishableKey = await generatePublishableKey(appContainer) |
| 144 | storeHeaders = generateStoreHeaders({ publishableKey }) |
| 145 | await createAdminUser(dbConnection, adminHeaders, appContainer) |
| 146 | |
| 147 | const result = await createAuthenticatedCustomer(api, storeHeaders, { |
| 148 | first_name: "tony", |
| 149 | last_name: "stark", |
| 150 | email: "tony@test-industries.com", |
| 151 | }) |
| 152 | |
| 153 | customer = result.customer |
| 154 | storeHeadersWithCustomer = { |
| 155 | headers: { |
| 156 | ...storeHeaders.headers, |
| 157 | authorization: `Bearer ${result.jwt}`, |
| 158 | }, |
| 159 | } |
| 160 | |
| 161 | const { region } = await seedStorefrontDefaults(appContainer, "dkk") |
| 162 | |
| 163 | defaultRegion = region |
| 164 | |
| 165 | salesChannel = ( |
| 166 | await api.post( |
| 167 | "/admin/sales-channels", |
| 168 | { name: "test sales channel", description: "channel" }, |
| 169 | adminHeaders |
| 170 | ) |
| 171 | ).data.sales_channel |
| 172 | |
| 173 | await dbUtils.snapshot() |
| 174 | }) |
| 175 | |
| 176 | describe("CreateCartWorkflow", () => { |
| 177 | it("should create a cart", async () => { |
| 178 | const region = await regionModuleService.createRegions({ |
| 179 | name: "US", |
| 180 | currency_code: "usd", |
| 181 | }) |
| 182 | |
| 183 | const salesChannel = await scModuleService.createSalesChannels({ |
| 184 | name: "Webshop", |
| 185 | }) |
| 186 | |
| 187 | const location = await stockLocationModule.createStockLocations({ |
| 188 | name: "Warehouse", |
| 189 | }) |
| 190 | |
| 191 | const [product] = await productModule.createProducts([ |
| 192 | { |
| 193 | title: "Test product", |
| 194 | status: ProductStatus.PUBLISHED, |
| 195 | variants: [ |
| 196 | { |
| 197 | title: "Test variant", |
| 198 | }, |
| 199 | ], |
| 200 | }, |
| 201 | ]) |
| 202 | |
| 203 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 204 | sku: "inv-1234", |
| 205 | }) |
| 206 | |
| 207 | await inventoryModule.createInventoryLevels([ |
| 208 | { |
| 209 | inventory_item_id: inventoryItem.id, |
| 210 | location_id: location.id, |
| 211 | stocked_quantity: 2, |
| 212 | reserved_quantity: 0, |
| 213 | }, |
| 214 | ]) |
| 215 | |
| 216 | const priceSet = await pricingModule.createPriceSets({ |
| 217 | prices: [ |
| 218 | { |
| 219 | amount: 3000, |
| 220 | currency_code: "usd", |
| 221 | }, |
| 222 | ], |
| 223 | }) |
| 224 | |
| 225 | await pricingModule.createPricePreferences({ |
| 226 | attribute: "currency_code", |
| 227 | value: "usd", |
| 228 | is_tax_inclusive: true, |
| 229 | }) |
| 230 | |
| 231 | await remoteLink.create([ |
| 232 | { |
| 233 | [Modules.PRODUCT]: { |
| 234 | variant_id: product.variants[0].id, |
| 235 | }, |
| 236 | [Modules.PRICING]: { |
| 237 | price_set_id: priceSet.id, |
| 238 | }, |
| 239 | }, |
| 240 | { |
| 241 | [Modules.SALES_CHANNEL]: { |
| 242 | sales_channel_id: salesChannel.id, |
| 243 | }, |
| 244 | [Modules.STOCK_LOCATION]: { |
| 245 | stock_location_id: location.id, |
| 246 | }, |
| 247 | }, |
| 248 | { |
| 249 | [Modules.PRODUCT]: { |
| 250 | variant_id: product.variants[0].id, |
| 251 | }, |
| 252 | [Modules.INVENTORY]: { |
| 253 | inventory_item_id: inventoryItem.id, |
| 254 | }, |
| 255 | }, |
| 256 | ]) |
| 257 | |
| 258 | const { result } = await createCartWorkflow(appContainer).run({ |
| 259 | input: { |
| 260 | email: "tony@stark.com", |
| 261 | currency_code: "usd", |
| 262 | region_id: region.id, |
| 263 | sales_channel_id: salesChannel.id, |
| 264 | items: [ |
| 265 | { |
| 266 | variant_id: product.variants[0].id, |
| 267 | quantity: 1, |
| 268 | }, |
| 269 | ], |
| 270 | }, |
| 271 | }) |
| 272 | |
| 273 | const cart = await cartModuleService.retrieveCart(result.id, { |
| 274 | relations: ["items"], |
| 275 | }) |
| 276 | |
| 277 | expect(normalizeBigNumbers(cart)).toEqual( |
| 278 | expect.objectContaining({ |
| 279 | currency_code: "usd", |
| 280 | email: "tony@stark.com", |
| 281 | region_id: region.id, |
| 282 | sales_channel_id: salesChannel.id, |
| 283 | customer_id: expect.any(String), |
| 284 | items: expect.arrayContaining([ |
| 285 | expect.objectContaining({ |
| 286 | quantity: 1, |
| 287 | unit_price: 3000, |
| 288 | is_tax_inclusive: true, |
| 289 | }), |
| 290 | ]), |
| 291 | }) |
| 292 | ) |
| 293 | }) |
| 294 | |
| 295 | it("should create a cart with items using customer-group price list", async () => { |
| 296 | const region = await regionModuleService.createRegions({ |
| 297 | name: "US", |
| 298 | currency_code: "usd", |
| 299 | }) |
| 300 | |
| 301 | const salesChannel = await scModuleService.createSalesChannels({ |
| 302 | name: "Webshop", |
| 303 | }) |
| 304 | |
| 305 | const customer = await customerModule.createCustomers({ |
| 306 | first_name: "Test", |
| 307 | last_name: "Test", |
| 308 | }) |
| 309 | |
| 310 | const customer_group = await customerModule.createCustomerGroups({ |
| 311 | name: "Test Group", |
| 312 | }) |
| 313 | |
| 314 | await customerModule.addCustomerToGroup({ |
| 315 | customer_id: customer.id, |
| 316 | customer_group_id: customer_group.id, |
| 317 | }) |
| 318 | |
| 319 | const location = await stockLocationModule.createStockLocations({ |
| 320 | name: "Warehouse", |
| 321 | }) |
| 322 | |
| 323 | const [product] = await productModule.createProducts([ |
| 324 | { |
| 325 | title: "Test product", |
| 326 | status: ProductStatus.PUBLISHED, |
| 327 | variants: [ |
| 328 | { |
| 329 | title: "Test variant", |
| 330 | }, |
| 331 | ], |
| 332 | }, |
| 333 | ]) |
| 334 | |
| 335 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 336 | sku: "inv-1234", |
| 337 | }) |
| 338 | |
| 339 | await inventoryModule.createInventoryLevels([ |
| 340 | { |
| 341 | inventory_item_id: inventoryItem.id, |
| 342 | location_id: location.id, |
| 343 | stocked_quantity: 2, |
| 344 | }, |
| 345 | ]) |
| 346 | |
| 347 | const priceSet = await pricingModule.createPriceSets({ |
| 348 | prices: [ |
| 349 | { |
| 350 | amount: 3000, |
| 351 | currency_code: "usd", |
| 352 | }, |
| 353 | ], |
| 354 | }) |
| 355 | |
| 356 | await pricingModule.createPricePreferences({ |
| 357 | attribute: "currency_code", |
| 358 | value: "usd", |
| 359 | is_tax_inclusive: true, |
| 360 | }) |
| 361 | |
| 362 | await pricingModule.createPriceLists([ |
| 363 | { |
| 364 | title: "test price list", |
| 365 | description: "test", |
| 366 | status: PriceListStatus.ACTIVE, |
| 367 | type: PriceListType.OVERRIDE, |
| 368 | prices: [ |
| 369 | { |
| 370 | amount: 1500, |
| 371 | currency_code: "usd", |
| 372 | price_set_id: priceSet.id, |
| 373 | }, |
| 374 | ], |
| 375 | rules: { |
| 376 | "customer.groups.id": [customer_group.id], |
| 377 | }, |
| 378 | }, |
| 379 | ]) |
| 380 | |
| 381 | await remoteLink.create([ |
| 382 | { |
| 383 | [Modules.PRODUCT]: { |
| 384 | variant_id: product.variants[0].id, |
| 385 | }, |
| 386 | [Modules.PRICING]: { |
| 387 | price_set_id: priceSet.id, |
| 388 | }, |
| 389 | }, |
| 390 | { |
| 391 | [Modules.SALES_CHANNEL]: { |
| 392 | sales_channel_id: salesChannel.id, |
| 393 | }, |
| 394 | [Modules.STOCK_LOCATION]: { |
| 395 | stock_location_id: location.id, |
| 396 | }, |
| 397 | }, |
| 398 | { |
| 399 | [Modules.PRODUCT]: { |
| 400 | variant_id: product.variants[0].id, |
| 401 | }, |
| 402 | [Modules.INVENTORY]: { |
| 403 | inventory_item_id: inventoryItem.id, |
| 404 | }, |
| 405 | }, |
| 406 | ]) |
| 407 | |
| 408 | const { result } = await createCartWorkflow(appContainer).run({ |
| 409 | input: { |
| 410 | currency_code: "usd", |
| 411 | region_id: region.id, |
| 412 | customer_id: customer.id, |
| 413 | sales_channel_id: salesChannel.id, |
| 414 | items: [ |
| 415 | { |
| 416 | variant_id: product.variants[0].id, |
| 417 | quantity: 1, |
| 418 | }, |
| 419 | ], |
| 420 | }, |
| 421 | }) |
| 422 | |
| 423 | const cart = await cartModuleService.retrieveCart(result.id, { |
| 424 | relations: ["items"], |
| 425 | }) |
| 426 | |
| 427 | expect(normalizeBigNumbers(cart)).toEqual( |
| 428 | expect.objectContaining({ |
| 429 | currency_code: "usd", |
| 430 | items: expect.arrayContaining([ |
| 431 | expect.objectContaining({ |
| 432 | unit_price: 1500, |
| 433 | is_tax_inclusive: true, |
| 434 | quantity: 1, |
| 435 | }), |
| 436 | ]), |
| 437 | }) |
| 438 | ) |
| 439 | }) |
| 440 | |
| 441 | it("should revert if the cart creation fails", async () => { |
| 442 | const region = await regionModuleService.createRegions({ |
| 443 | name: "US", |
| 444 | currency_code: "usd", |
| 445 | }) |
| 446 | |
| 447 | const salesChannel = await scModuleService.createSalesChannels({ |
| 448 | name: "Webshop", |
| 449 | }) |
| 450 | |
| 451 | const location = await stockLocationModule.createStockLocations({ |
| 452 | name: "Warehouse", |
| 453 | }) |
| 454 | |
| 455 | const [product] = await productModule.createProducts([ |
| 456 | { |
| 457 | title: "Test product", |
| 458 | variants: [ |
| 459 | { |
| 460 | title: "Test variant", |
| 461 | }, |
| 462 | ], |
| 463 | }, |
| 464 | ]) |
| 465 | |
| 466 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 467 | sku: "inv-1234", |
| 468 | requires_shipping: false, |
| 469 | }) |
| 470 | |
| 471 | await inventoryModule.createInventoryLevels([ |
| 472 | { |
| 473 | inventory_item_id: inventoryItem.id, |
| 474 | location_id: location.id, |
| 475 | stocked_quantity: 2, |
| 476 | reserved_quantity: 0, |
| 477 | }, |
| 478 | ]) |
| 479 | |
| 480 | const priceSet = await pricingModule.createPriceSets({ |
| 481 | prices: [ |
| 482 | { |
| 483 | amount: 3000, |
| 484 | currency_code: "usd", |
| 485 | }, |
| 486 | ], |
| 487 | }) |
| 488 | |
| 489 | await remoteLink.create([ |
| 490 | { |
| 491 | [Modules.PRODUCT]: { |
| 492 | variant_id: product.variants[0].id, |
| 493 | }, |
| 494 | [Modules.PRICING]: { |
| 495 | price_set_id: priceSet.id, |
| 496 | }, |
| 497 | }, |
| 498 | { |
| 499 | [Modules.SALES_CHANNEL]: { |
| 500 | sales_channel_id: salesChannel.id, |
| 501 | }, |
| 502 | [Modules.STOCK_LOCATION]: { |
| 503 | stock_location_id: location.id, |
| 504 | }, |
| 505 | }, |
| 506 | { |
| 507 | [Modules.PRODUCT]: { |
| 508 | variant_id: product.variants[0].id, |
| 509 | }, |
| 510 | [Modules.INVENTORY]: { |
| 511 | inventory_item_id: inventoryItem.id, |
| 512 | }, |
| 513 | }, |
| 514 | ]) |
| 515 | |
| 516 | const workflow = createCartWorkflow(appContainer) |
| 517 | |
| 518 | workflow.addAction( |
| 519 | "throw", |
| 520 | { |
| 521 | invoke: async function failStep() { |
| 522 | throw new Error(`Failed to create cart`) |
| 523 | }, |
| 524 | }, |
| 525 | { |
| 526 | noCompensation: true, |
| 527 | } |
| 528 | ) |
| 529 | |
| 530 | const { transaction } = await workflow.run({ |
| 531 | throwOnError: false, |
| 532 | input: { |
| 533 | email: "tony@stark.com", |
| 534 | currency_code: "usd", |
| 535 | region_id: region.id, |
| 536 | sales_channel_id: salesChannel.id, |
| 537 | items: [ |
| 538 | { |
| 539 | variant_id: product.variants[0].id, |
| 540 | quantity: 1, |
| 541 | }, |
| 542 | ], |
| 543 | }, |
| 544 | }) |
| 545 | |
| 546 | // TODO: the state must be "reverted" when runAsStep of sync flows can be reverted |
| 547 | expect(transaction.flow.state).toEqual("failed") |
| 548 | }) |
| 549 | |
| 550 | it("should throw when no regions exist", async () => { |
| 551 | await regionModuleService.deleteRegions(defaultRegion.id) |
| 552 | |
| 553 | const { errors } = await createCartWorkflow(appContainer).run({ |
| 554 | input: { |
| 555 | email: "tony@stark.com", |
| 556 | currency_code: "usd", |
| 557 | }, |
| 558 | throwOnError: false, |
| 559 | }) |
| 560 | |
| 561 | expect(errors).toEqual([ |
| 562 | expect.objectContaining({ |
| 563 | error: expect.objectContaining({ message: "No regions found" }), |
| 564 | }), |
| 565 | ]) |
| 566 | }) |
| 567 | |
| 568 | it("should throw if variants are out of stock", async () => { |
| 569 | const salesChannel = await scModuleService.createSalesChannels({ |
| 570 | name: "Webshop", |
| 571 | }) |
| 572 | |
| 573 | const location = await stockLocationModule.createStockLocations({ |
| 574 | name: "Warehouse", |
| 575 | }) |
| 576 | |
| 577 | const [product] = await productModule.createProducts([ |
| 578 | { |
| 579 | title: "Test product", |
| 580 | status: ProductStatus.PUBLISHED, |
| 581 | variants: [ |
| 582 | { |
| 583 | title: "Test variant", |
| 584 | }, |
| 585 | ], |
| 586 | }, |
| 587 | ]) |
| 588 | |
| 589 | const inventoryItem = ( |
| 590 | await api.post( |
| 591 | `/admin/inventory-items`, |
| 592 | { |
| 593 | sku: "inv-1234", |
| 594 | location_levels: [ |
| 595 | { |
| 596 | location_id: location.id, |
| 597 | stocked_quantity: 2, |
| 598 | }, |
| 599 | ], |
| 600 | }, |
| 601 | adminHeaders |
| 602 | ) |
| 603 | ).data.inventory_item |
| 604 | |
| 605 | await api.post( |
| 606 | `/admin/reservations`, |
| 607 | { |
| 608 | line_item_id: "line-item-id-1", |
| 609 | inventory_item_id: inventoryItem.id, |
| 610 | location_id: location.id, |
| 611 | description: "test description", |
| 612 | quantity: 2, |
| 613 | }, |
| 614 | adminHeaders |
| 615 | ) |
| 616 | |
| 617 | const region = await regionModuleService.createRegions({ |
| 618 | name: "US", |
| 619 | currency_code: "usd", |
| 620 | }) |
| 621 | |
| 622 | const priceSet = await pricingModule.createPriceSets({ |
| 623 | prices: [ |
| 624 | { |
| 625 | amount: 3000, |
| 626 | currency_code: "usd", |
| 627 | }, |
| 628 | ], |
| 629 | }) |
| 630 | |
| 631 | await remoteLink.create([ |
| 632 | { |
| 633 | [Modules.PRODUCT]: { |
| 634 | variant_id: product.variants[0].id, |
| 635 | }, |
| 636 | [Modules.PRICING]: { |
| 637 | price_set_id: priceSet.id, |
| 638 | }, |
| 639 | }, |
| 640 | { |
| 641 | [Modules.SALES_CHANNEL]: { |
| 642 | sales_channel_id: salesChannel.id, |
| 643 | }, |
| 644 | [Modules.STOCK_LOCATION]: { |
| 645 | stock_location_id: location.id, |
| 646 | }, |
| 647 | }, |
| 648 | { |
| 649 | [Modules.PRODUCT]: { |
| 650 | variant_id: product.variants[0].id, |
| 651 | }, |
| 652 | [Modules.INVENTORY]: { |
| 653 | inventory_item_id: inventoryItem.id, |
| 654 | }, |
| 655 | }, |
| 656 | ]) |
| 657 | |
| 658 | const { errors } = await createCartWorkflow(appContainer).run({ |
| 659 | input: { |
| 660 | region_id: region.id, |
| 661 | sales_channel_id: salesChannel.id, |
| 662 | items: [ |
| 663 | { |
| 664 | variant_id: product.variants[0].id, |
| 665 | quantity: 1, |
| 666 | }, |
| 667 | ], |
| 668 | }, |
| 669 | throwOnError: false, |
| 670 | }) |
| 671 | |
| 672 | expect(errors).toEqual([ |
| 673 | { |
| 674 | action: "confirm-item-inventory-as-step", |
| 675 | handlerType: "invoke", |
| 676 | error: expect.objectContaining({ |
| 677 | message: expect.stringContaining( |
| 678 | "Some variant does not have the required inventory" |
| 679 | ), |
| 680 | }), |
| 681 | }, |
| 682 | ]) |
| 683 | }) |
| 684 | |
| 685 | it("should pass if variants are out of stock but allow_backorder is true", async () => { |
| 686 | const salesChannel = await scModuleService.createSalesChannels({ |
| 687 | name: "Webshop", |
| 688 | }) |
| 689 | |
| 690 | const location = await stockLocationModule.createStockLocations({ |
| 691 | name: "Warehouse", |
| 692 | }) |
| 693 | |
| 694 | const [product] = await productModule.createProducts([ |
| 695 | { |
| 696 | title: "Test product", |
| 697 | status: ProductStatus.PUBLISHED, |
| 698 | variants: [ |
| 699 | { |
| 700 | title: "Test variant", |
| 701 | allow_backorder: true, |
| 702 | }, |
| 703 | ], |
| 704 | }, |
| 705 | ]) |
| 706 | |
| 707 | const inventoryItem = ( |
| 708 | await api.post( |
| 709 | `/admin/inventory-items`, |
| 710 | { |
| 711 | sku: "inv-1234", |
| 712 | location_levels: [ |
| 713 | { |
| 714 | location_id: location.id, |
| 715 | stocked_quantity: 2, |
| 716 | }, |
| 717 | ], |
| 718 | }, |
| 719 | adminHeaders |
| 720 | ) |
| 721 | ).data.inventory_item |
| 722 | |
| 723 | await api.post( |
| 724 | `/admin/reservations`, |
| 725 | { |
| 726 | line_item_id: "line-item-id-1", |
| 727 | inventory_item_id: inventoryItem.id, |
| 728 | location_id: location.id, |
| 729 | description: "test description", |
| 730 | quantity: 2, |
| 731 | }, |
| 732 | adminHeaders |
| 733 | ) |
| 734 | |
| 735 | const region = await regionModuleService.createRegions({ |
| 736 | name: "US", |
| 737 | currency_code: "usd", |
| 738 | }) |
| 739 | |
| 740 | const priceSet = await pricingModule.createPriceSets({ |
| 741 | prices: [ |
| 742 | { |
| 743 | amount: 3000, |
| 744 | currency_code: "usd", |
| 745 | }, |
| 746 | ], |
| 747 | }) |
| 748 | |
| 749 | await remoteLink.create([ |
| 750 | { |
| 751 | [Modules.PRODUCT]: { |
| 752 | variant_id: product.variants[0].id, |
| 753 | }, |
| 754 | [Modules.PRICING]: { |
| 755 | price_set_id: priceSet.id, |
| 756 | }, |
| 757 | }, |
| 758 | { |
| 759 | [Modules.SALES_CHANNEL]: { |
| 760 | sales_channel_id: salesChannel.id, |
| 761 | }, |
| 762 | [Modules.STOCK_LOCATION]: { |
| 763 | stock_location_id: location.id, |
| 764 | }, |
| 765 | }, |
| 766 | { |
| 767 | [Modules.PRODUCT]: { |
| 768 | variant_id: product.variants[0].id, |
| 769 | }, |
| 770 | [Modules.INVENTORY]: { |
| 771 | inventory_item_id: inventoryItem.id, |
| 772 | }, |
| 773 | }, |
| 774 | ]) |
| 775 | |
| 776 | const { errors, result } = await createCartWorkflow(appContainer).run( |
| 777 | { |
| 778 | input: { |
| 779 | region_id: region.id, |
| 780 | sales_channel_id: salesChannel.id, |
| 781 | items: [ |
| 782 | { |
| 783 | variant_id: product.variants[0].id, |
| 784 | quantity: 1, |
| 785 | }, |
| 786 | ], |
| 787 | }, |
| 788 | throwOnError: false, |
| 789 | } |
| 790 | ) |
| 791 | expect(errors).toEqual([]) |
| 792 | |
| 793 | const cart = await cartModuleService.retrieveCart(result.id, { |
| 794 | relations: ["items"], |
| 795 | }) |
| 796 | |
| 797 | expect(cart.items).toEqual([ |
| 798 | expect.objectContaining({ |
| 799 | quantity: 1, |
| 800 | variant_id: product.variants[0].id, |
| 801 | }), |
| 802 | ]) |
| 803 | }) |
| 804 | |
| 805 | it("should throw if sales channel is disabled", async () => { |
| 806 | const salesChannel = await scModuleService.createSalesChannels({ |
| 807 | name: "Webshop", |
| 808 | is_disabled: true, |
| 809 | }) |
| 810 | |
| 811 | const { errors } = await createCartWorkflow(appContainer).run({ |
| 812 | input: { |
| 813 | sales_channel_id: salesChannel.id, |
| 814 | }, |
| 815 | throwOnError: false, |
| 816 | }) |
| 817 | |
| 818 | expect(errors).toEqual([ |
| 819 | { |
| 820 | action: "find-sales-channel", |
| 821 | handlerType: "invoke", |
| 822 | error: expect.objectContaining({ |
| 823 | message: `Unable to assign cart to disabled Sales Channel: Webshop`, |
| 824 | }), |
| 825 | }, |
| 826 | ]) |
| 827 | }) |
| 828 | |
| 829 | describe("setPricingContext hook", () => { |
| 830 | it("should use context provided by the hook", async () => { |
| 831 | const region = await regionModuleService.createRegions({ |
| 832 | name: "US", |
| 833 | currency_code: "usd", |
| 834 | }) |
| 835 | |
| 836 | const salesChannel = await scModuleService.createSalesChannels({ |
| 837 | name: "Webshop", |
| 838 | }) |
| 839 | |
| 840 | const location = await stockLocationModule.createStockLocations({ |
| 841 | name: "Warehouse", |
| 842 | }) |
| 843 | |
| 844 | const [product] = await productModule.createProducts([ |
| 845 | { |
| 846 | title: "Test product", |
| 847 | status: ProductStatus.PUBLISHED, |
| 848 | variants: [ |
| 849 | { |
| 850 | title: "Test variant", |
| 851 | }, |
| 852 | ], |
| 853 | }, |
| 854 | ]) |
| 855 | |
| 856 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 857 | sku: "inv-1234", |
| 858 | }) |
| 859 | |
| 860 | await inventoryModule.createInventoryLevels([ |
| 861 | { |
| 862 | inventory_item_id: inventoryItem.id, |
| 863 | location_id: location.id, |
| 864 | stocked_quantity: 2, |
| 865 | reserved_quantity: 0, |
| 866 | }, |
| 867 | ]) |
| 868 | |
| 869 | const priceSet = await pricingModule.createPriceSets({ |
| 870 | prices: [ |
| 871 | { |
| 872 | amount: 3000, |
| 873 | currency_code: "usd", |
| 874 | }, |
| 875 | ], |
| 876 | }) |
| 877 | |
| 878 | await pricingModule.createPricePreferences({ |
| 879 | attribute: "currency_code", |
| 880 | value: "usd", |
| 881 | is_tax_inclusive: true, |
| 882 | }) |
| 883 | |
| 884 | await remoteLink.create([ |
| 885 | { |
| 886 | [Modules.PRODUCT]: { |
| 887 | variant_id: product.variants[0].id, |
| 888 | }, |
| 889 | [Modules.PRICING]: { |
| 890 | price_set_id: priceSet.id, |
| 891 | }, |
| 892 | }, |
| 893 | { |
| 894 | [Modules.SALES_CHANNEL]: { |
| 895 | sales_channel_id: salesChannel.id, |
| 896 | }, |
| 897 | [Modules.STOCK_LOCATION]: { |
| 898 | stock_location_id: location.id, |
| 899 | }, |
| 900 | }, |
| 901 | { |
| 902 | [Modules.PRODUCT]: { |
| 903 | variant_id: product.variants[0].id, |
| 904 | }, |
| 905 | [Modules.INVENTORY]: { |
| 906 | inventory_item_id: inventoryItem.id, |
| 907 | }, |
| 908 | }, |
| 909 | ]) |
| 910 | |
| 911 | setPricingContextHook = function (input) { |
| 912 | expect(input.region).toEqual( |
| 913 | expect.objectContaining({ |
| 914 | id: region.id, |
| 915 | }) |
| 916 | ) |
| 917 | expect(input.variantIds).toEqual([product.variants[0].id]) |
| 918 | expect(input.salesChannel).toEqual( |
| 919 | expect.objectContaining({ |
| 920 | id: salesChannel.id, |
| 921 | }) |
| 922 | ) |
| 923 | expect(input.customerData).toEqual( |
| 924 | expect.objectContaining({ |
| 925 | email: "tony@stark.com", |
| 926 | }) |
| 927 | ) |
| 928 | |
| 929 | return new StepResponse({ |
| 930 | unit_price: 100, |
| 931 | }) |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Tried jest, but for some reasons it is not able to provide |
| 936 | * correct arguments passed to the function |
| 937 | */ |
| 938 | let calculatePricesHasBeenCalled = false |
| 939 | |
| 940 | const originalFn = pricingModule.calculatePrices.bind(pricingModule) |
| 941 | pricingModule.calculatePrices = function (...args) { |
| 942 | calculatePricesHasBeenCalled = true |
| 943 | |
| 944 | const pricingContext = args[1]!.context |
| 945 | |
| 946 | expect(pricingContext).toEqual( |
| 947 | expect.objectContaining({ |
| 948 | unit_price: 100, |
| 949 | region_id: region.id, |
| 950 | currency_code: "usd", |
| 951 | }) |
| 952 | ) |
| 953 | |
| 954 | return originalFn.bind(pricingModule)(...args) |
| 955 | } |
| 956 | |
| 957 | const { result } = await createCartWorkflow(appContainer).run({ |
| 958 | input: { |
| 959 | email: "tony@stark.com", |
| 960 | currency_code: "usd", |
| 961 | region_id: region.id, |
| 962 | sales_channel_id: salesChannel.id, |
| 963 | items: [ |
| 964 | { |
| 965 | variant_id: product.variants[0].id, |
| 966 | quantity: 1, |
| 967 | }, |
| 968 | ], |
| 969 | }, |
| 970 | }) |
| 971 | |
| 972 | setPricingContextHook = undefined |
| 973 | pricingModule.calculatePrices = originalFn |
| 974 | |
| 975 | expect(calculatePricesHasBeenCalled).toBe(true) |
| 976 | |
| 977 | const cart = await cartModuleService.retrieveCart(result.id, { |
| 978 | relations: ["items"], |
| 979 | }) |
| 980 | |
| 981 | expect(normalizeBigNumbers(cart)).toEqual( |
| 982 | expect.objectContaining({ |
| 983 | currency_code: "usd", |
| 984 | email: "tony@stark.com", |
| 985 | region_id: region.id, |
| 986 | sales_channel_id: salesChannel.id, |
| 987 | customer_id: expect.any(String), |
| 988 | items: expect.arrayContaining([ |
| 989 | expect.objectContaining({ |
| 990 | quantity: 1, |
| 991 | unit_price: 3000, |
| 992 | is_tax_inclusive: true, |
| 993 | }), |
| 994 | ]), |
| 995 | }) |
| 996 | ) |
| 997 | }) |
| 998 | |
| 999 | it("should not be able to override the 'customer_id', 'region_id', and the 'currency_code' from the setPricingContext hook", async () => { |
| 1000 | const region = await regionModuleService.createRegions({ |
| 1001 | name: "US", |
| 1002 | currency_code: "usd", |
| 1003 | }) |
| 1004 | |
| 1005 | const salesChannel = await scModuleService.createSalesChannels({ |
| 1006 | name: "Webshop", |
| 1007 | }) |
| 1008 | |
| 1009 | const location = await stockLocationModule.createStockLocations({ |
| 1010 | name: "Warehouse", |
| 1011 | }) |
| 1012 | |
| 1013 | const [product] = await productModule.createProducts([ |
| 1014 | { |
| 1015 | title: "Test product", |
| 1016 | status: ProductStatus.PUBLISHED, |
| 1017 | variants: [ |
| 1018 | { |
| 1019 | title: "Test variant", |
| 1020 | }, |
| 1021 | ], |
| 1022 | }, |
| 1023 | ]) |
| 1024 | |
| 1025 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 1026 | sku: "inv-1234", |
| 1027 | }) |
| 1028 | |
| 1029 | await inventoryModule.createInventoryLevels([ |
| 1030 | { |
| 1031 | inventory_item_id: inventoryItem.id, |
| 1032 | location_id: location.id, |
| 1033 | stocked_quantity: 2, |
| 1034 | reserved_quantity: 0, |
| 1035 | }, |
| 1036 | ]) |
| 1037 | |
| 1038 | const priceSet = await pricingModule.createPriceSets({ |
| 1039 | prices: [ |
| 1040 | { |
| 1041 | amount: 3000, |
| 1042 | currency_code: "usd", |
| 1043 | }, |
| 1044 | ], |
| 1045 | }) |
| 1046 | |
| 1047 | await pricingModule.createPricePreferences({ |
| 1048 | attribute: "currency_code", |
| 1049 | value: "usd", |
| 1050 | is_tax_inclusive: true, |
| 1051 | }) |
| 1052 | |
| 1053 | await remoteLink.create([ |
| 1054 | { |
| 1055 | [Modules.PRODUCT]: { |
| 1056 | variant_id: product.variants[0].id, |
| 1057 | }, |
| 1058 | [Modules.PRICING]: { |
| 1059 | price_set_id: priceSet.id, |
| 1060 | }, |
| 1061 | }, |
| 1062 | { |
| 1063 | [Modules.SALES_CHANNEL]: { |
| 1064 | sales_channel_id: salesChannel.id, |
| 1065 | }, |
| 1066 | [Modules.STOCK_LOCATION]: { |
| 1067 | stock_location_id: location.id, |
| 1068 | }, |
| 1069 | }, |
| 1070 | { |
| 1071 | [Modules.PRODUCT]: { |
| 1072 | variant_id: product.variants[0].id, |
| 1073 | }, |
| 1074 | [Modules.INVENTORY]: { |
| 1075 | inventory_item_id: inventoryItem.id, |
| 1076 | }, |
| 1077 | }, |
| 1078 | ]) |
| 1079 | |
| 1080 | setPricingContextHook = function (input) { |
| 1081 | expect(input.region).toEqual( |
| 1082 | expect.objectContaining({ |
| 1083 | id: region.id, |
| 1084 | }) |
| 1085 | ) |
| 1086 | expect(input.variantIds).toEqual([product.variants[0].id]) |
| 1087 | expect(input.salesChannel).toEqual( |
| 1088 | expect.objectContaining({ |
| 1089 | id: salesChannel.id, |
| 1090 | }) |
| 1091 | ) |
| 1092 | expect(input.customerData).toEqual( |
| 1093 | expect.objectContaining({ |
| 1094 | email: "tony@stark.com", |
| 1095 | }) |
| 1096 | ) |
| 1097 | |
| 1098 | return new StepResponse({ |
| 1099 | unit_price: 200, |
| 1100 | currency_code: "inr", |
| 1101 | customer_id: "1", |
| 1102 | region_id: "1", |
| 1103 | }) |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Tried jest, but for some reasons it is not able to provide |
| 1108 | * correct arguments passed to the function |
| 1109 | */ |
| 1110 | let calculatePricesHasBeenCalled = false |
| 1111 | |
| 1112 | const originalFn = pricingModule.calculatePrices.bind(pricingModule) |
| 1113 | pricingModule.calculatePrices = function (...args) { |
| 1114 | calculatePricesHasBeenCalled = true |
| 1115 | |
| 1116 | const pricingContext = args[1]!.context |
| 1117 | |
| 1118 | expect(pricingContext).toEqual( |
| 1119 | expect.objectContaining({ |
| 1120 | unit_price: 200, |
| 1121 | region_id: region.id, |
| 1122 | currency_code: "usd", |
| 1123 | }) |
| 1124 | ) |
| 1125 | expect(pricingContext?.customer_id).toBeDefined() |
| 1126 | expect(pricingContext?.customer_id).not.toEqual("1") |
| 1127 | |
| 1128 | return originalFn.bind(pricingModule)(...args) |
| 1129 | } |
| 1130 | |
| 1131 | const { result } = await createCartWorkflow(appContainer).run({ |
| 1132 | input: { |
| 1133 | email: "tony@stark.com", |
| 1134 | currency_code: "usd", |
| 1135 | region_id: region.id, |
| 1136 | sales_channel_id: salesChannel.id, |
| 1137 | items: [ |
| 1138 | { |
| 1139 | variant_id: product.variants[0].id, |
| 1140 | quantity: 1, |
| 1141 | }, |
| 1142 | ], |
| 1143 | }, |
| 1144 | }) |
| 1145 | |
| 1146 | setPricingContextHook = undefined |
| 1147 | pricingModule.calculatePrices = originalFn |
| 1148 | |
| 1149 | expect(calculatePricesHasBeenCalled).toBe(true) |
| 1150 | |
| 1151 | const cart = await cartModuleService.retrieveCart(result.id, { |
| 1152 | relations: ["items"], |
| 1153 | }) |
| 1154 | |
| 1155 | expect(normalizeBigNumbers(cart)).toEqual( |
| 1156 | expect.objectContaining({ |
| 1157 | currency_code: "usd", |
| 1158 | email: "tony@stark.com", |
| 1159 | region_id: region.id, |
| 1160 | sales_channel_id: salesChannel.id, |
| 1161 | customer_id: expect.any(String), |
| 1162 | items: expect.arrayContaining([ |
| 1163 | expect.objectContaining({ |
| 1164 | quantity: 1, |
| 1165 | unit_price: 3000, |
| 1166 | is_tax_inclusive: true, |
| 1167 | }), |
| 1168 | ]), |
| 1169 | }) |
| 1170 | ) |
| 1171 | }) |
| 1172 | |
| 1173 | it("should disallow non object response from the setPricingContext hook", async () => { |
| 1174 | const region = await regionModuleService.createRegions({ |
| 1175 | name: "US", |
| 1176 | currency_code: "usd", |
| 1177 | }) |
| 1178 | |
| 1179 | const salesChannel = await scModuleService.createSalesChannels({ |
| 1180 | name: "Webshop", |
| 1181 | }) |
| 1182 | |
| 1183 | const location = await stockLocationModule.createStockLocations({ |
| 1184 | name: "Warehouse", |
| 1185 | }) |
| 1186 | |
| 1187 | const [product] = await productModule.createProducts([ |
| 1188 | { |
| 1189 | title: "Test product", |
| 1190 | variants: [ |
| 1191 | { |
| 1192 | title: "Test variant", |
| 1193 | }, |
| 1194 | ], |
| 1195 | }, |
| 1196 | ]) |
| 1197 | |
| 1198 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 1199 | sku: "inv-1234", |
| 1200 | }) |
| 1201 | |
| 1202 | await inventoryModule.createInventoryLevels([ |
| 1203 | { |
| 1204 | inventory_item_id: inventoryItem.id, |
| 1205 | location_id: location.id, |
| 1206 | stocked_quantity: 2, |
| 1207 | reserved_quantity: 0, |
| 1208 | }, |
| 1209 | ]) |
| 1210 | |
| 1211 | const priceSet = await pricingModule.createPriceSets({ |
| 1212 | prices: [ |
| 1213 | { |
| 1214 | amount: 3000, |
| 1215 | currency_code: "usd", |
| 1216 | }, |
| 1217 | ], |
| 1218 | }) |
| 1219 | |
| 1220 | await pricingModule.createPricePreferences({ |
| 1221 | attribute: "currency_code", |
| 1222 | value: "usd", |
| 1223 | is_tax_inclusive: true, |
| 1224 | }) |
| 1225 | |
| 1226 | await remoteLink.create([ |
| 1227 | { |
| 1228 | [Modules.PRODUCT]: { |
| 1229 | variant_id: product.variants[0].id, |
| 1230 | }, |
| 1231 | [Modules.PRICING]: { |
| 1232 | price_set_id: priceSet.id, |
| 1233 | }, |
| 1234 | }, |
| 1235 | { |
| 1236 | [Modules.SALES_CHANNEL]: { |
| 1237 | sales_channel_id: salesChannel.id, |
| 1238 | }, |
| 1239 | [Modules.STOCK_LOCATION]: { |
| 1240 | stock_location_id: location.id, |
| 1241 | }, |
| 1242 | }, |
| 1243 | { |
| 1244 | [Modules.PRODUCT]: { |
| 1245 | variant_id: product.variants[0].id, |
| 1246 | }, |
| 1247 | [Modules.INVENTORY]: { |
| 1248 | inventory_item_id: inventoryItem.id, |
| 1249 | }, |
| 1250 | }, |
| 1251 | ]) |
| 1252 | |
| 1253 | setPricingContextHook = function (input) { |
| 1254 | expect(input.region).toEqual( |
| 1255 | expect.objectContaining({ |
| 1256 | id: region.id, |
| 1257 | }) |
| 1258 | ) |
| 1259 | expect(input.variantIds).toEqual([product.variants[0].id]) |
| 1260 | expect(input.salesChannel).toEqual( |
| 1261 | expect.objectContaining({ |
| 1262 | id: salesChannel.id, |
| 1263 | }) |
| 1264 | ) |
| 1265 | expect(input.customerData).toEqual( |
| 1266 | expect.objectContaining({ |
| 1267 | email: "tony@stark.com", |
| 1268 | }) |
| 1269 | ) |
| 1270 | |
| 1271 | return new StepResponse([1]) |
| 1272 | } |
| 1273 | |
| 1274 | const { errors } = await createCartWorkflow(appContainer).run({ |
| 1275 | throwOnError: false, |
| 1276 | input: { |
| 1277 | email: "tony@stark.com", |
| 1278 | currency_code: "usd", |
| 1279 | region_id: region.id, |
| 1280 | sales_channel_id: salesChannel.id, |
| 1281 | items: [ |
| 1282 | { |
| 1283 | variant_id: product.variants[0].id, |
| 1284 | quantity: 1, |
| 1285 | }, |
| 1286 | ], |
| 1287 | }, |
| 1288 | }) |
| 1289 | |
| 1290 | setPricingContextHook = undefined |
| 1291 | |
| 1292 | expect(errors).toHaveLength(1) |
| 1293 | expect(errors[0]).toEqual( |
| 1294 | expect.objectContaining({ |
| 1295 | action: "get-setPricingContext-result", |
| 1296 | handlerType: "invoke", |
| 1297 | error: expect.objectContaining({ |
| 1298 | issues: [ |
| 1299 | { |
| 1300 | code: "invalid_type", |
| 1301 | expected: "record", |
| 1302 | message: "Invalid input: expected record, received array", |
| 1303 | path: [], |
| 1304 | }, |
| 1305 | ], |
| 1306 | }), |
| 1307 | }) |
| 1308 | ) |
| 1309 | }) |
| 1310 | }) |
| 1311 | |
| 1312 | describe("compensation", () => { |
| 1313 | it("should delete created customer if cart-creation fails", async () => { |
| 1314 | expect.assertions(2) |
| 1315 | const workflow = createCartWorkflow(appContainer) |
| 1316 | |
| 1317 | workflow.appendAction("throw", findOrCreateCustomerStepId, { |
| 1318 | invoke: async function failStep() { |
| 1319 | throw new Error(`Failed to create cart`) |
| 1320 | }, |
| 1321 | }) |
| 1322 | |
| 1323 | const { errors } = await workflow.run({ |
| 1324 | input: { |
| 1325 | email: "tony@stark-industries.com", |
| 1326 | }, |
| 1327 | throwOnError: false, |
| 1328 | }) |
| 1329 | |
| 1330 | expect(errors).toEqual([ |
| 1331 | { |
| 1332 | action: "throw", |
| 1333 | handlerType: "invoke", |
| 1334 | error: expect.objectContaining({ |
| 1335 | message: `Failed to create cart`, |
| 1336 | }), |
| 1337 | }, |
| 1338 | ]) |
| 1339 | |
| 1340 | const customers = await customerModule.listCustomers({ |
| 1341 | email: "tony@stark-industries.com", |
| 1342 | }) |
| 1343 | |
| 1344 | expect(customers).toHaveLength(0) |
| 1345 | }) |
| 1346 | |
| 1347 | it("should not delete existing customer if cart-creation fails", async () => { |
| 1348 | expect.assertions(2) |
| 1349 | const workflow = createCartWorkflow(appContainer) |
| 1350 | |
| 1351 | workflow.appendAction("throw", findOrCreateCustomerStepId, { |
| 1352 | invoke: async function failStep() { |
| 1353 | throw new Error(`Failed to create cart`) |
| 1354 | }, |
| 1355 | }) |
| 1356 | |
| 1357 | const customer = await customerModule.createCustomers({ |
| 1358 | email: "tony@stark-industries.com", |
| 1359 | }) |
| 1360 | |
| 1361 | const { errors } = await workflow.run({ |
| 1362 | input: { |
| 1363 | customer_id: customer.id, |
| 1364 | }, |
| 1365 | throwOnError: false, |
| 1366 | }) |
| 1367 | |
| 1368 | expect(errors).toEqual([ |
| 1369 | { |
| 1370 | action: "throw", |
| 1371 | handlerType: "invoke", |
| 1372 | error: expect.objectContaining({ |
| 1373 | message: `Failed to create cart`, |
| 1374 | }), |
| 1375 | }, |
| 1376 | ]) |
| 1377 | |
| 1378 | const customers = await customerModule.listCustomers({ |
| 1379 | email: "tony@stark-industries.com", |
| 1380 | }) |
| 1381 | |
| 1382 | expect(customers).toHaveLength(1) |
| 1383 | }) |
| 1384 | }) |
| 1385 | }) |
| 1386 | |
| 1387 | describe("UpdateCartWorkflow", () => { |
| 1388 | it("should remove item with custom price when region is updated", async () => { |
| 1389 | const hookCallback = jest.fn() |
| 1390 | addToCartWorkflow.hooks.validate((data) => { |
| 1391 | hookCallback(data) |
| 1392 | }) |
| 1393 | |
| 1394 | const salesChannel = await scModuleService.createSalesChannels({ |
| 1395 | name: "Webshop", |
| 1396 | }) |
| 1397 | |
| 1398 | const location = await stockLocationModule.createStockLocations({ |
| 1399 | name: "Warehouse", |
| 1400 | }) |
| 1401 | |
| 1402 | const regions = await regionModuleService.createRegions([ |
| 1403 | { |
| 1404 | name: "US", |
| 1405 | currency_code: "usd", |
| 1406 | }, |
| 1407 | { |
| 1408 | name: "EU", |
| 1409 | currency_code: "eur", |
| 1410 | }, |
| 1411 | ]) |
| 1412 | |
| 1413 | let cart = await cartModuleService.createCarts({ |
| 1414 | currency_code: "usd", |
| 1415 | sales_channel_id: salesChannel.id, |
| 1416 | region_id: regions.find((r) => r.currency_code === "usd")!.id, |
| 1417 | }) |
| 1418 | |
| 1419 | const [product] = await productModule.createProducts([ |
| 1420 | { |
| 1421 | title: "Test product", |
| 1422 | status: ProductStatus.PUBLISHED, |
| 1423 | variants: [ |
| 1424 | { |
| 1425 | title: "Test variant", |
| 1426 | manage_inventory: false, |
| 1427 | }, |
| 1428 | ], |
| 1429 | }, |
| 1430 | ]) |
| 1431 | |
| 1432 | const priceSet = await pricingModule.createPriceSets({ |
| 1433 | prices: [ |
| 1434 | { |
| 1435 | amount: 3000, |
| 1436 | currency_code: "usd", |
| 1437 | }, |
| 1438 | { |
| 1439 | amount: 2000, |
| 1440 | currency_code: "eur", |
| 1441 | }, |
| 1442 | ], |
| 1443 | }) |
| 1444 | |
| 1445 | await pricingModule.createPricePreferences([ |
| 1446 | { |
| 1447 | attribute: "currency_code", |
| 1448 | value: "usd", |
| 1449 | is_tax_inclusive: true, |
| 1450 | }, |
| 1451 | ]) |
| 1452 | |
| 1453 | await remoteLink.create([ |
| 1454 | { |
| 1455 | [Modules.PRODUCT]: { |
| 1456 | variant_id: product.variants[0].id, |
| 1457 | }, |
| 1458 | [Modules.PRICING]: { |
| 1459 | price_set_id: priceSet.id, |
| 1460 | }, |
| 1461 | }, |
| 1462 | { |
| 1463 | [Modules.SALES_CHANNEL]: { |
| 1464 | sales_channel_id: salesChannel.id, |
| 1465 | }, |
| 1466 | [Modules.STOCK_LOCATION]: { |
| 1467 | stock_location_id: location.id, |
| 1468 | }, |
| 1469 | }, |
| 1470 | ]) |
| 1471 | |
| 1472 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 1473 | select: ["id", "region_id", "currency_code", "sales_channel_id"], |
| 1474 | }) |
| 1475 | |
| 1476 | const wfInput = { |
| 1477 | items: [ |
| 1478 | { |
| 1479 | variant_id: product.variants[0].id, |
| 1480 | quantity: 1, |
| 1481 | }, |
| 1482 | { |
| 1483 | title: "Test item", |
| 1484 | subtitle: "Test subtitle", |
| 1485 | thumbnail: "some-url", |
| 1486 | requires_shipping: true, |
| 1487 | is_discountable: false, |
| 1488 | is_tax_inclusive: false, |
| 1489 | unit_price: 1500, |
| 1490 | metadata: { |
| 1491 | foo: "bar", |
| 1492 | }, |
| 1493 | quantity: 1, |
| 1494 | }, |
| 1495 | ], |
| 1496 | cart_id: cart.id, |
| 1497 | } |
| 1498 | await addToCartWorkflow(appContainer).run({ |
| 1499 | input: wfInput, |
| 1500 | }) |
| 1501 | |
| 1502 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 1503 | relations: ["items"], |
| 1504 | }) |
| 1505 | |
| 1506 | expect(hookCallback).toHaveBeenCalledWith({ |
| 1507 | cart: { |
| 1508 | completed_at: null, |
| 1509 | id: expect.stringContaining("cart_"), |
| 1510 | sales_channel_id: expect.stringContaining("sc_"), |
| 1511 | currency_code: "usd", |
| 1512 | locale: null, |
| 1513 | region_id: expect.stringContaining("reg_"), |
| 1514 | shipping_address: null, |
| 1515 | item_total: 0, |
| 1516 | total: 0, |
| 1517 | email: null, |
| 1518 | customer_id: null, |
| 1519 | }, |
| 1520 | input: wfInput, |
| 1521 | }) |
| 1522 | |
| 1523 | expect(normalizeBigNumbers(cart)).toEqual( |
| 1524 | expect.objectContaining({ |
| 1525 | id: cart.id, |
| 1526 | currency_code: "usd", |
| 1527 | items: expect.arrayContaining([ |
| 1528 | expect.objectContaining({ |
| 1529 | // Regular line item |
| 1530 | id: expect.any(String), |
| 1531 | is_discountable: true, |
| 1532 | is_tax_inclusive: true, |
| 1533 | is_custom_price: false, |
| 1534 | quantity: 1, |
| 1535 | requires_shipping: false, // product doesn't have a shipping profile nor inventory items that require shipping |
| 1536 | title: "Test product", |
| 1537 | subtitle: "Test variant", |
| 1538 | unit_price: 3000, |
| 1539 | updated_at: expect.any(Date), |
| 1540 | }), |
| 1541 | expect.objectContaining({ |
| 1542 | // Custom line item |
| 1543 | id: expect.any(String), |
| 1544 | is_discountable: false, |
| 1545 | is_giftcard: false, |
| 1546 | is_tax_inclusive: false, |
| 1547 | is_custom_price: true, |
| 1548 | quantity: 1, |
| 1549 | metadata: { |
| 1550 | foo: "bar", |
| 1551 | }, |
| 1552 | requires_shipping: true, // overriden when adding to cart |
| 1553 | subtitle: "Test subtitle", |
| 1554 | thumbnail: "some-url", |
| 1555 | title: "Test item", |
| 1556 | unit_price: 1500, |
| 1557 | updated_at: expect.any(Date), |
| 1558 | }), |
| 1559 | ]), |
| 1560 | }) |
| 1561 | ) |
| 1562 | |
| 1563 | await updateCartWorkflow(appContainer).run({ |
| 1564 | input: { |
| 1565 | id: cart.id, |
| 1566 | region_id: regions.find((r) => r.currency_code === "eur")!.id, |
| 1567 | }, |
| 1568 | }) |
| 1569 | |
| 1570 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 1571 | relations: ["items"], |
| 1572 | }) |
| 1573 | |
| 1574 | expect(normalizeBigNumbers(cart)).toEqual( |
| 1575 | expect.objectContaining({ |
| 1576 | id: cart.id, |
| 1577 | currency_code: "eur", |
| 1578 | items: expect.arrayContaining([ |
| 1579 | expect.objectContaining({ |
| 1580 | // Regular line item |
| 1581 | id: expect.any(String), |
| 1582 | is_discountable: true, |
| 1583 | is_tax_inclusive: false, |
| 1584 | is_custom_price: false, |
| 1585 | quantity: 1, |
| 1586 | requires_shipping: false, |
| 1587 | title: "Test product", |
| 1588 | subtitle: "Test variant", |
| 1589 | unit_price: 2000, |
| 1590 | updated_at: expect.any(Date), |
| 1591 | }), |
| 1592 | ]), |
| 1593 | }) |
| 1594 | ) |
| 1595 | expect(cart.items?.length).toEqual(1) |
| 1596 | }) |
| 1597 | |
| 1598 | it("should update cart shipping address fields", async () => { |
| 1599 | const salesChannel = await scModuleService.createSalesChannels({ |
| 1600 | name: "Webshop", |
| 1601 | }) |
| 1602 | |
| 1603 | const regions = await regionModuleService.createRegions([ |
| 1604 | { |
| 1605 | name: "US", |
| 1606 | currency_code: "usd", |
| 1607 | countries: ["us"], |
| 1608 | }, |
| 1609 | ]) |
| 1610 | |
| 1611 | let cart = await cartModuleService.createCarts({ |
| 1612 | currency_code: "usd", |
| 1613 | sales_channel_id: salesChannel.id, |
| 1614 | region_id: regions[0].id, |
| 1615 | shipping_address: { |
| 1616 | first_name: "John", |
| 1617 | last_name: "Doe", |
| 1618 | address_1: "123 Main St", |
| 1619 | city: "New York", |
| 1620 | country_code: "us", |
| 1621 | postal_code: "10001", |
| 1622 | }, |
| 1623 | }) |
| 1624 | |
| 1625 | const shippingAddressId = cart.shipping_address?.id |
| 1626 | |
| 1627 | await updateCartWorkflow(appContainer).run({ |
| 1628 | input: { |
| 1629 | id: cart.id, |
| 1630 | shipping_address: { |
| 1631 | id: shippingAddressId, |
| 1632 | first_name: "Jane", |
| 1633 | last_name: "Smith", |
| 1634 | address_1: "456 Oak Ave", |
| 1635 | city: "Los Angeles", |
| 1636 | country_code: "us", |
| 1637 | postal_code: "90001", |
| 1638 | }, |
| 1639 | }, |
| 1640 | }) |
| 1641 | |
| 1642 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 1643 | relations: ["shipping_address"], |
| 1644 | }) |
| 1645 | |
| 1646 | expect(cart.shipping_address).toEqual( |
| 1647 | expect.objectContaining({ |
| 1648 | id: shippingAddressId, |
| 1649 | first_name: "Jane", |
| 1650 | last_name: "Smith", |
| 1651 | address_1: "456 Oak Ave", |
| 1652 | city: "Los Angeles", |
| 1653 | country_code: "us", |
| 1654 | postal_code: "90001", |
| 1655 | }) |
| 1656 | ) |
| 1657 | }) |
| 1658 | |
| 1659 | it("should update cart billing address fields", async () => { |
| 1660 | const salesChannel = await scModuleService.createSalesChannels({ |
| 1661 | name: "Webshop", |
| 1662 | }) |
| 1663 | |
| 1664 | const regions = await regionModuleService.createRegions([ |
| 1665 | { |
| 1666 | name: "US", |
| 1667 | currency_code: "usd", |
| 1668 | countries: ["us"], |
| 1669 | }, |
| 1670 | ]) |
| 1671 | |
| 1672 | let cart = await cartModuleService.createCarts({ |
| 1673 | currency_code: "usd", |
| 1674 | sales_channel_id: salesChannel.id, |
| 1675 | region_id: regions[0].id, |
| 1676 | billing_address: { |
| 1677 | first_name: "John", |
| 1678 | last_name: "Doe", |
| 1679 | address_1: "123 Main St", |
| 1680 | city: "New York", |
| 1681 | country_code: "us", |
| 1682 | postal_code: "10001", |
| 1683 | }, |
| 1684 | }) |
| 1685 | |
| 1686 | const billingAddressId = cart.billing_address?.id |
| 1687 | |
| 1688 | await updateCartWorkflow(appContainer).run({ |
| 1689 | input: { |
| 1690 | id: cart.id, |
| 1691 | billing_address: { |
| 1692 | id: billingAddressId, |
| 1693 | first_name: "Jane", |
| 1694 | last_name: "Smith", |
| 1695 | address_1: "456 Oak Ave", |
| 1696 | city: "Los Angeles", |
| 1697 | country_code: "us", |
| 1698 | postal_code: "90001", |
| 1699 | }, |
| 1700 | }, |
| 1701 | }) |
| 1702 | |
| 1703 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 1704 | relations: ["billing_address"], |
| 1705 | }) |
| 1706 | |
| 1707 | expect(cart.billing_address).toEqual( |
| 1708 | expect.objectContaining({ |
| 1709 | id: billingAddressId, |
| 1710 | first_name: "Jane", |
| 1711 | last_name: "Smith", |
| 1712 | address_1: "456 Oak Ave", |
| 1713 | city: "Los Angeles", |
| 1714 | country_code: "us", |
| 1715 | postal_code: "90001", |
| 1716 | }) |
| 1717 | ) |
| 1718 | }) |
| 1719 | |
| 1720 | it("should update both shipping and billing addresses simultaneously", async () => { |
| 1721 | const salesChannel = await scModuleService.createSalesChannels({ |
| 1722 | name: "Webshop", |
| 1723 | }) |
| 1724 | |
| 1725 | const regions = await regionModuleService.createRegions([ |
| 1726 | { |
| 1727 | name: "US", |
| 1728 | currency_code: "usd", |
| 1729 | countries: ["us"], |
| 1730 | }, |
| 1731 | ]) |
| 1732 | |
| 1733 | let cart = await cartModuleService.createCarts({ |
| 1734 | currency_code: "usd", |
| 1735 | sales_channel_id: salesChannel.id, |
| 1736 | region_id: regions[0].id, |
| 1737 | shipping_address: { |
| 1738 | first_name: "John", |
| 1739 | last_name: "Doe", |
| 1740 | address_1: "123 Main St", |
| 1741 | city: "New York", |
| 1742 | country_code: "us", |
| 1743 | postal_code: "10001", |
| 1744 | }, |
| 1745 | billing_address: { |
| 1746 | first_name: "John", |
| 1747 | last_name: "Doe", |
| 1748 | address_1: "789 Business Blvd", |
| 1749 | city: "Chicago", |
| 1750 | country_code: "us", |
| 1751 | postal_code: "60601", |
| 1752 | }, |
| 1753 | }) |
| 1754 | |
| 1755 | const shippingAddressId = cart.shipping_address?.id |
| 1756 | const billingAddressId = cart.billing_address?.id |
| 1757 | |
| 1758 | await updateCartWorkflow(appContainer).run({ |
| 1759 | input: { |
| 1760 | id: cart.id, |
| 1761 | shipping_address: { |
| 1762 | id: shippingAddressId, |
| 1763 | first_name: "Jane", |
| 1764 | last_name: "Smith", |
| 1765 | address_1: "456 Oak Ave", |
| 1766 | city: "Los Angeles", |
| 1767 | country_code: "us", |
| 1768 | postal_code: "90001", |
| 1769 | }, |
| 1770 | billing_address: { |
| 1771 | id: billingAddressId, |
| 1772 | first_name: "Jane", |
| 1773 | last_name: "Smith", |
| 1774 | address_1: "321 Corporate Dr", |
| 1775 | city: "San Francisco", |
| 1776 | country_code: "us", |
| 1777 | postal_code: "94102", |
| 1778 | }, |
| 1779 | }, |
| 1780 | }) |
| 1781 | |
| 1782 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 1783 | relations: ["shipping_address", "billing_address"], |
| 1784 | }) |
| 1785 | |
| 1786 | expect(cart.shipping_address).toEqual( |
| 1787 | expect.objectContaining({ |
| 1788 | id: shippingAddressId, |
| 1789 | first_name: "Jane", |
| 1790 | last_name: "Smith", |
| 1791 | address_1: "456 Oak Ave", |
| 1792 | city: "Los Angeles", |
| 1793 | postal_code: "90001", |
| 1794 | }) |
| 1795 | ) |
| 1796 | |
| 1797 | expect(cart.billing_address).toEqual( |
| 1798 | expect.objectContaining({ |
| 1799 | id: billingAddressId, |
| 1800 | first_name: "Jane", |
| 1801 | last_name: "Smith", |
| 1802 | address_1: "321 Corporate Dr", |
| 1803 | city: "San Francisco", |
| 1804 | postal_code: "94102", |
| 1805 | }) |
| 1806 | ) |
| 1807 | }) |
| 1808 | |
| 1809 | it("should rollback address updates on workflow failure", async () => { |
| 1810 | const salesChannel = await scModuleService.createSalesChannels({ |
| 1811 | name: "Webshop", |
| 1812 | }) |
| 1813 | |
| 1814 | const regions = await regionModuleService.createRegions([ |
| 1815 | { |
| 1816 | name: "US", |
| 1817 | currency_code: "usd", |
| 1818 | countries: ["us"], |
| 1819 | }, |
| 1820 | ]) |
| 1821 | |
| 1822 | let cart = await cartModuleService.createCarts({ |
| 1823 | currency_code: "usd", |
| 1824 | sales_channel_id: salesChannel.id, |
| 1825 | region_id: regions[0].id, |
| 1826 | shipping_address: { |
| 1827 | first_name: "John", |
| 1828 | last_name: "Doe", |
| 1829 | address_1: "123 Main St", |
| 1830 | city: "New York", |
| 1831 | country_code: "us", |
| 1832 | postal_code: "10001", |
| 1833 | }, |
| 1834 | }) |
| 1835 | |
| 1836 | const originalShippingAddress = { ...cart.shipping_address } |
| 1837 | const shippingAddressId = cart.shipping_address?.id |
| 1838 | |
| 1839 | const workflow = updateCartWorkflow(appContainer) |
| 1840 | |
| 1841 | workflow.appendAction("throw", "update-carts", { |
| 1842 | invoke: async function failStep() { |
| 1843 | throw new Error("Simulated failure") |
| 1844 | }, |
| 1845 | }) |
| 1846 | |
| 1847 | const { errors } = await workflow.run({ |
| 1848 | input: { |
| 1849 | id: cart.id, |
| 1850 | shipping_address: { |
| 1851 | id: shippingAddressId, |
| 1852 | first_name: "Jane", |
| 1853 | last_name: "Smith", |
| 1854 | address_1: "456 Oak Ave", |
| 1855 | city: "Los Angeles", |
| 1856 | country_code: "us", |
| 1857 | postal_code: "90001", |
| 1858 | }, |
| 1859 | }, |
| 1860 | throwOnError: false, |
| 1861 | }) |
| 1862 | |
| 1863 | expect(errors).toBeDefined() |
| 1864 | expect(errors?.length).toBeGreaterThan(0) |
| 1865 | |
| 1866 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 1867 | relations: ["shipping_address"], |
| 1868 | }) |
| 1869 | |
| 1870 | expect(cart.shipping_address).toEqual( |
| 1871 | expect.objectContaining({ |
| 1872 | id: shippingAddressId, |
| 1873 | first_name: originalShippingAddress.first_name, |
| 1874 | last_name: originalShippingAddress.last_name, |
| 1875 | address_1: originalShippingAddress.address_1, |
| 1876 | city: originalShippingAddress.city, |
| 1877 | postal_code: originalShippingAddress.postal_code, |
| 1878 | }) |
| 1879 | ) |
| 1880 | }) |
| 1881 | }) |
| 1882 | |
| 1883 | describe("AddToCartWorkflow", () => { |
| 1884 | it("should add item to cart", async () => { |
| 1885 | const salesChannel = await scModuleService.createSalesChannels({ |
| 1886 | name: "Webshop", |
| 1887 | }) |
| 1888 | |
| 1889 | const location = await stockLocationModule.createStockLocations({ |
| 1890 | name: "Warehouse", |
| 1891 | }) |
| 1892 | |
| 1893 | let cart = await cartModuleService.createCarts({ |
| 1894 | currency_code: "usd", |
| 1895 | sales_channel_id: salesChannel.id, |
| 1896 | }) |
| 1897 | |
| 1898 | const [product] = await productModule.createProducts([ |
| 1899 | { |
| 1900 | title: "Test product", |
| 1901 | status: ProductStatus.PUBLISHED, |
| 1902 | variants: [ |
| 1903 | { |
| 1904 | title: "Test variant", |
| 1905 | }, |
| 1906 | ], |
| 1907 | }, |
| 1908 | ]) |
| 1909 | |
| 1910 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 1911 | sku: "inv-1234", |
| 1912 | }) |
| 1913 | |
| 1914 | await inventoryModule.createInventoryLevels([ |
| 1915 | { |
| 1916 | inventory_item_id: inventoryItem.id, |
| 1917 | location_id: location.id, |
| 1918 | stocked_quantity: 2, |
| 1919 | reserved_quantity: 0, |
| 1920 | }, |
| 1921 | ]) |
| 1922 | |
| 1923 | const priceSet = await pricingModule.createPriceSets({ |
| 1924 | prices: [ |
| 1925 | { |
| 1926 | amount: 3000, |
| 1927 | currency_code: "usd", |
| 1928 | }, |
| 1929 | ], |
| 1930 | }) |
| 1931 | |
| 1932 | await pricingModule.createPricePreferences({ |
| 1933 | attribute: "currency_code", |
| 1934 | value: "usd", |
| 1935 | is_tax_inclusive: true, |
| 1936 | }) |
| 1937 | |
| 1938 | await remoteLink.create([ |
| 1939 | { |
| 1940 | [Modules.PRODUCT]: { |
| 1941 | variant_id: product.variants[0].id, |
| 1942 | }, |
| 1943 | [Modules.PRICING]: { |
| 1944 | price_set_id: priceSet.id, |
| 1945 | }, |
| 1946 | }, |
| 1947 | { |
| 1948 | [Modules.SALES_CHANNEL]: { |
| 1949 | sales_channel_id: salesChannel.id, |
| 1950 | }, |
| 1951 | [Modules.STOCK_LOCATION]: { |
| 1952 | stock_location_id: location.id, |
| 1953 | }, |
| 1954 | }, |
| 1955 | { |
| 1956 | [Modules.PRODUCT]: { |
| 1957 | variant_id: product.variants[0].id, |
| 1958 | }, |
| 1959 | [Modules.INVENTORY]: { |
| 1960 | inventory_item_id: inventoryItem.id, |
| 1961 | }, |
| 1962 | }, |
| 1963 | ]) |
| 1964 | |
| 1965 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 1966 | select: ["id", "region_id", "currency_code", "sales_channel_id"], |
| 1967 | }) |
| 1968 | |
| 1969 | await addToCartWorkflow(appContainer).run({ |
| 1970 | input: { |
| 1971 | items: [ |
| 1972 | { |
| 1973 | variant_id: product.variants[0].id, |
| 1974 | quantity: 1, |
| 1975 | }, |
| 1976 | ], |
| 1977 | cart_id: cart.id, |
| 1978 | }, |
| 1979 | }) |
| 1980 | |
| 1981 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 1982 | relations: ["items"], |
| 1983 | }) |
| 1984 | |
| 1985 | expect(normalizeBigNumbers(cart)).toEqual( |
| 1986 | expect.objectContaining({ |
| 1987 | id: cart.id, |
| 1988 | currency_code: "usd", |
| 1989 | items: expect.arrayContaining([ |
| 1990 | expect.objectContaining({ |
| 1991 | unit_price: 3000, |
| 1992 | is_tax_inclusive: true, |
| 1993 | quantity: 1, |
| 1994 | title: "Test product", |
| 1995 | subtitle: "Test variant", |
| 1996 | }), |
| 1997 | ]), |
| 1998 | }) |
| 1999 | ) |
| 2000 | }) |
| 2001 | |
| 2002 | it("should add one item with variant thumbnail and one item with product thumbnail", async () => { |
| 2003 | const salesChannel = await scModuleService.createSalesChannels({ |
| 2004 | name: "Webshop", |
| 2005 | }) |
| 2006 | |
| 2007 | const location = await stockLocationModule.createStockLocations({ |
| 2008 | name: "Warehouse", |
| 2009 | }) |
| 2010 | |
| 2011 | let cart = await cartModuleService.createCarts({ |
| 2012 | currency_code: "usd", |
| 2013 | sales_channel_id: salesChannel.id, |
| 2014 | }) |
| 2015 | |
| 2016 | await remoteLink.create([ |
| 2017 | { |
| 2018 | [Modules.SALES_CHANNEL]: { |
| 2019 | sales_channel_id: salesChannel.id, |
| 2020 | }, |
| 2021 | [Modules.STOCK_LOCATION]: { |
| 2022 | stock_location_id: location.id, |
| 2023 | }, |
| 2024 | }, |
| 2025 | ]) |
| 2026 | |
| 2027 | const [product1, product2] = await productModule.createProducts([ |
| 2028 | { |
| 2029 | title: "Test product 1", |
| 2030 | status: ProductStatus.PUBLISHED, |
| 2031 | thumbnail: "product-thumbnail-1", |
| 2032 | variants: [ |
| 2033 | { |
| 2034 | title: "Test variant 1", |
| 2035 | manage_inventory: false, |
| 2036 | }, |
| 2037 | ], |
| 2038 | }, |
| 2039 | { |
| 2040 | title: "Test product 2", |
| 2041 | status: ProductStatus.PUBLISHED, |
| 2042 | thumbnail: "product-thumbnail-2", |
| 2043 | variants: [ |
| 2044 | { |
| 2045 | title: "Test variant 2", |
| 2046 | manage_inventory: false, |
| 2047 | thumbnail: "variant-thumbnail-2", |
| 2048 | }, |
| 2049 | ], |
| 2050 | }, |
| 2051 | ]) |
| 2052 | |
| 2053 | const priceSet1 = await pricingModule.createPriceSets({ |
| 2054 | prices: [ |
| 2055 | { |
| 2056 | amount: 30, |
| 2057 | currency_code: "usd", |
| 2058 | }, |
| 2059 | ], |
| 2060 | }) |
| 2061 | |
| 2062 | const priceSet2 = await pricingModule.createPriceSets({ |
| 2063 | prices: [ |
| 2064 | { |
| 2065 | amount: 30, |
| 2066 | currency_code: "usd", |
| 2067 | }, |
| 2068 | ], |
| 2069 | }) |
| 2070 | |
| 2071 | await pricingModule.createPricePreferences({ |
| 2072 | attribute: "currency_code", |
| 2073 | value: "usd", |
| 2074 | is_tax_inclusive: true, |
| 2075 | }) |
| 2076 | |
| 2077 | await remoteLink.create([ |
| 2078 | { |
| 2079 | [Modules.PRODUCT]: { |
| 2080 | variant_id: product1.variants[0].id, |
| 2081 | }, |
| 2082 | [Modules.PRICING]: { |
| 2083 | price_set_id: priceSet1.id, |
| 2084 | }, |
| 2085 | }, |
| 2086 | ]) |
| 2087 | |
| 2088 | await remoteLink.create([ |
| 2089 | { |
| 2090 | [Modules.PRODUCT]: { |
| 2091 | variant_id: product2.variants[0].id, |
| 2092 | }, |
| 2093 | [Modules.PRICING]: { |
| 2094 | price_set_id: priceSet2.id, |
| 2095 | }, |
| 2096 | }, |
| 2097 | ]) |
| 2098 | |
| 2099 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2100 | select: ["id", "region_id", "currency_code", "sales_channel_id"], |
| 2101 | }) |
| 2102 | |
| 2103 | await addToCartWorkflow(appContainer).run({ |
| 2104 | input: { |
| 2105 | items: [ |
| 2106 | { |
| 2107 | variant_id: product1.variants[0].id, |
| 2108 | quantity: 1, |
| 2109 | }, |
| 2110 | { |
| 2111 | variant_id: product2.variants[0].id, |
| 2112 | quantity: 1, |
| 2113 | }, |
| 2114 | ], |
| 2115 | cart_id: cart.id, |
| 2116 | }, |
| 2117 | }) |
| 2118 | |
| 2119 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2120 | relations: ["items"], |
| 2121 | }) |
| 2122 | |
| 2123 | expect(cart.items).toHaveLength(2) |
| 2124 | expect(cart.items).toEqual( |
| 2125 | expect.arrayContaining([ |
| 2126 | expect.objectContaining({ |
| 2127 | variant_id: product1.variants.find( |
| 2128 | (v) => v.title === "Test variant 1" |
| 2129 | )!.id, |
| 2130 | thumbnail: "product-thumbnail-1", |
| 2131 | }), |
| 2132 | expect.objectContaining({ |
| 2133 | variant_id: product2.variants.find( |
| 2134 | (v) => v.title === "Test variant 2" |
| 2135 | )!.id, |
| 2136 | thumbnail: "variant-thumbnail-2", |
| 2137 | }), |
| 2138 | ]) |
| 2139 | ) |
| 2140 | }) |
| 2141 | |
| 2142 | it("should add custom item to cart", async () => { |
| 2143 | const salesChannel = await scModuleService.createSalesChannels({ |
| 2144 | name: "Webshop", |
| 2145 | }) |
| 2146 | |
| 2147 | const location = await stockLocationModule.createStockLocations({ |
| 2148 | name: "Warehouse", |
| 2149 | }) |
| 2150 | |
| 2151 | let cart = await cartModuleService.createCarts({ |
| 2152 | currency_code: "usd", |
| 2153 | sales_channel_id: salesChannel.id, |
| 2154 | }) |
| 2155 | |
| 2156 | await remoteLink.create([ |
| 2157 | { |
| 2158 | [Modules.SALES_CHANNEL]: { |
| 2159 | sales_channel_id: salesChannel.id, |
| 2160 | }, |
| 2161 | [Modules.STOCK_LOCATION]: { |
| 2162 | stock_location_id: location.id, |
| 2163 | }, |
| 2164 | }, |
| 2165 | ]) |
| 2166 | |
| 2167 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2168 | select: ["id", "region_id", "currency_code", "sales_channel_id"], |
| 2169 | }) |
| 2170 | |
| 2171 | await addToCartWorkflow(appContainer).run({ |
| 2172 | input: { |
| 2173 | items: [ |
| 2174 | { |
| 2175 | title: "Test item", |
| 2176 | subtitle: "Test subtitle", |
| 2177 | thumbnail: "some-url", |
| 2178 | requires_shipping: true, |
| 2179 | is_discountable: false, |
| 2180 | is_tax_inclusive: false, |
| 2181 | unit_price: 3000, |
| 2182 | metadata: { |
| 2183 | foo: "bar", |
| 2184 | }, |
| 2185 | quantity: 1, |
| 2186 | }, |
| 2187 | ], |
| 2188 | cart_id: cart.id, |
| 2189 | }, |
| 2190 | }) |
| 2191 | |
| 2192 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2193 | relations: ["items"], |
| 2194 | }) |
| 2195 | |
| 2196 | expect(normalizeBigNumbers(cart)).toEqual( |
| 2197 | expect.objectContaining({ |
| 2198 | id: cart.id, |
| 2199 | currency_code: "usd", |
| 2200 | items: [ |
| 2201 | expect.objectContaining({ |
| 2202 | cart_id: expect.any(String), |
| 2203 | compare_at_unit_price: null, |
| 2204 | created_at: expect.any(Date), |
| 2205 | deleted_at: null, |
| 2206 | id: expect.any(String), |
| 2207 | is_discountable: false, |
| 2208 | is_giftcard: false, |
| 2209 | is_tax_inclusive: false, |
| 2210 | is_custom_price: true, |
| 2211 | metadata: { |
| 2212 | foo: "bar", |
| 2213 | }, |
| 2214 | product_collection: null, |
| 2215 | product_description: null, |
| 2216 | product_handle: null, |
| 2217 | product_id: null, |
| 2218 | product_subtitle: null, |
| 2219 | product_title: null, |
| 2220 | product_type: null, |
| 2221 | product_type_id: null, |
| 2222 | quantity: 1, |
| 2223 | raw_compare_at_unit_price: null, |
| 2224 | raw_unit_price: { |
| 2225 | precision: 20, |
| 2226 | value: "3000", |
| 2227 | }, |
| 2228 | requires_shipping: true, |
| 2229 | subtitle: "Test subtitle", |
| 2230 | thumbnail: "some-url", |
| 2231 | title: "Test item", |
| 2232 | unit_price: 3000, |
| 2233 | updated_at: expect.any(Date), |
| 2234 | variant_barcode: null, |
| 2235 | variant_id: null, |
| 2236 | variant_option_values: null, |
| 2237 | variant_sku: null, |
| 2238 | variant_title: null, |
| 2239 | }), |
| 2240 | ], |
| 2241 | }) |
| 2242 | ) |
| 2243 | }) |
| 2244 | |
| 2245 | it("should add item to cart with price list", async () => { |
| 2246 | const salesChannel = await scModuleService.createSalesChannels({ |
| 2247 | name: "Webshop", |
| 2248 | }) |
| 2249 | |
| 2250 | const customer = await customerModule.createCustomers({ |
| 2251 | first_name: "Test", |
| 2252 | last_name: "Test", |
| 2253 | }) |
| 2254 | |
| 2255 | const customer_group = await customerModule.createCustomerGroups({ |
| 2256 | name: "Test Group", |
| 2257 | }) |
| 2258 | |
| 2259 | await customerModule.addCustomerToGroup({ |
| 2260 | customer_id: customer.id, |
| 2261 | customer_group_id: customer_group.id, |
| 2262 | }) |
| 2263 | |
| 2264 | const location = await stockLocationModule.createStockLocations({ |
| 2265 | name: "Warehouse", |
| 2266 | }) |
| 2267 | |
| 2268 | let cart = await cartModuleService.createCarts({ |
| 2269 | currency_code: "usd", |
| 2270 | sales_channel_id: salesChannel.id, |
| 2271 | customer_id: customer.id, |
| 2272 | }) |
| 2273 | |
| 2274 | const [product] = await productModule.createProducts([ |
| 2275 | { |
| 2276 | title: "Test product", |
| 2277 | status: ProductStatus.PUBLISHED, |
| 2278 | variants: [ |
| 2279 | { |
| 2280 | title: "Test variant", |
| 2281 | }, |
| 2282 | ], |
| 2283 | }, |
| 2284 | ]) |
| 2285 | |
| 2286 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 2287 | sku: "inv-1234", |
| 2288 | }) |
| 2289 | |
| 2290 | await inventoryModule.createInventoryLevels([ |
| 2291 | { |
| 2292 | inventory_item_id: inventoryItem.id, |
| 2293 | location_id: location.id, |
| 2294 | stocked_quantity: 2, |
| 2295 | }, |
| 2296 | ]) |
| 2297 | |
| 2298 | const priceSet = await pricingModule.createPriceSets({ |
| 2299 | prices: [ |
| 2300 | { |
| 2301 | amount: 3000, |
| 2302 | currency_code: "usd", |
| 2303 | }, |
| 2304 | ], |
| 2305 | }) |
| 2306 | |
| 2307 | await pricingModule.createPricePreferences({ |
| 2308 | attribute: "currency_code", |
| 2309 | value: "usd", |
| 2310 | is_tax_inclusive: true, |
| 2311 | }) |
| 2312 | |
| 2313 | await pricingModule.createPriceLists([ |
| 2314 | { |
| 2315 | title: "test price list", |
| 2316 | description: "test", |
| 2317 | status: PriceListStatus.ACTIVE, |
| 2318 | type: PriceListType.OVERRIDE, |
| 2319 | prices: [ |
| 2320 | { |
| 2321 | amount: 1500, |
| 2322 | currency_code: "usd", |
| 2323 | price_set_id: priceSet.id, |
| 2324 | }, |
| 2325 | ], |
| 2326 | rules: { |
| 2327 | "customer.groups.id": [customer_group.id], |
| 2328 | }, |
| 2329 | }, |
| 2330 | ]) |
| 2331 | |
| 2332 | await remoteLink.create([ |
| 2333 | { |
| 2334 | [Modules.PRODUCT]: { |
| 2335 | variant_id: product.variants[0].id, |
| 2336 | }, |
| 2337 | [Modules.PRICING]: { |
| 2338 | price_set_id: priceSet.id, |
| 2339 | }, |
| 2340 | }, |
| 2341 | { |
| 2342 | [Modules.SALES_CHANNEL]: { |
| 2343 | sales_channel_id: salesChannel.id, |
| 2344 | }, |
| 2345 | [Modules.STOCK_LOCATION]: { |
| 2346 | stock_location_id: location.id, |
| 2347 | }, |
| 2348 | }, |
| 2349 | { |
| 2350 | [Modules.PRODUCT]: { |
| 2351 | variant_id: product.variants[0].id, |
| 2352 | }, |
| 2353 | [Modules.INVENTORY]: { |
| 2354 | inventory_item_id: inventoryItem.id, |
| 2355 | }, |
| 2356 | }, |
| 2357 | ]) |
| 2358 | |
| 2359 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2360 | select: ["id", "region_id", "currency_code", "sales_channel_id"], |
| 2361 | }) |
| 2362 | |
| 2363 | await addToCartWorkflow(appContainer).run({ |
| 2364 | input: { |
| 2365 | items: [ |
| 2366 | { |
| 2367 | variant_id: product.variants[0].id, |
| 2368 | quantity: 1, |
| 2369 | }, |
| 2370 | ], |
| 2371 | cart_id: cart.id, |
| 2372 | }, |
| 2373 | }) |
| 2374 | |
| 2375 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2376 | relations: ["items"], |
| 2377 | }) |
| 2378 | |
| 2379 | expect(normalizeBigNumbers(cart)).toEqual( |
| 2380 | expect.objectContaining({ |
| 2381 | id: cart.id, |
| 2382 | currency_code: "usd", |
| 2383 | items: expect.arrayContaining([ |
| 2384 | expect.objectContaining({ |
| 2385 | unit_price: 1500, |
| 2386 | is_tax_inclusive: true, |
| 2387 | quantity: 1, |
| 2388 | title: "Test product", |
| 2389 | subtitle: "Test variant", |
| 2390 | }), |
| 2391 | ]), |
| 2392 | }) |
| 2393 | ) |
| 2394 | }) |
| 2395 | |
| 2396 | it("should throw if product is not published", async () => { |
| 2397 | const salesChannel = await scModuleService.createSalesChannels({ |
| 2398 | name: "Webshop", |
| 2399 | }) |
| 2400 | |
| 2401 | const location = await stockLocationModule.createStockLocations({ |
| 2402 | name: "Warehouse", |
| 2403 | }) |
| 2404 | |
| 2405 | let cart = await cartModuleService.createCarts({ |
| 2406 | currency_code: "usd", |
| 2407 | sales_channel_id: salesChannel.id, |
| 2408 | }) |
| 2409 | |
| 2410 | const [product] = await productModule.createProducts([ |
| 2411 | { |
| 2412 | title: "Test product", |
| 2413 | status: ProductStatus.DRAFT, |
| 2414 | variants: [ |
| 2415 | { |
| 2416 | title: "Test variant", |
| 2417 | }, |
| 2418 | ], |
| 2419 | }, |
| 2420 | ]) |
| 2421 | |
| 2422 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 2423 | sku: "inv-1234", |
| 2424 | }) |
| 2425 | |
| 2426 | await inventoryModule.createInventoryLevels([ |
| 2427 | { |
| 2428 | inventory_item_id: inventoryItem.id, |
| 2429 | location_id: location.id, |
| 2430 | stocked_quantity: 2, |
| 2431 | reserved_quantity: 0, |
| 2432 | }, |
| 2433 | ]) |
| 2434 | |
| 2435 | const priceSet = await pricingModule.createPriceSets({ |
| 2436 | prices: [ |
| 2437 | { |
| 2438 | amount: 3000, |
| 2439 | currency_code: "usd", |
| 2440 | }, |
| 2441 | ], |
| 2442 | }) |
| 2443 | |
| 2444 | await pricingModule.createPricePreferences({ |
| 2445 | attribute: "currency_code", |
| 2446 | value: "usd", |
| 2447 | is_tax_inclusive: true, |
| 2448 | }) |
| 2449 | |
| 2450 | await remoteLink.create([ |
| 2451 | { |
| 2452 | [Modules.PRODUCT]: { |
| 2453 | variant_id: product.variants[0].id, |
| 2454 | }, |
| 2455 | [Modules.PRICING]: { |
| 2456 | price_set_id: priceSet.id, |
| 2457 | }, |
| 2458 | }, |
| 2459 | { |
| 2460 | [Modules.SALES_CHANNEL]: { |
| 2461 | sales_channel_id: salesChannel.id, |
| 2462 | }, |
| 2463 | [Modules.STOCK_LOCATION]: { |
| 2464 | stock_location_id: location.id, |
| 2465 | }, |
| 2466 | }, |
| 2467 | { |
| 2468 | [Modules.PRODUCT]: { |
| 2469 | variant_id: product.variants[0].id, |
| 2470 | }, |
| 2471 | [Modules.INVENTORY]: { |
| 2472 | inventory_item_id: inventoryItem.id, |
| 2473 | }, |
| 2474 | }, |
| 2475 | ]) |
| 2476 | |
| 2477 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2478 | select: ["id", "region_id", "currency_code", "sales_channel_id"], |
| 2479 | }) |
| 2480 | |
| 2481 | const { errors } = await addToCartWorkflow(appContainer).run({ |
| 2482 | input: { |
| 2483 | items: [ |
| 2484 | { |
| 2485 | variant_id: product.variants[0].id, |
| 2486 | quantity: 1, |
| 2487 | }, |
| 2488 | ], |
| 2489 | cart_id: cart.id, |
| 2490 | }, |
| 2491 | throwOnError: false, |
| 2492 | }) |
| 2493 | |
| 2494 | expect(errors).toEqual([ |
| 2495 | { |
| 2496 | action: "get-variant-items-with-prices-workflow-as-step", |
| 2497 | handlerType: "invoke", |
| 2498 | error: expect.objectContaining({ |
| 2499 | message: expect.stringContaining( |
| 2500 | `Variants ${product.variants[0].id} do not exist or belong to a product that is not published` |
| 2501 | ), |
| 2502 | }), |
| 2503 | }, |
| 2504 | ]) |
| 2505 | }) |
| 2506 | |
| 2507 | it("should throw if variant doesn't exist", async () => { |
| 2508 | const salesChannel = await scModuleService.createSalesChannels({ |
| 2509 | name: "Webshop", |
| 2510 | }) |
| 2511 | |
| 2512 | const cart = await cartModuleService.createCarts({ |
| 2513 | currency_code: "usd", |
| 2514 | sales_channel_id: salesChannel.id, |
| 2515 | }) |
| 2516 | |
| 2517 | const { errors } = await addToCartWorkflow(appContainer).run({ |
| 2518 | input: { |
| 2519 | items: [ |
| 2520 | { |
| 2521 | variant_id: "var_1234", |
| 2522 | quantity: 1, |
| 2523 | }, |
| 2524 | ], |
| 2525 | cart_id: cart.id, |
| 2526 | }, |
| 2527 | throwOnError: false, |
| 2528 | }) |
| 2529 | |
| 2530 | expect(errors).toEqual([ |
| 2531 | { |
| 2532 | action: "get-variant-items-with-prices-workflow-as-step", |
| 2533 | handlerType: "invoke", |
| 2534 | error: expect.objectContaining({ |
| 2535 | message: expect.stringContaining( |
| 2536 | `Variants var_1234 do not exist or belong to a product that is not published` |
| 2537 | ), |
| 2538 | }), |
| 2539 | }, |
| 2540 | ]) |
| 2541 | }) |
| 2542 | |
| 2543 | it("should throw if no price sets for variant exist", async () => { |
| 2544 | const salesChannel = await scModuleService.createSalesChannels({ |
| 2545 | name: "Webshop", |
| 2546 | }) |
| 2547 | |
| 2548 | const location = await stockLocationModule.createStockLocations({ |
| 2549 | name: "Warehouse", |
| 2550 | }) |
| 2551 | |
| 2552 | const cart = await cartModuleService.createCarts({ |
| 2553 | currency_code: "usd", |
| 2554 | sales_channel_id: salesChannel.id, |
| 2555 | }) |
| 2556 | |
| 2557 | const [product] = await productModule.createProducts([ |
| 2558 | { |
| 2559 | title: "Test product", |
| 2560 | variants: [ |
| 2561 | { |
| 2562 | title: "Test variant", |
| 2563 | }, |
| 2564 | ], |
| 2565 | }, |
| 2566 | ]) |
| 2567 | |
| 2568 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 2569 | sku: "inv-1234", |
| 2570 | }) |
| 2571 | |
| 2572 | await inventoryModule.createInventoryLevels([ |
| 2573 | { |
| 2574 | inventory_item_id: inventoryItem.id, |
| 2575 | location_id: location.id, |
| 2576 | stocked_quantity: 2, |
| 2577 | reserved_quantity: 0, |
| 2578 | }, |
| 2579 | ]) |
| 2580 | |
| 2581 | await remoteLink.create([ |
| 2582 | { |
| 2583 | [Modules.SALES_CHANNEL]: { |
| 2584 | sales_channel_id: salesChannel.id, |
| 2585 | }, |
| 2586 | [Modules.STOCK_LOCATION]: { |
| 2587 | stock_location_id: location.id, |
| 2588 | }, |
| 2589 | }, |
| 2590 | { |
| 2591 | [Modules.PRODUCT]: { |
| 2592 | variant_id: product.variants[0].id, |
| 2593 | }, |
| 2594 | [Modules.INVENTORY]: { |
| 2595 | inventory_item_id: inventoryItem.id, |
| 2596 | }, |
| 2597 | }, |
| 2598 | ]) |
| 2599 | |
| 2600 | const { errors } = await addToCartWorkflow(appContainer).run({ |
| 2601 | input: { |
| 2602 | items: [ |
| 2603 | { |
| 2604 | variant_id: product.variants[0].id, |
| 2605 | quantity: 1, |
| 2606 | }, |
| 2607 | ], |
| 2608 | cart_id: cart.id, |
| 2609 | }, |
| 2610 | throwOnError: false, |
| 2611 | }) |
| 2612 | |
| 2613 | expect(errors).toEqual([ |
| 2614 | { |
| 2615 | action: "get-variant-items-with-prices-workflow-as-step", |
| 2616 | handlerType: "invoke", |
| 2617 | error: expect.objectContaining({ |
| 2618 | message: expect.stringContaining( |
| 2619 | `Variants with IDs ${product.variants[0].id} do not have a price` |
| 2620 | ), |
| 2621 | }), |
| 2622 | }, |
| 2623 | ]) |
| 2624 | }) |
| 2625 | |
| 2626 | it("should add item to cart with unit_price when variant has no price set", async () => { |
| 2627 | const salesChannel = await scModuleService.createSalesChannels({ |
| 2628 | name: "Webshop", |
| 2629 | }) |
| 2630 | |
| 2631 | const location = await stockLocationModule.createStockLocations({ |
| 2632 | name: "Warehouse", |
| 2633 | }) |
| 2634 | |
| 2635 | let cart = await cartModuleService.createCarts({ |
| 2636 | currency_code: "usd", |
| 2637 | sales_channel_id: salesChannel.id, |
| 2638 | }) |
| 2639 | |
| 2640 | const [product] = await productModule.createProducts([ |
| 2641 | { |
| 2642 | title: "Test product", |
| 2643 | status: ProductStatus.PUBLISHED, |
| 2644 | variants: [ |
| 2645 | { |
| 2646 | title: "Test variant", |
| 2647 | }, |
| 2648 | ], |
| 2649 | }, |
| 2650 | ]) |
| 2651 | |
| 2652 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 2653 | sku: "inv-1234", |
| 2654 | }) |
| 2655 | |
| 2656 | await inventoryModule.createInventoryLevels([ |
| 2657 | { |
| 2658 | inventory_item_id: inventoryItem.id, |
| 2659 | location_id: location.id, |
| 2660 | stocked_quantity: 2, |
| 2661 | reserved_quantity: 0, |
| 2662 | }, |
| 2663 | ]) |
| 2664 | |
| 2665 | await remoteLink.create([ |
| 2666 | { |
| 2667 | [Modules.SALES_CHANNEL]: { |
| 2668 | sales_channel_id: salesChannel.id, |
| 2669 | }, |
| 2670 | [Modules.STOCK_LOCATION]: { |
| 2671 | stock_location_id: location.id, |
| 2672 | }, |
| 2673 | }, |
| 2674 | { |
| 2675 | [Modules.PRODUCT]: { |
| 2676 | variant_id: product.variants[0].id, |
| 2677 | }, |
| 2678 | [Modules.INVENTORY]: { |
| 2679 | inventory_item_id: inventoryItem.id, |
| 2680 | }, |
| 2681 | }, |
| 2682 | ]) |
| 2683 | |
| 2684 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2685 | select: ["id", "region_id", "currency_code", "sales_channel_id"], |
| 2686 | }) |
| 2687 | |
| 2688 | // Add item with unit_price specified even though variant has no price set |
| 2689 | await addToCartWorkflow(appContainer).run({ |
| 2690 | input: { |
| 2691 | items: [ |
| 2692 | { |
| 2693 | variant_id: product.variants[0].id, |
| 2694 | quantity: 1, |
| 2695 | unit_price: 5000, |
| 2696 | }, |
| 2697 | ], |
| 2698 | cart_id: cart.id, |
| 2699 | }, |
| 2700 | }) |
| 2701 | |
| 2702 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2703 | relations: ["items"], |
| 2704 | }) |
| 2705 | |
| 2706 | expect(normalizeBigNumbers(cart)).toEqual( |
| 2707 | expect.objectContaining({ |
| 2708 | id: cart.id, |
| 2709 | currency_code: "usd", |
| 2710 | items: expect.arrayContaining([ |
| 2711 | expect.objectContaining({ |
| 2712 | unit_price: 5000, |
| 2713 | is_custom_price: true, |
| 2714 | quantity: 1, |
| 2715 | title: "Test product", |
| 2716 | subtitle: "Test variant", |
| 2717 | }), |
| 2718 | ]), |
| 2719 | }) |
| 2720 | ) |
| 2721 | }) |
| 2722 | |
| 2723 | describe("setPricingContext hook", () => { |
| 2724 | it("should use context provided by the hook", async () => { |
| 2725 | const salesChannel = await scModuleService.createSalesChannels({ |
| 2726 | name: "Webshop", |
| 2727 | }) |
| 2728 | |
| 2729 | const location = await stockLocationModule.createStockLocations({ |
| 2730 | name: "Warehouse", |
| 2731 | }) |
| 2732 | |
| 2733 | let cart = await cartModuleService.createCarts({ |
| 2734 | currency_code: "usd", |
| 2735 | sales_channel_id: salesChannel.id, |
| 2736 | }) |
| 2737 | |
| 2738 | const [product] = await productModule.createProducts([ |
| 2739 | { |
| 2740 | title: "Test product", |
| 2741 | status: ProductStatus.PUBLISHED, |
| 2742 | variants: [ |
| 2743 | { |
| 2744 | title: "Test variant", |
| 2745 | }, |
| 2746 | ], |
| 2747 | }, |
| 2748 | ]) |
| 2749 | |
| 2750 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 2751 | sku: "inv-1234", |
| 2752 | }) |
| 2753 | |
| 2754 | await inventoryModule.createInventoryLevels([ |
| 2755 | { |
| 2756 | inventory_item_id: inventoryItem.id, |
| 2757 | location_id: location.id, |
| 2758 | stocked_quantity: 2, |
| 2759 | reserved_quantity: 0, |
| 2760 | }, |
| 2761 | ]) |
| 2762 | |
| 2763 | const priceSet = await pricingModule.createPriceSets({ |
| 2764 | prices: [ |
| 2765 | { |
| 2766 | amount: 3000, |
| 2767 | currency_code: "usd", |
| 2768 | }, |
| 2769 | ], |
| 2770 | }) |
| 2771 | |
| 2772 | await pricingModule.createPricePreferences({ |
| 2773 | attribute: "currency_code", |
| 2774 | value: "usd", |
| 2775 | is_tax_inclusive: true, |
| 2776 | }) |
| 2777 | |
| 2778 | await remoteLink.create([ |
| 2779 | { |
| 2780 | [Modules.PRODUCT]: { |
| 2781 | variant_id: product.variants[0].id, |
| 2782 | }, |
| 2783 | [Modules.PRICING]: { |
| 2784 | price_set_id: priceSet.id, |
| 2785 | }, |
| 2786 | }, |
| 2787 | { |
| 2788 | [Modules.SALES_CHANNEL]: { |
| 2789 | sales_channel_id: salesChannel.id, |
| 2790 | }, |
| 2791 | [Modules.STOCK_LOCATION]: { |
| 2792 | stock_location_id: location.id, |
| 2793 | }, |
| 2794 | }, |
| 2795 | { |
| 2796 | [Modules.PRODUCT]: { |
| 2797 | variant_id: product.variants[0].id, |
| 2798 | }, |
| 2799 | [Modules.INVENTORY]: { |
| 2800 | inventory_item_id: inventoryItem.id, |
| 2801 | }, |
| 2802 | }, |
| 2803 | ]) |
| 2804 | |
| 2805 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2806 | select: ["id", "region_id", "currency_code", "sales_channel_id"], |
| 2807 | }) |
| 2808 | |
| 2809 | setPricingContextHook = function (input) { |
| 2810 | expect(input.cart).toEqual(expect.objectContaining(cart)) |
| 2811 | expect(input.variantIds).toEqual([product.variants[0].id]) |
| 2812 | return new StepResponse({ |
| 2813 | unit_price: 100, |
| 2814 | }) |
| 2815 | } |
| 2816 | |
| 2817 | /** |
| 2818 | * Tried jest, but for some reasons it is not able to provide |
| 2819 | * correct arguments passed to the function |
| 2820 | */ |
| 2821 | let pricingContext: any |
| 2822 | let calculatePricessHaveBeenCalled = false |
| 2823 | const originalFn = pricingModule.calculatePrices.bind(pricingModule) |
| 2824 | pricingModule.calculatePrices = function (...args) { |
| 2825 | pricingContext = args[1]! |
| 2826 | calculatePricessHaveBeenCalled = true |
| 2827 | |
| 2828 | expect(pricingContext).toEqual( |
| 2829 | expect.objectContaining({ |
| 2830 | context: expect.objectContaining({ |
| 2831 | unit_price: 100, |
| 2832 | currency_code: "usd", |
| 2833 | }), |
| 2834 | }) |
| 2835 | ) |
| 2836 | |
| 2837 | return originalFn.bind(pricingModule)(...args) |
| 2838 | } |
| 2839 | |
| 2840 | await addToCartWorkflow(appContainer).run({ |
| 2841 | input: { |
| 2842 | items: [ |
| 2843 | { |
| 2844 | variant_id: product.variants[0].id, |
| 2845 | quantity: 1, |
| 2846 | }, |
| 2847 | ], |
| 2848 | cart_id: cart.id, |
| 2849 | }, |
| 2850 | }) |
| 2851 | |
| 2852 | setPricingContextHook = undefined |
| 2853 | pricingModule.calculatePrices = originalFn |
| 2854 | |
| 2855 | expect(calculatePricessHaveBeenCalled).toBe(true) |
| 2856 | |
| 2857 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2858 | relations: ["items"], |
| 2859 | }) |
| 2860 | |
| 2861 | expect(normalizeBigNumbers(cart)).toEqual( |
| 2862 | expect.objectContaining({ |
| 2863 | id: cart.id, |
| 2864 | currency_code: "usd", |
| 2865 | items: expect.arrayContaining([ |
| 2866 | expect.objectContaining({ |
| 2867 | unit_price: 3000, |
| 2868 | is_tax_inclusive: true, |
| 2869 | quantity: 1, |
| 2870 | title: "Test product", |
| 2871 | }), |
| 2872 | ]), |
| 2873 | }) |
| 2874 | ) |
| 2875 | }) |
| 2876 | |
| 2877 | it("should not be able to override the 'customer_id', 'region_id', and the 'currency_code' from the setPricingContext hook", async () => { |
| 2878 | const salesChannel = await scModuleService.createSalesChannels({ |
| 2879 | name: "Webshop", |
| 2880 | }) |
| 2881 | |
| 2882 | const location = await stockLocationModule.createStockLocations({ |
| 2883 | name: "Warehouse", |
| 2884 | }) |
| 2885 | |
| 2886 | let cart = await cartModuleService.createCarts({ |
| 2887 | currency_code: "usd", |
| 2888 | sales_channel_id: salesChannel.id, |
| 2889 | }) |
| 2890 | |
| 2891 | const [product] = await productModule.createProducts([ |
| 2892 | { |
| 2893 | title: "Test product", |
| 2894 | status: ProductStatus.PUBLISHED, |
| 2895 | variants: [ |
| 2896 | { |
| 2897 | title: "Test variant", |
| 2898 | }, |
| 2899 | ], |
| 2900 | }, |
| 2901 | ]) |
| 2902 | |
| 2903 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 2904 | sku: "inv-1234", |
| 2905 | }) |
| 2906 | |
| 2907 | await inventoryModule.createInventoryLevels([ |
| 2908 | { |
| 2909 | inventory_item_id: inventoryItem.id, |
| 2910 | location_id: location.id, |
| 2911 | stocked_quantity: 2, |
| 2912 | reserved_quantity: 0, |
| 2913 | }, |
| 2914 | ]) |
| 2915 | |
| 2916 | const priceSet = await pricingModule.createPriceSets({ |
| 2917 | prices: [ |
| 2918 | { |
| 2919 | amount: 3000, |
| 2920 | currency_code: "usd", |
| 2921 | }, |
| 2922 | ], |
| 2923 | }) |
| 2924 | |
| 2925 | await pricingModule.createPricePreferences({ |
| 2926 | attribute: "currency_code", |
| 2927 | value: "usd", |
| 2928 | is_tax_inclusive: true, |
| 2929 | }) |
| 2930 | |
| 2931 | await remoteLink.create([ |
| 2932 | { |
| 2933 | [Modules.PRODUCT]: { |
| 2934 | variant_id: product.variants[0].id, |
| 2935 | }, |
| 2936 | [Modules.PRICING]: { |
| 2937 | price_set_id: priceSet.id, |
| 2938 | }, |
| 2939 | }, |
| 2940 | { |
| 2941 | [Modules.SALES_CHANNEL]: { |
| 2942 | sales_channel_id: salesChannel.id, |
| 2943 | }, |
| 2944 | [Modules.STOCK_LOCATION]: { |
| 2945 | stock_location_id: location.id, |
| 2946 | }, |
| 2947 | }, |
| 2948 | { |
| 2949 | [Modules.PRODUCT]: { |
| 2950 | variant_id: product.variants[0].id, |
| 2951 | }, |
| 2952 | [Modules.INVENTORY]: { |
| 2953 | inventory_item_id: inventoryItem.id, |
| 2954 | }, |
| 2955 | }, |
| 2956 | ]) |
| 2957 | |
| 2958 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 2959 | select: [ |
| 2960 | "id", |
| 2961 | "region_id", |
| 2962 | "currency_code", |
| 2963 | "sales_channel_id", |
| 2964 | "customer_id", |
| 2965 | ], |
| 2966 | }) |
| 2967 | |
| 2968 | setPricingContextHook = function (input) { |
| 2969 | expect(input.cart).toEqual(expect.objectContaining(cart)) |
| 2970 | expect(input.variantIds).toEqual([product.variants[0].id]) |
| 2971 | return new StepResponse({ |
| 2972 | unit_price: 200, |
| 2973 | currency_code: "inr", |
| 2974 | customer_id: "1", |
| 2975 | region_id: "1", |
| 2976 | }) |
| 2977 | } |
| 2978 | |
| 2979 | /** |
| 2980 | * Tried jest, but for some reasons it is not able to provide |
| 2981 | * correct arguments passed to the function |
| 2982 | */ |
| 2983 | let pricingContext: any |
| 2984 | let calculatePricessHaveBeenCalled = false |
| 2985 | |
| 2986 | const originalFn = pricingModule.calculatePrices.bind(pricingModule) |
| 2987 | pricingModule.calculatePrices = function (...args) { |
| 2988 | pricingContext = args[1]! |
| 2989 | calculatePricessHaveBeenCalled = true |
| 2990 | |
| 2991 | expect(pricingContext).toEqual( |
| 2992 | expect.objectContaining({ |
| 2993 | context: expect.objectContaining({ |
| 2994 | unit_price: 200, |
| 2995 | region_id: cart.region_id, |
| 2996 | customer_id: cart.customer_id, |
| 2997 | currency_code: "usd", |
| 2998 | }), |
| 2999 | }) |
| 3000 | ) |
| 3001 | |
| 3002 | return originalFn.bind(pricingModule)(...arguments) |
| 3003 | } |
| 3004 | |
| 3005 | await addToCartWorkflow(appContainer).run({ |
| 3006 | input: { |
| 3007 | items: [ |
| 3008 | { |
| 3009 | variant_id: product.variants[0].id, |
| 3010 | quantity: 1, |
| 3011 | }, |
| 3012 | ], |
| 3013 | cart_id: cart.id, |
| 3014 | }, |
| 3015 | }) |
| 3016 | |
| 3017 | setPricingContextHook = undefined |
| 3018 | pricingModule.calculatePrices = originalFn |
| 3019 | |
| 3020 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 3021 | relations: ["items"], |
| 3022 | }) |
| 3023 | |
| 3024 | expect(calculatePricessHaveBeenCalled).toBe(true) |
| 3025 | |
| 3026 | expect(normalizeBigNumbers(cart)).toEqual( |
| 3027 | expect.objectContaining({ |
| 3028 | id: cart.id, |
| 3029 | currency_code: "usd", |
| 3030 | items: expect.arrayContaining([ |
| 3031 | expect.objectContaining({ |
| 3032 | unit_price: 3000, |
| 3033 | is_tax_inclusive: true, |
| 3034 | quantity: 1, |
| 3035 | title: "Test product", |
| 3036 | }), |
| 3037 | ]), |
| 3038 | }) |
| 3039 | ) |
| 3040 | }) |
| 3041 | }) |
| 3042 | }) |
| 3043 | |
| 3044 | describe("updateLineItemInCartWorkflow", () => { |
| 3045 | it("should update item in cart", async () => { |
| 3046 | const salesChannel = await scModuleService.createSalesChannels({ |
| 3047 | name: "Webshop", |
| 3048 | }) |
| 3049 | |
| 3050 | const location = await stockLocationModule.createStockLocations({ |
| 3051 | name: "Warehouse", |
| 3052 | }) |
| 3053 | |
| 3054 | const [product] = await productModule.createProducts([ |
| 3055 | { |
| 3056 | title: "Test product", |
| 3057 | variants: [ |
| 3058 | { |
| 3059 | title: "Test variant", |
| 3060 | }, |
| 3061 | ], |
| 3062 | }, |
| 3063 | ]) |
| 3064 | |
| 3065 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 3066 | sku: "inv-1234", |
| 3067 | }) |
| 3068 | |
| 3069 | await inventoryModule.createInventoryLevels([ |
| 3070 | { |
| 3071 | inventory_item_id: inventoryItem.id, |
| 3072 | location_id: location.id, |
| 3073 | stocked_quantity: 2, |
| 3074 | reserved_quantity: 0, |
| 3075 | }, |
| 3076 | ]) |
| 3077 | |
| 3078 | const priceSet = await pricingModule.createPriceSets({ |
| 3079 | prices: [ |
| 3080 | { |
| 3081 | amount: 3000, |
| 3082 | currency_code: "usd", |
| 3083 | }, |
| 3084 | ], |
| 3085 | }) |
| 3086 | |
| 3087 | await remoteLink.create([ |
| 3088 | { |
| 3089 | [Modules.PRODUCT]: { |
| 3090 | variant_id: product.variants[0].id, |
| 3091 | }, |
| 3092 | [Modules.PRICING]: { |
| 3093 | price_set_id: priceSet.id, |
| 3094 | }, |
| 3095 | }, |
| 3096 | { |
| 3097 | [Modules.SALES_CHANNEL]: { |
| 3098 | sales_channel_id: salesChannel.id, |
| 3099 | }, |
| 3100 | [Modules.STOCK_LOCATION]: { |
| 3101 | stock_location_id: location.id, |
| 3102 | }, |
| 3103 | }, |
| 3104 | { |
| 3105 | [Modules.PRODUCT]: { |
| 3106 | variant_id: product.variants[0].id, |
| 3107 | }, |
| 3108 | [Modules.INVENTORY]: { |
| 3109 | inventory_item_id: inventoryItem.id, |
| 3110 | }, |
| 3111 | }, |
| 3112 | ]) |
| 3113 | |
| 3114 | let cart = await cartModuleService.createCarts({ |
| 3115 | currency_code: "usd", |
| 3116 | sales_channel_id: salesChannel.id, |
| 3117 | items: [ |
| 3118 | { |
| 3119 | variant_id: product.variants[0].id, |
| 3120 | quantity: 1, |
| 3121 | unit_price: 5000, |
| 3122 | is_custom_price: true, |
| 3123 | title: "Test product", |
| 3124 | }, |
| 3125 | ], |
| 3126 | }) |
| 3127 | |
| 3128 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 3129 | select: ["id", "region_id", "currency_code"], |
| 3130 | relations: ["items", "items.variant_id", "items.metadata"], |
| 3131 | }) |
| 3132 | |
| 3133 | const item = cart.items?.[0]! |
| 3134 | |
| 3135 | await updateLineItemInCartWorkflow(appContainer).run({ |
| 3136 | input: { |
| 3137 | item_id: item.id, |
| 3138 | update: { |
| 3139 | metadata: { |
| 3140 | foo: "bar", |
| 3141 | }, |
| 3142 | quantity: 2, |
| 3143 | }, |
| 3144 | cart_id: cart.id, |
| 3145 | }, |
| 3146 | throwOnError: false, |
| 3147 | }) |
| 3148 | |
| 3149 | const updatedItem = await cartModuleService.retrieveLineItem(item.id) |
| 3150 | |
| 3151 | expect(updatedItem).toEqual( |
| 3152 | expect.objectContaining({ |
| 3153 | id: item.id, |
| 3154 | unit_price: 5000, |
| 3155 | is_custom_price: true, |
| 3156 | quantity: 2, |
| 3157 | title: "Test product", |
| 3158 | }) |
| 3159 | ) |
| 3160 | }) |
| 3161 | |
| 3162 | it("should update custom item in cart", async () => { |
| 3163 | const salesChannel = await scModuleService.createSalesChannels({ |
| 3164 | name: "Webshop", |
| 3165 | }) |
| 3166 | |
| 3167 | const location = await stockLocationModule.createStockLocations({ |
| 3168 | name: "Warehouse", |
| 3169 | }) |
| 3170 | |
| 3171 | let cart = await cartModuleService.createCarts({ |
| 3172 | currency_code: "usd", |
| 3173 | sales_channel_id: salesChannel.id, |
| 3174 | items: [ |
| 3175 | { |
| 3176 | title: "Test item", |
| 3177 | subtitle: "Test subtitle", |
| 3178 | thumbnail: "some-url", |
| 3179 | requires_shipping: true, |
| 3180 | is_discountable: false, |
| 3181 | is_tax_inclusive: false, |
| 3182 | is_custom_price: true, |
| 3183 | variant_id: "some_random_id", |
| 3184 | unit_price: 3000, |
| 3185 | metadata: { |
| 3186 | foo: "bar", |
| 3187 | }, |
| 3188 | quantity: 1, |
| 3189 | }, |
| 3190 | ], |
| 3191 | }) |
| 3192 | |
| 3193 | await remoteLink.create([ |
| 3194 | { |
| 3195 | [Modules.SALES_CHANNEL]: { |
| 3196 | sales_channel_id: salesChannel.id, |
| 3197 | }, |
| 3198 | [Modules.STOCK_LOCATION]: { |
| 3199 | stock_location_id: location.id, |
| 3200 | }, |
| 3201 | }, |
| 3202 | ]) |
| 3203 | |
| 3204 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 3205 | select: ["id", "region_id", "currency_code", "sales_channel_id"], |
| 3206 | relations: ["items"], |
| 3207 | }) |
| 3208 | |
| 3209 | await updateLineItemInCartWorkflow(appContainer).run({ |
| 3210 | input: { |
| 3211 | cart_id: cart.id, |
| 3212 | item_id: cart.items?.[0]!.id!, |
| 3213 | update: { |
| 3214 | quantity: 2, |
| 3215 | title: "Some other title", |
| 3216 | }, |
| 3217 | }, |
| 3218 | }) |
| 3219 | |
| 3220 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 3221 | relations: ["items"], |
| 3222 | }) |
| 3223 | |
| 3224 | expect(normalizeBigNumbers(cart)).toEqual( |
| 3225 | expect.objectContaining({ |
| 3226 | id: cart.id, |
| 3227 | currency_code: "usd", |
| 3228 | items: [ |
| 3229 | expect.objectContaining({ |
| 3230 | cart_id: expect.any(String), |
| 3231 | compare_at_unit_price: null, |
| 3232 | created_at: expect.any(Date), |
| 3233 | deleted_at: null, |
| 3234 | id: expect.any(String), |
| 3235 | is_discountable: false, |
| 3236 | is_giftcard: false, |
| 3237 | is_tax_inclusive: false, |
| 3238 | is_custom_price: true, |
| 3239 | metadata: { |
| 3240 | foo: "bar", |
| 3241 | }, |
| 3242 | product_collection: null, |
| 3243 | product_description: null, |
| 3244 | product_handle: null, |
| 3245 | product_id: null, |
| 3246 | product_subtitle: null, |
| 3247 | product_title: null, |
| 3248 | product_type: null, |
| 3249 | product_type_id: null, |
| 3250 | quantity: 2, |
| 3251 | raw_compare_at_unit_price: null, |
| 3252 | raw_unit_price: { |
| 3253 | precision: 20, |
| 3254 | value: "3000", |
| 3255 | }, |
| 3256 | requires_shipping: true, |
| 3257 | subtitle: "Test subtitle", |
| 3258 | thumbnail: "some-url", |
| 3259 | title: "Some other title", |
| 3260 | unit_price: 3000, |
| 3261 | updated_at: expect.any(Date), |
| 3262 | variant_barcode: null, |
| 3263 | variant_id: "some_random_id", |
| 3264 | variant_option_values: null, |
| 3265 | variant_sku: null, |
| 3266 | variant_title: null, |
| 3267 | }), |
| 3268 | ], |
| 3269 | }) |
| 3270 | ) |
| 3271 | |
| 3272 | await updateLineItemInCartWorkflow(appContainer).run({ |
| 3273 | input: { |
| 3274 | cart_id: cart.id, |
| 3275 | item_id: cart.items?.[0]!.id!, |
| 3276 | update: { |
| 3277 | quantity: 4, |
| 3278 | }, |
| 3279 | }, |
| 3280 | }) |
| 3281 | |
| 3282 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 3283 | relations: ["items"], |
| 3284 | }) |
| 3285 | |
| 3286 | expect(normalizeBigNumbers(cart)).toEqual( |
| 3287 | expect.objectContaining({ |
| 3288 | id: cart.id, |
| 3289 | currency_code: "usd", |
| 3290 | items: [ |
| 3291 | expect.objectContaining({ |
| 3292 | cart_id: expect.any(String), |
| 3293 | compare_at_unit_price: null, |
| 3294 | created_at: expect.any(Date), |
| 3295 | deleted_at: null, |
| 3296 | id: expect.any(String), |
| 3297 | is_discountable: false, |
| 3298 | is_giftcard: false, |
| 3299 | is_tax_inclusive: false, |
| 3300 | is_custom_price: true, |
| 3301 | metadata: { |
| 3302 | foo: "bar", |
| 3303 | }, |
| 3304 | product_collection: null, |
| 3305 | product_description: null, |
| 3306 | product_handle: null, |
| 3307 | product_id: null, |
| 3308 | product_subtitle: null, |
| 3309 | product_title: null, |
| 3310 | product_type: null, |
| 3311 | product_type_id: null, |
| 3312 | quantity: 4, |
| 3313 | raw_compare_at_unit_price: null, |
| 3314 | raw_unit_price: { |
| 3315 | precision: 20, |
| 3316 | value: "3000", |
| 3317 | }, |
| 3318 | requires_shipping: true, |
| 3319 | subtitle: "Test subtitle", |
| 3320 | thumbnail: "some-url", |
| 3321 | title: "Some other title", |
| 3322 | unit_price: 3000, |
| 3323 | updated_at: expect.any(Date), |
| 3324 | variant_barcode: null, |
| 3325 | variant_id: "some_random_id", |
| 3326 | variant_option_values: null, |
| 3327 | variant_sku: null, |
| 3328 | variant_title: null, |
| 3329 | }), |
| 3330 | ], |
| 3331 | }) |
| 3332 | ) |
| 3333 | }) |
| 3334 | |
| 3335 | it("should update unit price of regular item in cart", async () => { |
| 3336 | const salesChannel = await scModuleService.createSalesChannels({ |
| 3337 | name: "Webshop", |
| 3338 | }) |
| 3339 | |
| 3340 | const location = await stockLocationModule.createStockLocations({ |
| 3341 | name: "Warehouse", |
| 3342 | }) |
| 3343 | |
| 3344 | const [product] = await productModule.createProducts([ |
| 3345 | { |
| 3346 | title: "Test product", |
| 3347 | variants: [ |
| 3348 | { |
| 3349 | title: "Test variant", |
| 3350 | }, |
| 3351 | ], |
| 3352 | }, |
| 3353 | ]) |
| 3354 | |
| 3355 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 3356 | sku: "inv-1234", |
| 3357 | }) |
| 3358 | |
| 3359 | await inventoryModule.createInventoryLevels([ |
| 3360 | { |
| 3361 | inventory_item_id: inventoryItem.id, |
| 3362 | location_id: location.id, |
| 3363 | stocked_quantity: 2, |
| 3364 | reserved_quantity: 0, |
| 3365 | }, |
| 3366 | ]) |
| 3367 | |
| 3368 | const priceSet = await pricingModule.createPriceSets({ |
| 3369 | prices: [ |
| 3370 | { |
| 3371 | amount: 3000, |
| 3372 | currency_code: "usd", |
| 3373 | }, |
| 3374 | ], |
| 3375 | }) |
| 3376 | |
| 3377 | await remoteLink.create([ |
| 3378 | { |
| 3379 | [Modules.PRODUCT]: { |
| 3380 | variant_id: product.variants[0].id, |
| 3381 | }, |
| 3382 | [Modules.PRICING]: { |
| 3383 | price_set_id: priceSet.id, |
| 3384 | }, |
| 3385 | }, |
| 3386 | { |
| 3387 | [Modules.SALES_CHANNEL]: { |
| 3388 | sales_channel_id: salesChannel.id, |
| 3389 | }, |
| 3390 | [Modules.STOCK_LOCATION]: { |
| 3391 | stock_location_id: location.id, |
| 3392 | }, |
| 3393 | }, |
| 3394 | { |
| 3395 | [Modules.PRODUCT]: { |
| 3396 | variant_id: product.variants[0].id, |
| 3397 | }, |
| 3398 | [Modules.INVENTORY]: { |
| 3399 | inventory_item_id: inventoryItem.id, |
| 3400 | }, |
| 3401 | }, |
| 3402 | ]) |
| 3403 | |
| 3404 | let cart = await cartModuleService.createCarts({ |
| 3405 | currency_code: "usd", |
| 3406 | sales_channel_id: salesChannel.id, |
| 3407 | items: [ |
| 3408 | { |
| 3409 | variant_id: product.variants[0].id, |
| 3410 | quantity: 1, |
| 3411 | unit_price: 5000, |
| 3412 | title: "Test item", |
| 3413 | }, |
| 3414 | ], |
| 3415 | }) |
| 3416 | |
| 3417 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 3418 | select: ["id", "region_id", "currency_code"], |
| 3419 | relations: ["items", "items.variant_id", "items.metadata"], |
| 3420 | }) |
| 3421 | |
| 3422 | const item = cart.items?.[0]! |
| 3423 | |
| 3424 | await updateLineItemInCartWorkflow(appContainer).run({ |
| 3425 | input: { |
| 3426 | cart_id: cart.id, |
| 3427 | item_id: item.id, |
| 3428 | update: { |
| 3429 | metadata: { |
| 3430 | foo: "bar", |
| 3431 | }, |
| 3432 | unit_price: 4000, |
| 3433 | quantity: 2, |
| 3434 | }, |
| 3435 | }, |
| 3436 | }) |
| 3437 | |
| 3438 | const updatedItem = await cartModuleService.retrieveLineItem(item.id) |
| 3439 | |
| 3440 | expect(updatedItem).toEqual( |
| 3441 | expect.objectContaining({ |
| 3442 | id: item.id, |
| 3443 | unit_price: 4000, |
| 3444 | is_custom_price: true, |
| 3445 | quantity: 2, |
| 3446 | title: "Test item", |
| 3447 | }) |
| 3448 | ) |
| 3449 | }) |
| 3450 | }) |
| 3451 | |
| 3452 | describe("updateLineItemInCartWorkflow", () => { |
| 3453 | it("should update item in cart with price list", async () => { |
| 3454 | const salesChannel = await scModuleService.createSalesChannels({ |
| 3455 | name: "Webshop", |
| 3456 | }) |
| 3457 | |
| 3458 | const customer = await customerModule.createCustomers({ |
| 3459 | first_name: "Test", |
| 3460 | last_name: "Test", |
| 3461 | }) |
| 3462 | |
| 3463 | const customer_group = await customerModule.createCustomerGroups({ |
| 3464 | name: "Test Group", |
| 3465 | }) |
| 3466 | |
| 3467 | await customerModule.addCustomerToGroup({ |
| 3468 | customer_id: customer.id, |
| 3469 | customer_group_id: customer_group.id, |
| 3470 | }) |
| 3471 | |
| 3472 | const location = await stockLocationModule.createStockLocations({ |
| 3473 | name: "Warehouse", |
| 3474 | }) |
| 3475 | |
| 3476 | const [product] = await productModule.createProducts([ |
| 3477 | { |
| 3478 | title: "Test product", |
| 3479 | variants: [ |
| 3480 | { |
| 3481 | title: "Test variant", |
| 3482 | }, |
| 3483 | ], |
| 3484 | }, |
| 3485 | ]) |
| 3486 | |
| 3487 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 3488 | sku: "inv-1234", |
| 3489 | }) |
| 3490 | |
| 3491 | await inventoryModule.createInventoryLevels([ |
| 3492 | { |
| 3493 | inventory_item_id: inventoryItem.id, |
| 3494 | location_id: location.id, |
| 3495 | stocked_quantity: 2, |
| 3496 | }, |
| 3497 | ]) |
| 3498 | |
| 3499 | const priceSet = await pricingModule.createPriceSets({ |
| 3500 | prices: [ |
| 3501 | { |
| 3502 | amount: 3000, |
| 3503 | currency_code: "usd", |
| 3504 | }, |
| 3505 | ], |
| 3506 | }) |
| 3507 | |
| 3508 | await pricingModule.createPriceLists([ |
| 3509 | { |
| 3510 | title: "test price list", |
| 3511 | description: "test", |
| 3512 | status: PriceListStatus.ACTIVE, |
| 3513 | type: PriceListType.OVERRIDE, |
| 3514 | prices: [ |
| 3515 | { |
| 3516 | amount: 1500, |
| 3517 | currency_code: "usd", |
| 3518 | price_set_id: priceSet.id, |
| 3519 | }, |
| 3520 | ], |
| 3521 | rules: { |
| 3522 | "customer.groups.id": [customer_group.id], |
| 3523 | }, |
| 3524 | }, |
| 3525 | ]) |
| 3526 | |
| 3527 | await remoteLink.create([ |
| 3528 | { |
| 3529 | [Modules.PRODUCT]: { |
| 3530 | variant_id: product.variants[0].id, |
| 3531 | }, |
| 3532 | [Modules.PRICING]: { |
| 3533 | price_set_id: priceSet.id, |
| 3534 | }, |
| 3535 | }, |
| 3536 | { |
| 3537 | [Modules.SALES_CHANNEL]: { |
| 3538 | sales_channel_id: salesChannel.id, |
| 3539 | }, |
| 3540 | [Modules.STOCK_LOCATION]: { |
| 3541 | stock_location_id: location.id, |
| 3542 | }, |
| 3543 | }, |
| 3544 | { |
| 3545 | [Modules.PRODUCT]: { |
| 3546 | variant_id: product.variants[0].id, |
| 3547 | }, |
| 3548 | [Modules.INVENTORY]: { |
| 3549 | inventory_item_id: inventoryItem.id, |
| 3550 | }, |
| 3551 | }, |
| 3552 | ]) |
| 3553 | |
| 3554 | let cart = await cartModuleService.createCarts({ |
| 3555 | currency_code: "usd", |
| 3556 | sales_channel_id: salesChannel.id, |
| 3557 | customer_id: customer.id, |
| 3558 | items: [ |
| 3559 | { |
| 3560 | variant_id: product.variants[0].id, |
| 3561 | quantity: 1, |
| 3562 | title: "Test item", |
| 3563 | unit_price: 5000, |
| 3564 | }, |
| 3565 | ], |
| 3566 | }) |
| 3567 | |
| 3568 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 3569 | select: ["id", "region_id", "currency_code"], |
| 3570 | relations: ["items", "items.variant_id", "items.metadata"], |
| 3571 | }) |
| 3572 | |
| 3573 | const item = cart.items?.[0]! |
| 3574 | |
| 3575 | await updateLineItemInCartWorkflow(appContainer).run({ |
| 3576 | input: { |
| 3577 | cart_id: cart.id, |
| 3578 | item_id: item.id, |
| 3579 | update: { |
| 3580 | metadata: { |
| 3581 | foo: "bar", |
| 3582 | }, |
| 3583 | quantity: 2, |
| 3584 | }, |
| 3585 | }, |
| 3586 | throwOnError: true, |
| 3587 | }) |
| 3588 | |
| 3589 | const updatedItem = await cartModuleService.retrieveLineItem(item.id) |
| 3590 | |
| 3591 | expect(updatedItem).toEqual( |
| 3592 | expect.objectContaining({ |
| 3593 | id: item.id, |
| 3594 | unit_price: 1500, |
| 3595 | quantity: 2, |
| 3596 | }) |
| 3597 | ) |
| 3598 | }) |
| 3599 | |
| 3600 | it("should throw insufficient inventory", async () => { |
| 3601 | const salesChannel = await scModuleService.createSalesChannels({ |
| 3602 | name: "Webshop", |
| 3603 | }) |
| 3604 | |
| 3605 | const location = await stockLocationModule.createStockLocations({ |
| 3606 | name: "Warehouse", |
| 3607 | }) |
| 3608 | |
| 3609 | const [product] = await productModule.createProducts([ |
| 3610 | { |
| 3611 | title: "Test product", |
| 3612 | variants: [ |
| 3613 | { |
| 3614 | title: "Test variant", |
| 3615 | }, |
| 3616 | ], |
| 3617 | }, |
| 3618 | ]) |
| 3619 | |
| 3620 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 3621 | sku: "inv-1234", |
| 3622 | }) |
| 3623 | |
| 3624 | await inventoryModule.createInventoryLevels([ |
| 3625 | { |
| 3626 | inventory_item_id: inventoryItem.id, |
| 3627 | location_id: location.id, |
| 3628 | stocked_quantity: 2, |
| 3629 | }, |
| 3630 | ]) |
| 3631 | |
| 3632 | const priceSet = await pricingModule.createPriceSets({ |
| 3633 | prices: [ |
| 3634 | { |
| 3635 | amount: 3000, |
| 3636 | currency_code: "usd", |
| 3637 | }, |
| 3638 | ], |
| 3639 | }) |
| 3640 | |
| 3641 | await remoteLink.create([ |
| 3642 | { |
| 3643 | [Modules.PRODUCT]: { |
| 3644 | variant_id: product.variants[0].id, |
| 3645 | }, |
| 3646 | [Modules.PRICING]: { |
| 3647 | price_set_id: priceSet.id, |
| 3648 | }, |
| 3649 | }, |
| 3650 | { |
| 3651 | [Modules.SALES_CHANNEL]: { |
| 3652 | sales_channel_id: salesChannel.id, |
| 3653 | }, |
| 3654 | [Modules.STOCK_LOCATION]: { |
| 3655 | stock_location_id: location.id, |
| 3656 | }, |
| 3657 | }, |
| 3658 | { |
| 3659 | [Modules.PRODUCT]: { |
| 3660 | variant_id: product.variants[0].id, |
| 3661 | }, |
| 3662 | [Modules.INVENTORY]: { |
| 3663 | inventory_item_id: inventoryItem.id, |
| 3664 | }, |
| 3665 | }, |
| 3666 | ]) |
| 3667 | |
| 3668 | let cart = await cartModuleService.createCarts({ |
| 3669 | currency_code: "usd", |
| 3670 | sales_channel_id: salesChannel.id, |
| 3671 | items: [ |
| 3672 | { |
| 3673 | variant_id: product.variants[0].id, |
| 3674 | quantity: 1, |
| 3675 | unit_price: 5000, |
| 3676 | title: "Test item", |
| 3677 | }, |
| 3678 | ], |
| 3679 | }) |
| 3680 | |
| 3681 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 3682 | select: ["id", "region_id", "currency_code"], |
| 3683 | relations: ["items", "items.variant_id", "items.metadata"], |
| 3684 | }) |
| 3685 | |
| 3686 | const item = cart.items?.[0]! |
| 3687 | |
| 3688 | const { errors } = await updateLineItemInCartWorkflow( |
| 3689 | appContainer |
| 3690 | ).run({ |
| 3691 | input: { |
| 3692 | cart_id: cart.id, |
| 3693 | item_id: item.id, |
| 3694 | update: { |
| 3695 | metadata: { |
| 3696 | foo: "bar", |
| 3697 | }, |
| 3698 | quantity: 3, |
| 3699 | }, |
| 3700 | }, |
| 3701 | throwOnError: false, |
| 3702 | }) |
| 3703 | |
| 3704 | expect(errors).toEqual([ |
| 3705 | { |
| 3706 | action: "confirm-item-inventory-as-step", |
| 3707 | handlerType: "invoke", |
| 3708 | error: expect.objectContaining({ |
| 3709 | message: `Some variant does not have the required inventory`, |
| 3710 | }), |
| 3711 | }, |
| 3712 | ]) |
| 3713 | }) |
| 3714 | |
| 3715 | describe("compensation", () => { |
| 3716 | it("should revert line item update to original state", async () => { |
| 3717 | expect.assertions(2) |
| 3718 | const workflow = updateLineItemInCartWorkflow(appContainer) |
| 3719 | |
| 3720 | workflow.appendAction("throw", updateLineItemsStepId, { |
| 3721 | invoke: async function failStep() { |
| 3722 | throw new Error(`Failed to update something after line items`) |
| 3723 | }, |
| 3724 | }) |
| 3725 | |
| 3726 | const salesChannel = await scModuleService.createSalesChannels({ |
| 3727 | name: "Webshop", |
| 3728 | }) |
| 3729 | |
| 3730 | const location = await stockLocationModule.createStockLocations({ |
| 3731 | name: "Warehouse", |
| 3732 | }) |
| 3733 | |
| 3734 | const [product] = await productModule.createProducts([ |
| 3735 | { |
| 3736 | title: "Test product", |
| 3737 | variants: [ |
| 3738 | { |
| 3739 | title: "Test variant", |
| 3740 | }, |
| 3741 | ], |
| 3742 | }, |
| 3743 | ]) |
| 3744 | |
| 3745 | const inventoryItem = await inventoryModule.createInventoryItems({ |
| 3746 | sku: "inv-1234", |
| 3747 | }) |
| 3748 | |
| 3749 | await inventoryModule.createInventoryLevels([ |
| 3750 | { |
| 3751 | inventory_item_id: inventoryItem.id, |
| 3752 | location_id: location.id, |
| 3753 | stocked_quantity: 2, |
| 3754 | }, |
| 3755 | ]) |
| 3756 | |
| 3757 | const priceSet = await pricingModule.createPriceSets({ |
| 3758 | prices: [ |
| 3759 | { |
| 3760 | amount: 3000, |
| 3761 | currency_code: "usd", |
| 3762 | }, |
| 3763 | ], |
| 3764 | }) |
| 3765 | |
| 3766 | await remoteLink.create([ |
| 3767 | { |
| 3768 | [Modules.PRODUCT]: { |
| 3769 | variant_id: product.variants[0].id, |
| 3770 | }, |
| 3771 | [Modules.PRICING]: { |
| 3772 | price_set_id: priceSet.id, |
| 3773 | }, |
| 3774 | }, |
| 3775 | { |
| 3776 | [Modules.SALES_CHANNEL]: { |
| 3777 | sales_channel_id: salesChannel.id, |
| 3778 | }, |
| 3779 | [Modules.STOCK_LOCATION]: { |
| 3780 | stock_location_id: location.id, |
| 3781 | }, |
| 3782 | }, |
| 3783 | { |
| 3784 | [Modules.PRODUCT]: { |
| 3785 | variant_id: product.variants[0].id, |
| 3786 | }, |
| 3787 | [Modules.INVENTORY]: { |
| 3788 | inventory_item_id: inventoryItem.id, |
| 3789 | }, |
| 3790 | }, |
| 3791 | ]) |
| 3792 | |
| 3793 | let cart = await cartModuleService.createCarts({ |
| 3794 | sales_channel_id: salesChannel.id, |
| 3795 | currency_code: "usd", |
| 3796 | items: [ |
| 3797 | { |
| 3798 | variant_id: product.variants[0].id, |
| 3799 | quantity: 1, |
| 3800 | unit_price: 3000, |
| 3801 | title: "Test item", |
| 3802 | }, |
| 3803 | ], |
| 3804 | }) |
| 3805 | |
| 3806 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 3807 | select: ["id", "region_id", "currency_code"], |
| 3808 | relations: ["items", "items.variant_id", "items.metadata"], |
| 3809 | }) |
| 3810 | |
| 3811 | const item = cart.items?.[0]! |
| 3812 | |
| 3813 | const { errors } = await workflow.run({ |
| 3814 | input: { |
| 3815 | cart_id: cart.id, |
| 3816 | item_id: item.id, |
| 3817 | update: { |
| 3818 | metadata: { |
| 3819 | foo: "bar", |
| 3820 | }, |
| 3821 | title: "Test item updated", |
| 3822 | quantity: 2, |
| 3823 | }, |
| 3824 | }, |
| 3825 | throwOnError: false, |
| 3826 | }) |
| 3827 | |
| 3828 | expect(errors).toEqual([ |
| 3829 | { |
| 3830 | action: "throw", |
| 3831 | handlerType: "invoke", |
| 3832 | error: expect.objectContaining({ |
| 3833 | message: `Failed to update something after line items`, |
| 3834 | }), |
| 3835 | }, |
| 3836 | ]) |
| 3837 | |
| 3838 | const updatedItem = await cartModuleService.retrieveLineItem( |
| 3839 | item.id |
| 3840 | ) |
| 3841 | |
| 3842 | expect(updatedItem).toEqual( |
| 3843 | expect.objectContaining({ |
| 3844 | id: item.id, |
| 3845 | unit_price: 3000, |
| 3846 | quantity: 1, |
| 3847 | title: "Test item", |
| 3848 | }) |
| 3849 | ) |
| 3850 | }) |
| 3851 | }) |
| 3852 | }) |
| 3853 | |
| 3854 | describe("deleteLineItems", () => { |
| 3855 | it("should delete items in cart", async () => { |
| 3856 | const cart = await cartModuleService.createCarts({ |
| 3857 | currency_code: "usd", |
| 3858 | items: [ |
| 3859 | { |
| 3860 | quantity: 1, |
| 3861 | unit_price: 5000, |
| 3862 | title: "Test item", |
| 3863 | }, |
| 3864 | ], |
| 3865 | }) |
| 3866 | |
| 3867 | const items = await cartModuleService.listLineItems({ |
| 3868 | cart_id: cart.id, |
| 3869 | }) |
| 3870 | |
| 3871 | await deleteLineItemsWorkflow(appContainer).run({ |
| 3872 | input: { |
| 3873 | cart_id: cart.id, |
| 3874 | ids: items.map((i) => i.id), |
| 3875 | }, |
| 3876 | throwOnError: false, |
| 3877 | }) |
| 3878 | |
| 3879 | const [deletedItem] = await cartModuleService.listLineItems({ |
| 3880 | id: items.map((i) => i.id), |
| 3881 | }) |
| 3882 | |
| 3883 | expect(deletedItem).toBeUndefined() |
| 3884 | }) |
| 3885 | |
| 3886 | describe("compensation", () => { |
| 3887 | it("should restore line item if delete fails", async () => { |
| 3888 | const workflow = deleteLineItemsWorkflow(appContainer) |
| 3889 | |
| 3890 | workflow.appendAction("throw", deleteLineItemsStepId, { |
| 3891 | invoke: async function failStep() { |
| 3892 | throw new Error( |
| 3893 | `Failed to do something after deleting line items` |
| 3894 | ) |
| 3895 | }, |
| 3896 | }) |
| 3897 | |
| 3898 | const cart = await cartModuleService.createCarts({ |
| 3899 | currency_code: "usd", |
| 3900 | items: [ |
| 3901 | { |
| 3902 | quantity: 1, |
| 3903 | unit_price: 3000, |
| 3904 | title: "Test item", |
| 3905 | }, |
| 3906 | ], |
| 3907 | }) |
| 3908 | |
| 3909 | const items = await cartModuleService.listLineItems({ |
| 3910 | cart_id: cart.id, |
| 3911 | }) |
| 3912 | |
| 3913 | const { errors } = await workflow.run({ |
| 3914 | input: { |
| 3915 | cart_id: cart.id, |
| 3916 | ids: items.map((i) => i.id), |
| 3917 | }, |
| 3918 | throwOnError: false, |
| 3919 | }) |
| 3920 | |
| 3921 | expect(errors).toEqual([ |
| 3922 | { |
| 3923 | action: "throw", |
| 3924 | handlerType: "invoke", |
| 3925 | error: expect.objectContaining({ |
| 3926 | message: `Failed to do something after deleting line items`, |
| 3927 | }), |
| 3928 | }, |
| 3929 | ]) |
| 3930 | |
| 3931 | const updatedItem = await cartModuleService.retrieveLineItem( |
| 3932 | items[0].id |
| 3933 | ) |
| 3934 | |
| 3935 | expect(updatedItem).not.toBeUndefined() |
| 3936 | }) |
| 3937 | }) |
| 3938 | }) |
| 3939 | |
| 3940 | describe("createPaymentCollectionForCart", () => { |
| 3941 | it("should create a payment collection and link it to cart", async () => { |
| 3942 | const cart = await cartModuleService.createCarts({ |
| 3943 | currency_code: "dkk", |
| 3944 | region_id: defaultRegion.id, |
| 3945 | items: [ |
| 3946 | { |
| 3947 | quantity: 1, |
| 3948 | unit_price: 5000, |
| 3949 | title: "Test item", |
| 3950 | }, |
| 3951 | ], |
| 3952 | }) |
| 3953 | |
| 3954 | await createPaymentCollectionForCartWorkflow(appContainer).run({ |
| 3955 | input: { |
| 3956 | cart_id: cart.id, |
| 3957 | }, |
| 3958 | throwOnError: false, |
| 3959 | }) |
| 3960 | |
| 3961 | const result = await remoteQuery( |
| 3962 | { |
| 3963 | cart: { |
| 3964 | fields: ["id"], |
| 3965 | payment_collection: { |
| 3966 | fields: ["id", "amount", "currency_code"], |
| 3967 | }, |
| 3968 | }, |
| 3969 | }, |
| 3970 | { |
| 3971 | cart: { |
| 3972 | id: cart.id, |
| 3973 | }, |
| 3974 | } |
| 3975 | ) |
| 3976 | |
| 3977 | expect(result).toEqual([ |
| 3978 | expect.objectContaining({ |
| 3979 | id: cart.id, |
| 3980 | payment_collection: expect.objectContaining({ |
| 3981 | amount: 5000, |
| 3982 | currency_code: "dkk", |
| 3983 | }), |
| 3984 | }), |
| 3985 | ]) |
| 3986 | }) |
| 3987 | |
| 3988 | describe("compensation", () => { |
| 3989 | it("should dismiss cart <> payment collection link and delete created payment collection", async () => { |
| 3990 | const workflow = |
| 3991 | createPaymentCollectionForCartWorkflow(appContainer) |
| 3992 | |
| 3993 | workflow.addAction("throw", { |
| 3994 | invoke: async function failStep() { |
| 3995 | throw new Error( |
| 3996 | `Failed to do something after linking cart and payment collection` |
| 3997 | ) |
| 3998 | }, |
| 3999 | }) |
| 4000 | |
| 4001 | const region = await regionModuleService.createRegions({ |
| 4002 | name: "US", |
| 4003 | currency_code: "usd", |
| 4004 | }) |
| 4005 | |
| 4006 | const cart = await cartModuleService.createCarts({ |
| 4007 | currency_code: "usd", |
| 4008 | region_id: region.id, |
| 4009 | items: [ |
| 4010 | { |
| 4011 | quantity: 1, |
| 4012 | unit_price: 5000, |
| 4013 | title: "Test item", |
| 4014 | }, |
| 4015 | ], |
| 4016 | }) |
| 4017 | |
| 4018 | const { errors } = await workflow.run({ |
| 4019 | input: { |
| 4020 | cart_id: cart.id, |
| 4021 | }, |
| 4022 | throwOnError: false, |
| 4023 | }) |
| 4024 | |
| 4025 | expect(errors).toEqual([ |
| 4026 | { |
| 4027 | action: "throw", |
| 4028 | handlerType: "invoke", |
| 4029 | error: expect.objectContaining({ |
| 4030 | message: `Failed to do something after linking cart and payment collection`, |
| 4031 | }), |
| 4032 | }, |
| 4033 | ]) |
| 4034 | |
| 4035 | const carts = await remoteQuery( |
| 4036 | { |
| 4037 | cart: { |
| 4038 | fields: ["id"], |
| 4039 | payment_collection: { |
| 4040 | fields: ["id", "amount", "currency_code"], |
| 4041 | }, |
| 4042 | }, |
| 4043 | }, |
| 4044 | { |
| 4045 | cart: { |
| 4046 | id: cart.id, |
| 4047 | }, |
| 4048 | } |
| 4049 | ) |
| 4050 | |
| 4051 | const payCols = await remoteQuery({ |
| 4052 | payment_collection: { |
| 4053 | fields: ["id"], |
| 4054 | }, |
| 4055 | }) |
| 4056 | |
| 4057 | expect(carts).toEqual([ |
| 4058 | expect.objectContaining({ |
| 4059 | id: cart.id, |
| 4060 | payment_collection: undefined, |
| 4061 | }), |
| 4062 | ]) |
| 4063 | expect(payCols.length).toEqual(0) |
| 4064 | }) |
| 4065 | }) |
| 4066 | }) |
| 4067 | |
| 4068 | describe("refreshPaymentCollectionForCart", () => { |
| 4069 | it("should refresh a payment collection for a cart", async () => { |
| 4070 | const cart = await cartModuleService.createCarts({ |
| 4071 | currency_code: "dkk", |
| 4072 | region_id: defaultRegion.id, |
| 4073 | items: [ |
| 4074 | { |
| 4075 | quantity: 1, |
| 4076 | unit_price: 5000, |
| 4077 | title: "Test item", |
| 4078 | }, |
| 4079 | ], |
| 4080 | }) |
| 4081 | |
| 4082 | const paymentCollection = |
| 4083 | await paymentModule.createPaymentCollections({ |
| 4084 | amount: 5001, |
| 4085 | currency_code: "dkk", |
| 4086 | }) |
| 4087 | |
| 4088 | const paymentSession = await paymentModule.createPaymentSession( |
| 4089 | paymentCollection.id, |
| 4090 | { |
| 4091 | amount: 5001, |
| 4092 | currency_code: "dkk", |
| 4093 | data: {}, |
| 4094 | provider_id: "pp_system_default", |
| 4095 | } |
| 4096 | ) |
| 4097 | |
| 4098 | await remoteLink.create([ |
| 4099 | { |
| 4100 | [Modules.CART]: { |
| 4101 | cart_id: cart.id, |
| 4102 | }, |
| 4103 | [Modules.PAYMENT]: { |
| 4104 | payment_collection_id: paymentCollection.id, |
| 4105 | }, |
| 4106 | }, |
| 4107 | ]) |
| 4108 | |
| 4109 | await refreshPaymentCollectionForCartWorkflow(appContainer).run({ |
| 4110 | input: { |
| 4111 | cart_id: cart.id, |
| 4112 | }, |
| 4113 | throwOnError: false, |
| 4114 | }) |
| 4115 | |
| 4116 | const updatedPaymentCollection = |
| 4117 | await paymentModule.retrievePaymentCollection(paymentCollection.id) |
| 4118 | |
| 4119 | expect(updatedPaymentCollection).toEqual( |
| 4120 | expect.objectContaining({ |
| 4121 | id: paymentCollection.id, |
| 4122 | amount: 5000, |
| 4123 | }) |
| 4124 | ) |
| 4125 | |
| 4126 | const sessionShouldNotExist = await paymentModule.listPaymentSessions( |
| 4127 | { id: paymentSession.id }, |
| 4128 | { withDeleted: true } |
| 4129 | ) |
| 4130 | |
| 4131 | expect(sessionShouldNotExist).toHaveLength(0) |
| 4132 | }) |
| 4133 | |
| 4134 | describe("compensation", () => { |
| 4135 | it("should revert payment collection amount and create a new payment session", async () => { |
| 4136 | const region = await regionModuleService.createRegions({ |
| 4137 | name: "US", |
| 4138 | currency_code: "usd", |
| 4139 | }) |
| 4140 | |
| 4141 | const testCart = await cartModuleService.createCarts({ |
| 4142 | currency_code: "usd", |
| 4143 | region_id: region.id, |
| 4144 | items: [ |
| 4145 | { |
| 4146 | quantity: 1, |
| 4147 | unit_price: 5000, |
| 4148 | title: "Test item", |
| 4149 | }, |
| 4150 | ], |
| 4151 | }) |
| 4152 | |
| 4153 | const paymentCollection = |
| 4154 | await paymentModule.createPaymentCollections({ |
| 4155 | amount: 5000, |
| 4156 | currency_code: "dkk", |
| 4157 | }) |
| 4158 | |
| 4159 | const paymentSession = await paymentModule.createPaymentSession( |
| 4160 | paymentCollection.id, |
| 4161 | { |
| 4162 | amount: 5000, |
| 4163 | currency_code: "dkk", |
| 4164 | data: {}, |
| 4165 | provider_id: "pp_system_default", |
| 4166 | } |
| 4167 | ) |
| 4168 | |
| 4169 | await remoteLink.create([ |
| 4170 | { |
| 4171 | [Modules.CART]: { |
| 4172 | cart_id: testCart.id, |
| 4173 | }, |
| 4174 | [Modules.PAYMENT]: { |
| 4175 | payment_collection_id: paymentCollection.id, |
| 4176 | }, |
| 4177 | }, |
| 4178 | ]) |
| 4179 | |
| 4180 | const workflow = |
| 4181 | refreshPaymentCollectionForCartWorkflow(appContainer) |
| 4182 | |
| 4183 | workflow.appendAction("throw", updatePaymentCollectionStepId, { |
| 4184 | invoke: async function failStep() { |
| 4185 | throw new Error( |
| 4186 | `Failed to do something after updating payment collections` |
| 4187 | ) |
| 4188 | }, |
| 4189 | }) |
| 4190 | |
| 4191 | const { errors } = await workflow.run({ |
| 4192 | input: { |
| 4193 | cart_id: testCart.id, |
| 4194 | }, |
| 4195 | throwOnError: false, |
| 4196 | }) |
| 4197 | |
| 4198 | expect(errors).toEqual([ |
| 4199 | { |
| 4200 | action: "throw", |
| 4201 | handlerType: "invoke", |
| 4202 | error: expect.objectContaining({ |
| 4203 | message: `Failed to do something after updating payment collections`, |
| 4204 | }), |
| 4205 | }, |
| 4206 | ]) |
| 4207 | |
| 4208 | const updatedPaymentCollection = |
| 4209 | await paymentModule.retrievePaymentCollection( |
| 4210 | paymentCollection.id, |
| 4211 | { |
| 4212 | relations: ["payment_sessions"], |
| 4213 | } |
| 4214 | ) |
| 4215 | |
| 4216 | const sessions = await paymentModule.listPaymentSessions({ |
| 4217 | payment_collection_id: paymentCollection.id, |
| 4218 | }) |
| 4219 | |
| 4220 | expect(sessions).toHaveLength(1) |
| 4221 | expect(sessions[0].id).not.toEqual(paymentSession.id) |
| 4222 | expect(sessions[0]).toEqual( |
| 4223 | expect.objectContaining({ |
| 4224 | id: expect.any(String), |
| 4225 | amount: 5000, |
| 4226 | currency_code: "dkk", |
| 4227 | }) |
| 4228 | ) |
| 4229 | expect(updatedPaymentCollection).toEqual( |
| 4230 | expect.objectContaining({ |
| 4231 | id: paymentCollection.id, |
| 4232 | amount: 5000, |
| 4233 | }) |
| 4234 | ) |
| 4235 | }) |
| 4236 | }) |
| 4237 | }) |
| 4238 | |
| 4239 | describe("AddShippingMethodToCartWorkflow", () => { |
| 4240 | let cart |
| 4241 | let shippingProfile |
| 4242 | let fulfillmentSet |
| 4243 | let priceSet |
| 4244 | let region |
| 4245 | let stockLocation |
| 4246 | |
| 4247 | beforeEach(async () => { |
| 4248 | region = ( |
| 4249 | await api.post( |
| 4250 | "/admin/regions", |
| 4251 | { |
| 4252 | name: "test-region", |
| 4253 | currency_code: "usd", |
| 4254 | countries: ["us"], |
| 4255 | }, |
| 4256 | adminHeaders |
| 4257 | ) |
| 4258 | ).data.region |
| 4259 | |
| 4260 | stockLocation = ( |
| 4261 | await api.post( |
| 4262 | `/admin/stock-locations`, |
| 4263 | { name: "test location" }, |
| 4264 | adminHeaders |
| 4265 | ) |
| 4266 | ).data.stock_location |
| 4267 | |
| 4268 | await api.post( |
| 4269 | `/admin/stock-locations/${stockLocation.id}/sales-channels`, |
| 4270 | { add: [salesChannel.id] }, |
| 4271 | adminHeaders |
| 4272 | ) |
| 4273 | |
| 4274 | shippingProfile = ( |
| 4275 | await api.post( |
| 4276 | `/admin/shipping-profiles`, |
| 4277 | { name: "Test", type: "default" }, |
| 4278 | adminHeaders |
| 4279 | ) |
| 4280 | ).data.shipping_profile |
| 4281 | |
| 4282 | const fulfillmentSets = ( |
| 4283 | await api.post( |
| 4284 | `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`, |
| 4285 | { |
| 4286 | name: "Test", |
| 4287 | type: "test-type", |
| 4288 | }, |
| 4289 | adminHeaders |
| 4290 | ) |
| 4291 | ).data.stock_location.fulfillment_sets |
| 4292 | |
| 4293 | fulfillmentSet = ( |
| 4294 | await api.post( |
| 4295 | `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`, |
| 4296 | { |
| 4297 | name: "Test", |
| 4298 | geo_zones: [{ type: "country", country_code: "us" }], |
| 4299 | }, |
| 4300 | adminHeaders |
| 4301 | ) |
| 4302 | ).data.fulfillment_set |
| 4303 | |
| 4304 | await api.post( |
| 4305 | `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`, |
| 4306 | { add: ["manual_test-provider"] }, |
| 4307 | adminHeaders |
| 4308 | ) |
| 4309 | |
| 4310 | cart = ( |
| 4311 | await api.post( |
| 4312 | `/store/carts`, |
| 4313 | { |
| 4314 | currency_code: "usd", |
| 4315 | region_id: region.id, |
| 4316 | sales_channel_id: salesChannel.id, |
| 4317 | }, |
| 4318 | storeHeaders |
| 4319 | ) |
| 4320 | ).data.cart |
| 4321 | |
| 4322 | await api.post( |
| 4323 | "/admin/price-preferences", |
| 4324 | { |
| 4325 | attribute: "currency_code", |
| 4326 | value: "usd", |
| 4327 | is_tax_inclusive: true, |
| 4328 | }, |
| 4329 | adminHeaders |
| 4330 | ) |
| 4331 | }) |
| 4332 | |
| 4333 | it("should add shipping method to cart", async () => { |
| 4334 | const shippingOption = ( |
| 4335 | await api.post( |
| 4336 | `/admin/shipping-options`, |
| 4337 | { |
| 4338 | name: "Test shipping option", |
| 4339 | service_zone_id: fulfillmentSet.service_zones[0].id, |
| 4340 | shipping_profile_id: shippingProfile.id, |
| 4341 | provider_id: "manual_test-provider", |
| 4342 | price_type: "flat", |
| 4343 | type: { |
| 4344 | label: "Test type", |
| 4345 | description: "Test description", |
| 4346 | code: "test-code", |
| 4347 | }, |
| 4348 | prices: [{ amount: 3000, currency_code: "usd" }], |
| 4349 | rules: [ |
| 4350 | { |
| 4351 | operator: RuleOperator.EQ, |
| 4352 | attribute: "is_return", |
| 4353 | value: "false", |
| 4354 | }, |
| 4355 | { |
| 4356 | operator: RuleOperator.EQ, |
| 4357 | attribute: "enabled_in_store", |
| 4358 | value: "true", |
| 4359 | }, |
| 4360 | ], |
| 4361 | }, |
| 4362 | adminHeaders |
| 4363 | ) |
| 4364 | ).data.shipping_option |
| 4365 | |
| 4366 | await addShippingMethodToCartWorkflow(appContainer).run({ |
| 4367 | input: { |
| 4368 | options: [{ id: shippingOption.id }], |
| 4369 | cart_id: cart.id, |
| 4370 | }, |
| 4371 | }) |
| 4372 | |
| 4373 | cart = (await api.get(`/store/carts/${cart.id}`, storeHeaders)).data |
| 4374 | .cart |
| 4375 | |
| 4376 | expect(normalizeBigNumbers(cart)).toEqual( |
| 4377 | expect.objectContaining({ |
| 4378 | id: cart.id, |
| 4379 | currency_code: "usd", |
| 4380 | shipping_methods: [ |
| 4381 | expect.objectContaining({ |
| 4382 | amount: 3000, |
| 4383 | is_tax_inclusive: true, |
| 4384 | }), |
| 4385 | ], |
| 4386 | }) |
| 4387 | ) |
| 4388 | }) |
| 4389 | |
| 4390 | it("should add shipping method to cart using custom rules to fetch", async () => { |
| 4391 | const shippingOption = ( |
| 4392 | await api.post( |
| 4393 | `/admin/shipping-options`, |
| 4394 | { |
| 4395 | name: "Test shipping option 1", |
| 4396 | service_zone_id: fulfillmentSet.service_zones[0].id, |
| 4397 | shipping_profile_id: shippingProfile.id, |
| 4398 | provider_id: "manual_test-provider", |
| 4399 | price_type: "flat", |
| 4400 | type: { |
| 4401 | label: "Test type", |
| 4402 | description: "Test description", |
| 4403 | code: "test-code", |
| 4404 | }, |
| 4405 | prices: [{ amount: 3_000, currency_code: "usd" }], |
| 4406 | rules: [ |
| 4407 | { |
| 4408 | operator: RuleOperator.EQ, |
| 4409 | attribute: "is_return", |
| 4410 | value: "false", |
| 4411 | }, |
| 4412 | { |
| 4413 | operator: RuleOperator.EQ, |
| 4414 | attribute: "enabled_in_store", |
| 4415 | value: "true", |
| 4416 | }, |
| 4417 | { |
| 4418 | operator: RuleOperator.EQ, |
| 4419 | attribute: "customer_status", |
| 4420 | value: "vip", |
| 4421 | }, |
| 4422 | ], |
| 4423 | }, |
| 4424 | adminHeaders |
| 4425 | ) |
| 4426 | ).data.shipping_option |
| 4427 | |
| 4428 | setShippingOptionsContextHook = function () { |
| 4429 | return new StepResponse({ |
| 4430 | customer_status: "vip", |
| 4431 | }) |
| 4432 | } |
| 4433 | |
| 4434 | await addShippingMethodToCartWorkflow(appContainer).run({ |
| 4435 | input: { |
| 4436 | options: [{ id: shippingOption.id }], |
| 4437 | cart_id: cart.id, |
| 4438 | }, |
| 4439 | }) |
| 4440 | |
| 4441 | cart = (await api.get(`/store/carts/${cart.id}`, storeHeaders)).data |
| 4442 | .cart |
| 4443 | |
| 4444 | expect(normalizeBigNumbers(cart)).toEqual( |
| 4445 | expect.objectContaining({ |
| 4446 | id: cart.id, |
| 4447 | currency_code: "usd", |
| 4448 | shipping_methods: [ |
| 4449 | expect.objectContaining({ |
| 4450 | amount: 3_000, |
| 4451 | is_tax_inclusive: true, |
| 4452 | }), |
| 4453 | ], |
| 4454 | }) |
| 4455 | ) |
| 4456 | }) |
| 4457 | |
| 4458 | it("should throw error when shipping option is not valid", async () => { |
| 4459 | const shippingOption = ( |
| 4460 | await api.post( |
| 4461 | `/admin/shipping-options`, |
| 4462 | { |
| 4463 | name: "Test shipping option", |
| 4464 | service_zone_id: fulfillmentSet.service_zones[0].id, |
| 4465 | shipping_profile_id: shippingProfile.id, |
| 4466 | provider_id: "manual_test-provider", |
| 4467 | price_type: "flat", |
| 4468 | type: { |
| 4469 | label: "Test type", |
| 4470 | description: "Test description", |
| 4471 | code: "test-code", |
| 4472 | }, |
| 4473 | rules: [ |
| 4474 | { |
| 4475 | operator: RuleOperator.EQ, |
| 4476 | attribute: "shipping_address.city", |
| 4477 | value: "sf", |
| 4478 | }, |
| 4479 | ], |
| 4480 | prices: [{ amount: 3000, currency_code: "usd" }], |
| 4481 | }, |
| 4482 | adminHeaders |
| 4483 | ) |
| 4484 | ).data.shipping_option |
| 4485 | |
| 4486 | const { errors } = await addShippingMethodToCartWorkflow( |
| 4487 | appContainer |
| 4488 | ).run({ |
| 4489 | input: { options: [{ id: shippingOption.id }], cart_id: cart.id }, |
| 4490 | throwOnError: false, |
| 4491 | }) |
| 4492 | |
| 4493 | // Rules are setup only for Germany, this should throw an error |
| 4494 | expect(errors).toEqual([ |
| 4495 | expect.objectContaining({ |
| 4496 | error: expect.objectContaining({ |
| 4497 | message: `Shipping Options are invalid for cart.`, |
| 4498 | type: "invalid_data", |
| 4499 | }), |
| 4500 | }), |
| 4501 | ]) |
| 4502 | }) |
| 4503 | |
| 4504 | it("should throw error when shipping option is not present in the db", async () => { |
| 4505 | const { errors } = await addShippingMethodToCartWorkflow( |
| 4506 | appContainer |
| 4507 | ).run({ |
| 4508 | input: { |
| 4509 | options: [{ id: "does-not-exist" }], |
| 4510 | cart_id: cart.id, |
| 4511 | }, |
| 4512 | throwOnError: false, |
| 4513 | }) |
| 4514 | |
| 4515 | // Rules are setup only for Berlin, this should throw an error |
| 4516 | expect(errors).toEqual([ |
| 4517 | expect.objectContaining({ |
| 4518 | error: expect.objectContaining({ |
| 4519 | message: "Shipping Options are invalid for cart.", |
| 4520 | type: "invalid_data", |
| 4521 | }), |
| 4522 | }), |
| 4523 | ]) |
| 4524 | }) |
| 4525 | |
| 4526 | it("should add shipping method with custom data", async () => { |
| 4527 | const shippingOption = ( |
| 4528 | await api.post( |
| 4529 | `/admin/shipping-options`, |
| 4530 | { |
| 4531 | name: "Test shipping option", |
| 4532 | service_zone_id: fulfillmentSet.service_zones[0].id, |
| 4533 | shipping_profile_id: shippingProfile.id, |
| 4534 | provider_id: "manual_test-provider", |
| 4535 | price_type: "flat", |
| 4536 | type: { |
| 4537 | label: "Test type", |
| 4538 | description: "Test description", |
| 4539 | code: "test-code", |
| 4540 | }, |
| 4541 | rules: [ |
| 4542 | { |
| 4543 | operator: RuleOperator.EQ, |
| 4544 | attribute: "is_return", |
| 4545 | value: "false", |
| 4546 | }, |
| 4547 | { |
| 4548 | operator: RuleOperator.EQ, |
| 4549 | attribute: "enabled_in_store", |
| 4550 | value: "true", |
| 4551 | }, |
| 4552 | ], |
| 4553 | prices: [{ amount: 3000, currency_code: "usd" }], |
| 4554 | }, |
| 4555 | adminHeaders |
| 4556 | ) |
| 4557 | ).data.shipping_option |
| 4558 | |
| 4559 | await addShippingMethodToCartWorkflow(appContainer).run({ |
| 4560 | input: { |
| 4561 | options: [{ id: shippingOption.id, data: { test: "test" } }], |
| 4562 | cart_id: cart.id, |
| 4563 | }, |
| 4564 | }) |
| 4565 | |
| 4566 | cart = ( |
| 4567 | await api.get( |
| 4568 | `/store/carts/${cart.id}?fields=+shipping_methods.data`, |
| 4569 | storeHeaders |
| 4570 | ) |
| 4571 | ).data.cart |
| 4572 | |
| 4573 | expect(normalizeBigNumbers(cart)).toEqual( |
| 4574 | expect.objectContaining({ |
| 4575 | id: cart.id, |
| 4576 | shipping_methods: [ |
| 4577 | expect.objectContaining({ |
| 4578 | amount: 3000, |
| 4579 | is_tax_inclusive: true, |
| 4580 | data: { test: "test" }, |
| 4581 | shipping_option_id: shippingOption.id, |
| 4582 | }), |
| 4583 | ], |
| 4584 | }) |
| 4585 | ) |
| 4586 | }) |
| 4587 | }) |
| 4588 | |
| 4589 | describe("listShippingOptionsForCartWorkflow", () => { |
| 4590 | let cart |
| 4591 | let shippingProfile |
| 4592 | let fulfillmentSet |
| 4593 | let region |
| 4594 | let stockLocation |
| 4595 | |
| 4596 | beforeEach(async () => { |
| 4597 | region = ( |
| 4598 | await api.post( |
| 4599 | "/admin/regions", |
| 4600 | { |
| 4601 | name: "test-region", |
| 4602 | currency_code: "usd", |
| 4603 | countries: ["us"], |
| 4604 | }, |
| 4605 | adminHeaders |
| 4606 | ) |
| 4607 | ).data.region |
| 4608 | |
| 4609 | stockLocation = ( |
| 4610 | await api.post( |
| 4611 | `/admin/stock-locations`, |
| 4612 | { name: "test location" }, |
| 4613 | adminHeaders |
| 4614 | ) |
| 4615 | ).data.stock_location |
| 4616 | |
| 4617 | await api.post( |
| 4618 | `/admin/stock-locations/${stockLocation.id}/sales-channels`, |
| 4619 | { add: [salesChannel.id] }, |
| 4620 | adminHeaders |
| 4621 | ) |
| 4622 | |
| 4623 | shippingProfile = ( |
| 4624 | await api.post( |
| 4625 | `/admin/shipping-profiles`, |
| 4626 | { name: "Test", type: "default" }, |
| 4627 | adminHeaders |
| 4628 | ) |
| 4629 | ).data.shipping_profile |
| 4630 | |
| 4631 | const fulfillmentSets = ( |
| 4632 | await api.post( |
| 4633 | `/admin/stock-locations/${stockLocation.id}/fulfillment-sets?fields=*fulfillment_sets`, |
| 4634 | { |
| 4635 | name: "Test", |
| 4636 | type: "test-type", |
| 4637 | }, |
| 4638 | adminHeaders |
| 4639 | ) |
| 4640 | ).data.stock_location.fulfillment_sets |
| 4641 | |
| 4642 | fulfillmentSet = ( |
| 4643 | await api.post( |
| 4644 | `/admin/fulfillment-sets/${fulfillmentSets[0].id}/service-zones`, |
| 4645 | { |
| 4646 | name: "Test", |
| 4647 | geo_zones: [{ type: "country", country_code: "us" }], |
| 4648 | }, |
| 4649 | adminHeaders |
| 4650 | ) |
| 4651 | ).data.fulfillment_set |
| 4652 | |
| 4653 | await api.post( |
| 4654 | `/admin/stock-locations/${stockLocation.id}/fulfillment-providers`, |
| 4655 | { add: ["manual_test-provider"] }, |
| 4656 | adminHeaders |
| 4657 | ) |
| 4658 | |
| 4659 | cart = ( |
| 4660 | await api.post( |
| 4661 | `/store/carts`, |
| 4662 | { |
| 4663 | currency_code: "usd", |
| 4664 | region_id: region.id, |
| 4665 | sales_channel_id: salesChannel.id, |
| 4666 | }, |
| 4667 | storeHeaders |
| 4668 | ) |
| 4669 | ).data.cart |
| 4670 | |
| 4671 | await api.post( |
| 4672 | "/admin/price-preferences", |
| 4673 | { |
| 4674 | attribute: "currency_code", |
| 4675 | value: "usd", |
| 4676 | is_tax_inclusive: true, |
| 4677 | }, |
| 4678 | adminHeaders |
| 4679 | ) |
| 4680 | }) |
| 4681 | |
| 4682 | it("should list shipping options for cart", async () => { |
| 4683 | const shippingOption = ( |
| 4684 | await api.post( |
| 4685 | `/admin/shipping-options`, |
| 4686 | { |
| 4687 | name: "Test shipping option", |
| 4688 | service_zone_id: fulfillmentSet.service_zones[0].id, |
| 4689 | shipping_profile_id: shippingProfile.id, |
| 4690 | provider_id: "manual_test-provider", |
| 4691 | price_type: "flat", |
| 4692 | type: { |
| 4693 | label: "Test type", |
| 4694 | description: "Test description", |
| 4695 | code: "test-code", |
| 4696 | }, |
| 4697 | prices: [ |
| 4698 | { |
| 4699 | amount: 3000, |
| 4700 | currency_code: "usd", |
| 4701 | }, |
| 4702 | ], |
| 4703 | rules: [ |
| 4704 | { |
| 4705 | operator: RuleOperator.EQ, |
| 4706 | attribute: "is_return", |
| 4707 | value: "false", |
| 4708 | }, |
| 4709 | { |
| 4710 | operator: RuleOperator.EQ, |
| 4711 | attribute: "enabled_in_store", |
| 4712 | value: "true", |
| 4713 | }, |
| 4714 | ], |
| 4715 | }, |
| 4716 | adminHeaders |
| 4717 | ) |
| 4718 | ).data.shipping_option |
| 4719 | |
| 4720 | cart = (await api.get(`/store/carts/${cart.id}`, storeHeaders)).data |
| 4721 | .cart |
| 4722 | |
| 4723 | const { result } = await listShippingOptionsForCartWorkflow( |
| 4724 | appContainer |
| 4725 | ).run({ input: { cart_id: cart.id } }) |
| 4726 | |
| 4727 | expect(result).toEqual([ |
| 4728 | expect.objectContaining({ |
| 4729 | amount: 3000, |
| 4730 | id: shippingOption.id, |
| 4731 | }), |
| 4732 | ]) |
| 4733 | }) |
| 4734 | |
| 4735 | it("should exclude return shipping options for cart by default", async () => { |
| 4736 | const salesChannel = await scModuleService.createSalesChannels({ |
| 4737 | name: "Webshop", |
| 4738 | }) |
| 4739 | |
| 4740 | let cart = await cartModuleService.createCarts({ |
| 4741 | currency_code: "usd", |
| 4742 | region_id: region.id, |
| 4743 | sales_channel_id: salesChannel.id, |
| 4744 | shipping_address: { |
| 4745 | country_code: "us", |
| 4746 | }, |
| 4747 | }) |
| 4748 | |
| 4749 | const shippingOption = await fulfillmentModule.createShippingOptions([ |
| 4750 | { |
| 4751 | name: "Return shipping option", |
| 4752 | service_zone_id: fulfillmentSet.service_zones[0].id, |
| 4753 | shipping_profile_id: shippingProfile.id, |
| 4754 | provider_id: "manual_test-provider", |
| 4755 | rules: [ |
| 4756 | { |
| 4757 | operator: RuleOperator.EQ, |
| 4758 | attribute: "is_return", |
| 4759 | value: "true", |
| 4760 | }, |
| 4761 | ], |
| 4762 | price_type: "flat", |
| 4763 | type: { |
| 4764 | label: "Test type", |
| 4765 | description: "Test description", |
| 4766 | code: "test-code", |
| 4767 | }, |
| 4768 | }, |
| 4769 | { |
| 4770 | name: "Test shipping option", |
| 4771 | service_zone_id: fulfillmentSet.service_zones[0].id, |
| 4772 | shipping_profile_id: shippingProfile.id, |
| 4773 | provider_id: "manual_test-provider", |
| 4774 | price_type: "flat", |
| 4775 | type: { |
| 4776 | label: "Test type", |
| 4777 | description: "Test description", |
| 4778 | code: "test-code", |
| 4779 | }, |
| 4780 | }, |
| 4781 | ]) |
| 4782 | |
| 4783 | const priceSet = await pricingModule.createPriceSets({ |
| 4784 | prices: [ |
| 4785 | { |
| 4786 | amount: 3000, |
| 4787 | currency_code: "usd", |
| 4788 | }, |
| 4789 | ], |
| 4790 | }) |
| 4791 | |
| 4792 | const priceSetTwo = await pricingModule.createPriceSets({ |
| 4793 | prices: [ |
| 4794 | { |
| 4795 | amount: 3000, |
| 4796 | currency_code: "usd", |
| 4797 | }, |
| 4798 | ], |
| 4799 | }) |
| 4800 | |
| 4801 | await remoteLink.create([ |
| 4802 | { |
| 4803 | [Modules.SALES_CHANNEL]: { |
| 4804 | sales_channel_id: salesChannel.id, |
| 4805 | }, |
| 4806 | [Modules.STOCK_LOCATION]: { |
| 4807 | stock_location_id: stockLocation.id, |
| 4808 | }, |
| 4809 | }, |
| 4810 | { |
| 4811 | [Modules.STOCK_LOCATION]: { |
| 4812 | stock_location_id: stockLocation.id, |
| 4813 | }, |
| 4814 | [Modules.FULFILLMENT]: { |
| 4815 | fulfillment_set_id: fulfillmentSet.id, |
| 4816 | }, |
| 4817 | }, |
| 4818 | { |
| 4819 | [Modules.FULFILLMENT]: { |
| 4820 | shipping_option_id: shippingOption.find( |
| 4821 | (o) => o.name === "Test shipping option" |
| 4822 | )?.id, |
| 4823 | }, |
| 4824 | [Modules.PRICING]: { |
| 4825 | price_set_id: priceSet.id, |
| 4826 | }, |
| 4827 | }, |
| 4828 | { |
| 4829 | [Modules.FULFILLMENT]: { |
| 4830 | shipping_option_id: shippingOption.find( |
| 4831 | (o) => o.name === "Return shipping option" |
| 4832 | )?.id, |
| 4833 | }, |
| 4834 | [Modules.PRICING]: { |
| 4835 | price_set_id: priceSetTwo.id, |
| 4836 | }, |
| 4837 | }, |
| 4838 | ]) |
| 4839 | |
| 4840 | cart = await cartModuleService.retrieveCart(cart.id, { |
| 4841 | select: ["id"], |
| 4842 | relations: ["shipping_address"], |
| 4843 | }) |
| 4844 | |
| 4845 | const { result } = await listShippingOptionsForCartWorkflow( |
| 4846 | appContainer |
| 4847 | ).run({ |
| 4848 | input: { |
| 4849 | cart_id: cart.id, |
| 4850 | }, |
| 4851 | }) |
| 4852 | |
| 4853 | expect(result.length).toEqual(1) |
| 4854 | expect(result).toEqual([ |
| 4855 | expect.objectContaining({ |
| 4856 | name: "Test shipping option", |
| 4857 | }), |
| 4858 | ]) |
| 4859 | }) |
| 4860 | |
| 4861 | it("should list no shipping options for cart, if sales channel is not associated with location", async () => { |
| 4862 | const salesChannel = await scModuleService.createSalesChannels({ |
| 4863 | name: "Webshop", |
| 4864 | }) |
| 4865 | |
| 4866 | await api.post( |
| 4867 | `/store/carts/${cart.id}`, |
| 4868 | { sales_channel_id: salesChannel.id }, |
| 4869 | storeHeaders |
| 4870 | ) |
| 4871 | |
| 4872 | await api.post( |
| 4873 | `/admin/shipping-options`, |
| 4874 | { |
| 4875 | name: "Test shipping option", |
| 4876 | service_zone_id: fulfillmentSet.service_zones[0].id, |
| 4877 | shipping_profile_id: shippingProfile.id, |
| 4878 | provider_id: "manual_test-provider", |
| 4879 | price_type: "flat", |
| 4880 | type: { |
| 4881 | label: "Test type", |
| 4882 | description: "Test description", |
| 4883 | code: "test-code", |
| 4884 | }, |
| 4885 | prices: [ |
| 4886 | { |
| 4887 | amount: 3000, |
| 4888 | currency_code: "usd", |
| 4889 | }, |
| 4890 | ], |
| 4891 | rules: [ |
| 4892 | { |
| 4893 | operator: RuleOperator.EQ, |
| 4894 | attribute: "is_return", |
| 4895 | value: "false", |
| 4896 | }, |
| 4897 | { |
| 4898 | operator: RuleOperator.EQ, |
| 4899 | attribute: "enabled_in_store", |
| 4900 | value: "true", |
| 4901 | }, |
| 4902 | ], |
| 4903 | }, |
| 4904 | adminHeaders |
| 4905 | ) |
| 4906 | |
| 4907 | cart = (await api.get(`/store/carts/${cart.id}`, storeHeaders)).data |
| 4908 | .cart |
| 4909 | |
| 4910 | const { result } = await listShippingOptionsForCartWorkflow( |
| 4911 | appContainer |
| 4912 | ).run({ |
| 4913 | input: { cart_id: cart.id }, |
| 4914 | }) |
| 4915 | |
| 4916 | expect(result).toEqual([]) |
| 4917 | }) |
| 4918 | |
| 4919 | it("should not flag insufficient_inventory for a pickup option when item has allow_backorder=true and zero stock", async () => { |
| 4920 | const [product] = await productModule.createProducts([ |
| 4921 | { |
| 4922 | title: "Backorder pickup product", |
| 4923 | status: ProductStatus.PUBLISHED, |
| 4924 | variants: [ |
| 4925 | { |
| 4926 | title: "Variant", |
| 4927 | manage_inventory: true, |
| 4928 | allow_backorder: true, |
| 4929 | }, |
| 4930 | ], |
| 4931 | }, |
| 4932 | ]) |
| 4933 | |
| 4934 | const inventoryItem = ( |
| 4935 | await api.post( |
| 4936 | `/admin/inventory-items`, |
| 4937 | { |
| 4938 | sku: "backorder-pickup-1", |
| 4939 | location_levels: [ |
| 4940 | { |
| 4941 | location_id: stockLocation.id, |
| 4942 | stocked_quantity: 0, |
| 4943 | }, |
| 4944 | ], |
| 4945 | }, |
| 4946 | adminHeaders |
| 4947 | ) |
| 4948 | ).data.inventory_item |
| 4949 | |
| 4950 | const variantPriceSet = await pricingModule.createPriceSets({ |
| 4951 | prices: [{ amount: 1000, currency_code: "usd" }], |
| 4952 | }) |
| 4953 | |
| 4954 | await remoteLink.create([ |
| 4955 | { |
| 4956 | [Modules.PRODUCT]: { variant_id: product.variants[0].id }, |
| 4957 | [Modules.PRICING]: { price_set_id: variantPriceSet.id }, |
| 4958 | }, |
| 4959 | { |
| 4960 | [Modules.PRODUCT]: { variant_id: product.variants[0].id }, |
| 4961 | [Modules.INVENTORY]: { inventory_item_id: inventoryItem.id }, |
| 4962 | }, |
| 4963 | ]) |
| 4964 | |
| 4965 | const shippingOption = ( |
| 4966 | await api.post( |
| 4967 | `/admin/shipping-options`, |
| 4968 | { |
| 4969 | name: "Pickup at location", |
| 4970 | service_zone_id: fulfillmentSet.service_zones[0].id, |
| 4971 | shipping_profile_id: shippingProfile.id, |
| 4972 | provider_id: "manual_test-provider", |
| 4973 | price_type: "flat", |
| 4974 | type: { |
| 4975 | label: "Pickup", |
| 4976 | description: "Pickup at location", |
| 4977 | code: "pickup", |
| 4978 | }, |
| 4979 | prices: [ |
| 4980 | { |
| 4981 | amount: 3000, |
| 4982 | currency_code: "usd", |
| 4983 | }, |
| 4984 | ], |
| 4985 | rules: [ |
| 4986 | { |
| 4987 | operator: RuleOperator.EQ, |
| 4988 | attribute: "is_return", |
| 4989 | value: "false", |
| 4990 | }, |
| 4991 | { |
| 4992 | operator: RuleOperator.EQ, |
| 4993 | attribute: "enabled_in_store", |
| 4994 | value: "true", |
| 4995 | }, |
| 4996 | ], |
| 4997 | }, |
| 4998 | adminHeaders |
| 4999 | ) |
| 5000 | ).data.shipping_option |
Showing first 5,000 of 5,452 lines.
View raw