From 908537ea0bcf5dbf82f6aea63a123d85b464d8ef Mon Sep 17 00:00:00 2001 From: luch <32321690+luch1994@users.noreply.github.com> Date: Thu, 11 Jan 2024 14:04:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20(#40)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/apps/surveyManage/index.ts | 27 ++++++++++++ .../surveyManage/service/surveyService.ts | 42 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/server/src/apps/surveyManage/index.ts b/server/src/apps/surveyManage/index.ts index bc0d0ab4..b5af65f3 100644 --- a/server/src/apps/surveyManage/index.ts +++ b/server/src/apps/surveyManage/index.ts @@ -35,6 +35,33 @@ export default class SurveyManage { }; } + @SurveyServer({ type: 'http', method: 'post', routerName: '/create' }) + async create({ req }) { + const params = getValidateValue(Joi.object({ + remark: Joi.string().required(), + title: Joi.string().required(), + questionType: Joi.string().when('createMethod', { + is: 'copy', + then: Joi.allow(null), + otherwise: Joi.required(), + }), + createMethod: Joi.string().allow(null).default('basic'), + createFrom: Joi.string().when('createMethod', { + is: 'copy', + then: Joi.required(), + otherwise: Joi.allow(null), + }), + }).validate(req.body, { allowUnknown: true })); + params.userData = await userService.checkLogin({ req }); + const addRes = await surveyService.create(params); + return { + code: 200, + data: { + id: addRes.pageId, + }, + }; + } + @SurveyServer({ type: 'http', method: 'post', routerName: '/update' }) async update({ req }) { const surveyParams = getValidateValue(Joi.object({ diff --git a/server/src/apps/surveyManage/service/surveyService.ts b/server/src/apps/surveyManage/service/surveyService.ts index 355f7549..2a40f61c 100644 --- a/server/src/apps/surveyManage/service/surveyService.ts +++ b/server/src/apps/surveyManage/service/surveyService.ts @@ -78,6 +78,48 @@ class SurveyService { }; } + async create(surveyMetaInfo: { remark: string, questionType: QUESTION_TYPE, title: string, userData: UserType, createMethod: string; createFrom: string; }) { + const surveyMeta = await mongo.getCollection({ collectionName: 'surveyMeta' }); + const now = Date.now(); + const surveyPath = await this.getNewSurveyPath(); + + let originSurvey; + if (surveyMetaInfo.createMethod === 'copy') { + originSurvey = await this.get({ surveyId: surveyMetaInfo.createFrom, userData: surveyMetaInfo.userData }); + surveyMetaInfo.questionType = originSurvey.surveyMetaRes.questionType; + } + + const surveyMetaRes = await surveyMeta.insertOne({ + surveyPath, + remark: surveyMetaInfo.remark, + questionType: surveyMetaInfo.questionType, + createMethod: surveyMetaInfo.createMethod || 'basic', + createFrom: surveyMetaInfo.createFrom || '', + title: surveyMetaInfo.title, + creator: surveyMetaInfo.userData.username, + owner: surveyMetaInfo.userData.username, + curStatus: getStatusObject({ status: SURVEY_STATUS.new }), + createDate: now, + updateDate: now, + }); + const pageId = surveyMetaRes.insertedId.toString(); + const surveyConf = await mongo.getCollection({ collectionName: 'surveyConf' }); + const code = originSurvey ? originSurvey.surveyConfRes.code : await this.getCodeData({ + questionType: surveyMetaInfo.questionType, + }); + const surveyConfRes = await surveyConf.insertOne({ + pageId, + pageType: surveyMetaInfo.questionType, + curStatus: getStatusObject({ status: SURVEY_STATUS.new }), + code, + }); + return { + pageId, + surveyMetaRes, + surveyConfRes + }; + } + async update(surveyParams: { surveyId: string, remark: string, title: string, userData: UserType }) { const surveyMeta = await mongo.getCollection({ collectionName: 'surveyMeta' }); const _id = mongo.getObjectIdByStr(surveyParams.surveyId);