From 39e0d6f7821f89a5453a139f741762ce2a3b3747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chy=C5=82a?= Date: Fri, 6 Dec 2024 10:41:58 +0100 Subject: [PATCH] Prevent voucher channel update when voucher create failed (#5304) * Prevent voucher channel update when create failed * Add changelog --- .changeset/pretty-beds-tell.md | 5 ++ .../views/VoucherCreate/handlers.test.ts | 64 +++++++++++++++++++ src/discounts/views/VoucherCreate/handlers.ts | 7 +- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 .changeset/pretty-beds-tell.md create mode 100644 src/discounts/views/VoucherCreate/handlers.test.ts diff --git a/.changeset/pretty-beds-tell.md b/.changeset/pretty-beds-tell.md new file mode 100644 index 00000000000..fb1d3aa559a --- /dev/null +++ b/.changeset/pretty-beds-tell.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Prevent a call update channel after voucher create when no voucher id returns from response diff --git a/src/discounts/views/VoucherCreate/handlers.test.ts b/src/discounts/views/VoucherCreate/handlers.test.ts new file mode 100644 index 00000000000..d7080d027ad --- /dev/null +++ b/src/discounts/views/VoucherCreate/handlers.test.ts @@ -0,0 +1,64 @@ +import { VoucherDetailsPageFormData } from "@dashboard/discounts/components/VoucherDetailsPage"; +import { DiscountTypeEnum, RequirementsPicker } from "@dashboard/discounts/types"; +import { VoucherTypeEnum } from "@dashboard/graphql"; + +import { createHandler } from "./handlers"; + +const formData: VoucherDetailsPageFormData = { + applyOncePerCustomer: false, + applyOncePerOrder: false, + onlyForStaff: false, + channelListings: [], + name: "name", + discountType: DiscountTypeEnum.SHIPPING, + endDate: "2021-01-01", + endTime: "00:00", + hasEndDate: false, + hasUsageLimit: false, + minCheckoutItemsQuantity: "1", + requirementsPicker: RequirementsPicker.NONE, + startDate: "2021-02-01", + startTime: "00:00", + type: VoucherTypeEnum.ENTIRE_ORDER, + codes: [], + usageLimit: 1, + used: 1, + singleUse: false, + metadata: [], + privateMetadata: [], +}; + +describe("createHandler", () => { + it("should not call channel update when voucherCreate return error", async () => { + // Arrange + const voucherCreate = jest.fn().mockResolvedValue({ errors: ["error"] }); + const updateChannels = jest.fn(); + const validateFn = jest.fn().mockReturnValue(true); + const handler = createHandler(voucherCreate, updateChannels, validateFn); + + // Act + const result = await handler(formData); + + // Assert + expect(result).toEqual({ errors: ["Could not update channels"] }); + expect(voucherCreate).toHaveBeenCalled(); + expect(updateChannels).not.toHaveBeenCalled(); + }); + + it("should call channel update when voucherCreate successes", async () => { + // Arrange + const voucherCreate = jest + .fn() + .mockResolvedValue({ data: { voucherCreate: { voucher: { id: "id" } } } }); + const updateChannels = jest.fn(); + const validateFn = jest.fn().mockReturnValue(true); + const handler = createHandler(voucherCreate, updateChannels, validateFn); + + // Act + await handler(formData); + + // Assert + expect(voucherCreate).toHaveBeenCalled(); + expect(updateChannels).toHaveBeenCalled(); + }); +}); diff --git a/src/discounts/views/VoucherCreate/handlers.ts b/src/discounts/views/VoucherCreate/handlers.ts index 20d3b6c3dfb..2e24357810e 100644 --- a/src/discounts/views/VoucherCreate/handlers.ts +++ b/src/discounts/views/VoucherCreate/handlers.ts @@ -1,4 +1,3 @@ -// @ts-strict-ignore import { FetchResult } from "@apollo/client"; import { VoucherDetailsPageFormData } from "@dashboard/discounts/components/VoucherDetailsPage"; import { getChannelsVariables } from "@dashboard/discounts/handlers"; @@ -60,6 +59,12 @@ export function createHandler( return { errors }; } + if (!response?.data?.voucherCreate?.voucher) { + return { + errors: ["Could not update channels"], + }; + } + const channelsUpdateErrors = await extractMutationErrors( updateChannels({ variables: getChannelsVariables(