Refactored communication with partner app to use custom objects + error handling BOD-83 BOD-138 BOD-139

This commit is contained in:
Patrick Fic
2020-06-02 17:46:51 -07:00
parent 6060def9f4
commit 59f21b95b1
12 changed files with 176 additions and 55 deletions

View File

@@ -6,7 +6,7 @@ import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { auth } from "../../firebase/firebase.utils";
import { UPDATE_INVOICE } from "../../graphql/invoices.queries";
import { UPDATE_INVOICES } from "../../graphql/invoices.queries";
import { selectBodyshop } from "../../redux/user/user.selectors";
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
@@ -19,7 +19,7 @@ export function InvoiceExportButton({
loadingCallback,
}) {
const { t } = useTranslation();
const [updateInvoice] = useMutation(UPDATE_INVOICE);
const [updateInvoice] = useMutation(UPDATE_INVOICES);
const [loading, setLoading] = useState(false);
const handleQbxml = async () => {
@@ -51,6 +51,7 @@ export function InvoiceExportButton({
}
let PartnerResponse;
try {
PartnerResponse = await axios.post(
"http://e9c5a8ed9079.ngrok.io/qb/",
@@ -66,26 +67,42 @@ export function InvoiceExportButton({
return;
}
const invoiceUpdateResponse = await updateInvoice({
variables: {
invoiceId: invoiceId,
invoice: {
exported: true,
exported_at: new Date(),
console.log("handleQbxml -> PartnerResponse", PartnerResponse);
const failedTransactions = PartnerResponse.data.filter((r) => !r.success);
const successfulTransactions = PartnerResponse.data.filter(
(r) => r.success
);
if (failedTransactions.length > 0) {
//Uh oh. At least one was no good.
failedTransactions.map((ft) =>
notification["error"]({
message: t("invoices.errors.exporting", {
error: ft.errorMessage || "",
}),
})
);
}
if (successfulTransactions.length > 0) {
const invoiceUpdateResponse = await updateInvoice({
variables: {
invoiceIdList: successfulTransactions.map((st) => st.id),
invoice: {
exported: true,
exported_at: new Date(),
},
},
},
});
if (!!!invoiceUpdateResponse.errors) {
notification["success"]({
message: t("jobs.successes.exported"),
});
} else {
notification["error"]({
message: t("jobs.errors.exporting", {
error: JSON.stringify(invoiceUpdateResponse.error),
}),
});
if (!!!invoiceUpdateResponse.errors) {
notification["success"]({
message: t("jobs.successes.exported"),
});
} else {
notification["error"]({
message: t("jobs.errors.exporting", {
error: JSON.stringify(invoiceUpdateResponse.error),
}),
});
}
}
if (!!loadingCallback) loadingCallback(false);