Files
Superalgos/MergedRepos/SaWebApi/src/schema/mutations/AddBot.js
T

72 lines
1.9 KiB
JavaScript

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,
type: auxBot.type,
profilePicture: auxBot.profilePicture,
cloneId: bot.cloneId,
products: [auxProduct]
}
auxTeam.bots.push(newBotClone)
userEcosystem.markModified("devTeams")
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;