Add tx helpers related to sending a review

This commit is contained in:
Vesa Luusua 2019-01-22 21:48:16 +02:00
parent c33afc9b0c
commit 538faf945d
2 changed files with 17 additions and 12 deletions

View file

@ -7,12 +7,11 @@ import { types as sdkTypes } from '../../util/sdkLoader';
import { isTransactionsTransitionInvalidTransition, storableError } from '../../util/errors';
import {
txIsEnquired,
getReview1Transition,
getReview2Transition,
txHasFirstReviewBy,
TRANSITION_ACCEPT,
TRANSITION_DECLINE,
TRANSITION_REVIEW_1_BY_CUSTOMER,
TRANSITION_REVIEW_1_BY_PROVIDER,
TRANSITION_REVIEW_2_BY_CUSTOMER,
TRANSITION_REVIEW_2_BY_PROVIDER,
} from '../../util/transaction';
import * as log from '../../util/log';
import {
@ -443,8 +442,7 @@ const IMAGE_VARIANTS = {
// If other party has already sent a review, we need to make transition to
// TRANSITION_REVIEW_2_BY_<CUSTOMER/PROVIDER>
const sendReviewAsSecond = (id, params, role, dispatch, sdk) => {
const transition =
role === CUSTOMER ? TRANSITION_REVIEW_2_BY_CUSTOMER : TRANSITION_REVIEW_2_BY_PROVIDER;
const transition = getReview2Transition(role === CUSTOMER);
const include = REVIEW_TX_INCLUDES;
@ -470,8 +468,7 @@ const sendReviewAsSecond = (id, params, role, dispatch, sdk) => {
// So, error is likely to happen and then we must try another state transition
// by calling sendReviewAsSecond().
const sendReviewAsFirst = (id, params, role, dispatch, sdk) => {
const transition =
role === CUSTOMER ? TRANSITION_REVIEW_1_BY_CUSTOMER : TRANSITION_REVIEW_1_BY_PROVIDER;
const transition = getReview1Transition(role === CUSTOMER);
const include = REVIEW_TX_INCLUDES;
return sdk.transactions
@ -498,10 +495,7 @@ const sendReviewAsFirst = (id, params, role, dispatch, sdk) => {
export const sendReview = (role, tx, reviewRating, reviewContent) => (dispatch, getState, sdk) => {
const params = { reviewRating, reviewContent };
const txStateOtherPartyFirst =
role === CUSTOMER
? tx.attributes.lastTransition === TRANSITION_REVIEW_1_BY_PROVIDER
: tx.attributes.lastTransition === TRANSITION_REVIEW_1_BY_CUSTOMER;
const txStateOtherPartyFirst = txHasFirstReviewBy(tx, role !== CUSTOMER);
dispatch(sendReviewRequest());

View file

@ -74,8 +74,19 @@ export const txIsCompleted = tx => txLastTransition(tx) === TRANSITION_COMPLETE;
const firstReviewTransitions = [TRANSITION_REVIEW_1_BY_CUSTOMER, TRANSITION_REVIEW_1_BY_PROVIDER];
export const txHasFirstReview = tx => firstReviewTransitions.includes(txLastTransition(tx));
export const txHasFirstReviewBy = (tx, isCustomer) =>
isCustomer
? txLastTransition(tx) === TRANSITION_REVIEW_1_BY_CUSTOMER
: txLastTransition(tx) === TRANSITION_REVIEW_1_BY_PROVIDER;
export const txIsReviewed = tx => areReviewsCompleted(txLastTransition(tx));
export const getReview1Transition = isCustomer =>
isCustomer ? TRANSITION_REVIEW_1_BY_CUSTOMER : TRANSITION_REVIEW_1_BY_PROVIDER;
export const getReview2Transition = isCustomer =>
isCustomer ? TRANSITION_REVIEW_2_BY_CUSTOMER : TRANSITION_REVIEW_2_BY_PROVIDER;
// Check if tx transition is followed by a state where
// reviews are completed
export const areReviewsCompleted = transition => {