import {ApiCall} from "tsrpc";
import {ReqOpen, ResOpen} from "../../../shared/protocols/event/leichonglibao/PtlOpen";

export default async function (call: ApiCall<ReqOpen, ResOpen>) {
    let data = await G.mongodb.cEvent(`leichonglibao${call.req.hdid}`).findOne(
        {type: `leichonglibao${call.req.hdid}`, uid: call.uid}
    )
    let change = {opentime: G.time}
    if (!data) {
        Object.assign(change, {sc: false, buy: []})
    }

    // 每天open红点
    G.mongodb.cEvent(`leichonglibao${call.req.hdid}`).updateOne(
        {type: `leichonglibao${call.req.hdid}`, uid: call.uid}, {$set: change}, {upsert: true}
    )

    let temp = Object.assign((data || {}), change) as ResOpen & { opentime }

    call.succ({sc: temp.sc, buy: temp.buy});
}