pre 创建操作 #45

This commit is contained in:
728
2023-11-25 10:59:20 +08:00
parent 45e17c4b03
commit 1c78eb4a09
4 changed files with 106 additions and 6 deletions

View File

@ -18,8 +18,8 @@
- `usr.ts` 用户数据操作 `user`
- `soc.ts` 俱乐部数据操作 `sociation` `social`
- `agd.ts` 活动数据操作 `agenda`
- `rec.ts` 记录数据操作 `record`
- `msg.ts` 文章数据操作 `message`
- `rec.ts` 记录数据操作 `record` - `credit` `debit` `earn`
- `msg.ts` 文章数据操作 `message` - `wsl` `lit`
- `aut.ts` 权限数据操作 `author` `authority`
* `pra` 业务操作 `praxic`
- `can.ts` 操作权限

View File

@ -1,9 +1,31 @@
import { is_id, is_lim, is_nbr, lim_code } from "../eid/is.ts";
import type { Psg } from "./pas.ts"
import type { Pas, Psg } from "./pas.ts"
import type { Soc } from "../eid/typ.ts"
import type { Pre } from "./pre.ts"
import { is_cdt, is_id, is_in, is_lim, is_nam, is_nbr, is_rec, lim_code } from "../eid/is.ts"
import { is_adm } from "../ont/adm.ts"
// deno-lint-ignore no-explicit-any
export type Ret<T extends (...args: any) => any> = Awaited<ReturnType<T>>
export function is_aut(
pas: Pas,
aut?: Pas["aut"][0],
): boolean {
return aut ? pas.aut.includes(aut) : pas.aut.length > 0
}
export function is_sec(
pas: Pas,
soc?: Soc["_id"],
): boolean {
return soc ? is_in(pas.sec, soc) : pas.sec.length > 0
}
export function is_usr(
pas: Pas,
soc?: Soc["_id"],
): boolean {
return soc ? pas.cdt.some(c => c._id.soc == soc) : pas.cdt.length > 0
}
export function is_psg(
p: Psg
): boolean {
@ -15,3 +37,19 @@ export function is_psg(
}
return false
}
export function is_pre(
pas: Pas,
p: Pre,
): boolean {
switch (p.pre) {
case "usr": return (is_aut(pas) || is_sec(pas)) && is_nbr(p.nbr) && is_adm(p.adm1, p.adm2)
case "soc": return is_aut(pas, "aut") && is_nam(p.nam) && is_adm(p.adm1, p.adm2)
case "agd": return is_sec(pas, p.soc) && is_nam(p.nam)
case "cdt": return is_sec(pas, p.cdt._id.soc) && is_cdt(p.cdt) && p.cdt.sec == pas.usr
case "dbt": return is_usr(pas, p.dbt._id.soc) && p.dbt._id.usr == pas.usr && is_rec(p.dbt)
case "ern": return is_sec(pas, p.ern._id.soc) && is_rec(p.ern) && p.ern.sec == pas.usr
case "wsl": case "lit": return is_aut(pas, p.pre) && is_nam(p.nam)
}
return false
}

View File

@ -1,7 +1,8 @@
import { Ret } from "./can.ts"
import type { Ret } from "./can.ts"
import { Psg, pas, psg } from "./pas.ts"
import { Pre, pre } from "./pre.ts"
export type Pos = Psg
export type Pos = Psg | Pre
export type PosRet = Ret<typeof psg>
export async function pos(
@ -15,6 +16,8 @@ export async function pos(
const p = jwt ? await pas(jwt) : null
if ("psg" in json) return psg(p, json)
else if (!p) return { ret: null }
else if ("pre" in json) return pre(p, json)
return { ret: null }
}

59
src/pra/pre.ts Normal file
View File

@ -0,0 +1,59 @@
import type { Agd, Cdt, Dbt, Ern, Lit, Soc, Usr, Wsl } from "../eid/typ.ts"
import type { Pas } from "./pas.ts"
import { agd_c } from "../eid/agd.ts"
import { soc_c, soc_r } from "../eid/soc.ts"
import { usr_c } from "../eid/usr.ts"
import { Ret, is_pre } from "./can.ts"
export type Pre = {
pre: "usr",
nbr: NonNullable<Usr["nbr"]>,
adm1: Usr["adm1"],
adm2: Usr["adm2"],
} | {
pre: "soc",
nam: Soc["nam"],
adm1: Soc["adm1"],
adm2: Soc["adm2"],
} | {
pre: "agd",
nam: Agd["nam"],
soc: Agd["soc"],
} | {
pre: "cdt",
cdt: Cdt,
} | {
pre: "dbt",
dbt: Dbt,
} | {
pre: "ern",
ern: Ern,
} | {
pre: "wsl",
nam: Wsl["nam"],
} | {
pre: "lit",
nam: Lit["nam"],
}
export type PreRet = {
usr: Ret<typeof usr_c>,
soc: Ret<typeof soc_c>,
agd: Ret<typeof agd_c>,
}
export async function pre(
pas: Pas,
p: Pre,
) {
if (!is_pre(pas, p)) return null
switch (p.pre) {
case "usr": return usr_c(p.nbr, p.adm1, p.adm2)
case "soc": return soc_c(p.nam, p.adm1, p.adm2)
case "agd": {
const s = await soc_r(p.soc, { adm1: 1, adm2: 1 })
return s ? agd_c(p.nam, s.adm1, s.adm2, p.soc) : null
}
}
return null
}