Skip to content

Commit

Permalink
put 更新操作与测试 #45
Browse files Browse the repository at this point in the history
  • Loading branch information
LDmoxeii committed May 4, 2024
1 parent bc454cc commit 7bdae3b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/eid/aut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export async function aut_f(
}

export async function aut_u(
a: Omit<Aut, "_id">,
a: Omit<Aut, "_id" | "sup">,
): DocU {
if (!is_aut({ _id: 1, ...a })) return null
if (!is_aut({ _id: 1, ...a, sup: [1, 2] })) return null
try {
const { matchedCount, modifiedCount } = await coll.aut.updateOne({ _id: 1 }, { $set: a })
if (matchedCount > 0) return modifiedCount > 0 ? 1 : 0
Expand Down
12 changes: 8 additions & 4 deletions src/pra/pos.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { Ret } from "./can.ts"
export type { PsgRet } from "./pas.ts"
export type { PreRet } from "./pre.ts"
export type { PutRet } from "./put.ts"

import { Psg, pas, psg } from "./pas.ts"
import { Pre, pre } from "./pre.ts"
import { Put, put } from "./put.ts"

export type { PsgRet } from "./pas.ts"
export type { PreRet } from "./pre.ts"
export type Pos = Psg | Pre | Put

export type Pos = Psg | Pre
export type PosRet = {
ret: Ret<typeof psg>["ret"] | Ret<typeof pre>,
ret: Ret<typeof psg>["ret"] | Ret<typeof pre> | Ret<typeof put>,
jwt?: string | null,
etag?: "",
}
Expand All @@ -25,6 +28,7 @@ export async function pos(
if ("psg" in json) return psg(p, json)
else if (!p) return { ret: null, jwt: null }
else if ("pre" in json) ret = await pre(p, json)
else if ("put" in json) ret = await put(p, json)

return ret ? { ret, etag: "" } : { ret }
}
97 changes: 55 additions & 42 deletions src/pra/put.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,101 @@
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 { agd_d, agd_u } from "../eid/agd.ts"
import { aut_u } from "../eid/aut.ts"
import { coll } from "../eid/db.ts"
import { is_lim, len_aug } from "../eid/is.ts"
import { msg_d, msg_u } from "../eid/msg.ts"
import { cdt_u, dbt_u, rec_d, rec_r, rec_s } from "../eid/rec.ts"
import { soc_d, soc_u } from "../eid/soc.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"

// 更新操作
import { Ret, is_put } from "./can.ts"

export type Put = {
put: "usr", // 更新用户数据 (本人)
put: "usr",
usr: Usr["_id"],
nam: Usr["nam"],
adm1: Usr["adm1"],
adm2: Usr["adm2"],
msg: Usr["msg"],
} | {
put: "soc", // 更新俱乐部数据 (管理员)
put: "soc",
soc: Soc["_id"],
nam: Soc["nam"],
adm1: Soc["adm1"],
adm2: Soc["adm2"],
sec: Soc["sec"],
} | {
put: "soc", // 更新俱乐部数据 (管理员)
del: Soc["_id"], // 删除俱乐部
put: "soc",
del: Soc["_id"],
} | {
put: "soc", // 更新俱乐部数据 (联络员)
put: "soc",
soc: Soc["_id"],
msg: Soc["msg"],
} | {
put: "soc", // 更新俱乐部数据 (联络员)
put: "soc",
soc: Soc["_id"],
agr: Soc["agr"]["msg"],
} | {
put: "agd", // 更新活动数据 (联络员)
del: Agd["_id"], // 删除活动
put: "agd",
del: Agd["_id"],
} | {
put: "agd", // 更新活动数据 (联络员)
put: "agd",
agd: Agd["_id"],
nam: Agd["nam"],
adm1: Agd["adm1"],
adm2: Agd["adm2"],
msg: Agd["msg"],
} | {
put: "cdt", // 更新积分记录(联络员)
del: Cdt["_id"], // 删除积分记录
put: "cdt",
del: Cdt["_id"],
} | {
put: "cdt", // 更新积分记录(联络员)
aug: Cdt["_id"], // 追加项
put: "cdt",
aug: Cdt["_id"],
msg: Cdt["msg"],
amt: Cdt["amt"],
} | {
put: "cdt", // 更新积分记录(联络员)
dim: Cdt["_id"], // 撤回追加项
put: "cdt",
dim: Cdt["_id"],
} | {
put: "cdt", // 更新积分记录(会员)
agr: Cdt["_id"], // 同意用户协议
put: "cdt",
agr: Cdt["_id"],
} | {
put: "cdt", // 更新积分记录(会员)
mov: Cdt["_id"], // 转让积分至 mov
put: "cdt",
mov: Cdt["_id"],
msg: Cdt["msg"],
amt: Cdt["amt"],
} | {
put: "cdt", // 更新积分记录(会员)
quo: Cdt["_id"]["soc"], // 签到并使用 quota
put: "cdt",
quo: Cdt["_id"]["soc"],
msg: Cdt["msg"],
} | {
put: "dbt", // 更新积分使用(联络员)
del: Dbt["_id"], // 删除积分使用
put: "dbt",
del: Dbt["_id"],
} | {
put: "dbt", // 更新积分使用(联络员)
sec: Dbt["_id"], // 确认积分使用
put: "dbt",
sec: Dbt["_id"],
} | {
put: "dbt", // 更新积分使用(会员)
dbt: Dbt["_id"], // 反馈
put: "dbt",
dbt: Dbt["_id"],
rev: NonNullable<Dbt["rev"]>,
} | {
put: "ern", // 更新贡献记录(联络员)
del: Ern["_id"], // 删除
put: "ern",
del: Ern["_id"],
} | {
put: "wsl" | "lit", // 更新文章(编辑)
put: "wsl" | "lit",
id: Msg["_id"],
nam: Msg["nam"],
msg: Msg["msg"],
} | {
put: "wsl" | "lit", // 更新文章(编辑)
id: Msg["_id"], // 置顶
put: "wsl" | "lit",
id: Msg["_id"],
pin: boolean,
} | {
put: "wsl" | "lit", // 更新文章(编辑)
del: Msg["_id"], // 删除
put: "wsl" | "lit",
del: Msg["_id"],
} | {
put: "aut", // 更新管理员权限(超级管理员)
put: "aut",
aut: Aut["aut"],
wsl: Aut["wsl"],
lit: Aut["lit"],
Expand Down Expand Up @@ -145,7 +146,19 @@ export async function put(
return cdt_u(c._id, { $push: { aug: { msg: p.msg, amt: 0, utc: now, usr: pas.usr } } })
}
break
}
} case "dbt": case "ern": {
if ("del" in p) return rec_d(coll[p.put], p.del)
else if ("sec" in p) return dbt_u(p.sec, { sec: pas.usr })
else if ("rev" in p) return dbt_u(p.dbt, { rev: p.rev })
break
} case "wsl": case "lit": {
if ("nam" in p) return msg_u(coll[p.put], p.id, { $set: { nam: p.nam, msg: p.msg, "utc.put": now } })
else if ("pin" in p) return msg_u(coll[p.put], p.id, p.pin ? { $set: { pin: true } } : { $unset: { pin: true } })
else if ("del" in p) return msg_d(coll[p.put], p.del)
break
} case "aut": return aut_u({ aut: p.aut, wsl: p.wsl, lit: p.lit })


}
return null
}
2 changes: 1 addition & 1 deletion tst/eid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ Deno.test("msg", async () => {

Deno.test("aut", async () => {
assertEquals({ sup: [1, 2], aut: [2], wsl: [], lit: [] }, await aut())
assertEquals(1, await aut_u({ sup: [1, 2], aut: [2, 3], wsl: [3, 4], lit: [3, 4] }))
assertEquals(1, await aut_u({ aut: [2, 3], wsl: [3, 4], lit: [3, 4] }))
assertEquals(["aut", "wsl", "lit",], await aut_f(3))
})
43 changes: 40 additions & 3 deletions tst/pos.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,55 @@ Deno.test("pos", async () => {
const { sms } = (await usr_r({ _id: u2 }, { sms: 1 }))!
const { jwt } = (await pos(json({ psg: "code", nbr: nbr[1], code: sms!.code })))! as PosRet
assertEquals([
3, 1,
null, null, null, null, null, null,
1, 1,
null, null, null,
null, null, null, null, null,
1, 1, 1,
1, 1, 1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,

], (await Promise.all([
pos(json({ pre: "usr", nbr: nbr[2], adm1, adm2 }), jwt!),
await pos(json({ pre: "soc", nam: "俱乐部", adm1, adm2 }), jwt!),

pos(json({ put: "soc", soc: 1, msg: "msg" }), jwt!),
pos(json({ put: "soc", soc: 1, agr: "agr" }), jwt!),

pos(json({ pre: "agd", nam: "活动", soc: 1 }), jwt!),
pos(json({ pre: "cdt", cdt }), jwt!),
pos(json({ pre: "dbt", dbt }), jwt!),
pos(json({ pre: "ern", ern }), jwt!),
pos(json({ pre: "wsl", nam: "文章" }), jwt!),
pos(json({ pre: "lit", nam: "文章" }), jwt!),
])).map(r => r.ret))
pos(json({ put: "usr", usr: u2, nam: "用户", adm1, adm2, msg: "msg" }), jwt!),
await pos(json({ put: "soc", soc: 1, nam: "俱乐部改", adm1, adm2, sec: [u2] }), jwt!),
await pos(json({ put: "aut", aut: [2, 3], wsl: [2], lit: [2] }), jwt!),

await pos(json({ pre: "cdt", cdt }), jwt!),
pos(json({ pre: "cdt", cdt: cdt2 }), jwt!),
pos(json({ pre: "dbt", dbt }), jwt!),
pos(json({ pre: "ern", ern }), jwt!),
pos(json({ pre: "wsl", nam: "文章" }), jwt!),
pos(json({ pre: "lit", nam: "文章" }), jwt!),

pos(json({ put: "soc", soc: 1, msg: "msg" }), jwt!),
pos(json({ put: "soc", soc: 1, agr: "agr" }), jwt!),
await pos(json({ pre: "agd", nam: "活动", soc: 1 }), jwt!),
pos(json({ put: "agd", agd: 1, nam: "活动改", adm1, adm2, msg: "msg" }), jwt!),

pos(json({ put: "cdt", aug: cdt._id, msg: "msg", amt: 1 }), jwt!),
pos(json({ put: "cdt", agr: cdt._id }), jwt!),
pos(json({ put: "cdt", quo: 1, msg: "签到" }), jwt!),
pos(json({ put: "dbt", sec: dbt._id }), jwt!),
pos(json({ put: "dbt", dbt: dbt._id, rev: { msg: "rev", rev: 3, utc } }), jwt!),
pos(json({ put: "wsl", id: 1, nam: "文章改", msg: "msg" }), jwt!),
pos(json({ put: "wsl", id: 1, pin: true }), jwt!),
])).map(r => r.ret ? 1 : null))
assertEquals([null, 1, null], (await Promise.all([
pos(json({ pre: "dbt", dbt: dbt2 }), jwt!),
await pos(json({ put: "cdt", mov: cdt2._id, msg: "mov", amt: 5 }), jwt!),
pos(json({ put: "cdt", mov: cdt2._id, msg: "mov", amt: 1 }), jwt!),
])).map(r => r.ret ? 1 : null))
})

0 comments on commit 7bdae3b

Please sign in to comment.