How to refactoring function when I've to do many things in one function I always want to improve my code by refactoring but when I've to work with complicated function that have many thing to do . How can I refactoring them. 

Here is my data for example I get data from request

My DB structure look like this

 DB
    id  PK
    blog int
    date_end string
    date_start string
    employee_id FK [ by table employee ]
    total_time_in_hr int
    value int

//my data
req = {
            data: [
                {
                    blog: 1,
                    date_end: "19-02-2020",
                    date_start: "19-02-2020",
                    employee_id: [2290,3],
                    work_id: 1,
                    total_time_in_hr: 10,
                    value: 5100
                }
            ]
    };


First I've to loop my data then I've to loop again in my employee_id and insert them into DB

//looop data
async store({ request, response }) {
    const data = req.data
    await Promise.all(
        data.map(async (getData, i) =&gt; {
            const empId = getData.employee_id;
            const workId = getData.work_id;
            let date_start = getData.date_start;
            let date_end = getData.date_end;
            const value = getData.value;
            date_start = moment(date_start, "DD-MM-YYYY").format(
                "YYYY-MM-DD"
            );
            date_start = moment(date_end, "DD-MM-YYYY").format(
                "YYYY-MM-DD"
            );
            await Promise.all(
                empId.map(async (empId, i) =&gt; {
//insert to my DB
                    const storeData = new MyDB();
                    storeData.employee_id = empId;
                    storeData.work_id = workId;
                    storeData.value = value;
                    storeData.date_start = date_start;
                    storeData.date_end = date_end;
                    storeData.status = 3;
                    storeData.group_name = groupName;
                    await storeData.save();
// insert to my Log DB
                    const createLog = new MyDBLog();
                    createLog.my_db_id = storeData.id;
                    createLog.value = value;
                    createLog.msg = 'somthing';
                    createLog.status = 3;
                    createLog.total_time = 0;
                    await createLog.save();

                    //this part in my work I've to make json and insert it to another DB
                    const data = {
                        work_id: workId,
                        blog: getData.blog,
                        total_time: getData.total_time_in_hr,
                        my_db_id: storeData.id
                    };
                    const store = await AnotherDB.create(data);
                })
            );
            return { workId, value };
        })
    ).then(async data =&gt; {
//update last Value
        await Promise.all(
            data.map(async ({ workId, value }) =&gt; {
                const updateTotalAssign = await AssignWork.query()
                    .where("work_id", workId)
                    .update({ total_value: value });
            })
        );
    });
    return response.status(200).json({
        status: 200,
        success: true,
        message: "success"
    });
}


As you see my code are very mess up How to refactoring function and clean code this kind of function
A low-hanging fruit will be defining some interfaces of datastores such as MyDB so that some details can be hidden from the controller code. For instance:

 const storeData = new MyDB();
 storeData.employee_id = empId;
 storeData.work_id = workId;
 storeData.value = value;
 storeData.date_start = date_start;
 storeData.date_end = date_end;
 storeData.status = 3;
 storeData.group_name = groupName;
 await storeData.save();


can be refactored to something like:

 MyDBBuilder
     .with(getData)
     .withStatus(3)
     .withGroupName(groupName)
     .save();


The complexity comes from the fact that it takes 4 steps to complete this business transaction. Unless there is a way to redefine data schema, all we can do here is to encapsulate the complexity and not expose it in the controller code. 
In some cases, it might be appropriate to have all the db operations in some stored procedure, if you are not concerned with portability.