diff --git a/src/pra/doc.ts b/src/pra/doc.ts index 0e7fafb..3f18024 100644 --- a/src/pra/doc.ts +++ b/src/pra/doc.ts @@ -1,8 +1,9 @@ -import type { Usr } from "../eid/typ.ts" +import type { Soc, Usr } from "../eid/typ.ts" import { coll } from "../eid/db.ts" import { id, idadm, idnam } from "../eid/id.ts" import { usr_r } from "../eid/usr.ts" -import { cdt_a, rec_f, rec_n, rec_s } from "../eid/rec.ts"; +import { cdt_a, rec_f, rec_n, rec_s } from "../eid/rec.ts" +import { soc_r } from "../eid/soc.ts" export async function adm( ) { @@ -41,3 +42,27 @@ export async function usr( sum: { cdt: cdt_n, dbt: dbt_n, ern: ern_n }, } } + +export async function soc( + soc: Soc["_id"] +) { + const [s, a, c, dbt_s, ern_s] = await Promise.all([ + soc_r(soc), id(coll.agd, { soc }), + rec_f(coll.cdt, { soc }, { now: Date.now() }), + rec_s(coll.dbt, { soc }, {}), + rec_s(coll.ern, { soc }, {}), + ]) + if (!s) return null + const [sec, cdt, agd] = await Promise.all([ + idnam(coll.usr, s.sec), + idnam(coll.usr, c.map(c => c._id.usr)), + idnam(coll.agd, a), + ]) + return { + ...s, sec, cdt, agd, sum: { + cdt: c.reduce((s, c) => s + cdt_a(c), 0), + dbt: dbt_s[0] ? dbt_s[0].amt : 0, + ern: ern_s[0] ? ern_s[0].amt : 0, + } + } +} diff --git a/src/pra/que.ts b/src/pra/que.ts index 99baf0e..bab1b39 100644 --- a/src/pra/que.ts +++ b/src/pra/que.ts @@ -1,7 +1,7 @@ import type { Agd, Msg, Rec, Soc, Usr } from "../eid/typ.ts" import { json } from "../ont/json.ts" import { Ret } from "./can.ts" -import { adm, usr } from "./doc.ts" +import { adm, soc, usr } from "./doc.ts" export type Que = { que: "adm", @@ -33,6 +33,7 @@ export type Que = { export type QueRet = { adm: Ret, usr: Ret, + soc: Ret, } export function que( @@ -43,6 +44,7 @@ export function que( if (q) switch (q.que) { case "adm": return adm() case "usr": return usr("nam" in q ? { nam: q.nam } : { _id: q.usr }) + case "soc": return soc(q.soc) } return null diff --git a/tst/que.test.ts b/tst/que.test.ts index 0db9944..7a8b421 100644 --- a/tst/que.test.ts +++ b/tst/que.test.ts @@ -5,6 +5,7 @@ import { QueRet, que } from "../src/pra/que.ts" import { assertEquals } from "./mod.test.ts" import { usr_c, usr_u } from "../src/eid/usr.ts" import { rec_c } from "../src/eid/rec.ts" +import { agd_c } from "../src/eid/agd.ts" await db("tst", true) @@ -27,11 +28,17 @@ Deno.test("que", async () => { ern: [{ nam: "俱乐部", soc: 1, amt: 30 }], sum: { cdt: 3, dbt: 3, ern: 3 }, } + const soc: QueRet["soc"] = { + _id: 1, utc: now, nam: "俱乐部", adm1: "江苏", adm2: "苏州", msg: "", + sec: [[1, "1"], [2, "用户"]], agr: { msg: "", utc: 0 }, + cdt: [[2, "用户"]], agd: [[1, "活动"]], + sum: { cdt: 10, dbt: 15, ern: 30 }, + } assertEquals([ - 1, 2, 1, 1, 1, ...cdt.flatMap(c => [c._id, c._id, c._id]), + 1, 2, 1, 1, 1, 1, ...cdt.flatMap(c => [c._id, c._id, c._id]), ], await Promise.all([ await usr_c(nbr[0], "四川", "成都"), await usr_c(nbr[1], "四川", "成都"), - usr_u(2, { $set: { nam: "用户" } }), + agd_c("活动", "江苏", "苏州", 1), usr_u(2, { $set: { nam: "用户" } }), await soc_c("俱乐部", "江苏", "苏州"), soc_u(1, { $set: { sec: [1, 2] } }), ...cdt.flatMap(c => [rec_c(coll.cdt, c), rec_c(coll.dbt, { ...c, amt: 5 }), rec_c(coll.ern, c)]), ])) @@ -41,4 +48,5 @@ Deno.test("que", async () => { ) assertEquals(usr, { ...await que(`?que="usr"&usr=2`) as QueRet["usr"], utc: now }) assertEquals(usr, { ...await que(`?que="usr"&nam="用户"`) as QueRet["usr"], utc: now }) + assertEquals(soc, { ...await que(`?que="soc"&soc=1`) as QueRet["soc"], utc: now }) })