/*
 * @Author: sunhaolin@hotoa.com
 * @Date: 2022-12-21 15:05:25
 * @LastEditors: sunhaolin@hotoa.com
 * @LastEditTime: 2023-03-01
 * @Description: 表单管理模块
 */
const { getCollection } = require('../utils/collection');

const formManager = {
    /**
     * 获取指定分类下的表单
     * @param {string} spaceId 工作区ID
     * @param {string} categoryId 分类ID
     * @param {object} fields 查询字段投影
     * @returns {Promise<Array>} 表单列表
     */
    async getCategoriesForms(spaceId, categoryId, fields) {
        const formsCollection = await getCollection('forms');
        const projection = fields ? { projection: fields } : undefined;
        
        return formsCollection.find({
            space: spaceId,
            category: categoryId,
            state: "enabled"
        }, projection).toArray();
    },

    /**
     * 获取未分类的表单
     * @param {string} spaceId 工作区ID
     * @param {object} fields 查询字段投影
     * @returns {Promise<Array>} 表单列表
     */
    async getUnCategoriesForms(spaceId, fields) {
        const formsCollection = await getCollection('forms');
        const projection = fields ? { projection: fields } : undefined;
        
        return formsCollection.find({
            space: spaceId,
            category: { $in: [null, ""] },
            state: "enabled"
        }, projection).toArray();
    }
};

module.exports = formManager;