1234567891011121314151617181920212223242526272829303132333435 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const assert = require('assert');
- const { ObjectId } = require('mongoose').Types;
- // 问卷
- class QuestionnaireService extends CrudService {
- constructor(ctx) {
- super(ctx, 'questionnaire');
- this.model = this.ctx.model.Questionnaire;
- this.question = this.ctx.service.question;
- }
- async fetch({ id }) {
- const data = await this.model.findById(id, '+questions');
- if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定问卷!');
- const obj = data.questions;
- if (obj.every(e => _.isObject(e))) return data;
- // 如果到这里了,说明不全是现编的题,有从库里选的,是string类型的id,过滤出这部分
- const ids = obj.filter(f => _.isString(f));
- if (ids.length > 0) {
- const partQuests = await this.question.getQuestions(ids);
- // 查出了后将对应位置的id换成题目
- for (const i of partQuests) {
- const index = obj.findIndex(f => _.isString(f) && ObjectId(i._id).equals(f));
- obj[index] = i;
- }
- data.questions = obj;
- }
- return data;
- }
- }
- module.exports = QuestionnaireService;
|