put 更新操作 #45

This commit is contained in:
728
2023-12-01 18:35:20 +08:00
parent 11d5c15012
commit 889bcee7f5
2 changed files with 59 additions and 0 deletions

View File

@ -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(

View File

@ -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<typeof usr_u>,
soc: Ret<typeof soc_u>,
agd: Ret<typeof agd_u>,
cdt: Ret<typeof cdt_u>,
}
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
}