Skip to content

Commit

Permalink
[Feat] exportToNormalEntity
Browse files Browse the repository at this point in the history
Created univertial utility method for converting entity exports to
normal usable but not frozen data.

References #59, #60, #61 and #62.
  • Loading branch information
angel-penchev committed Feb 2, 2021
1 parent 426e390 commit 05b5326
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 53 deletions.
33 changes: 33 additions & 0 deletions server/services/core/entities/utilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Converts an entity export object to normal object.
*
* @export
* @template T - export object type
* @template U - normalized object type
* @param {T} object - export object
* @return {U} - normalized object
*/
export function exportToNormalEntity<T extends Object, U extends Object>(
object: T,
): U {
// @ts-ignore ts(2322) - This is so we still get return typing without TS
// complaining about potential type mismatch.
return Object.fromEntries(
Object.entries(object).map((entry) => entry[0].startsWith('get') ? [
// Converting object key from 'getKeyName' to 'keyName'
entry[0].replace(/get[A-Z]/, entry[0][3].toLowerCase()),

// Checking whether there is a nested object within the value. If there
// is, this function is run on it. Otherwise only the result from the
// getter is stored.
typeof entry[1] === 'object' ? exportToNormalEntity(entry[1]()) :
entry[1](),
] : []),
);
}

const entityUtilities = Object.freeze({
exportToNormalEntity: exportToNormalEntity,
});

export default entityUtilities;
29 changes: 21 additions & 8 deletions server/services/delivery-service/src/entities/delivery/delivery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import {Delivery, Validator} from '../../../../core/@types/global';
import {
Delivery,
LocationExport,
Validator,
} from '../../../../core/@types/global';
import {exportToNormalEntity} from '../../../../core/entities/utilities';

// Defining the maximum flight distance in kilometers.
const maxDistanceRoute = 5;
Expand Down Expand Up @@ -26,7 +31,7 @@ export default function buildCreateDelivery({
// Internal parameters
const id = generateIdentifier();
const createdOn = Date.now();
let completedOn: number = undefined;
let completedOn: number | undefined = undefined;

// Construction data validation
// Identifier validation
Expand All @@ -49,7 +54,9 @@ export default function buildCreateDelivery({
}

try {
validator.validateLocation(drone.getHomeLocation());
validator.validateLocation(
exportToNormalEntity(drone.getHomeLocation()),
);
} catch (e) {
throw new Error('Delivery drone location error: ' + e.message);
}
Expand All @@ -71,7 +78,7 @@ export default function buildCreateDelivery({
// Route length validation
try {
validator.validateRoute(
drone.getHomeLocation(),
exportToNormalEntity(drone.getHomeLocation()),
senderLocation,
receiverLocation,
maxDistanceRoute,
Expand All @@ -87,10 +94,16 @@ export default function buildCreateDelivery({

// Module exporting
return Object.freeze({
getId: () => id,
getOrderId: () => orderId,
getSenderLocation: () => senderLocation,
getReceiverLocation: () => receiverLocation,
getId: (): string => id,
getOrderId: (): string => orderId,
getSenderLocation: (): LocationExport => Object.freeze({
getLatitude: (): number => senderLocation.latitude,
getLongitude: (): number => senderLocation.longitude,
}),
getReceiverLocation: (): LocationExport => Object.freeze({
getLatitude: (): number => receiverLocation.latitude,
getLongitude: (): number => receiverLocation.longitude,
}),
getDrone: () => drone,
getCreatedOn: () => createdOn,
getCompletedOn: () => completedOn,
Expand Down

This file was deleted.

17 changes: 11 additions & 6 deletions server/services/order-service/src/usecases/create-order.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
Order,
SharedQueue,
DatabaseController,
OrderDatabaseController,
OrderExport,
} from '../../../core/@types/global';
import makeOrder, {decompressOrder} from '../entities/order';
import makeOrder from '../entities/order';
import config from '../../../core/config';
import {exportToNormalEntity} from '../../../core/entities/utilities';

/**
* Handles a user order request, stores it in local db and notifies other
Expand All @@ -20,26 +22,29 @@ import config from '../../../core/config';
export default function buildCreateOrder({
sharedQueue,
ordersDatabase,
}: {sharedQueue: SharedQueue, ordersDatabase: DatabaseController}): Function {
}: {
sharedQueue: SharedQueue,
ordersDatabase: OrderDatabaseController
}): Function {
return async function createOrder(orderInfo: Order) {
// Internal parameter
let order;

// Emitting an 'ORDER_DENIED' event in shared queue on invalid order
// Emitting an 'ORDER_DECLINED' event in shared queue on invalid order
try {
order = makeOrder(orderInfo);
} catch (e) {
sharedQueue.emit([
config.inboundLoggerServiceQueue,
], {
subject: 'ORDER_DENIED',
subject: 'ORDER_DECLINED',
body: e.message,
});
throw e;
}

// Emitting an 'ORDER_APPROVED' event in shared queue on valid order
const decompressedOrder = decompressOrder(order);
const decompressedOrder = exportToNormalEntity<OrderExport, Order>(order);
sharedQueue.emit([
config.inboundPaymentServiceQueue,
config.inboundDeliveryServiceQueue,
Expand Down

0 comments on commit 05b5326

Please sign in to comment.