feat: 新增复制功能 (#40)

This commit is contained in:
luch 2024-01-11 14:04:50 +08:00 committed by GitHub
parent 8a02d36132
commit d01d50a9d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 0 deletions

View File

@ -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' }) @SurveyServer({ type: 'http', method: 'post', routerName: '/update' })
async update({ req }) { async update({ req }) {
const surveyParams = getValidateValue(Joi.object({ const surveyParams = getValidateValue(Joi.object({

View File

@ -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 }) { async update(surveyParams: { surveyId: string, remark: string, title: string, userData: UserType }) {
const surveyMeta = await mongo.getCollection({ collectionName: 'surveyMeta' }); const surveyMeta = await mongo.getCollection({ collectionName: 'surveyMeta' });
const _id = mongo.getObjectIdByStr(surveyParams.surveyId); const _id = mongo.getObjectIdByStr(surveyParams.surveyId);