Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
convert vehicle lifecycle sample to es7 syntax (#141)
Browse files Browse the repository at this point in the history
* convert vehicle lifecycle sample to es7 syntax

Signed-off-by: andrew-coleman <[email protected]>

* remove globals from .eslintrc and add to directive in code comments

Signed-off-by: andrew-coleman <[email protected]>
  • Loading branch information
andrew-coleman authored and Dave Kelsey committed Jan 15, 2018
1 parent 46590cb commit a8ccbf7
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 487 deletions.
1 change: 0 additions & 1 deletion packages/vehicle-lifecycle-network/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
coverage
dist
go
lib
node_modules
out
10 changes: 3 additions & 7 deletions packages/vehicle-lifecycle-network/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ env:
mocha: true
extends: 'eslint:recommended'
parserOptions:
ecmaVersion: 8
sourceType:
- script
globals:
getAssetRegistry: true
getFactory: true
getParticipantRegistry: true
getCurrentParticipant: true
rules:
indent:
- error
Expand All @@ -25,13 +21,13 @@ rules:
- error
- always
no-unused-vars:
- 0
- error
- args: none
no-var: error
no-console: off
curly: error
eqeqeq: error
no-throw-literal: error
strict: error
dot-notation: error
no-tabs: error
no-trailing-spaces: error
Expand Down
134 changes: 60 additions & 74 deletions packages/vehicle-lifecycle-network/lib/manufacturer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,109 +12,95 @@
* limitations under the License.
*/

/* global getFactory getAssetRegistry emit */

/**
* Place an order for a vehicle
* @param {org.acme.vehicle.lifecycle.manufacturer.PlaceOrder} placeOrder - the PlaceOrder transaction
* @transaction
*/
function placeOrder(placeOrder) {
async function placeOrder(placeOrder) { // eslint-disable-line no-unused-vars
console.log('placeOrder');

var factory = getFactory();
var NS_M = 'org.acme.vehicle.lifecycle.manufacturer';
var NS = 'org.acme.vehicle.lifecycle';
var NS_D = 'org.vda';
const factory = getFactory();
const NS_M = 'org.acme.vehicle.lifecycle.manufacturer';
const NS = 'org.acme.vehicle.lifecycle';

var order = factory.newResource(NS_M, 'Order', placeOrder.orderId);
const order = factory.newResource(NS_M, 'Order', placeOrder.orderId);
order.vehicleDetails = placeOrder.vehicleDetails;
order.orderStatus = 'PLACED';
order.manufacturer = placeOrder.manufacturer;
order.orderer = factory.newRelationship(NS, 'PrivateOwner', placeOrder.orderer.getIdentifier());

// save the order
return getAssetRegistry(order.getFullyQualifiedType())
.then(function (registry) {
return registry.add(order);
})
.then(function(){
var placeOrderEvent = factory.newEvent(NS_M, 'PlaceOrderEvent');
placeOrderEvent.orderId = order.orderId;
placeOrderEvent.vehicleDetails = order.vehicleDetails;
emit(placeOrderEvent);
});
const registry = await getAssetRegistry(order.getFullyQualifiedType());
await registry.add(order);
const placeOrderEvent = factory.newEvent(NS_M, 'PlaceOrderEvent');
placeOrderEvent.orderId = order.orderId;
placeOrderEvent.vehicleDetails = order.vehicleDetails;
emit(placeOrderEvent);
}

/**
* Update the status of an order
* @param {org.acme.vehicle.lifecycle.manufacturer.UpdateOrderStatus} updateOrderStatus - the UpdateOrderStatus transaction
* @transaction
*/
function updateOrderStatus(updateOrderStatus) {
async function updateOrderStatus(updateOrderStatus) { // eslint-disable-line no-unused-vars
console.log('updateOrderStatus');

var factory = getFactory();
var NS_M = 'org.acme.vehicle.lifecycle.manufacturer';
var NS = 'org.acme.vehicle.lifecycle';
var NS_D = 'org.vda';
const factory = getFactory();
const NS_M = 'org.acme.vehicle.lifecycle.manufacturer';
const NS = 'org.acme.vehicle.lifecycle';
const NS_D = 'org.vda';

// save the new status of the order
updateOrderStatus.order.orderStatus = updateOrderStatus.orderStatus;

// get vehicle registry
return getAssetRegistry(NS_D + '.Vehicle')
.then(function(registry) {
if (updateOrderStatus.orderStatus === 'VIN_ASSIGNED') {
var vehicle = factory.newResource(NS_D, 'Vehicle', updateOrderStatus.vin );
vehicle.vehicleDetails = updateOrderStatus.order.vehicleDetails;
vehicle.vehicleDetails.vin = updateOrderStatus.vin;
vehicle.vehicleStatus = 'OFF_THE_ROAD';
return registry.add(vehicle);
} else if(updateOrderStatus.orderStatus === 'OWNER_ASSIGNED') {
if (!updateOrderStatus.order.orderer.vehicles) {
updateOrderStatus.order.orderer.vehicles = [];
}
// get vehicle registry
const registry = await getAssetRegistry(NS_D + '.Vehicle');
if (updateOrderStatus.orderStatus === 'VIN_ASSIGNED') {
const vehicle = factory.newResource(NS_D, 'Vehicle', updateOrderStatus.vin);
vehicle.vehicleDetails = updateOrderStatus.order.vehicleDetails;
vehicle.vehicleDetails.vin = updateOrderStatus.vin;
vehicle.vehicleStatus = 'OFF_THE_ROAD';
return registry.add(vehicle);
} else if (updateOrderStatus.orderStatus === 'OWNER_ASSIGNED') {
if (!updateOrderStatus.order.orderer.vehicles) {
updateOrderStatus.order.orderer.vehicles = [];
}

return registry.get(updateOrderStatus.vin)
.then(function(vehicle) {
vehicle.vehicleStatus = 'ACTIVE';
vehicle.owner = factory.newRelationship('org.acme.vehicle.lifecycle', 'PrivateOwner', updateOrderStatus.order.orderer.email);
vehicle.numberPlate = updateOrderStatus.numberPlate || '';
vehicle.vehicleDetails.numberPlate = updateOrderStatus.numberPlate || '';
vehicle.vehicleDetails.v5c = updateOrderStatus.v5c || '';
if (!vehicle.logEntries) {
vehicle.logEntries = [];
}
var logEntry = factory.newConcept(NS_D, 'VehicleTransferLogEntry');
logEntry.vehicle = factory.newRelationship(NS_D, 'Vehicle', updateOrderStatus.vin);
logEntry.buyer = factory.newRelationship(NS, 'PrivateOwner', updateOrderStatus.order.orderer.email);
logEntry.timestamp = updateOrderStatus.timestamp;
vehicle.logEntries.push(logEntry);
return registry.update(vehicle);
});
}
})
.then(function() {
// get order registry
return getAssetRegistry(updateOrderStatus.order.getFullyQualifiedType());
})
.then(function(registry) {
// update order status
updateOrderStatus.order.vehicleDetails.vin = updateOrderStatus.vin || '';

if (!updateOrderStatus.order.statusUpdates) {
updateOrderStatus.order.statusUpdates = [];
}
const vehicle = await registry.get(updateOrderStatus.vin);
vehicle.vehicleStatus = 'ACTIVE';
vehicle.owner = factory.newRelationship('org.acme.vehicle.lifecycle', 'PrivateOwner', updateOrderStatus.order.orderer.email);
vehicle.numberPlate = updateOrderStatus.numberPlate || '';
vehicle.vehicleDetails.numberPlate = updateOrderStatus.numberPlate || '';
vehicle.vehicleDetails.v5c = updateOrderStatus.v5c || '';
if (!vehicle.logEntries) {
vehicle.logEntries = [];
}
const logEntry = factory.newConcept(NS_D, 'VehicleTransferLogEntry');
logEntry.vehicle = factory.newRelationship(NS_D, 'Vehicle', updateOrderStatus.vin);
logEntry.buyer = factory.newRelationship(NS, 'PrivateOwner', updateOrderStatus.order.orderer.email);
logEntry.timestamp = updateOrderStatus.timestamp;
vehicle.logEntries.push(logEntry);
await registry.update(vehicle);
}

updateOrderStatus.order.statusUpdates.push(updateOrderStatus);
// get order registry
const orderRegistry = await getAssetRegistry(updateOrderStatus.order.getFullyQualifiedType());
// update order status
updateOrderStatus.order.vehicleDetails.vin = updateOrderStatus.vin || '';

return registry.update(updateOrderStatus.order);
})
.then(function(){
var updateOrderStatusEvent = factory.newEvent(NS_M, 'UpdateOrderStatusEvent');
updateOrderStatusEvent.orderStatus = updateOrderStatus.order.orderStatus;
updateOrderStatusEvent.order = updateOrderStatus.order;
emit(updateOrderStatusEvent);
});

if (!updateOrderStatus.order.statusUpdates) {
updateOrderStatus.order.statusUpdates = [];
}

updateOrderStatus.order.statusUpdates.push(updateOrderStatus);

await orderRegistry.update(updateOrderStatus.order);
const updateOrderStatusEvent = factory.newEvent(NS_M, 'UpdateOrderStatusEvent');
updateOrderStatusEvent.orderStatus = updateOrderStatus.order.orderStatus;
updateOrderStatusEvent.order = updateOrderStatus.order;
emit(updateOrderStatusEvent);
}
Loading

0 comments on commit a8ccbf7

Please sign in to comment.