saleor-dashboard/cypress/support/api/utils/ordersUtils.js

311 lines
7.2 KiB
JavaScript
Raw Normal View History

import * as checkoutRequest from "../requests/Checkout";
import * as orderRequest from "../requests/Order";
import {
getPaymentMethodStripeId,
2022-08-04 10:59:42 +00:00
sendConfirmationToStripe,
} from "../requests/stripe";
import { createProductInChannel } from "./products/productsUtils";
export function createWaitingForCaptureOrder({
channelSlug,
email,
variantsList,
shippingMethodName,
2022-08-04 10:59:42 +00:00
address,
}) {
let checkout;
const auth = "token";
cy.loginInShop();
return checkoutRequest
.createCheckout({
channelSlug,
email,
variantsList,
address,
billingAddress: address,
2022-08-04 10:59:42 +00:00
auth,
})
.then(({ checkout: checkoutResp }) => {
checkout = checkoutResp;
const shippingMethodId = getShippingMethodIdFromCheckout(
checkout,
2022-08-04 10:59:42 +00:00
shippingMethodName,
);
checkoutRequest.addShippingMethod(checkout.id, shippingMethodId);
})
.then(() => addPayment(checkout.id))
.then(() => checkoutRequest.completeCheckout(checkout.id))
.then(({ order }) => ({ checkout, order }));
}
export function getShippingMethodIdFromCheckout(checkout, shippingMethodName) {
const availableShippingMethodsLength = checkout.shippingMethods.length;
if (availableShippingMethodsLength === 0) {
return null;
} else {
return checkout.shippingMethods.find(
2022-08-04 10:59:42 +00:00
element => element.name === shippingMethodName,
).id;
}
}
export function updateShippingInCheckout(checkoutToken, shippingMethodName) {
return checkoutRequest.getCheckout(checkoutToken).then(checkout => {
const shippingMethodId = getShippingMethodIdFromCheckout(
checkout,
2022-08-04 10:59:42 +00:00
shippingMethodName,
);
return checkoutRequest.checkoutShippingMethodUpdate(
checkout.id,
2022-08-04 10:59:42 +00:00
shippingMethodId,
);
});
}
export function createCheckoutWithVoucher({
channelSlug,
email = "email@example.com",
variantsList,
productQuantity = 1,
address,
shippingMethodName,
voucherCode,
2022-08-04 10:59:42 +00:00
auth,
}) {
let checkout;
return checkoutRequest
.createCheckout({
channelSlug,
productQuantity,
email,
variantsList,
address,
billingAddress: address,
2022-08-04 10:59:42 +00:00
auth,
})
.then(({ checkout: checkoutResp }) => {
checkout = checkoutResp;
const shippingMethodId = getShippingMethodIdFromCheckout(
checkout,
2022-08-04 10:59:42 +00:00
shippingMethodName,
);
checkoutRequest.addShippingMethod(checkout.id, shippingMethodId);
})
.then(() => {
checkoutRequest.addVoucher(checkout.id, voucherCode);
})
.then(resp => ({
addPromoCodeResp: resp.body.data.checkoutAddPromoCode,
2022-08-04 10:59:42 +00:00
checkout,
}));
}
export function purchaseProductWithPromoCode({
channelSlug,
email = "email@example.com",
variantsList,
address,
shippingMethodName,
voucherCode,
2022-08-04 10:59:42 +00:00
auth,
}) {
let checkout;
return createCheckoutWithVoucher({
channelSlug,
email,
variantsList,
address,
shippingMethodName,
voucherCode,
2022-08-04 10:59:42 +00:00
auth,
})
.then(({ checkout: checkoutResp }) => {
checkout = checkoutResp;
addPayment(checkout.id);
})
.then(() => checkoutRequest.completeCheckout(checkout.id))
.then(({ order }) => ({ checkout, order }));
}
export function createReadyToFulfillOrder({
customerId,
shippingMethod,
channelId,
Feature - channels per plugin (#1093) * Update schema * Update queries, mutations, and types * Add render with dividers util function * Add plugin details channels card component * Update plugin details to use channels * Update stories * Update plugin configuration type across the app, fix some other types, temporarily comment some things out in plugins list so types match" * Update schema * Update types * Update plugins list to show channels and global statuses, add plugin channel status, update status label component * Add render with dividers util function * Comment out some stuff for types to match - temporary * Add useChannelsSearchWithLoadMore util to imitate loading more from backend for channels list with load more * Change filters logic to be able to display multiple fields in a field section and add it to plugins view * Add scroll option to plugin availability popup on plugin list * Fix plugin list page story * Temporarily comment some stuff out, fix some types * Add filters errors WIP * Fix filters not updating list * Add error handling to plugins list filters and filters in general * Rename some components according to review * Move useChannelsSearch and useChannelsSearchWithLoadMore to hooks, change some imports accordingly * Fix imports * Move render collection with dividers to a component, fix usages * Replace channels with load more and search query to base channels query * Change render with dividers function to take in a component instead of render function * Update tests * Extract messages * Remove unnecessary imports * Fix filters - autocomplete messing items order sometimes & some fields not working * Update plugin update mutation variables - change channelId to channel * fix failing tests * Add test ids * fix failing tests * fix failing tests * Rename misc.tsx to ts * Remove usage of render collection with diviers, change it to CollectionWithDividers component * Remove unnecessary imports * Update messages ids * Update snapshots Co-authored-by: Karolina Rakoczy <rakoczy.karolina@gmail.com>
2021-05-11 13:58:09 +00:00
variantsList,
2022-08-04 10:59:42 +00:00
address,
}) {
let order;
return orderRequest
.createDraftOrder({ customerId, channelId, address })
.then(orderResp => {
order = orderResp;
assignVariantsToOrder(order, variantsList);
})
.then(orderResp => {
shippingMethod = getShippingMethodIdFromCheckout(
orderResp.order,
shippingMethod.name,
);
orderRequest.addShippingMethod(order.id, shippingMethod);
})
2022-08-04 10:59:42 +00:00
.then(() => orderRequest.completeOrder(order.id))
.then(() => orderRequest.markOrderAsPaid(order.id));
}
export function createFulfilledOrder({
customerId,
shippingMethod,
channelId,
variantsList,
address,
warehouse,
2022-08-04 10:59:42 +00:00
quantity = 1,
}) {
return createReadyToFulfillOrder({
customerId,
shippingMethod,
channelId,
variantsList,
2022-08-04 10:59:42 +00:00
address,
}).then(({ order }) => {
orderRequest.fulfillOrder({
orderId: order.id,
warehouse,
quantity,
2022-08-04 10:59:42 +00:00
linesId: order.lines,
});
});
}
export function createOrder({
customerId,
shippingMethod,
channelId,
Feature - channels per plugin (#1093) * Update schema * Update queries, mutations, and types * Add render with dividers util function * Add plugin details channels card component * Update plugin details to use channels * Update stories * Update plugin configuration type across the app, fix some other types, temporarily comment some things out in plugins list so types match" * Update schema * Update types * Update plugins list to show channels and global statuses, add plugin channel status, update status label component * Add render with dividers util function * Comment out some stuff for types to match - temporary * Add useChannelsSearchWithLoadMore util to imitate loading more from backend for channels list with load more * Change filters logic to be able to display multiple fields in a field section and add it to plugins view * Add scroll option to plugin availability popup on plugin list * Fix plugin list page story * Temporarily comment some stuff out, fix some types * Add filters errors WIP * Fix filters not updating list * Add error handling to plugins list filters and filters in general * Rename some components according to review * Move useChannelsSearch and useChannelsSearchWithLoadMore to hooks, change some imports accordingly * Fix imports * Move render collection with dividers to a component, fix usages * Replace channels with load more and search query to base channels query * Change render with dividers function to take in a component instead of render function * Update tests * Extract messages * Remove unnecessary imports * Fix filters - autocomplete messing items order sometimes & some fields not working * Update plugin update mutation variables - change channelId to channel * fix failing tests * Add test ids * fix failing tests * fix failing tests * Rename misc.tsx to ts * Remove usage of render collection with diviers, change it to CollectionWithDividers component * Remove unnecessary imports * Update messages ids * Update snapshots Co-authored-by: Karolina Rakoczy <rakoczy.karolina@gmail.com>
2021-05-11 13:58:09 +00:00
variantsList,
2022-08-04 10:59:42 +00:00
address,
}) {
let order;
return orderRequest
.createDraftOrder({ customerId, channelId, address })
.then(orderResp => {
order = orderResp;
assignVariantsToOrder(order, variantsList);
})
.then(orderResp => {
shippingMethod = getShippingMethodIdFromCheckout(
orderResp.order,
shippingMethod.name,
);
orderRequest.addShippingMethod(order.id, shippingMethod);
})
.then(() => orderRequest.completeOrder(order.id))
.then(() => order);
}
function assignVariantsToOrder(order, variantsList) {
variantsList.forEach(variantElement => {
orderRequest.addProductToOrder(order.id, variantElement.id);
});
}
Feature - channels per plugin (#1093) * Update schema * Update queries, mutations, and types * Add render with dividers util function * Add plugin details channels card component * Update plugin details to use channels * Update stories * Update plugin configuration type across the app, fix some other types, temporarily comment some things out in plugins list so types match" * Update schema * Update types * Update plugins list to show channels and global statuses, add plugin channel status, update status label component * Add render with dividers util function * Comment out some stuff for types to match - temporary * Add useChannelsSearchWithLoadMore util to imitate loading more from backend for channels list with load more * Change filters logic to be able to display multiple fields in a field section and add it to plugins view * Add scroll option to plugin availability popup on plugin list * Fix plugin list page story * Temporarily comment some stuff out, fix some types * Add filters errors WIP * Fix filters not updating list * Add error handling to plugins list filters and filters in general * Rename some components according to review * Move useChannelsSearch and useChannelsSearchWithLoadMore to hooks, change some imports accordingly * Fix imports * Move render collection with dividers to a component, fix usages * Replace channels with load more and search query to base channels query * Change render with dividers function to take in a component instead of render function * Update tests * Extract messages * Remove unnecessary imports * Fix filters - autocomplete messing items order sometimes & some fields not working * Update plugin update mutation variables - change channelId to channel * fix failing tests * Add test ids * fix failing tests * fix failing tests * Rename misc.tsx to ts * Remove usage of render collection with diviers, change it to CollectionWithDividers component * Remove unnecessary imports * Update messages ids * Update snapshots Co-authored-by: Karolina Rakoczy <rakoczy.karolina@gmail.com>
2021-05-11 13:58:09 +00:00
export function addPayment(checkoutId) {
return checkoutRequest.addPayment({
checkoutId,
gateway: "mirumee.payments.dummy",
2022-08-04 10:59:42 +00:00
token: "not-charged",
});
}
export function addAdyenPayment(checkoutId, amount) {
return checkoutRequest.addPayment({
checkoutId,
gateway: "mirumee.payments.adyen",
2022-08-04 10:59:42 +00:00
amount,
});
}
export function addStripePayment(checkoutId, amount, token) {
return checkoutRequest.addPayment({
checkoutId,
gateway: "saleor.payments.stripe",
amount,
2022-08-04 10:59:42 +00:00
token,
});
}
export function createAndCompleteCheckoutWithoutShipping({
channelSlug,
email,
variantsList,
billingAddress,
2022-08-04 10:59:42 +00:00
auth,
}) {
let checkout;
return checkoutRequest
.createCheckout({ channelSlug, email, variantsList, billingAddress, auth })
.then(({ checkout: checkoutResp }) => {
checkout = checkoutResp;
addPayment(checkout.id);
})
.then(() => checkoutRequest.completeCheckout(checkout.id))
.then(({ order }) => ({ checkout, order }));
}
export function createOrderWithNewProduct({
attributeId,
categoryId,
productTypeId,
channel,
name,
warehouseId,
quantityInWarehouse = 1,
trackInventory = true,
shippingMethod,
2022-08-04 10:59:42 +00:00
address,
}) {
let variantsList;
return createProductInChannel({
attributeId,
categoryId,
productTypeId,
channelId: channel.id,
name,
warehouseId,
quantityInWarehouse,
2022-08-04 10:59:42 +00:00
trackInventory,
})
.then(({ variantsList: variantsListResp }) => {
variantsList = variantsListResp;
createWaitingForCaptureOrder({
channelSlug: channel.slug,
email: "email@example.com",
variantsList,
shippingMethodName: shippingMethod.name,
2022-08-04 10:59:42 +00:00
address,
});
})
.then(({ order, checkout }) => ({ order, checkout, variantsList }));
}
export function addStripePaymentAndGetConfirmationData({
card,
checkoutId,
2022-08-04 10:59:42 +00:00
amount,
}) {
let paymentMethodId;
return getPaymentMethodStripeId(card)
.then(resp => {
paymentMethodId = resp.body.id;
addStripePayment(checkoutId, amount, resp.body.id);
})
.then(() => {
checkoutRequest.completeCheckout(checkoutId);
})
.then(resp => {
const confirmationData = JSON.parse(resp.confirmationData);
sendConfirmationToStripe(paymentMethodId, confirmationData.id, false);
})
.then(resp => resp);
}