diff --git a/src/eid/is.ts b/src/eid/is.ts index a6ee10d..f56dd16 100644 --- a/src/eid/is.ts +++ b/src/eid/is.ts @@ -18,6 +18,7 @@ export const len_msg = 2048 * 8 export const len_rec_frm = 2048 * 8 export const len_rec_to = 64 export const len_rec_now = 1024 +export const len_aug = 64 export const len_jwt = 512 export function is_lim( diff --git a/src/pra/put.ts b/src/pra/put.ts index 2333b77..be2ab13 100644 --- a/src/pra/put.ts +++ b/src/pra/put.ts @@ -1,4 +1,12 @@ import type { Agd, Aut, Cdt, Dbt, Ern, Msg, Soc, Usr } from "../eid/typ.ts" +import type { Pas } from "./pas.ts" +import { Ret, is_put } from "./can.ts" +import { coll } from "../eid/db.ts" +import { is_lim, len_aug } from "../eid/is.ts" +import { usr_u } from "../eid/usr.ts" +import { soc_u, soc_d } from "../eid/soc.ts" +import { agd_d, agd_u } from "../eid/agd.ts" +import { cdt_u, rec_d, rec_r, rec_s } from "../eid/rec.ts" // 更新操作 @@ -91,3 +99,53 @@ export type Put = { wsl: Aut["wsl"], lit: Aut["lit"], } + +export type PutRet = { + usr: Ret, + soc: Ret, + agd: Ret, + cdt: Ret, +} + +export async function put( + pas: Pas, + p: Put, +) { + if (!is_put(pas, p)) return null + const now = Date.now() + switch (p.put) { + case "usr": return usr_u(p.usr, { $set: { nam: p.nam, adm1: p.adm1, adm2: p.adm2, msg: p.msg } }) + case "soc": { + if ("nam" in p) return soc_u(p.soc, { $set: { nam: p.nam, adm1: p.adm1, adm2: p.adm2, sec: p.sec } }) + else if ("del" in p) return soc_d(p.del) + else if ("msg" in p) return soc_u(p.soc, { $set: { msg: p.msg } }) + else if ("agr" in p) return soc_u(p.soc, { $set: { agr: { msg: p.agr, utc: p.agr.length > 0 ? now : 0 } } }) + break + } case "agd": { + if ("del" in p) return agd_d(p.del) + else if ("nam" in p) return agd_u(p.agd, { $set: { nam: p.nam, adm1: p.adm1, adm2: p.adm2, msg: p.msg } }) + break + } case "cdt": { + if ("del" in p) return rec_d(coll.cdt, p.del) + else if ("aug" in p) return cdt_u(p.aug, { $push: { aug: { msg: p.msg, amt: p.amt, utc: now, usr: pas.usr } } }) + else if ("dim" in p) return cdt_u(p.dim, { $pop: { aug: 1 } }) + else if ("agr" in p) return cdt_u(p.agr, { $set: { "utc.agr": now } }) + else if ("mov" in p) { + const [frm, to] = [pas.cdt.find(c => c._id.soc == p.mov.soc), await rec_r(coll.cdt, { _id: p.mov }, { now })] + if (!frm || !to || !is_lim(Math.max(frm.aug?.length ?? 0, to.aug?.length ?? 0), len_aug - 1)) return null + const aug = frm.aug ? frm.aug.reduce((a, b) => a + Math.min(b.amt, 0), frm.amt) : frm.amt + const [dbt] = await rec_s(coll.dbt, { usr: frm._id.usr, soc: frm._id.soc }, { frm: frm.utc.eft }) + if (aug >= (dbt ? dbt.amt : 0) + p.amt) { + const f = await cdt_u(frm._id, { $push: { aug: { msg: p.msg, amt: -p.amt, utc: now, usr: pas.usr } } }) + if (f) return cdt_u(to._id, { $push: { aug: { msg: p.msg, amt: p.amt, utc: now, usr: pas.usr } } }) + } + } else if ("quo" in p) { + const c = pas.cdt.find(c => c._id.soc == p.quo) + if (c && is_lim(c.aug?.length ?? 0, len_aug - 1)) + return cdt_u(c._id, { $push: { aug: { msg: p.msg, amt: 0, utc: now, usr: pas.usr } } }) + } + break + } + } + return null +}