/*
 * @Author: sunhaolin@hotoa.com
 * @Date: 2022-12-08 15:16:53
 * @LastEditors: baozhoutao@steedos.com
 * @LastEditTime: 2024-02-27 15:18:55
 * @Description: 
 */
'use strict';
// @ts-check
const { getObject } = require('@steedos/objectql');

const uuflowManager = require('../manager/uuflow_manager')

module.exports = {
    listenTo: 'instances',

    afterUpdate: async function () {
        const { doc, previousDoc } = this;
        if (doc.current_step_name !== previousDoc.current_step_name) {
            return uuflowManager.triggerRecordInstanceQueue(doc._id, doc.record_ids, doc.current_step_name, doc.flow, doc.state);
        }
    },

    afterDelete: async function () {
        const { previousDoc } = this;
        if(previousDoc){
            let recordIds = previousDoc.record_ids;
            if (recordIds && recordIds.length == 1) {
                let recordObjName = recordIds[0].o;
                let recordId = recordIds[0].ids[0];
                if (recordObjName && recordId) {
                    await objectql.getObject(recordObjName).update(recordId, { "instances": null, "instance_state": null, "locked": false })
                }
            }
        }
    },

}