/*
 * @Author: baozhoutao@steedos.com
 * @Date: 2022-09-15 13:09:51
 * @LastEditors: 殷亮辉 yinlianghui@hotoa.com
 * @LastEditTime: 2025-12-17 18:09:28
 * @Description: 
 */
const express = require("express");
const router = express.Router();
const { requireAuthentication } = require("@steedos/auth");
const _ = require('lodash');
const objectql = require('@steedos/objectql');
const UUFlowManager = require('../manager/uuflow_manager');
router.post('/api/workflow/v2/instance/upgrade', requireAuthentication, async function (req, res) {
    try {
        let userSession = req.user;
        const { instance } = req.body;
        const record = await objectql.getObject('instances').findOne(instance._id, {fields:[
            'flow','form','applicant_name','applicant_organization','applicant_organization_fullname','applicant_organization_name',
            'code', 'flow_version', 'form_version', 'submit_date', 'flow_name', 'code', 'traces', 'state'
        ]});
        if(!record){
            return res.status(200).send({
                instance: false
            });
        }
        if(record.state !== 'draft'){
            return res.status(200).send({
                instance: true
            });
        }
        const ins = await UUFlowManager.draft_save_instance(Object.assign(record, instance), userSession.userId);
        res.status(200).send({
            'instance': ins
        });
    } catch (error) {
        console.error(error);
        res.status(200).send({
            error: error.message
        });
    }
});
exports.default = router;