Skip to content

Commit

Permalink
Merge pull request #14495 from minkyngkm/revert-14462
Browse files Browse the repository at this point in the history
Revert "Restrict channel user from editing billing details"
  • Loading branch information
minkyngkm authored Nov 21, 2024
2 parents d580c2b + 4ba6dd7 commit 01d3d25
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
FormContext,
defaultValues,
} from "advantage/distributor/utils/FormContext";
import { DistributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
import { distributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
import { ChannelOfferFactory } from "advantage/offers/tests/factories/channelOffers";
import { UserSubscriptionMarketplace } from "advantage/api/enum";

Expand All @@ -27,7 +27,7 @@ const mockSubscription: SubscriptionItem = {
const mockContextValue = {
...defaultValues,
subscriptionList: [mockSubscription] as SubscriptionItem[],
products: [DistributorProduct] as ChannelProduct[],
products: [distributorProduct] as ChannelProduct[],
offer: ChannelOfferFactory.build({ id: "offer-id-1" }),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
FormContext,
defaultValues,
} from "advantage/distributor/utils/FormContext";
import { DistributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
import { distributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";

const mockSubscription: SubscriptionItem = {
id: "mocked-id-1",
Expand All @@ -25,7 +25,7 @@ const mockContextValue = {
...defaultValues,
subscriptionList: [mockSubscription] as SubscriptionItem[],
setSubscriptionList: jest.fn(),
products: [DistributorProduct] as ChannelProduct[],
products: [distributorProduct] as ChannelProduct[],
duration: 1,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Formik } from "formik";
import { Elements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
import { fireEvent, render, screen } from "@testing-library/react";
import { DistributorProduct, UAProduct } from "../../utils/test/Mocks";
import { UAProduct } from "../../utils/test/Mocks";
import Taxes from "./Taxes";

describe("TaxesTests", () => {
Expand Down Expand Up @@ -112,7 +112,7 @@ describe("TaxesTests", () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const initialValues = {
const intialValues = {
country: "GB",
};
const products = [
Expand All @@ -123,7 +123,7 @@ describe("TaxesTests", () => {
];
render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Formik initialValues={intialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<Taxes products={products} setError={jest.fn()} />
</Elements>
Expand Down Expand Up @@ -158,7 +158,7 @@ describe("TaxesTests", () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const initialValues = {
const intialValues = {
country: "GB",
VATNumber: "GB123123123",
};
Expand All @@ -171,7 +171,7 @@ describe("TaxesTests", () => {
];
render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Formik initialValues={intialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<Taxes products={products} setError={jest.fn()} />
</Elements>
Expand Down Expand Up @@ -212,62 +212,4 @@ describe("TaxesTests", () => {
expect(screen.getByTestId("country")).toHaveTextContent("United Kingdom");
expect(screen.getByTestId("vat-number")).toHaveTextContent("GB123123123");
});

it("Edit button should be displayed for pro users", () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const initialValues = {
country: "GB",
VATNumber: "GB123123123",
marketPlace: "canonical-ua",
};

const products = [
{
product: UAProduct,
quantity: 1,
},
];
render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<Taxes products={products} setError={jest.fn()} />
</Elements>
</Formik>
</QueryClientProvider>,
);

expect(screen.queryByRole("button", { name: "Edit" })).toBeInTheDocument();
});

it("Edit button should not be displayed for channel users", () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const initialValues = {
country: "GB",
VATNumber: "GB123123123",
marketPlace: "canonical-pro-channel",
};

const products = [
{
product: DistributorProduct,
quantity: 1,
},
];
render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<Taxes products={products} setError={jest.fn()} />
</Elements>
</Formik>
</QueryClientProvider>,
);

expect(screen.queryByTestId("tax-edit-button")).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
import { getLabel } from "advantage/subscribe/react/utils/utils";
import postCustomerTaxInfo from "../../hooks/postCustomerTaxInfo";
import { CheckoutProducts, FormValues } from "../../utils/types";
import { UserSubscriptionMarketplace } from "advantage/api/enum";

type TaxesProps = {
products: CheckoutProducts[];
Expand All @@ -36,14 +35,7 @@ const Taxes = ({ products, setError }: TaxesProps) => {
setErrors: setFormikErrors,
} = useFormikContext<FormValues>();
const [isEditing, setIsEditing] = useState(!initialValues.country);
const isChannelUser =
products[0]?.product?.marketplace ===
UserSubscriptionMarketplace.CanonicalProChannel;
const queryClient = useQueryClient();
const postCustomerTaxInfoMutation = postCustomerTaxInfo();
const hasZeroPriceValue = products.some(
(item) => item.product.price.value === 0,
);

useEffect(() => {
if (errors.VATNumber) {
Expand All @@ -61,6 +53,10 @@ const Taxes = ({ products, setError }: TaxesProps) => {
}
}, [initialValues]);

const postCustomerTaxInfoMutation = postCustomerTaxInfo();
const hasZeroPriceValue = products.some(
(item) => item.product.price.value === 0,
);
const onSaveClick = () => {
setIsEditing(false);
setFieldTouched("isTaxSaved", false);
Expand All @@ -74,12 +70,8 @@ const Taxes = ({ products, setError }: TaxesProps) => {
},
{
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ["preview"],
});
queryClient.invalidateQueries({
queryKey: ["customerInfo"],
});
queryClient.invalidateQueries({ queryKey: ["preview"] });
queryClient.invalidateQueries({ queryKey: ["customerInfo"] });
},
onError: (error) => {
setFieldValue("Description", false);
Expand Down Expand Up @@ -298,52 +290,43 @@ const Taxes = ({ products, setError }: TaxesProps) => {
error={touched?.isTaxSaved && errors?.isTaxSaved}
/>
</Col>
{!isChannelUser && (
<>
<hr />
<div
className="u-align--right"
style={{ marginTop: "calc(.5rem - 1.5px)" }}
>
{isEditing ? (
<>
{window.accountId && !!initialValues.country && (
<ActionButton
onClick={() => {
setFieldValue("country", initialValues.country);
setFieldValue("usState", initialValues.usState);
setFieldValue("caProvince", initialValues.caProvince);
setFieldValue("VATNumber", initialValues.VATNumber);
setIsEditing(false);
setFieldValue("isTaxSaved", true);
setFieldTouched("isTaxSaved", false);
}}
>
Cancel
</ActionButton>
)}
<ActionButton
onClick={onSaveClick}
disabled={
!values.country ||
(values.country === "US" && !values.usState) ||
(values.country === "CA" && !values.caProvince)
}
>
Save
</ActionButton>
</>
) : (
<hr />
<div
className="u-align--right"
style={{ marginTop: "calc(.5rem - 1.5px)" }}
>
{isEditing ? (
<>
{window.accountId && !!initialValues.country ? (
<ActionButton
onClick={onEditClick}
data-testid="tax-edit-button"
onClick={() => {
setFieldValue("country", initialValues.country);
setFieldValue("usState", initialValues.usState);
setFieldValue("caProvince", initialValues.caProvince);
setFieldValue("VATNumber", initialValues.VATNumber);
setIsEditing(false);
setFieldValue("isTaxSaved", true);
setFieldTouched("isTaxSaved", false);
}}
>
Edit
Cancel
</ActionButton>
)}
</div>
</>
)}
) : null}
<ActionButton
onClick={onSaveClick}
disabled={
!values.country ||
(values.country === "US" && !values.usState) ||
(values.country === "CA" && !values.caProvince)
}
>
Save
</ActionButton>
</>
) : (
<ActionButton onClick={onEditClick}>Edit</ActionButton>
)}
</div>
</Row>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Elements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
import { fireEvent, render, screen } from "@testing-library/react";
import UserInfoForm from "./UserInfoForm";
import { UserSubscriptionMarketplace } from "advantage/api/enum";

describe("UserInfoFormTests", () => {
let queryClient: QueryClient;
Expand All @@ -18,7 +17,7 @@ describe("UserInfoFormTests", () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const initialValues = {
const intialValues = {
name: "Joe",
organisationName: "Canonical",
buyingFor: "organisation",
Expand All @@ -37,7 +36,7 @@ describe("UserInfoFormTests", () => {

render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Formik initialValues={intialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<UserInfoForm setError={jest.fn()} />
</Elements>
Expand Down Expand Up @@ -76,54 +75,4 @@ describe("UserInfoFormTests", () => {
"AB12 3CD",
);
});

it("Channel user should be able to edit only card number", () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const initialValues = {
name: "Min",
marketplace: UserSubscriptionMarketplace.CanonicalProChannel,
buyingFor: "organisation",
organisationName: "Canonical",
address: "ABC Street",
city: "DEF City",
postalCode: "AB12 3CD",
defaultPaymentMethod: {
brand: "visa",
country: "US",
expMonth: 4,
expYear: 2024,
id: "pm_ABCDEF",
last4: "4242",
},
};

render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<UserInfoForm setError={jest.fn()} />
</Elements>
</Formik>
</QueryClientProvider>,
);

fireEvent.click(screen.getByRole("button", { name: "Edit" }));
fireEvent.change(screen.getByTestId("field-card-number"), {
target: { value: "1234 5678 1234 5678" },
});

expect(screen.getByTestId("customer-name")).toHaveTextContent("Min");
expect(screen.getByTestId("organisation-name")).toHaveTextContent(
"Canonical",
);
expect(screen.getByTestId("customer-address")).toHaveTextContent(
"ABC Street",
);
expect(screen.getByTestId("customer-city")).toHaveTextContent("DEF City");
expect(screen.getByTestId("customer-postal-code")).toHaveTextContent(
"AB12 3CD",
);
});
});
Loading

0 comments on commit 01d3d25

Please sign in to comment.