2023-06-29 13:40:51 +00:00
|
|
|
import { getVariant } from "../../../../support/api/requests/Product";
|
2021-10-28 14:43:26 +00:00
|
|
|
import {
|
|
|
|
addChannelToSale,
|
|
|
|
createSale,
|
|
|
|
deleteSale,
|
|
|
|
getSales,
|
2023-06-29 13:40:51 +00:00
|
|
|
updateSale,
|
2021-10-28 14:43:26 +00:00
|
|
|
} from "../../requests/Discounts/Sales";
|
2021-03-23 10:15:39 +00:00
|
|
|
|
|
|
|
export function deleteSalesStartsWith(startsWith) {
|
|
|
|
cy.deleteElementsStartsWith(deleteSale, getSales, startsWith);
|
|
|
|
}
|
2021-10-28 14:43:26 +00:00
|
|
|
|
|
|
|
export function createSaleInChannel({
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
value,
|
|
|
|
channelId,
|
2023-06-29 13:40:51 +00:00
|
|
|
discountValue = value,
|
2021-10-28 14:43:26 +00:00
|
|
|
}) {
|
|
|
|
let sale;
|
|
|
|
return createSale({
|
|
|
|
name,
|
|
|
|
type,
|
2023-06-29 13:40:51 +00:00
|
|
|
value,
|
2021-10-28 14:43:26 +00:00
|
|
|
})
|
|
|
|
.then(saleResp => {
|
|
|
|
sale = saleResp;
|
|
|
|
addChannelToSale(sale.id, channelId, discountValue);
|
|
|
|
})
|
|
|
|
.then(() => sale);
|
|
|
|
}
|
2023-06-29 13:40:51 +00:00
|
|
|
export function getVariantWithSaleStatus(
|
|
|
|
{ variantId, channelSlug, onSaleStatus },
|
|
|
|
retries = 0,
|
|
|
|
) {
|
|
|
|
return getVariant(variantId, channelSlug).then(salesResponse => {
|
|
|
|
if (salesResponse.pricing.onSale === onSaleStatus) {
|
|
|
|
return;
|
|
|
|
} else if (retries > 4) {
|
|
|
|
throw new Error(
|
|
|
|
`Variant field onSale should have value: ${onSaleStatus} but has opposite. Retried for ${retries} times`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
cy.wait(5000);
|
|
|
|
getVariantWithSaleStatus(
|
|
|
|
{ variantId, channelSlug, onSaleStatus },
|
|
|
|
retries + 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-10-28 14:43:26 +00:00
|
|
|
|
|
|
|
export function createSaleInChannelWithProduct({
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
value,
|
|
|
|
channelId,
|
|
|
|
variants,
|
2023-06-29 13:40:51 +00:00
|
|
|
productId,
|
2021-10-28 14:43:26 +00:00
|
|
|
}) {
|
|
|
|
let sale;
|
|
|
|
return createSaleInChannel({
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
value,
|
2023-06-29 13:40:51 +00:00
|
|
|
channelId,
|
2021-10-28 14:43:26 +00:00
|
|
|
})
|
|
|
|
.then(saleResp => {
|
|
|
|
sale = saleResp;
|
|
|
|
updateSale({
|
|
|
|
saleId: sale.id,
|
|
|
|
variants,
|
2023-06-29 13:40:51 +00:00
|
|
|
productId,
|
2021-10-28 14:43:26 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => sale);
|
|
|
|
}
|