From 9a6b8a1572ae2e4c754bdbde528ee0da8a8d1ad9 Mon Sep 17 00:00:00 2001 From: Matias Date: Wed, 3 Jul 2019 20:10:19 -0300 Subject: [PATCH] Add and remove bot to ecosystem --- src/schema/mutations/AddBot.js | 70 ++++++++++++++++++++++++++++++ src/schema/mutations/RemoveBot.js | 63 +++++++++++++++++++++++++++ src/schema/mutations/index.js | 6 ++- src/schema/types/input/Bot.js | 7 ++- src/schema/types/input/DataSet.js | 19 ++++++++ src/schema/types/input/Exchange.js | 15 +++++++ src/schema/types/input/Plotter.js | 17 ++++++++ src/schema/types/input/Product.js | 24 ++++++++++ src/schema/types/input/index.js | 10 ++++- 9 files changed, 227 insertions(+), 4 deletions(-) create mode 100644 src/schema/mutations/AddBot.js create mode 100644 src/schema/mutations/RemoveBot.js create mode 100644 src/schema/types/input/DataSet.js create mode 100644 src/schema/types/input/Exchange.js create mode 100644 src/schema/types/input/Plotter.js create mode 100644 src/schema/types/input/Product.js diff --git a/src/schema/mutations/AddBot.js b/src/schema/mutations/AddBot.js new file mode 100644 index 000000000..d269a1d5f --- /dev/null +++ b/src/schema/mutations/AddBot.js @@ -0,0 +1,70 @@ +import logger from '../../utils/logger' +import { BotInputType, } from '../types/input' +import { BotType, } from '../types' +import { Ecosystem } from '../../models' + +export const args = { bot: { type: BotInputType } } + +const resolve = async (parent, { bot }, context) => { + logger.debug('addBot -> Dev team: %s. Bot: %s', bot.devTeam, bot.codeName) + try { + + let userEcosystem = await Ecosystem.findOne({ + authId: context.userId + }) + + if (!userEcosystem) { + throw 'Ecosystem not found for the user.' + } + + let newBotClone + for (let i = 0; i < userEcosystem.devTeams.length; i++) { + const auxTeam = userEcosystem.devTeams[i] + if (auxTeam.codeName === bot.devTeam) { + for (let j = 0; j < auxTeam.bots.length; j++) { + const auxBot = auxTeam.bots[j] + if (auxBot.codeName === bot.codeName) { + for (let k = 0; k < auxBot.products.length; k++) { + const auxProduct = auxBot.products[k] + if (auxProduct.codeName === bot.productCodeName) { + newBotClone = { + displayName: auxBot.displayName, + codeName: auxBot.codeName + '-' + bot.cloneId, + type: auxBot.type, + profilePicture: auxBot.profilePicture, + cloneId: bot.cloneId, + products: [auxProduct] + } + auxTeam.bots.push(newBotClone) + break + } + } + break + } + } + break + } + } + + if (newBotClone) { + await userEcosystem.save() + return newBotClone + } else { + throw 'Algobot not found.' + } + + } catch (err) { + logger.error('addBot -> Error: %s', err.message) + throw err + } +} + +const mutation = { + addBot: { + type: BotType, + args, + resolve, + } +} + +export default mutation; diff --git a/src/schema/mutations/RemoveBot.js b/src/schema/mutations/RemoveBot.js new file mode 100644 index 000000000..a09ce89f1 --- /dev/null +++ b/src/schema/mutations/RemoveBot.js @@ -0,0 +1,63 @@ +import logger from '../../utils/logger' +import { BotInputType, } from '../types/input' +import { BotType, } from '../types' +import { Ecosystem } from '../../models' + +export const args = { bot: { type: BotInputType } } + +const resolve = async (parent, { bot }, context) => { + logger.debug('removeBot -> Dev team: %s. Bot: %s', bot.devTeam, bot.codeName) + try { + + let userEcosystem = await Ecosystem.findOne({ + authId: context.userId + }) + + if (!userEcosystem) { + throw 'Ecosystem not found for the user.' + } + + let removedBotClone + for (let i = 0; i < userEcosystem.devTeams.length; i++) { + const auxTeam = userEcosystem.devTeams[i] + if (auxTeam.codeName === bot.devTeam) { + for (let j = 0; j < auxTeam.bots.length; j++) { + const auxBot = auxTeam.bots[j] + if (auxBot.codeName + '-' + bot.cloneId === bot.codeName) { + for (let k = 0; k < auxBot.products.length; k++) { + const auxProduct = auxBot.products[k] + if (auxProduct.codeName === bot.productCodeName) { + removedBotClone = auxBot + auxTeam.bots.splice(j, 1) + break + } + } + break + } + } + break + } + } + + if (removedBotClone) { + await userEcosystem.save() + return removedBotClone + } else { + throw 'Algobot not found.' + } + + } catch (err) { + logger.error('removeBot -> Error: %s', err.message) + throw err + } +} + +const mutation = { + removeBot: { + type: BotType, + args, + resolve, + } +} + +export default mutation; diff --git a/src/schema/mutations/index.js b/src/schema/mutations/index.js index 7e0b35711..be7aad941 100644 --- a/src/schema/mutations/index.js +++ b/src/schema/mutations/index.js @@ -5,13 +5,17 @@ import { import AddTeamMutation from './AddTeam' import DeleteTeamMutation from './DeleteTeam' import CreateFileMutation from './CreateFile' +import AddBotMutation from './AddBot' +import RemoveBotMutation from './RemoveBot' const Mutation = new GraphQLObjectType({ name: 'Mutation', fields: Object.assign( AddTeamMutation, DeleteTeamMutation, - CreateFileMutation + CreateFileMutation, + AddBotMutation, + RemoveBotMutation ) }) diff --git a/src/schema/types/input/Bot.js b/src/schema/types/input/Bot.js index 5b1ed1096..b71172621 100644 --- a/src/schema/types/input/Bot.js +++ b/src/schema/types/input/Bot.js @@ -8,11 +8,14 @@ const Type = new GraphQLInputObjectType({ name: 'BotInput', description: 'Payload for bot input.', fields: () => ({ + devTeam: { type: new GraphQLNonNull(GraphQLString) }, codeName: { type: new GraphQLNonNull(GraphQLString) }, - displayName: { type: new GraphQLNonNull(GraphQLString) }, + productCodeName: { type: new GraphQLNonNull(GraphQLString) }, + displayName: { type: GraphQLString }, repo: { type: GraphQLString }, configFile: { type: GraphQLString }, - cloneId: { type: GraphQLString } + cloneId: { type: GraphQLString }, + validPeriod: { type: GraphQLString }, }), }); diff --git a/src/schema/types/input/DataSet.js b/src/schema/types/input/DataSet.js new file mode 100644 index 000000000..71efbf934 --- /dev/null +++ b/src/schema/types/input/DataSet.js @@ -0,0 +1,19 @@ +import { + GraphQLInputObjectType, + GraphQLString, + GraphQLList +} from 'graphql'; + +const Type = new GraphQLInputObjectType({ + name: 'DataSetInput', + description: 'Payload for data set input.', + fields: () => ({ + codeName: { type: new GraphQLNonNull(GraphQLString) }, + type: { type: new GraphQLNonNull(GraphQLString) }, + filePath: { type: new GraphQLNonNull(GraphQLString) }, + fileName: { type: new GraphQLNonNull(GraphQLString) }, + validPeriods: { type: new GraphQLList(GraphQLString) } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Exchange.js b/src/schema/types/input/Exchange.js new file mode 100644 index 000000000..738aca4ac --- /dev/null +++ b/src/schema/types/input/Exchange.js @@ -0,0 +1,15 @@ +import { + GraphQLInputObjectType, + GraphQLString, + GraphQLNonNull +} from 'graphql'; + +const Type = new GraphQLInputObjectType({ + name: 'ExchangeInput', + description: 'Payload for exchange input.', + fields: () => ({ + name: { type: new GraphQLNonNull(GraphQLString) } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Plotter.js b/src/schema/types/input/Plotter.js new file mode 100644 index 000000000..1faa1a322 --- /dev/null +++ b/src/schema/types/input/Plotter.js @@ -0,0 +1,17 @@ +import { + GraphQLInputObjectType, + GraphQLString, + GraphQLNonNull +} from 'graphql'; + +const Type = new GraphQLInputObjectType({ + name: 'PlotterInput', + description: 'Payload for plotter input.', + fields: () => ({ + devTeam: { type: new GraphQLNonNull(GraphQLString) }, + codeName: { type: new GraphQLNonNull(GraphQLString) }, + moduleName: { type: new GraphQLNonNull(GraphQLString) } + }), +}); + +export default Type; diff --git a/src/schema/types/input/Product.js b/src/schema/types/input/Product.js new file mode 100644 index 000000000..9fb26fab2 --- /dev/null +++ b/src/schema/types/input/Product.js @@ -0,0 +1,24 @@ +import { + GraphQLInputObjectType, + GraphQLString, + GraphQLNonNull +} from 'graphql'; + +import { DataSetInputType } from './index'; +import { ExchangeInputType } from './index'; +import { PlotterInputType } from './index'; + +const Type = new GraphQLInputObjectType({ + name: 'ProductInput', + description: 'Payload for product input.', + fields: () => ({ + codeName: { type: new GraphQLNonNull(GraphQLString) }, + displayName: { type: new GraphQLNonNull(GraphQLString) }, + description: { type: GraphQLString }, + dataSets: { type: new GraphQLList(DataSetInputType) }, + exchangeList: { type: new GraphQLList(ExchangeInputType) }, + plotter: { type: PlotterInputType } + }), +}); + +export default Type; diff --git a/src/schema/types/input/index.js b/src/schema/types/input/index.js index 2e3d3dd55..6344400c3 100644 --- a/src/schema/types/input/index.js +++ b/src/schema/types/input/index.js @@ -3,11 +3,19 @@ import BotInputType from './Bot' import HostInputType from './Host' import FileInputType from './File' import DeleteTeamInputType from './DeleteTeam' +import DataSetInputType from './DataSet' +import ExchangeInputType from './Exchange' +import PlotterInputType from './Plotter' +import ProductInputType from './Product' export { TeamInputType, BotInputType, HostInputType, FileInputType, - DeleteTeamInputType + DeleteTeamInputType, + DataSetInputType, + ExchangeInputType, + PlotterInputType, + ProductInputType }