Skip to content

Commit

Permalink
Prevent voucher channel update when voucher create failed (#5304)
Browse files Browse the repository at this point in the history
* Prevent voucher channel update when create failed

* Add changelog
  • Loading branch information
poulch committed Dec 6, 2024
1 parent 38d9a92 commit 36fb327
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pretty-beds-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Prevent a call update channel after voucher create when no voucher id returns from response
64 changes: 64 additions & 0 deletions src/discounts/views/VoucherCreate/handlers.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
7 changes: 6 additions & 1 deletion src/discounts/views/VoucherCreate/handlers.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 36fb327

Please sign in to comment.