'use strict';
// @ts-check
const objectql = require('@steedos/objectql');
const express = require('express');
const router = express.Router();
const { requireAuthentication } = require("@steedos/auth");
const WorkflowManager = require('../manager/workflow_manager');
const _ = require('underscore');
const { getCollection } = require('../utils/collection');

router.get('/api/workflow/v2/instance/:instanceId/permission', requireAuthentication, async function (req, res) {
    try {
        const { instanceId } = req.params;
        const userSession = req.user;
        const current_user_info = userSession;
        const current_user_id = current_user_info.userId;
        const db = {
            users: await getCollection('users'),
            instances: await getCollection('instances')
        }
        const user = await db.users.findOne({_id: current_user_id});
        const instance = await db.instances.findOne({_id: instanceId})
        console.log(`user`, user)
        console.log(`instance`, instance)
        const permission = await WorkflowManager.hasInstancePermissions(user, instance)
        res.status(200).send({
            data: {
                permission: permission
            },
            msg: "",
            status: 0
        });
    } catch (e) {
        console.log(`e`, e)
        res.status(200).send({
            errors: [{ errorMessage: e.message }]
        });
    }
});
exports.default = router;
