import { ApiCall } from "tsrpc";
import { ActionLog } from '../../public/actionLog/actionLog';
import { ReqTaskOpen, ResTaskOpen } from "../../shared/protocols/gonghui/PtlTaskOpen";
import { PublicShared } from '../../shared/public/public';

export default async function (call: ApiCall<ReqTaskOpen, ResTaskOpen>) {
    let gh = await call.conn.gonghui;
    let data = (await G.mongodb.collection('gonghuiUser').findOne({ uid: call.uid }))?.task;

    if (!gh.data.task || PublicShared.getToDayZeroTime() > gh.data.task.refreshTime) {
        gh.data.task = {
            finised: 0,
            refreshTime: G.time
        };
        gh.updateDb({ $set: { task: gh.data.task } });
    }

    if (!data || PublicShared.getToDayZeroTime() > data.refreshTime) {
        data = {
            ghRec: [],
            myRec: [],
            refreshTime: G.time
        };
        G.mongodb.collection('gonghuiUser').updateOne({ uid: call.uid }, { $set: { task: data } }, { upsert: true });
    }

    call.succ({
        ...data,
        progress: await ActionLog.getDayLog(call.uid, ...G.gc.shili_com.task.map(t => t.taskId))
    });
}