Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core) unit tests for flow service (EWC-383) #469

Merged
merged 1 commit into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 51 additions & 48 deletions src/services/flow-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ const FlowManager = require('../sequelize/managers/flow-manager');
const AppHelper = require('../helpers/app-helper');
const Errors = require('../helpers/errors');
const ErrorMessages = require('../helpers/error-messages');
const Validation = require('../schemas');
const Validator = require('../schemas');
const ChangeTrackingService = require('./change-tracking-service');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

const _createFlow = async function (flowData, user, isCLI, transaction) {
await Validation.validate(flowData, Validation.schemas.flowCreate);
const createFlow = async function (flowData, user, isCLI, transaction) {
await Validator.validate(flowData, Validator.schemas.flowCreate);

await _checkForDuplicateName(flowData.name, {}, user.id, transaction);
await _checkForDuplicateName(flowData.name, null, user.id, transaction);

const flowToCreate = {
name: flowData.name,
Expand All @@ -40,7 +42,7 @@ const _createFlow = async function (flowData, user, isCLI, transaction) {
}
};

const _deleteFlow = async function (flowId, user, isCLI, transaction) {
const deleteFlow = async function (flowId, user, isCLI, transaction) {
const whereObj = {
id: flowId,
userId: user.id
Expand All @@ -52,25 +54,10 @@ const _deleteFlow = async function (flowId, user, isCLI, transaction) {
await FlowManager.delete(where, transaction);
};

async function _updateChangeTrackingsByFlowId(flowId, transaction) {
const flowWithMicroservices = await FlowManager.findFlowMicroservices({id: flowId}, transaction);
if (!flowWithMicroservices) {
throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_FLOW_ID, flowId));
}
const onlyUnique = (value, index, self) => self.indexOf(value) === index;
const iofogUuids = flowWithMicroservices.microservices
.map(obj => obj.iofogUuid)
.filter(onlyUnique)
.filter(val => val !== null);
for (let iofogUuid of iofogUuids) {
await ChangeTrackingService.update(iofogUuid, ChangeTrackingService.events.microserviceFull, transaction);
}
}

const _updateFlow = async function (flowData, flowId, user, isCLI, transaction) {
await Validation.validate(flowData, Validation.schemas.flowUpdate);
const updateFlow = async function (flowData, flowId, user, isCLI, transaction) {
await Validator.validate(flowData, Validator.schemas.flowUpdate);

const oldFlow = await _getFlow(flowId, user, isCLI, transaction);
const oldFlow = await getFlow(flowId, user, isCLI, transaction);
if (!oldFlow) {
throw new Errors.NotFoundError(ErrorMessages.INVALID_FLOW_ID)
}
Expand All @@ -97,20 +84,7 @@ const _updateFlow = async function (flowData, flowId, user, isCLI, transaction)
}
};

const _getFlow = async function (flowId, user, isCLI, transaction) {
const where = isCLI
? {id: flowId}
: {id: flowId, userId: user.id};

const flow = await FlowManager.findOneExcludeFields(where, transaction);

if (!flow) {
throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_FLOW_ID, flowId))
}
return flow
};

const _getUserFlows = async function (user, isCLI, transaction) {
const getUserFlows = async function (user, isCLI, transaction) {
const flow = {
userId: user.id
};
Expand All @@ -121,17 +95,31 @@ const _getUserFlows = async function (user, isCLI, transaction) {
}
};

const _getAllFlows = async function (isCLI, transaction) {
const getAllFlows = async function (isCLI, transaction) {
const flows = await FlowManager.findAll({}, transaction);
return {
flows: flows
}
};

const _checkForDuplicateName = async function (name, item, userId, transaction) {
const getFlow = async function (flowId, user, isCLI, transaction) {
const where = isCLI
? {id: flowId}
: {id: flowId, userId: user.id};

const flow = await FlowManager.findOneExcludeFields(where, transaction);

if (!flow) {
throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_FLOW_ID, flowId))
}
return flow
};


const _checkForDuplicateName = async function (name, flowId, userId, transaction) {
if (name) {
const where = item.id
? {name: name, id: {[Op.ne]: item.id, userId: userId}}
const where = flowId
? {name: name, id: {[Op.ne]: flowId, userId: userId}}
: {name: name, userId: userId};

const result = await FlowManager.findOne(where, transaction);
Expand All @@ -141,12 +129,27 @@ const _checkForDuplicateName = async function (name, item, userId, transaction)
}
};

async function _updateChangeTrackingsByFlowId(flowId, transaction) {
const flowWithMicroservices = await FlowManager.findFlowMicroservices({id: flowId}, transaction);
if (!flowWithMicroservices) {
throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_FLOW_ID, flowId));
}
const onlyUnique = (value, index, self) => self.indexOf(value) === index;
const iofogUuids = flowWithMicroservices.microservices
.map(obj => obj.iofogUuid)
.filter(onlyUnique)
.filter(val => val !== null);
for (const iofogUuid of iofogUuids) {
await ChangeTrackingService.update(iofogUuid, ChangeTrackingService.events.microserviceFull, transaction);
}
}

module.exports = {
createFlow: TransactionDecorator.generateTransaction(_createFlow),
deleteFlow: TransactionDecorator.generateTransaction(_deleteFlow),
updateFlow: TransactionDecorator.generateTransaction(_updateFlow),
getFlowWithTransaction: TransactionDecorator.generateTransaction(_getFlow),
getUserFlows: TransactionDecorator.generateTransaction(_getUserFlows),
getAllFlows: TransactionDecorator.generateTransaction(_getAllFlows),
getFlow: _getFlow
createFlow: TransactionDecorator.generateTransaction(createFlow),
deleteFlow: TransactionDecorator.generateTransaction(deleteFlow),
updateFlow: TransactionDecorator.generateTransaction(updateFlow),
getUserFlows: TransactionDecorator.generateTransaction(getUserFlows),
getAllFlows: TransactionDecorator.generateTransaction(getAllFlows),
getFlowWithTransaction: TransactionDecorator.generateTransaction(getFlow),
getFlow: getFlow
};
Loading