import { Transaction , User , TransactionRequestStatus , NotificationType , PaymentNotificationStatus , TransactionResponseItem , TransactionQueryPayload , TransactionDateRangePayload , TransactionAmountRangePayload , LikeNotification , CommentNotification , } from "../models" ; import faker from "faker" ; import Dinero from "dinero.js" ; import { flow , get , isEmpty , negate , curry , isEqual , join , pick , values , has , find , omit , map , drop , } from "lodash/fp" ; export const isRequestTransaction = ( transaction ) => flow ( get ( "requestStatus" ) , negate ( isEmpty ) ) ( transaction ) ; export const isPendingRequestTransaction = ( transaction ) => flow ( get ( "requestStatus" ) , isEqual ( TransactionRequestStatus . pending ) ) ( transaction ) ; export const isAcceptedRequestTransaction = ( transaction ) => flow ( get ( "requestStatus" ) , isEqual ( TransactionRequestStatus . accepted ) ) ( transaction ) ; export const isRejectedRequestTransaction = ( transaction ) => flow ( get ( "requestStatus" ) , isEqual ( TransactionRequestStatus . rejected ) ) ( transaction ) ; export const isPayment = negate ( isRequestTransaction ) ; export const getFakeAmount = ( min = 1000 , max = 50000 ) => parseInt ( faker . finance . amount ( min , max ) , 10 ) ; export const formatAmount = ( amount ) => Dinero ( { amount } ) . toFormat ( ) ; export const formatAmountSlider = ( amount ) => Dinero ( { amount } ) . toFormat ( "$0,0" ) ; export const payAppDifference = curry ( ( sender , transaction ) => Dinero ( { amount : get ( "balance" , sender ) } ) . subtract ( Dinero ( { amount : get ( "amount" , transaction ) } ) ) ) ; export const payAppAddition = curry ( ( sender , transaction ) => Dinero ( { amount : get ( "balance" , sender ) } ) . add ( Dinero ( { amount : get ( "amount" , transaction ) } ) ) ) ; export const getChargeAmount = ( sender , transaction ) => Math . abs ( payAppDifference ( sender , transaction ) . getAmount ( ) ) ; export const getTransferAmount = curry ( ( sender , transaction ) => Math . abs ( payAppDifference ( sender , transaction ) . getAmount ( ) ) ) ; export const getPayAppCreditedAmount = ( receiver , transaction ) => Math . abs ( payAppAddition ( receiver , transaction ) . getAmount ( ) ) ; export const hasSufficientFunds = ( sender , transaction ) => payAppDifference ( sender , transaction ) . isPositive ( ) ; export const receiverIsCurrentUser = ( currentUser , transaction ) => isEqual ( get ( "id" , currentUser ) , get ( "receiverId" , transaction ) ) ; export const formatFullName = ( user ) => flow ( pick ( [ "firstName" , "lastName" ] ) , values , join ( " " ) ) ( user ) ; export const isCommentNotification = ( notification ) : notification is CommentNotification => has ( "commentId" ) ( notification ) ; export const isLikeNotification = ( notification ) : notification is LikeNotification => has ( "likeId" ) ( notification ) ; export const isPaymentNotification = ( notification ) => has ( "status" ) ( notification ) ; export const isPaymentRequestedNotification = ( notification ) => flow ( get ( "status" ) , isEqual ( PaymentNotificationStatus . requested ) ) ( notification ) ; export const isPaymentReceivedNotification = ( notification ) => flow ( get ( "status" ) , isEqual ( PaymentNotificationStatus . received ) ) ( notification ) ; export const currentUserLikesTransaction = ( currentUser , transaction ) => flow ( find ( ( like ) => flow ( get ( "userId" ) , isEqual ( get ( "id" , currentUser ) ) ) ( like ) ) , negate ( isEmpty ) ) ( transaction . likes ) ; export const hasDateQueryFields = ( query ) => has ( "dateRangeStart" , query ) && has ( "dateRangeEnd" , query ) ; export const getDateQueryFields = ( query ) => pick ( [ "dateRangeStart" , "dateRangeEnd" ] , query ) ; export const omitDateQueryFields = ( query ) => omit ( [ "dateRangeStart" , "dateRangeEnd" ] , query ) ; export const hasAmountQueryFields = ( query ) => has ( "amountMin" , query ) && has ( "amountMax" , query ) ; export const getAmountQueryFields = ( query ) => pick ( [ "amountMin" , "amountMax" ] , query ) ; export const omitAmountQueryFields = ( query ) => omit ( [ "amountMin" , "amountMax" ] , query ) ; export const hasPaginationQueryFields = ( query ) => has ( "page" , query ) && has ( "limit" , query ) ; export const omitPaginationQueryFields = ( query ) => omit ( [ "page" , "limit" ] , query ) ; export const getQueryWithoutDateFields = ( query ) => query && hasDateQueryFields ( query ) ? omitDateQueryFields ( query ) : query ; export const getQueryWithoutAmountFields = ( query ) => query && hasAmountQueryFields ( query ) ? omitAmountQueryFields ( query ) : query ; export const getQueryWithoutFilterFields = ( query ) => flow ( omitAmountQueryFields , omitDateQueryFields , omitPaginationQueryFields ) ( query ) ; export const padAmountWithZeros = ( number ) => Math . ceil ( number * 1000 ) ; export const amountRangeValueText = ( value ) => flow ( padAmountWithZeros , formatAmount ) ( value ) ; export const amountRangeValueTextLabel = ( value ) => flow ( padAmountWithZeros , formatAmountSlider ) ( value ) ; export const formatAmountRangeValues = ( amountRangeValues ) => flow ( map ( padAmountWithZeros ) , map ( formatAmountSlider ) , join ( " - " ) ) ( amountRangeValues ) ; export const getPaginatedItems = ( page , limit , items ) => { const offset = ( page - 1 ) * limit ; const pagedItems = drop ( offset , items ) . slice ( 0 , limit ) ; return { totalPages : Math . ceil ( items . length / limit ) , data : pagedItems , } ; } ; export const startOfDayUTC = ( date ) => new Date ( new Date ( date ) . setUTCHours ( 0 , 0 , 0 , 0 ) ) ; export const endOfDayUTC = ( date ) => new Date ( new Date ( date ) . setUTCHours ( 23 , 59 , 59 , 999 ) ) ;