This commit is contained in:
728
2023-05-25 19:32:10 +08:00
parent 19a8d6ed3f
commit 0c44a3a99d
5 changed files with 33 additions and 12 deletions

View File

@ -8,7 +8,8 @@ export type Rol = {
res: Id["_id"][],
}
export type UpdateRel = {
rol: keyof Rol
rol: keyof Rol,
add: boolean,
} | {
rol: keyof Rol,
add: boolean,
@ -22,7 +23,7 @@ export async function rel_u<
_id: T["_id"],
u: UpdateRel,
): Promise<Update<T> | null> {
if ("add" in u && u.add) {
if (u.add && "uid" in u && is_id(u.uid)) {
const projection = {
[u.rol]: 1,
...u.rol === "sec" ? {} : { [`${u.rol}lim`]: 1 },
@ -36,8 +37,22 @@ export async function rel_u<
$addToSet: { [u.rol]: u.uid },
...u.rol === "uid" ? { $pull: { res: u.uid } } : {} // deno-lint-ignore no-explicit-any
} as any // deno-lint-ignore no-explicit-any
} else if ("add" in u && !u.add) return { $pull: { [u.rol]: u.uid } } as any // deno-lint-ignore no-explicit-any
else return { $set: { [u.rol]: [] } } as any
} else if (!u.add && "uid" in u && is_id(u.uid)) return { $pull: { [u.rol]: u.uid } } as any
else if (u.add && u.rol !== "res") {
const projection = {
[u.rol]: 1,
...u.rol === "sec" ? {} : { [`${u.rol}lim`]: 1 },
res: 1,
} // deno-lint-ignore no-explicit-any
const rel = await c.findOne({ _id } as any, { projection })
if (!rel) return null
const lim = u.rol === "sec" ? lim_sec : rel ? rel[`${u.rol}lim`] : 0
const id = [...new Set([...rel[u.rol], ...rel.res])].slice(0, lim)
return {
$set: { [u.rol]: id, res: rel.res.filter(n => !id.includes(n)) }, // deno-lint-ignore no-explicit-any
} as any // deno-lint-ignore no-explicit-any
} else if (!u.add) return { $set: { [u.rol]: [] } } as any
return null
}
export async function rol<

View File

@ -141,7 +141,7 @@ function is_put_idrel(
case "sec": return is_pre_rel(pas)
case "uid": return "uid" in p && p.uid === pas.uid && p.add === false || is_sec(pas, id)
case "res": return "uid" in p && p.uid === pas.uid && (p.add === false || !is_rej(pas))
|| !("uid" in p) && (is_aut(pas.aut, "aut") || is_sec(pas, id))
|| p.add === false && !("uid" in p) && (is_aut(pas.aut, "aut") || is_sec(pas, id))
}
return false
}

View File

@ -116,9 +116,9 @@ export async function pos(
if (typeof add === "boolean" && typeof uid === "number") {
if (typeof sid === "number") return put_soc(p.pas, sid, { rol, add, uid } as PutSoc)
else if (typeof aid === "number") return put_agd(p.pas, aid, { rol, add, uid } as PutAgd)
} else {
if (typeof sid === "number") return put_soc(p.pas, sid, { rol } as PutSoc)
else if (typeof aid === "number") return put_agd(p.pas, aid, { rol } as PutAgd)
} else if (typeof add === "boolean") {
if (typeof sid === "number") return put_soc(p.pas, sid, { rol, add } as PutSoc)
else if (typeof aid === "number") return put_agd(p.pas, aid, { rol, add } as PutAgd)
}
} else if (typeof ordid === "object" && Object.keys(ordid).length === 3) {
if (typeof ord === "boolean") return put_ord(p.pas, ordid, { ord })

View File

@ -213,8 +213,12 @@ Deno.test("put", async () => {
const wsl = await md_r(coll.wsl, 1, { nam: 1, md: 1, utc: 1, utcp: 1 })
assert(wsl!.utcp > wsl!.utc)
assertEquals({ nam: mdu.nam, md: mdu.md }, { nam: wsl!.nam, md: wsl!.md })
await pos({ jwt }, "put", json({ aid: 1, rol: "uid" }))
assertEquals({ _id: 1, uid: [] }, await agd_r(1, { uid: 1 }))
await pos({ jwt }, "put", json({ aid: 1, rol: "uid", add: false }))
await pos({ jwt }, "put", json({ aid: 1, rol: "sec", add: false }))
assertEquals({ _id: 1, sec: [], uid: [] }, await agd_r(1, { sec: 1, uid: 1 }))
await pos({ jwt }, "put", json({ aid: 1, rol: "res", uid: 1, add: true }))
await pos({ jwt }, "put", json({ aid: 1, rol: "sec", add: true }))
assertEquals({ _id: 1, sec: [1], res: [] }, await agd_r(1, { sec: 1, res: 1 }))
assertEquals({ _id: ordid, code: 1, ord: false, msg: "msg" }, await ord_r(ordid))
assertEquals([{ _id: workid, ref: [], rej: [], work: "work", msg: "updated" }], await rec_f(coll.work, 0))
await Promise.all([usr_d(1), soc_d(1), agd_d(1), md_d(coll.wsl, 1), ord_d(ordid), rec_d(coll.work, workid), aut_d(1)])

View File

@ -307,8 +307,9 @@ export function putrel(
const namid = new Map([...d.unam.entries()].map(([u, nam]) => [nam, u]))
if (is_aut(nav.pas.aut, "aut")) t.putsec.addEventListener("click", () => put(
`${id === "sid" ? "s" : "a"}${d._id}`, t.putsec.innerText, {
nam: { p1: "用户名:" }, val: {}, p: "put",
nam: { p1: "用户名:(留空以将所有申请人添加为联络员)" }, val: {}, p: "put",
b: p => {
if (p.p1 === "") return { [id]: d._id, rol: "sec", add: true }
const uid = namid.get(p.p1 ?? "")
return uid ? { [id]: d._id, rol: "sec", uid, add: !d.sec.includes(uid) } : null
},
@ -317,8 +318,9 @@ export function putrel(
})); else t.putsec.remove() // deno-lint-ignore no-explicit-any
if (is_sec(nav.pas, { [id]: d._id } as any)) t.putuid.addEventListener("click", () => put(
`${id === "sid" ? "s" : "a"}${d._id}`, t.putuid.innerText, {
nam: { p1: "用户名:" }, val: {}, p: "put",
nam: { p1: "用户名:(留空以将所有申请人添加为志愿者)" }, val: {}, p: "put",
b: p => {
if (p.p1 === "") return { [id]: d._id, rol: "uid", add: true }
const uid = namid.get(p.p1 ?? "")
return uid ? { [id]: d._id, rol: "uid", uid, add: !d.uid.includes(uid) } : null
},