record.ts
This file
Path
packages/project/src/attention/record.ts
101 lines · 2.8 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–15why, one step back1import type { Ask } from "@repo/protocol/domain/ask";2import type { LandingView } from "@repo/protocol/domain/landing";3import type { Policy } from "@repo/protocol/domain/policy";4import type { AskId, Handle, LandedSha, ProjectName } from "@repo/protocol/ids";5import { Array as Arr, DateTime, Option } from "effect";67export interface RecordEntry {8 readonly area: string;9 readonly at: DateTime.Utc;10 readonly handle: Handle;11 readonly landing: LandedSha;12 readonly project: ProjectName;13}1415export interface RecordInput {Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 16why, one step back16 /** Ask ids are unique only within a project: give one project's asks and landings. */Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 17–95why, one step back17 readonly asks: ReadonlyMap<AskId, Ask>;18 readonly landings: readonly LandingView[];19 readonly now: DateTime.Utc;20 readonly policy: Policy["record"];21 readonly reverted: ReadonlySet<LandedSha>;22}2324export interface RecordRule {25 readonly area: string;26 readonly atLeast: number;27}2829const personOf = (handle: Handle) => handle.toLowerCase();3031export const areaOf = (32 landing: LandingView,33 asks: ReadonlyMap<AskId, Ask>34): Option.Option<string> =>35 Option.fromUndefinedOr(landing.ask).pipe(36 Option.flatMap((id) => Option.fromUndefinedOr(asks.get(id))),37 Option.filter((ask) => ask.project === landing.project),38 Option.map((ask) => ask.territory.module)39 );4041const counts = (42 landedAt: DateTime.Utc,43 now: DateTime.Utc,44 window: Policy["record"]45) =>46 DateTime.isLessThanOrEqualTo(47 DateTime.add(landedAt, { days: window.countsAfterDays }),48 now49 ) &&50 DateTime.isLessThanOrEqualTo(51 now,52 DateTime.add(landedAt, { days: window.windowDays })53 );5455export const recordOf = (56 input: RecordInput57): ((handle: Handle) => readonly RecordEntry[]) => {58 const entries = Arr.flatMap(input.landings, (landing): RecordEntry[] => {59 if (60 input.reverted.has(landing.sha) ||61 !counts(landing.landedAt, input.now, input.policy)62 ) {63 return [];64 }6566 return Option.match(areaOf(landing, input.asks), {67 onNone: () => [],68 onSome: (area) => [69 {70 area,71 at: landing.landedAt,72 handle: landing.author,73 landing: landing.sha,74 project: landing.project,75 },76 ],77 });78 });7980 // A Map, not a record: `constructor` is a valid handle.81 const byPerson = new Map<string, RecordEntry[]>();8283 for (const entry of entries) {84 const held = byPerson.get(personOf(entry.handle));8586 if (held === undefined) {87 byPerson.set(personOf(entry.handle), [entry]);88 } else {89 held.push(entry);90 }91 }9293 return (handle) => byPerson.get(personOf(handle)) ?? [];94};95Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 96why, one step back96/** Counts every entry it is given: pass one project's entries. */Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 97–101why, one step back97export const meets = (98 entries: readonly RecordEntry[],99 rule: RecordRule100): boolean =>101 Arr.countBy(entries, ({ area }) => area === rule.area) >= rule.atLeast;