function onEndAuction(endAuction) {

    return getAssetRegistry('outbound.logistics.auction.TransportContractAuction').then(function (transportContractAuctionRegistry) {
        return transportContractAuctionRegistry.get(endAuction.auction.transportContractAuctionId).then(function (auction) {
            auction.auctionComplete = true;
            auction.endDate = endAuction.timestamp;
            return transportContractAuctionRegistry.update(auction);
        });
    }).then(function () {
        var factory = getFactory();
        var transportContract = factory.newResource('outbound.logistics.auction', 'TransportContract', endAuction.contractIdToCreate);
        return query('AllBidsOnAuction', { auction: 'resource:outbound.logistics.auction.TransportContractAuction#' + endAuction.auction.transportContractAuctionId }).then(function (bids) {
            var topBid = bids[0];
            bids.forEach(function (bid) {
                if (bid.bidValue > topBid.bidValue) {
                    topBid = bid;
                }
            });
            return topBid;
        }).then(function (topBid) {
            transportContract.transportContractId = endAuction.contractIdToCreate;
            transportContract.creationDate = endAuction.timestamp;
            transportContract.paymentAmount = topBid.bidValue;
            transportContract.route = endAuction.auction.route;
            transportContract.vehicle = endAuction.auction.vehicle;
            transportContract.managingLogisticsTeam = endAuction.auction.managingLogisticsTeam;
            transportContract.transportationCompany = topBid.bidder;

            return getAssetRegistry('outbound.logistics.auction.TransportContract').then(function (transportContractRegistry) {
                return transportContractRegistry.add(transportContract);
            });
        }).then(function () {
            return getAssetRegistry('outbound.logistics.vehicle.Car').then(function (carRegistry) {
                return carRegistry.get(endAuction.auction.vehicle.vin).then(function (vehicle) {

                    vehicle.transportPlan.routes.forEach(function (route, index) {
                        if (route.routeId === endAuction.auction.route.routeId) {
                            vehicle.transportPlan.routes[index].associatedContract = transportContract;

                            return carRegistry.update(vehicle);
                        }
                    });

                });
            });
        });
    });
}
/**
 * Update the auction so that it has ended.
 * Create a transport contract for the winning bidder and associate it with the correct route.
 * Update the car's transport plan so it has a contract associated with it.
 * @param {outbound.logistics.auction.EndAuction} endAuction
 * @transaction
 */
Update the auction so that it has ended.
Create a transport contract for the winning bidder and associate it with the correct route.
Update the car's transport plan so it has a contract associated with it.
Update the auction so that it has ended . Create a transport contract for the winning bidder and associate it with the correct route . Update the car ' s transport plan so it has a contract associated with it .
function onEndAuction(endAuction) {
    return getAssetRegistry('outbound.logistics.auction.TransportContractAuction').then(function (transportContractAuctionRegistry) {
        return transportContractAuctionRegistry.get(endAuction.auction.transportContractAuctionId).then(function (auction) {
            auction.auctionComplete = true;
            auction.endDate = endAuction.timestamp;
            return transportContractAuctionRegistry.update(auction);
        });
    }).then(function () {
        var factory = getFactory();
        var transportContract = factory.newResource('outbound.logistics.auction', 'TransportContract', endAuction.contractIdToCreate);
        return query('AllBidsOnAuction', { auction: 'resource:outbound.logistics.auction.TransportContractAuction#' + endAuction.auction.transportContractAuctionId }).then(function (bids) {
            var topBid = bids[0];
            bids.forEach(function (bid) {
                if (bid.bidValue > topBid.bidValue) {
                    topBid = bid;
                }
            });
            return topBid;
        }).then(function (topBid) {
            transportContract.transportContractId = endAuction.contractIdToCreate;
            transportContract.creationDate = endAuction.timestamp;
            transportContract.paymentAmount = topBid.bidValue;
            transportContract.route = endAuction.auction.route;
            transportContract.vehicle = endAuction.auction.vehicle;
            transportContract.managingLogisticsTeam = endAuction.auction.managingLogisticsTeam;
            transportContract.transportationCompany = topBid.bidder;
            return getAssetRegistry('outbound.logistics.auction.TransportContract').then(function (transportContractRegistry) {
                return transportContractRegistry.add(transportContract);
            });
        }).then(function () {
            return getAssetRegistry('outbound.logistics.vehicle.Car').then(function (carRegistry) {
                return carRegistry.get(endAuction.auction.vehicle.vin).then(function (vehicle) {
                    vehicle.transportPlan.routes.forEach(function (route, index) {
                        if (route.routeId === endAuction.auction.route.routeId) {
                            vehicle.transportPlan.routes[index].associatedContract = transportContract;
                            return carRegistry.update(vehicle);
                        }
                    });
                });
            });
        });
    });
}
function onEndAuction ( endAuction ) { return getAssetRegistry ( 'outbound.logistics.auction.TransportContractAuction' ) . then ( function ( transportContractAuctionRegistry ) { return transportContractAuctionRegistry . get ( endAuction . auction . transportContractAuctionId ) . then ( function ( auction ) { auction . auctionComplete = true ; auction . endDate = endAuction . timestamp ; return transportContractAuctionRegistry . update ( auction ) ; } ) ; } ) . then ( function ( ) { var factory = getFactory ( ) ; var transportContract = factory . newResource ( 'outbound.logistics.auction' , 'TransportContract' , endAuction . contractIdToCreate ) ; return query ( 'AllBidsOnAuction' , { auction : 'resource:outbound.logistics.auction.TransportContractAuction#' + endAuction . auction . transportContractAuctionId } ) . then ( function ( bids ) { var topBid = bids [ 0 ] ; bids . forEach ( function ( bid ) { if ( bid . bidValue > topBid . bidValue ) { topBid = bid ; } } ) ; return topBid ; } ) . then ( function ( topBid ) { transportContract . transportContractId = endAuction . contractIdToCreate ; transportContract . creationDate = endAuction . timestamp ; transportContract . paymentAmount = topBid . bidValue ; transportContract . route = endAuction . auction . route ; transportContract . vehicle = endAuction . auction . vehicle ; transportContract . managingLogisticsTeam = endAuction . auction . managingLogisticsTeam ; transportContract . transportationCompany = topBid . bidder ; return getAssetRegistry ( 'outbound.logistics.auction.TransportContract' ) . then ( function ( transportContractRegistry ) { return transportContractRegistry . add ( transportContract ) ; } ) ; } ) . then ( function ( ) { return getAssetRegistry ( 'outbound.logistics.vehicle.Car' ) . then ( function ( carRegistry ) { return carRegistry . get ( endAuction . auction . vehicle . vin ) . then ( function ( vehicle ) { vehicle . transportPlan . routes . forEach ( function ( route , index ) { if ( route . routeId === endAuction . auction . route . routeId ) { vehicle . transportPlan . routes [ index ] . associatedContract = transportContract ; return carRegistry . update ( vehicle ) ; } } ) ; } ) ; } ) ; } ) ; } ) ; }
Update the auction so that it has ended.
Update the auction so that it has ended .