Skip to content

Commit

Permalink
#450 reset status of initial item to available (#456)
Browse files Browse the repository at this point in the history
* #450 reset status of initial item to available

* fix notification

* show initial item name in notification
  • Loading branch information
daniel17903 authored May 29, 2022
1 parent 0e2b5e6 commit a4de80c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
let groups = [];
let inputContainer;
let footerButtonsWithContext;
let contextVars = {};
// needs to be reactive so that the injected context is updated when doc changes
$: doc, (footerButtonsWithContext = injectContext(config.footerButtons));
Expand All @@ -25,6 +26,7 @@
closePopup,
updateDoc: (updatedDoc) => (doc = { ...doc, ...updatedDoc }),
container: inputContainer,
contextVars,
});
} else {
return val;
Expand Down
41 changes: 19 additions & 22 deletions Frontend/src/data/rental/inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,17 @@ import {
customerAttributeStartsWithIgnoreCaseSelector,
itemAttributeStartsWithIgnoreCaseAndNotDeletedSelector,
activeRentalsForCustomerSelector,
itemById,
customerById,
customerByLastname,
itemByName,
} from "../selectors";

/**
* Whether the status of the selected item should be updated when a rental is created or completed.
* For items existing more than once this should always be false. For other items this can be toggled by the user.
*/
var updateItemStatus = true;

/**
* Whether the toggle for updateStatusOnWebsite is hidden.
*/
var hideToggleUpdateItemStatus = false;

const updateToggleStatus = (itemExistsMoreThanOnce) => {
const updateToggleStatus = (context, itemExistsMoreThanOnce) => {
if (itemExistsMoreThanOnce) {
updateItemStatus = false;
context.contextVars.updateItemStatus = false;
hideToggleUpdateItemStatus = true;
} else {
hideToggleUpdateItemStatus = false;
Expand All @@ -56,7 +47,7 @@ const updateItemOfRental = (context, item) => {
deposit: item.deposit,
});
showNotificationsIfNotAvailable(item);
updateToggleStatus(item.exists_more_than_once);
updateToggleStatus(context, item.exists_more_than_once);
};

const updateCustomerOfRental = (context, customer) => {
Expand Down Expand Up @@ -130,8 +121,19 @@ export default {
`Leihvorgang ${context.createNew ? "anlegen" : "bearbeiten"}`,
initialValues,
onMount: (context) => () => {
updateItemStatus = true;
hideToggleUpdateItemStatus = false;
/**
* Whether the status of the selected item should be updated when a rental is created or completed.
* For items existing more than once this should always be false. For other items this can be toggled by the user.
*/
context.contextVars.updateItemStatus = true;

/**
* The id of the item that belongs to this rental at the time of opening the input form. This is required to
* check if the item was changed when saving the rental.
*/
context.contextVars.initialItemId = context.doc.item_id;
context.contextVars.initialItemName = context.doc.item_name;
},
footerButtons: (context) => [
{
Expand All @@ -147,13 +149,7 @@ export default {
},
{
text: "Speichern",
onClick: () =>
onSave(
context.doc,
context.closePopup,
updateItemStatus,
context.createNew
),
onClick: () => onSave(context),
loadingText: "Leihvorgang wird gespeichert",
},
],
Expand Down Expand Up @@ -213,9 +209,10 @@ export default {
nobind: true,
hidden: () => hideToggleUpdateItemStatus,
props: {
value: updateItemStatus,
value: (context) => context.contextVars.updateItemStatus,
// onChange callback necessary because bind only works for doc attributes
onChange: (context) => (value) => (updateItemStatus = value),
onChange: (context) => (value) =>
(context.contextVars.updateItemStatus = value),
},
},

Expand Down
112 changes: 61 additions & 51 deletions Frontend/src/data/rental/onSave.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,12 @@ import { setNumericValuesDefault0 } from "../utils";
import { itemById } from "../selectors";
import Logger from "js-logger";

const fetchItem = async (rental) => {
if (rental.item_id) {
try {
const item = (
await Database.fetchDocsBySelector(itemById(rental.item_id))
)[0];
rental.image = item.image;
return item;
} catch (error) {
notifier.warning(
`Gegenstand '${rental.item_id}' konnte nicht geladen werden!`,
6000
);
Logger.error(error);
return undefined;
}
} else {
Logger.warn(
`Could not update item because rental ${rental._id} does not have an item_id.`
);
return undefined;
const fetchItemById = async (itemId) => {
try {
return (await Database.fetchDocsBySelector(itemById(itemId)))[0];
} catch (error) {
Logger.error(error);
throw `Failed to load item with id ${itemId}`;
}
};

Expand All @@ -44,44 +29,69 @@ const newItemStatus = (rental) => {
}
};

export default async (rental, closePopup, updateItemStatus, createNew) => {
setNumericValuesDefault0(rental, columns);
const updateItemStatus = async (item, status) => {
item.status = status;
await Database.updateDoc(item);
await WoocommerceClient.updateItem(item);
notifier.success(
`'${item.name}' wurde als ${
item.status === "instock" ? "verfügbar" : "verliehen"
} markiert.`
);
};

export default async (context) => {
const { doc, closePopup, createNew, contextVars } = context;
setNumericValuesDefault0(doc, columns);

// item changed, reset initial item to status available
if (
contextVars.initialItemId !== undefined &&
contextVars.initialItemId !== doc.item_id
) {
try {
const initialItem = await fetchItemById(contextVars.initialItemId);
await updateItemStatus(initialItem, "instock");
notifier.warning(
`Status von '${contextVars.initialItemName}' wurde auf 'verfügbar' geändert. Bitter überprüfe ob das stimmt.`,
{ persist: true }
);
} catch (error) {
Logger.error(
`Failed to update status of initial item with name ${contextVars.initialItemName} id ${contextVars.initialItemId}, ${error}`
);
notifier.warning(
`Status von '${contextVars.initialItemName}' konnte nicht aktualisiert werden. Bitte überprüfe den Status dieses Gegenstandes.`,
{ persist: true }
);
}
}

if (contextVars.updateItemStatus) {
try {
const item = await fetchItemById(doc.item_id);
doc.image = item.image;
await updateItemStatus(item, newItemStatus(doc));
} catch (error) {
Logger.error(
`Failed to update status of item with id ${doc.item_id}, ${error}`
);

if (updateItemStatus) {
const item = await fetchItem(rental);
if (item) {
item.status = newItemStatus(rental);
await Database.updateDoc(item)
.then(() => WoocommerceClient.updateItem(item))
.then(() => {
notifier.success(
`'${item.name}' wurde als ${
item.status === "instock" ? "verfügbar" : "verliehen"
} markiert.`
);
})
.catch((error) => {
notifier.danger(
`Status von '${item.name}' konnte nicht aktualisiert werden!`,
{ persist: true }
);
Logger.error(error);
});
} else {
Logger.warn(
`Did not update item of rental ${rental._id} because item not found.`
notifier.danger(
`Status des Gegenstandes mit ID '${doc.item_id}' konnte nicht aktualisiert werden!`,
{ persist: true }
);
}
} else {
Logger.debug(
`Did not update item of rental ${rental._id} because updateItemStatus is false.`
`Did not update item of rental ${doc._id} because updateItemStatus is false.`
);
}

await (createNew ? Database.createDoc(rental) : Database.updateDoc(rental))
.then((result) => notifier.success("Leihvorgang gespeichert!"))
.then(() => recentEmployeesStore.add(rental.passing_out_employee))
.then(() => recentEmployeesStore.add(rental.receiving_employee))
await (createNew ? Database.createDoc(doc) : Database.updateDoc(doc))
.then((_) => notifier.success("Leihvorgang gespeichert!"))
.then(() => recentEmployeesStore.add(doc.passing_out_employee))
.then(() => recentEmployeesStore.add(doc.receiving_employee))
.then(closePopup)
.catch((error) => {
notifier.danger("Leihvorgang konnte nicht gespeichert werden!", {
Expand Down

0 comments on commit a4de80c

Please sign in to comment.