Skip to content

Commit

Permalink
Merge pull request #67 from sharetribe/estimated-stock-tx
Browse files Browse the repository at this point in the history
Estimated stock tx
  • Loading branch information
kpuputti authored Aug 4, 2021
2 parents ff2ee48 + 4be9cf4 commit 9cf7255
Show file tree
Hide file tree
Showing 22 changed files with 319 additions and 160 deletions.
8 changes: 3 additions & 5 deletions src/components/OrderBreakdown/LineItemBasePriceMaybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { LINE_ITEM_NIGHT, LINE_ITEM_DAY, propTypes } from '../../util/types';
import css from './OrderBreakdown.module.css';

const LineItemBasePriceMaybe = props => {
const { transaction, unitType, intl } = props;
const { lineItems, unitType, intl } = props;
const isNightly = unitType === LINE_ITEM_NIGHT;
const isDaily = unitType === LINE_ITEM_DAY;
const translationKey = isNightly
Expand All @@ -18,9 +18,7 @@ const LineItemBasePriceMaybe = props => {
// Find correct line-item for given unitType prop.
// It should be one of the following: 'line-item/night, 'line-item/day', 'line-item/units', or 'line-item/time'
// These are defined in '../../util/types';
const unitPurchase = transaction.attributes.lineItems.find(
item => item.code === unitType && !item.reversal
);
const unitPurchase = lineItems.find(item => item.code === unitType && !item.reversal);

const quantity = unitPurchase ? unitPurchase.quantity.toString() : null;
const unitPrice = unitPurchase ? formatMoney(intl, unitPurchase.unitPrice) : null;
Expand All @@ -37,7 +35,7 @@ const LineItemBasePriceMaybe = props => {
};

LineItemBasePriceMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
unitType: propTypes.lineItemUnitType.isRequired,
intl: intlShape.isRequired,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const isValidCommission = commissionLineItem => {
};

const LineItemCustomerCommissionMaybe = props => {
const { transaction, isCustomer, intl } = props;
const { lineItems, isCustomer, intl } = props;

const customerCommissionLineItem = transaction.attributes.lineItems.find(
const customerCommissionLineItem = lineItems.find(
item => item.code === LINE_ITEM_CUSTOMER_COMMISSION && !item.reversal
);

Expand Down Expand Up @@ -53,7 +53,7 @@ const LineItemCustomerCommissionMaybe = props => {
};

LineItemCustomerCommissionMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
isCustomer: bool.isRequired,
intl: intlShape.isRequired,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { propTypes, LINE_ITEM_CUSTOMER_COMMISSION } from '../../util/types';
import css from './OrderBreakdown.module.css';

const LineItemCustomerCommissionRefundMaybe = props => {
const { transaction, isCustomer, intl } = props;
const { lineItems, isCustomer, intl } = props;

const refund = transaction.attributes.lineItems.find(
const refund = lineItems.find(
item => item.code === LINE_ITEM_CUSTOMER_COMMISSION && item.reversal
);

Expand All @@ -23,7 +23,7 @@ const LineItemCustomerCommissionRefundMaybe = props => {
};

LineItemCustomerCommissionRefundMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
intl: intlShape.isRequired,
};

Expand Down
6 changes: 3 additions & 3 deletions src/components/OrderBreakdown/LineItemPickupFeeMaybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { LINE_ITEM_PICKUP_FEE, propTypes } from '../../util/types';
import css from './OrderBreakdown.module.css';

const LineItemPickupFeeMaybe = props => {
const { transaction, intl } = props;
const { lineItems, intl } = props;

const pickupFeeLineItem = transaction.attributes.lineItems.find(
const pickupFeeLineItem = lineItems.find(
item => item.code === LINE_ITEM_PICKUP_FEE && !item.reversal
);

Expand All @@ -23,7 +23,7 @@ const LineItemPickupFeeMaybe = props => {
};

LineItemPickupFeeMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
intl: intlShape.isRequired,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const isValidCommission = commissionLineItem => {
};

const LineItemProviderCommissionMaybe = props => {
const { transaction, isProvider, intl } = props;
const { lineItems, isProvider, intl } = props;

const providerCommissionLineItem = transaction.attributes.lineItems.find(
const providerCommissionLineItem = lineItems.find(
item => item.code === LINE_ITEM_PROVIDER_COMMISSION && !item.reversal
);

Expand Down Expand Up @@ -52,7 +52,7 @@ const LineItemProviderCommissionMaybe = props => {
};

LineItemProviderCommissionMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
isProvider: bool.isRequired,
intl: intlShape.isRequired,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { propTypes, LINE_ITEM_PROVIDER_COMMISSION } from '../../util/types';
import css from './OrderBreakdown.module.css';

const LineItemProviderCommissionRefundMaybe = props => {
const { transaction, isProvider, intl } = props;
const { lineItems, isProvider, intl } = props;

const refund = transaction.attributes.lineItems.find(
const refund = lineItems.find(
item => item.code === LINE_ITEM_PROVIDER_COMMISSION && item.reversal
);

Expand All @@ -23,7 +23,7 @@ const LineItemProviderCommissionRefundMaybe = props => {
};

LineItemProviderCommissionRefundMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
intl: intlShape.isRequired,
};

Expand Down
10 changes: 5 additions & 5 deletions src/components/OrderBreakdown/LineItemRefundMaybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ const isCommission = lineItem => {
/**
* Returns non-commission, reversal line items
*/
const nonCommissionReversalLineItems = transaction => {
return transaction.attributes.lineItems.filter(item => !isCommission(item) && item.reversal);
const nonCommissionReversalLineItems = lineItems => {
return lineItems.filter(item => !isCommission(item) && item.reversal);
};

const LineItemRefundMaybe = props => {
const { transaction, intl } = props;
const { lineItems, intl } = props;

// all non-commission, reversal line items
const refundLineItems = nonCommissionReversalLineItems(transaction);
const refundLineItems = nonCommissionReversalLineItems(lineItems);

const refund = lineItemsTotal(refundLineItems);

Expand All @@ -63,7 +63,7 @@ const LineItemRefundMaybe = props => {
};

LineItemRefundMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
intl: intlShape.isRequired,
};

Expand Down
6 changes: 3 additions & 3 deletions src/components/OrderBreakdown/LineItemShippingFeeMaybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { LINE_ITEM_SHIPPING_FEE, propTypes } from '../../util/types';
import css from './OrderBreakdown.module.css';

const LineItemShippingFeeMaybe = props => {
const { transaction, intl } = props;
const { lineItems, intl } = props;

const shippingFeeLineItem = transaction.attributes.lineItems.find(
const shippingFeeLineItem = lineItems.find(
item => item.code === LINE_ITEM_SHIPPING_FEE && !item.reversal
);

Expand All @@ -23,7 +23,7 @@ const LineItemShippingFeeMaybe = props => {
};

LineItemShippingFeeMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
intl: intlShape.isRequired,
};

Expand Down
29 changes: 11 additions & 18 deletions src/components/OrderBreakdown/LineItemSubTotalMaybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,36 @@ const isCommission = lineItem => {
/**
* Returns non-commission, non-reversal line items
*/
const nonCommissionNonReversalLineItems = transaction => {
return transaction.attributes.lineItems.filter(item => !isCommission(item) && !item.reversal);
const nonCommissionNonReversalLineItems = lineItems => {
return lineItems.filter(item => !isCommission(item) && !item.reversal);
};

/**
* Checks if a transaction has a commission line-item for
* a given user role.
* Check if there is a commission line-item for the given user role.
*/
const txHasCommission = (transaction, userRole) => {
const hasCommission = (lineItems, userRole) => {
let commissionLineItem = null;

if (userRole === 'customer') {
commissionLineItem = transaction.attributes.lineItems.find(
item => item.code === LINE_ITEM_CUSTOMER_COMMISSION
);
commissionLineItem = lineItems.find(item => item.code === LINE_ITEM_CUSTOMER_COMMISSION);
} else if (userRole === 'provider') {
commissionLineItem = transaction.attributes.lineItems.find(
item => item.code === LINE_ITEM_PROVIDER_COMMISSION
);
commissionLineItem = lineItems.find(item => item.code === LINE_ITEM_PROVIDER_COMMISSION);
}
return !!commissionLineItem;
};

const LineItemSubTotalMaybe = props => {
const { transaction, unitType, userRole, intl } = props;
const { lineItems, unitType, userRole, intl } = props;

const refund = transaction.attributes.lineItems.find(
item => item.code === unitType && item.reversal
);
const refund = lineItems.find(item => item.code === unitType && item.reversal);

// Show unit purchase line total (unit price * quantity) as a subtotal.
// PLEASE NOTE that this assumes that the transaction doesn't have other
// line item types than the defined unit type (e.g. week, month, year).
const showSubTotal = txHasCommission(transaction, userRole) || refund;
const showSubTotal = hasCommission(lineItems, userRole) || refund;

// all non-reversal, non-commission line items
const subTotalLineItems = nonCommissionNonReversalLineItems(transaction);
const subTotalLineItems = nonCommissionNonReversalLineItems(lineItems);
// line totals of those line items combined
const subTotal = lineItemsTotal(subTotalLineItems);

Expand All @@ -95,7 +88,7 @@ const LineItemSubTotalMaybe = props => {
};

LineItemSubTotalMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
userRole: string.isRequired,
intl: intlShape.isRequired,
};
Expand Down
6 changes: 3 additions & 3 deletions src/components/OrderBreakdown/LineItemTotalPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { propTypes } from '../../util/types';

import css from './OrderBreakdown.module.css';

const LineItemUnitPrice = props => {
const LineItemTotalPrice = props => {
const { transaction, isProvider, intl } = props;

let providerTotalMessageId = 'OrderBreakdown.providerTotalDefault';
Expand Down Expand Up @@ -39,10 +39,10 @@ const LineItemUnitPrice = props => {
);
};

LineItemUnitPrice.propTypes = {
LineItemTotalPrice.propTypes = {
transaction: propTypes.transaction.isRequired,
isProvider: bool.isRequired,
intl: intlShape.isRequired,
};

export default LineItemUnitPrice;
export default LineItemTotalPrice;
8 changes: 3 additions & 5 deletions src/components/OrderBreakdown/LineItemUnknownItemsMaybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ import { LINE_ITEMS, propTypes } from '../../util/types';
import css from './OrderBreakdown.module.css';

const LineItemUnknownItemsMaybe = props => {
const { transaction, isProvider, intl } = props;
const { lineItems, isProvider, intl } = props;

// resolve unknown non-reversal line items
const allItems = transaction.attributes.lineItems.filter(
item => LINE_ITEMS.indexOf(item.code) === -1 && !item.reversal
);
const allItems = lineItems.filter(item => LINE_ITEMS.indexOf(item.code) === -1 && !item.reversal);

const items = isProvider
? allItems.filter(item => item.includeFor.includes('provider'))
Expand Down Expand Up @@ -52,7 +50,7 @@ const LineItemUnknownItemsMaybe = props => {
};

LineItemUnknownItemsMaybe.propTypes = {
transaction: propTypes.transaction.isRequired,
lineItems: propTypes.lineItems.isRequired,
intl: intlShape.isRequired,
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/OrderBreakdown/OrderBreakdown.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const CURRENCY = config.currency;
const exampleBooking = attributes => {
return {
id: new UUID('example-booking'),
type: 'payment',
type: 'booking',
attributes,
};
};
Expand Down
31 changes: 12 additions & 19 deletions src/components/OrderBreakdown/OrderBreakdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ export const OrderBreakdownComponent = props => {

const isCustomer = userRole === 'customer';
const isProvider = userRole === 'provider';
const lineItems = transaction.attributes.lineItems;

const hasCommissionLineItem = transaction.attributes.lineItems.find(item => {
const hasCommissionLineItem = lineItems.find(item => {
const hasCustomerCommission = isCustomer && item.code === LINE_ITEM_CUSTOMER_COMMISSION;
const hasProviderCommission = isProvider && item.code === LINE_ITEM_PROVIDER_COMMISSION;
return (hasCustomerCommission || hasProviderCommission) && !item.reversal;
Expand Down Expand Up @@ -96,37 +97,29 @@ export const OrderBreakdownComponent = props => {
<div className={classes}>
<LineItemBookingPeriod booking={booking} unitType={unitType} dateType={dateType} />

<LineItemBasePriceMaybe transaction={transaction} unitType={unitType} intl={intl} />
<LineItemShippingFeeMaybe transaction={transaction} intl={intl} />
<LineItemPickupFeeMaybe transaction={transaction} intl={intl} />
<LineItemUnknownItemsMaybe transaction={transaction} isProvider={isProvider} intl={intl} />
<LineItemBasePriceMaybe lineItems={lineItems} unitType={unitType} intl={intl} />
<LineItemShippingFeeMaybe lineItems={lineItems} intl={intl} />
<LineItemPickupFeeMaybe lineItems={lineItems} intl={intl} />
<LineItemUnknownItemsMaybe lineItems={lineItems} isProvider={isProvider} intl={intl} />

<LineItemSubTotalMaybe
transaction={transaction}
lineItems={lineItems}
unitType={unitType}
userRole={userRole}
intl={intl}
/>
<LineItemRefundMaybe transaction={transaction} intl={intl} />
<LineItemRefundMaybe lineItems={lineItems} intl={intl} />

<LineItemCustomerCommissionMaybe
transaction={transaction}
isCustomer={isCustomer}
intl={intl}
/>
<LineItemCustomerCommissionMaybe lineItems={lineItems} isCustomer={isCustomer} intl={intl} />
<LineItemCustomerCommissionRefundMaybe
transaction={transaction}
lineItems={lineItems}
isCustomer={isCustomer}
intl={intl}
/>

<LineItemProviderCommissionMaybe
transaction={transaction}
isProvider={isProvider}
intl={intl}
/>
<LineItemProviderCommissionMaybe lineItems={lineItems} isProvider={isProvider} intl={intl} />
<LineItemProviderCommissionRefundMaybe
transaction={transaction}
lineItems={lineItems}
isProvider={isProvider}
intl={intl}
/>
Expand Down
Loading

0 comments on commit 9cf7255

Please sign in to comment.