/*
 * @Author: sunhaolin@hotoa.com
 * @Date: 2022-12-21 15:03:26
 * @LastEditors: sunhaolin@hotoa.com
 * @LastEditTime: 2023-03-01 
 * @Description: 流程分类管理模块
 */
const { getCollection } = require('../utils/collection');
const formManager = require('./form_manager');

const flowManager = {
    /**
     * 获取指定分类下的所有流程
     * @param {string} spaceId 工作区ID
     * @param {string} categoryId 分类ID
     * @param {object} fields 查询字段投影
     * @returns {Promise<Array>} 流程列表
     */
    async getCategoriesFlows(spaceId, categoryId, fields = { _id: 1 }) {
        const categoriesForms = await formManager.getCategoriesForms(spaceId, categoryId, fields);
        const formIds = categoriesForms.map(form => form._id);
        
        const flowsCollection = await getCollection('flows');
        return flowsCollection.find({
            form: { $in: formIds }
        }).toArray();
    },

    /**
     * 获取未分类的流程
     * @param {string} spaceId 工作区ID 
     * @param {object} fields 查询字段投影
     * @returns {Promise<Array>} 流程列表
     */
    async getUnCategoriesFlows(spaceId, fields = { _id: 1 }) {
        const unCategoriesForms = await formManager.getUnCategoriesForms(spaceId, fields);
        const formIds = unCategoriesForms.map(form => form._id);
        
        const flowsCollection = await getCollection('flows');
        return flowsCollection.find({
            form: { $in: formIds }
        }).toArray();
    }
};

module.exports = flowManager;