From 3863a7f0a7d6b8c237d6de4bd86e3810259d52c9 Mon Sep 17 00:00:00 2001 From: Behzad Rabiei <53224485+Behzad-rabiei@users.noreply.github.com> Date: Tue, 16 Apr 2024 12:00:52 +0400 Subject: [PATCH] refactor dynamicModuleUpdate --- src/validations/module.validation.ts | 155 +++++++++++++++++++-------- 1 file changed, 108 insertions(+), 47 deletions(-) diff --git a/src/validations/module.validation.ts b/src/validations/module.validation.ts index 3631e54..6e742d8 100644 --- a/src/validations/module.validation.ts +++ b/src/validations/module.validation.ts @@ -1,4 +1,4 @@ -import Joi from 'joi'; +import Joi, { ObjectSchema } from 'joi'; import { objectId } from './custom.validation'; import { Types } from 'mongoose'; import { Request } from 'express'; @@ -26,58 +26,119 @@ const getModule = { }), }; -const dynamicModuleUpdate = (req: Request) => { - const module = req.module; - if (module?.name === 'hivemind') { - return { - params: Joi.object().keys({ - moduleId: Joi.required().custom(objectId), - }), - body: Joi.object() - .required() - .keys({ - options: Joi.object() - .required() - .keys({ - platforms: Joi.array().items( - Joi.object().keys({ - name: Joi.string().required().valid('discord'), - platform: Joi.string().custom(objectId).required(), - metadata: Joi.object() - .required() - .when('name', { - switch: [ - { - is: 'discord', - then: Joi.object().keys({ - answering: Joi.object().keys({ - selectedChannels: Joi.array().items(Joi.string()).required(), - }), - learning: Joi.object().keys({ - selectedChannels: Joi.array().items(Joi.string()).required(), - fromDate: Joi.date().required(), - }), - }), - }, - ], - otherwise: Joi.any().forbidden(), - }), - }), - ), - }), - }), - }; - } else { - req.allowInput = false; - return {}; - } -}; +// const dynamicModuleUpdate = (req: Request) => { +// const module = req.module; +// if (module?.name === 'hivemind') { +// return { +// params: Joi.object().keys({ +// moduleId: Joi.required().custom(objectId), +// }), +// body: Joi.object() +// .required() +// .keys({ +// options: Joi.object() +// .required() +// .keys({ +// platforms: Joi.array().items( +// Joi.object().keys({ +// name: Joi.string().required().valid('discord'), +// platform: Joi.string().custom(objectId).required(), +// metadata: Joi.object() +// .required() +// .when('name', { +// switch: [ +// { +// is: 'discord', +// then: Joi.object().keys({ +// answering: Joi.object().keys({ +// selectedChannels: Joi.array().items(Joi.string()).required(), +// }), +// learning: Joi.object().keys({ +// selectedChannels: Joi.array().items(Joi.string()).required(), +// fromDate: Joi.date().required(), +// }), +// }), +// }, +// ], +// otherwise: Joi.any().forbidden(), +// }), +// }), +// ), +// }), +// }), +// }; +// } else { +// req.allowInput = false; +// return {}; +// } +// }; + const deleteModule = { params: Joi.object().keys({ moduleId: Joi.string().custom(objectId).required(), }), }; +const hivemindDiscordMetadata = () => { + return Joi.object().keys({ + answering: Joi.object().keys({ + selectedChannels: Joi.array().items(Joi.string()).required(), + }), + learning: Joi.object().keys({ + selectedChannels: Joi.array().items(Joi.string()).required(), + fromDate: Joi.date().required(), + }), + }); +}; + +const hivemindOptions = () => { + return Joi.object().keys({ + platforms: Joi.array().items( + Joi.object().keys({ + name: Joi.string().required().valid('discord'), + platform: Joi.string().custom(objectId).required(), + metadata: Joi.when('name', { + switch: [ + { + is: 'discord', + then: hivemindDiscordMetadata(), + }, + ], + otherwise: Joi.any().forbidden(), + }).required(), + }), + ), + }); +}; + +const dynamicModuleUpdate = (req: any) => { + const moduleName = req.module?.name; + const paramsOption = { + params: Joi.object().keys({ + moduleId: Joi.string().custom(objectId).required(), + }), + }; + let bodyOption = {}; + + switch (moduleName) { + case 'hivemind': + bodyOption = { + body: Joi.object().required().keys({ + options: hivemindOptions(), + }), + }; + break; + default: + req.allowInput = false; + return {}; + } + + return { + ...paramsOption, + ...bodyOption, + }; +}; + export default { createModule, getModules,