Refactor addOrIncrementCategory()

This commit is contained in:
Dev 2023-04-24 14:19:42 +01:00
parent 8076ed9b29
commit 685859d57f

View File

@ -49,44 +49,31 @@ export class RagfairCategoriesService
/** /**
* Increment or decrement a category array * Increment or decrement a category array
* @param offer offer to process * @param offer Offer to process
* @param categories categories to update * @param categories Categories to update
* @param increment should item be incremented or decremented * @param increment (Optional) Should item be incremented or decremented
*/ */
protected addOrIncrementCategory(offer: IRagfairOffer, categories: Record<string, number>, increment = true ): void protected addOrIncrementCategory(offer: IRagfairOffer, categories: Record<string, number>, increment = true ): void
{ {
const itemId = offer.items[0]._tpl; const itemId = offer.items[0]._tpl;
if (increment) if (increment)
{ {
if (!categories[itemId]) categories[itemId] = categories[itemId]
{ ? categories[itemId] + 1
categories[itemId] = 1; : 1;
}
else
{
categories[itemId]++;
}
} }
else else
{ {
// No category, no work to do // No category, no work to do
if (!categories[itemId])
{
return;
}
// Key exists, decrement
if (categories[itemId]) if (categories[itemId])
{ {
categories[itemId]--; categories[itemId]--;
}
// remove category entirely as its 0 or less // Remove category entirely as its 0 or less
if (categories[itemId] < 1) if (categories[itemId] < 1)
{ {
delete categories[itemId]; delete categories[itemId];
}
} }
} }
} }