Compare commits
24 Commits
6a70713b30
...
hotfix/wal
| Author | SHA1 | Date | |
|---|---|---|---|
| 07e55d9f38 | |||
| 2885b503b3 | |||
| 062f5a436f | |||
| ba435dd6a6 | |||
| 3f21f7a7d6 | |||
| 5f960daf78 | |||
| 3b7b856e48 | |||
| cf36f1447f | |||
|
|
1f634eb465 | ||
|
|
84ab3bad99 | ||
|
|
6c8594633a | ||
|
|
064961471b | ||
|
|
e37aad9e1d | ||
|
|
7ec84fbc0f | ||
|
|
2f0b24ce88 | ||
|
|
e56649e5be | ||
|
|
1c492fe8db | ||
|
|
e139f5e227 | ||
|
|
016e7bee79 | ||
|
|
c437593a8c | ||
|
|
134021c91e | ||
|
|
5c779cb6e0 | ||
|
|
32c9859b38 | ||
|
|
283e3e3eb3 |
49
.agents/skills/caveman/SKILL.md
Normal file
49
.agents/skills/caveman/SKILL.md
Normal file
@@ -0,0 +1,49 @@
|
||||
---
|
||||
name: caveman
|
||||
description: >
|
||||
Ultra-compressed communication mode. Cuts token usage ~75% by dropping
|
||||
filler, articles, and pleasantries while keeping full technical accuracy.
|
||||
Use when user says "caveman mode", "talk like caveman", "use caveman",
|
||||
"less tokens", "be brief", or invokes /caveman.
|
||||
---
|
||||
|
||||
Respond terse like smart caveman. All technical substance stay. Only fluff die.
|
||||
|
||||
## Persistence
|
||||
|
||||
ACTIVE EVERY RESPONSE once triggered. No revert after many turns. No filler drift. Still active if unsure. Off only when user says "stop caveman" or "normal mode".
|
||||
|
||||
## Rules
|
||||
|
||||
Drop: articles (a/an/the), filler (just/really/basically/actually/simply), pleasantries (sure/certainly/of course/happy to), hedging. Fragments OK. Short synonyms (big not extensive, fix not "implement a solution for"). Abbreviate common terms (DB/auth/config/req/res/fn/impl). Strip conjunctions. Use arrows for causality (X -> Y). One word when one word enough.
|
||||
|
||||
Technical terms stay exact. Code blocks unchanged. Errors quoted exact.
|
||||
|
||||
Pattern: `[thing] [action] [reason]. [next step].`
|
||||
|
||||
Not: "Sure! I'd be happy to help you with that. The issue you're experiencing is likely caused by..."
|
||||
Yes: "Bug in auth middleware. Token expiry check use `<` not `<=`. Fix:"
|
||||
|
||||
### Examples
|
||||
|
||||
**"Why React component re-render?"**
|
||||
|
||||
> Inline obj prop -> new ref -> re-render. `useMemo`.
|
||||
|
||||
**"Explain database connection pooling."**
|
||||
|
||||
> Pool = reuse DB conn. Skip handshake -> fast under load.
|
||||
|
||||
## Auto-Clarity Exception
|
||||
|
||||
Drop caveman temporarily for: security warnings, irreversible action confirmations, multi-step sequences where fragment order risks misread, user asks to clarify or repeats question. Resume caveman after clear part done.
|
||||
|
||||
Example -- destructive op:
|
||||
|
||||
> **Warning:** This will permanently delete all rows in the `users` table and cannot be undone.
|
||||
>
|
||||
> ```sql
|
||||
> DROP TABLE users;
|
||||
> ```
|
||||
>
|
||||
> Caveman resume. Verify backup exist first.
|
||||
117
.agents/skills/diagnose/SKILL.md
Normal file
117
.agents/skills/diagnose/SKILL.md
Normal file
@@ -0,0 +1,117 @@
|
||||
---
|
||||
name: diagnose
|
||||
description: Disciplined diagnosis loop for hard bugs and performance regressions. Reproduce → minimise → hypothesise → instrument → fix → regression-test. Use when user says "diagnose this" / "debug this", reports a bug, says something is broken/throwing/failing, or describes a performance regression.
|
||||
---
|
||||
|
||||
# Diagnose
|
||||
|
||||
A discipline for hard bugs. Skip phases only when explicitly justified.
|
||||
|
||||
When exploring the codebase, use the project's domain glossary to get a clear mental model of the relevant modules, and check ADRs in the area you're touching.
|
||||
|
||||
## Phase 1 — Build a feedback loop
|
||||
|
||||
**This is the skill.** Everything else is mechanical. If you have a fast, deterministic, agent-runnable pass/fail signal for the bug, you will find the cause — bisection, hypothesis-testing, and instrumentation all just consume that signal. If you don't have one, no amount of staring at code will save you.
|
||||
|
||||
Spend disproportionate effort here. **Be aggressive. Be creative. Refuse to give up.**
|
||||
|
||||
### Ways to construct one — try them in roughly this order
|
||||
|
||||
1. **Failing test** at whatever seam reaches the bug — unit, integration, e2e.
|
||||
2. **Curl / HTTP script** against a running dev server.
|
||||
3. **CLI invocation** with a fixture input, diffing stdout against a known-good snapshot.
|
||||
4. **Headless browser script** (Playwright / Puppeteer) — drives the UI, asserts on DOM/console/network.
|
||||
5. **Replay a captured trace.** Save a real network request / payload / event log to disk; replay it through the code path in isolation.
|
||||
6. **Throwaway harness.** Spin up a minimal subset of the system (one service, mocked deps) that exercises the bug code path with a single function call.
|
||||
7. **Property / fuzz loop.** If the bug is "sometimes wrong output", run 1000 random inputs and look for the failure mode.
|
||||
8. **Bisection harness.** If the bug appeared between two known states (commit, dataset, version), automate "boot at state X, check, repeat" so you can `git bisect run` it.
|
||||
9. **Differential loop.** Run the same input through old-version vs new-version (or two configs) and diff outputs.
|
||||
10. **HITL bash script.** Last resort. If a human must click, drive _them_ with `scripts/hitl-loop.template.sh` so the loop is still structured. Captured output feeds back to you.
|
||||
|
||||
Build the right feedback loop, and the bug is 90% fixed.
|
||||
|
||||
### Iterate on the loop itself
|
||||
|
||||
Treat the loop as a product. Once you have _a_ loop, ask:
|
||||
|
||||
- Can I make it faster? (Cache setup, skip unrelated init, narrow the test scope.)
|
||||
- Can I make the signal sharper? (Assert on the specific symptom, not "didn't crash".)
|
||||
- Can I make it more deterministic? (Pin time, seed RNG, isolate filesystem, freeze network.)
|
||||
|
||||
A 30-second flaky loop is barely better than no loop. A 2-second deterministic loop is a debugging superpower.
|
||||
|
||||
### Non-deterministic bugs
|
||||
|
||||
The goal is not a clean repro but a **higher reproduction rate**. Loop the trigger 100×, parallelise, add stress, narrow timing windows, inject sleeps. A 50%-flake bug is debuggable; 1% is not — keep raising the rate until it's debuggable.
|
||||
|
||||
### When you genuinely cannot build a loop
|
||||
|
||||
Stop and say so explicitly. List what you tried. Ask the user for: (a) access to whatever environment reproduces it, (b) a captured artifact (HAR file, log dump, core dump, screen recording with timestamps), or (c) permission to add temporary production instrumentation. Do **not** proceed to hypothesise without a loop.
|
||||
|
||||
Do not proceed to Phase 2 until you have a loop you believe in.
|
||||
|
||||
## Phase 2 — Reproduce
|
||||
|
||||
Run the loop. Watch the bug appear.
|
||||
|
||||
Confirm:
|
||||
|
||||
- [ ] The loop produces the failure mode the **user** described — not a different failure that happens to be nearby. Wrong bug = wrong fix.
|
||||
- [ ] The failure is reproducible across multiple runs (or, for non-deterministic bugs, reproducible at a high enough rate to debug against).
|
||||
- [ ] You have captured the exact symptom (error message, wrong output, slow timing) so later phases can verify the fix actually addresses it.
|
||||
|
||||
Do not proceed until you reproduce the bug.
|
||||
|
||||
## Phase 3 — Hypothesise
|
||||
|
||||
Generate **3–5 ranked hypotheses** before testing any of them. Single-hypothesis generation anchors on the first plausible idea.
|
||||
|
||||
Each hypothesis must be **falsifiable**: state the prediction it makes.
|
||||
|
||||
> Format: "If <X> is the cause, then <changing Y> will make the bug disappear / <changing Z> will make it worse."
|
||||
|
||||
If you cannot state the prediction, the hypothesis is a vibe — discard or sharpen it.
|
||||
|
||||
**Show the ranked list to the user before testing.** They often have domain knowledge that re-ranks instantly ("we just deployed a change to #3"), or know hypotheses they've already ruled out. Cheap checkpoint, big time saver. Don't block on it — proceed with your ranking if the user is AFK.
|
||||
|
||||
## Phase 4 — Instrument
|
||||
|
||||
Each probe must map to a specific prediction from Phase 3. **Change one variable at a time.**
|
||||
|
||||
Tool preference:
|
||||
|
||||
1. **Debugger / REPL inspection** if the env supports it. One breakpoint beats ten logs.
|
||||
2. **Targeted logs** at the boundaries that distinguish hypotheses.
|
||||
3. Never "log everything and grep".
|
||||
|
||||
**Tag every debug log** with a unique prefix, e.g. `[DEBUG-a4f2]`. Cleanup at the end becomes a single grep. Untagged logs survive; tagged logs die.
|
||||
|
||||
**Perf branch.** For performance regressions, logs are usually wrong. Instead: establish a baseline measurement (timing harness, `performance.now()`, profiler, query plan), then bisect. Measure first, fix second.
|
||||
|
||||
## Phase 5 — Fix + regression test
|
||||
|
||||
Write the regression test **before the fix** — but only if there is a **correct seam** for it.
|
||||
|
||||
A correct seam is one where the test exercises the **real bug pattern** as it occurs at the call site. If the only available seam is too shallow (single-caller test when the bug needs multiple callers, unit test that can't replicate the chain that triggered the bug), a regression test there gives false confidence.
|
||||
|
||||
**If no correct seam exists, that itself is the finding.** Note it. The codebase architecture is preventing the bug from being locked down. Flag this for the next phase.
|
||||
|
||||
If a correct seam exists:
|
||||
|
||||
1. Turn the minimised repro into a failing test at that seam.
|
||||
2. Watch it fail.
|
||||
3. Apply the fix.
|
||||
4. Watch it pass.
|
||||
5. Re-run the Phase 1 feedback loop against the original (un-minimised) scenario.
|
||||
|
||||
## Phase 6 — Cleanup + post-mortem
|
||||
|
||||
Required before declaring done:
|
||||
|
||||
- [ ] Original repro no longer reproduces (re-run the Phase 1 loop)
|
||||
- [ ] Regression test passes (or absence of seam is documented)
|
||||
- [ ] All `[DEBUG-...]` instrumentation removed (`grep` the prefix)
|
||||
- [ ] Throwaway prototypes deleted (or moved to a clearly-marked debug location)
|
||||
- [ ] The hypothesis that turned out correct is stated in the commit / PR message — so the next debugger learns
|
||||
|
||||
**Then ask: what would have prevented this bug?** If the answer involves architectural change (no good test seam, tangled callers, hidden coupling) hand off to the `/improve-codebase-architecture` skill with the specifics. Make the recommendation **after** the fix is in, not before — you have more information now than when you started.
|
||||
41
.agents/skills/diagnose/scripts/hitl-loop.template.sh
Normal file
41
.agents/skills/diagnose/scripts/hitl-loop.template.sh
Normal file
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
# Human-in-the-loop reproduction loop.
|
||||
# Copy this file, edit the steps below, and run it.
|
||||
# The agent runs the script; the user follows prompts in their terminal.
|
||||
#
|
||||
# Usage:
|
||||
# bash hitl-loop.template.sh
|
||||
#
|
||||
# Two helpers:
|
||||
# step "<instruction>" → show instruction, wait for Enter
|
||||
# capture VAR "<question>" → show question, read response into VAR
|
||||
#
|
||||
# At the end, captured values are printed as KEY=VALUE for the agent to parse.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
step() {
|
||||
printf '\n>>> %s\n' "$1"
|
||||
read -r -p " [Enter when done] " _
|
||||
}
|
||||
|
||||
capture() {
|
||||
local var="$1" question="$2" answer
|
||||
printf '\n>>> %s\n' "$question"
|
||||
read -r -p " > " answer
|
||||
printf -v "$var" '%s' "$answer"
|
||||
}
|
||||
|
||||
# --- edit below ---------------------------------------------------------
|
||||
|
||||
step "Open the app at http://localhost:3000 and sign in."
|
||||
|
||||
capture ERRORED "Click the 'Export' button. Did it throw an error? (y/n)"
|
||||
|
||||
capture ERROR_MSG "Paste the error message (or 'none'):"
|
||||
|
||||
# --- edit above ---------------------------------------------------------
|
||||
|
||||
printf '\n--- Captured ---\n'
|
||||
printf 'ERRORED=%s\n' "$ERRORED"
|
||||
printf 'ERROR_MSG=%s\n' "$ERROR_MSG"
|
||||
10
.agents/skills/grill-me/SKILL.md
Normal file
10
.agents/skills/grill-me/SKILL.md
Normal file
@@ -0,0 +1,10 @@
|
||||
---
|
||||
name: grill-me
|
||||
description: Interview the user relentlessly about a plan or design until reaching shared understanding, resolving each branch of the decision tree. Use when user wants to stress-test a plan, get grilled on their design, or mentions "grill me".
|
||||
---
|
||||
|
||||
Interview me relentlessly about every aspect of this plan until we reach a shared understanding. Walk down each branch of the design tree, resolving dependencies between decisions one-by-one. For each question, provide your recommended answer.
|
||||
|
||||
Ask the questions one at a time.
|
||||
|
||||
If a question can be answered by exploring the codebase, explore the codebase instead.
|
||||
47
.agents/skills/grill-with-docs/ADR-FORMAT.md
Normal file
47
.agents/skills/grill-with-docs/ADR-FORMAT.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# ADR Format
|
||||
|
||||
ADRs live in `docs/adr/` and use sequential numbering: `0001-slug.md`, `0002-slug.md`, etc.
|
||||
|
||||
Create the `docs/adr/` directory lazily — only when the first ADR is needed.
|
||||
|
||||
## Template
|
||||
|
||||
```md
|
||||
# {Short title of the decision}
|
||||
|
||||
{1-3 sentences: what's the context, what did we decide, and why.}
|
||||
```
|
||||
|
||||
That's it. An ADR can be a single paragraph. The value is in recording *that* a decision was made and *why* — not in filling out sections.
|
||||
|
||||
## Optional sections
|
||||
|
||||
Only include these when they add genuine value. Most ADRs won't need them.
|
||||
|
||||
- **Status** frontmatter (`proposed | accepted | deprecated | superseded by ADR-NNNN`) — useful when decisions are revisited
|
||||
- **Considered Options** — only when the rejected alternatives are worth remembering
|
||||
- **Consequences** — only when non-obvious downstream effects need to be called out
|
||||
|
||||
## Numbering
|
||||
|
||||
Scan `docs/adr/` for the highest existing number and increment by one.
|
||||
|
||||
## When to offer an ADR
|
||||
|
||||
All three of these must be true:
|
||||
|
||||
1. **Hard to reverse** — the cost of changing your mind later is meaningful
|
||||
2. **Surprising without context** — a future reader will look at the code and wonder "why on earth did they do it this way?"
|
||||
3. **The result of a real trade-off** — there were genuine alternatives and you picked one for specific reasons
|
||||
|
||||
If a decision is easy to reverse, skip it — you'll just reverse it. If it's not surprising, nobody will wonder why. If there was no real alternative, there's nothing to record beyond "we did the obvious thing."
|
||||
|
||||
### What qualifies
|
||||
|
||||
- **Architectural shape.** "We're using a monorepo." "The write model is event-sourced, the read model is projected into Postgres."
|
||||
- **Integration patterns between contexts.** "Ordering and Billing communicate via domain events, not synchronous HTTP."
|
||||
- **Technology choices that carry lock-in.** Database, message bus, auth provider, deployment target. Not every library — just the ones that would take a quarter to swap out.
|
||||
- **Boundary and scope decisions.** "Customer data is owned by the Customer context; other contexts reference it by ID only." The explicit no-s are as valuable as the yes-s.
|
||||
- **Deliberate deviations from the obvious path.** "We're using manual SQL instead of an ORM because X." Anything where a reasonable reader would assume the opposite. These stop the next engineer from "fixing" something that was deliberate.
|
||||
- **Constraints not visible in the code.** "We can't use AWS because of compliance requirements." "Response times must be under 200ms because of the partner API contract."
|
||||
- **Rejected alternatives when the rejection is non-obvious.** If you considered GraphQL and picked REST for subtle reasons, record it — otherwise someone will suggest GraphQL again in six months.
|
||||
60
.agents/skills/grill-with-docs/CONTEXT-FORMAT.md
Normal file
60
.agents/skills/grill-with-docs/CONTEXT-FORMAT.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# CONTEXT.md Format
|
||||
|
||||
## Structure
|
||||
|
||||
```md
|
||||
# {Context Name}
|
||||
|
||||
{One or two sentence description of what this context is and why it exists.}
|
||||
|
||||
## Language
|
||||
|
||||
**Order**:
|
||||
{A one or two sentence description of the term}
|
||||
_Avoid_: Purchase, transaction
|
||||
|
||||
**Invoice**:
|
||||
A request for payment sent to a customer after delivery.
|
||||
_Avoid_: Bill, payment request
|
||||
|
||||
**Customer**:
|
||||
A person or organization that places orders.
|
||||
_Avoid_: Client, buyer, account
|
||||
```
|
||||
|
||||
## Rules
|
||||
|
||||
- **Be opinionated.** When multiple words exist for the same concept, pick the best one and list the others under `_Avoid_`.
|
||||
- **Keep definitions tight.** One or two sentences max. Define what it IS, not what it does.
|
||||
- **Only include terms specific to this project's context.** General programming concepts (timeouts, error types, utility patterns) don't belong even if the project uses them extensively. Before adding a term, ask: is this a concept unique to this context, or a general programming concept? Only the former belongs.
|
||||
- **Group terms under subheadings** when natural clusters emerge. If all terms belong to a single cohesive area, a flat list is fine.
|
||||
|
||||
## Single vs multi-context repos
|
||||
|
||||
**Single context (most repos):** One `CONTEXT.md` at the repo root.
|
||||
|
||||
**Multiple contexts:** A `CONTEXT-MAP.md` at the repo root lists the contexts, where they live, and how they relate to each other:
|
||||
|
||||
```md
|
||||
# Context Map
|
||||
|
||||
## Contexts
|
||||
|
||||
- [Ordering](./src/ordering/CONTEXT.md) — receives and tracks customer orders
|
||||
- [Billing](./src/billing/CONTEXT.md) — generates invoices and processes payments
|
||||
- [Fulfillment](./src/fulfillment/CONTEXT.md) — manages warehouse picking and shipping
|
||||
|
||||
## Relationships
|
||||
|
||||
- **Ordering → Fulfillment**: Ordering emits `OrderPlaced` events; Fulfillment consumes them to start picking
|
||||
- **Fulfillment → Billing**: Fulfillment emits `ShipmentDispatched` events; Billing consumes them to generate invoices
|
||||
- **Ordering ↔ Billing**: Shared types for `CustomerId` and `Money`
|
||||
```
|
||||
|
||||
The skill infers which structure applies:
|
||||
|
||||
- If `CONTEXT-MAP.md` exists, read it to find contexts
|
||||
- If only a root `CONTEXT.md` exists, single context
|
||||
- If neither exists, create a root `CONTEXT.md` lazily when the first term is resolved
|
||||
|
||||
When multiple contexts exist, infer which one the current topic relates to. If unclear, ask.
|
||||
88
.agents/skills/grill-with-docs/SKILL.md
Normal file
88
.agents/skills/grill-with-docs/SKILL.md
Normal file
@@ -0,0 +1,88 @@
|
||||
---
|
||||
name: grill-with-docs
|
||||
description: Grilling session that challenges your plan against the existing domain model, sharpens terminology, and updates documentation (CONTEXT.md, ADRs) inline as decisions crystallise. Use when user wants to stress-test a plan against their project's language and documented decisions.
|
||||
---
|
||||
|
||||
<what-to-do>
|
||||
|
||||
Interview me relentlessly about every aspect of this plan until we reach a shared understanding. Walk down each branch of the design tree, resolving dependencies between decisions one-by-one. For each question, provide your recommended answer.
|
||||
|
||||
Ask the questions one at a time, waiting for feedback on each question before continuing.
|
||||
|
||||
If a question can be answered by exploring the codebase, explore the codebase instead.
|
||||
|
||||
</what-to-do>
|
||||
|
||||
<supporting-info>
|
||||
|
||||
## Domain awareness
|
||||
|
||||
During codebase exploration, also look for existing documentation:
|
||||
|
||||
### File structure
|
||||
|
||||
Most repos have a single context:
|
||||
|
||||
```
|
||||
/
|
||||
├── CONTEXT.md
|
||||
├── docs/
|
||||
│ └── adr/
|
||||
│ ├── 0001-event-sourced-orders.md
|
||||
│ └── 0002-postgres-for-write-model.md
|
||||
└── src/
|
||||
```
|
||||
|
||||
If a `CONTEXT-MAP.md` exists at the root, the repo has multiple contexts. The map points to where each one lives:
|
||||
|
||||
```
|
||||
/
|
||||
├── CONTEXT-MAP.md
|
||||
├── docs/
|
||||
│ └── adr/ ← system-wide decisions
|
||||
├── src/
|
||||
│ ├── ordering/
|
||||
│ │ ├── CONTEXT.md
|
||||
│ │ └── docs/adr/ ← context-specific decisions
|
||||
│ └── billing/
|
||||
│ ├── CONTEXT.md
|
||||
│ └── docs/adr/
|
||||
```
|
||||
|
||||
Create files lazily — only when you have something to write. If no `CONTEXT.md` exists, create one when the first term is resolved. If no `docs/adr/` exists, create it when the first ADR is needed.
|
||||
|
||||
## During the session
|
||||
|
||||
### Challenge against the glossary
|
||||
|
||||
When the user uses a term that conflicts with the existing language in `CONTEXT.md`, call it out immediately. "Your glossary defines 'cancellation' as X, but you seem to mean Y — which is it?"
|
||||
|
||||
### Sharpen fuzzy language
|
||||
|
||||
When the user uses vague or overloaded terms, propose a precise canonical term. "You're saying 'account' — do you mean the Customer or the User? Those are different things."
|
||||
|
||||
### Discuss concrete scenarios
|
||||
|
||||
When domain relationships are being discussed, stress-test them with specific scenarios. Invent scenarios that probe edge cases and force the user to be precise about the boundaries between concepts.
|
||||
|
||||
### Cross-reference with code
|
||||
|
||||
When the user states how something works, check whether the code agrees. If you find a contradiction, surface it: "Your code cancels entire Orders, but you just said partial cancellation is possible — which is right?"
|
||||
|
||||
### Update CONTEXT.md inline
|
||||
|
||||
When a term is resolved, update `CONTEXT.md` right there. Don't batch these up — capture them as they happen. Use the format in [CONTEXT-FORMAT.md](./CONTEXT-FORMAT.md).
|
||||
|
||||
`CONTEXT.md` should be totally devoid of implementation details. Do not treat `CONTEXT.md` as a spec, a scratch pad, or a repository for implementation decisions. It is a glossary and nothing else.
|
||||
|
||||
### Offer ADRs sparingly
|
||||
|
||||
Only offer to create an ADR when all three are true:
|
||||
|
||||
1. **Hard to reverse** — the cost of changing your mind later is meaningful
|
||||
2. **Surprising without context** — a future reader will wonder "why did they do it this way?"
|
||||
3. **The result of a real trade-off** — there were genuine alternatives and you picked one for specific reasons
|
||||
|
||||
If any of the three is missing, skip the ADR. Use the format in [ADR-FORMAT.md](./ADR-FORMAT.md).
|
||||
|
||||
</supporting-info>
|
||||
15
.agents/skills/handoff/SKILL.md
Normal file
15
.agents/skills/handoff/SKILL.md
Normal file
@@ -0,0 +1,15 @@
|
||||
---
|
||||
name: handoff
|
||||
description: Compact the current conversation into a handoff document for another agent to pick up.
|
||||
argument-hint: "What will the next session be used for?"
|
||||
---
|
||||
|
||||
Write a handoff document summarising the current conversation so a fresh agent can continue the work. Save to the temporary directory of the user's OS - not the current workspace.
|
||||
|
||||
Include a "suggested skills" section in the document, which suggests skills that the agent should invoke.
|
||||
|
||||
Do not duplicate content already captured in other artifacts (PRDs, plans, ADRs, issues, commits, diffs). Reference them by path or URL instead.
|
||||
|
||||
Redact any sensitive information, such as API keys, passwords, or personally identifiable information.
|
||||
|
||||
If the user passed arguments, treat them as a description of what the next session will focus on and tailor the doc accordingly.
|
||||
37
.agents/skills/improve-codebase-architecture/DEEPENING.md
Normal file
37
.agents/skills/improve-codebase-architecture/DEEPENING.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Deepening
|
||||
|
||||
How to deepen a cluster of shallow modules safely, given its dependencies. Assumes the vocabulary in [LANGUAGE.md](LANGUAGE.md) — **module**, **interface**, **seam**, **adapter**.
|
||||
|
||||
## Dependency categories
|
||||
|
||||
When assessing a candidate for deepening, classify its dependencies. The category determines how the deepened module is tested across its seam.
|
||||
|
||||
### 1. In-process
|
||||
|
||||
Pure computation, in-memory state, no I/O. Always deepenable — merge the modules and test through the new interface directly. No adapter needed.
|
||||
|
||||
### 2. Local-substitutable
|
||||
|
||||
Dependencies that have local test stand-ins (PGLite for Postgres, in-memory filesystem). Deepenable if the stand-in exists. The deepened module is tested with the stand-in running in the test suite. The seam is internal; no port at the module's external interface.
|
||||
|
||||
### 3. Remote but owned (Ports & Adapters)
|
||||
|
||||
Your own services across a network boundary (microservices, internal APIs). Define a **port** (interface) at the seam. The deep module owns the logic; the transport is injected as an **adapter**. Tests use an in-memory adapter. Production uses an HTTP/gRPC/queue adapter.
|
||||
|
||||
Recommendation shape: *"Define a port at the seam, implement an HTTP adapter for production and an in-memory adapter for testing, so the logic sits in one deep module even though it's deployed across a network."*
|
||||
|
||||
### 4. True external (Mock)
|
||||
|
||||
Third-party services (Stripe, Twilio, etc.) you don't control. The deepened module takes the external dependency as an injected port; tests provide a mock adapter.
|
||||
|
||||
## Seam discipline
|
||||
|
||||
- **One adapter means a hypothetical seam. Two adapters means a real one.** Don't introduce a port unless at least two adapters are justified (typically production + test). A single-adapter seam is just indirection.
|
||||
- **Internal seams vs external seams.** A deep module can have internal seams (private to its implementation, used by its own tests) as well as the external seam at its interface. Don't expose internal seams through the interface just because tests use them.
|
||||
|
||||
## Testing strategy: replace, don't layer
|
||||
|
||||
- Old unit tests on shallow modules become waste once tests at the deepened module's interface exist — delete them.
|
||||
- Write new tests at the deepened module's interface. The **interface is the test surface**.
|
||||
- Tests assert on observable outcomes through the interface, not internal state.
|
||||
- Tests should survive internal refactors — they describe behaviour, not implementation. If a test has to change when the implementation changes, it's testing past the interface.
|
||||
123
.agents/skills/improve-codebase-architecture/HTML-REPORT.md
Normal file
123
.agents/skills/improve-codebase-architecture/HTML-REPORT.md
Normal file
@@ -0,0 +1,123 @@
|
||||
# HTML Report Format
|
||||
|
||||
The architectural review is rendered as a single self-contained HTML file in the OS temp directory. Tailwind and Mermaid both come from CDNs. Mermaid handles graph-shaped diagrams reliably; hand-built divs and inline SVG handle the more editorial visuals (mass diagrams, cross-sections). Mix the two — don't lean on Mermaid for everything, it'll start to look generic.
|
||||
|
||||
## Scaffold
|
||||
|
||||
```html
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Architecture review — {{repo name}}</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script type="module">
|
||||
import mermaid from "https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs";
|
||||
mermaid.initialize({ startOnLoad: true, theme: "neutral", securityLevel: "loose" });
|
||||
</script>
|
||||
<style>
|
||||
/* small custom layer for things Tailwind doesn't cover cleanly:
|
||||
dashed seam lines, hand-drawn-feeling arrow heads, etc. */
|
||||
.seam { stroke-dasharray: 4 4; }
|
||||
.leak { stroke: #dc2626; }
|
||||
.deep { background: linear-gradient(135deg, #0f172a, #1e293b); }
|
||||
</style>
|
||||
</head>
|
||||
<body class="bg-stone-50 text-slate-900 font-sans">
|
||||
<main class="max-w-5xl mx-auto px-6 py-12 space-y-12">
|
||||
<header>...</header>
|
||||
<section id="candidates" class="space-y-10">...</section>
|
||||
<section id="top-recommendation">...</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
## Header
|
||||
|
||||
Repo name, date, and a compact legend: solid box = module, dashed line = seam, red arrow = leakage, thick dark box = deep module. No introduction paragraph — straight into the candidates.
|
||||
|
||||
## Candidate card
|
||||
|
||||
The diagrams carry the weight. Prose is sparse, plain, and uses the glossary terms ([LANGUAGE.md](LANGUAGE.md)) without ceremony.
|
||||
|
||||
Each candidate is one `<article>`:
|
||||
|
||||
- **Title** — short, names the deepening (e.g. "Collapse the Order intake pipeline").
|
||||
- **Badge row** — recommendation strength (`Strong` = emerald, `Worth exploring` = amber, `Speculative` = slate), plus a tag for the dependency category (`in-process`, `local-substitutable`, `ports & adapters`, `mock`).
|
||||
- **Files** — monospaced list, `font-mono text-sm`.
|
||||
- **Before / After diagram** — the centrepiece. Two columns, side by side. See patterns below.
|
||||
- **Problem** — one sentence. What hurts.
|
||||
- **Solution** — one sentence. What changes.
|
||||
- **Wins** — bullets, ≤6 words each. e.g. "Tests hit one interface", "Pricing logic stops leaking", "Delete 4 shallow wrappers".
|
||||
- **ADR callout** (if applicable) — one line in an amber-tinted box.
|
||||
|
||||
No paragraphs of explanation. If the diagram needs a paragraph to be understood, redraw the diagram.
|
||||
|
||||
## Diagram patterns
|
||||
|
||||
Pick the pattern that fits the candidate. Mix them. Don't make every diagram look the same — variety is part of the point.
|
||||
|
||||
### Mermaid graph (the workhorse for dependencies / call flow)
|
||||
|
||||
Use a Mermaid `flowchart` or `graph` when the point is "X calls Y calls Z, and look at the mess." Wrap it in a Tailwind-styled card so it doesn't feel parachuted in. Style with classDef to colour leakage edges red and the deep module dark. Sequence diagrams work well for "before: 6 round-trips; after: 1."
|
||||
|
||||
```html
|
||||
<div class="rounded-lg border border-slate-200 bg-white p-4">
|
||||
<pre class="mermaid">
|
||||
flowchart LR
|
||||
A[OrderHandler] --> B[OrderValidator]
|
||||
B --> C[OrderRepo]
|
||||
C -.leak.-> D[PricingClient]
|
||||
classDef leak stroke:#dc2626,stroke-width:2px;
|
||||
class C,D leak
|
||||
</pre>
|
||||
</div>
|
||||
```
|
||||
|
||||
### Hand-built boxes-and-arrows (when Mermaid's layout fights you)
|
||||
|
||||
Modules as `<div>`s with borders and labels. Arrows as inline SVG `<line>` or `<path>` elements positioned absolutely over a relative container. Reach for this when you want the "after" diagram to feel like one thick-bordered deep module with greyed-out internals — Mermaid won't render that with the right weight.
|
||||
|
||||
### Cross-section (good for layered shallowness)
|
||||
|
||||
Stack horizontal bands (`h-12 border-l-4`) to show layers a call passes through. Before: 6 thin layers each doing nothing. After: 1 thick band labelled with the consolidated responsibility.
|
||||
|
||||
### Mass diagram (good for "interface as wide as implementation")
|
||||
|
||||
Two rectangles per module — one for interface surface area, one for implementation. Before: interface rectangle is nearly as tall as the implementation rectangle (shallow). After: interface rectangle is short, implementation rectangle is tall (deep).
|
||||
|
||||
### Call-graph collapse
|
||||
|
||||
Before: a tree of function calls rendered as nested boxes. After: the same tree collapsed into one box, with the now-internal calls shown faded inside it.
|
||||
|
||||
## Style guidance
|
||||
|
||||
- Lean editorial, not corporate-dashboard. Generous whitespace. Serif optional for headings (`font-serif` works well with stone/slate).
|
||||
- Colour sparingly: one accent (emerald or indigo) plus red for leakage and amber for warnings.
|
||||
- Keep diagrams ~320px tall so before/after sits comfortably side by side without scrolling.
|
||||
- Use `text-xs uppercase tracking-wider` for module labels inside diagrams — they should read as schematic, not as UI.
|
||||
- The only scripts are the Tailwind CDN and the Mermaid ESM import. The report is otherwise static — no app code, no interactivity beyond Mermaid's own rendering.
|
||||
|
||||
## Top recommendation section
|
||||
|
||||
One larger card. Candidate name, one sentence on why, anchor link to its card. That's it.
|
||||
|
||||
## Tone
|
||||
|
||||
Plain English, concise — but the architectural nouns and verbs come straight from [LANGUAGE.md](LANGUAGE.md). Concision is not an excuse to drift.
|
||||
|
||||
**Use exactly:** module, interface, implementation, depth, deep, shallow, seam, adapter, leverage, locality.
|
||||
|
||||
**Never substitute:** component, service, unit (for module) · API, signature (for interface) · boundary (for seam) · layer, wrapper (for module, when you mean module).
|
||||
|
||||
**Phrasings that fit the style:**
|
||||
|
||||
- "Order intake module is shallow — interface nearly matches the implementation."
|
||||
- "Pricing leaks across the seam."
|
||||
- "Deepen: one interface, one place to test."
|
||||
- "Two adapters justify the seam: HTTP in prod, in-memory in tests."
|
||||
|
||||
**Wins bullets** name the gain in glossary terms: *"locality: bugs concentrate in one module"*, *"leverage: one interface, N call sites"*, *"interface shrinks; implementation absorbs the wrappers"*. Don't write *"easier to maintain"* or *"cleaner code"* — those terms aren't in the glossary and don't earn their place.
|
||||
|
||||
No hedging, no throat-clearing, no "it's worth noting that…". If a sentence could be a bullet, make it a bullet. If a bullet could be cut, cut it. If a term isn't in [LANGUAGE.md](LANGUAGE.md), reach for one that is before inventing a new one.
|
||||
@@ -0,0 +1,44 @@
|
||||
# Interface Design
|
||||
|
||||
When the user wants to explore alternative interfaces for a chosen deepening candidate, use this parallel sub-agent pattern. Based on "Design It Twice" (Ousterhout) — your first idea is unlikely to be the best.
|
||||
|
||||
Uses the vocabulary in [LANGUAGE.md](LANGUAGE.md) — **module**, **interface**, **seam**, **adapter**, **leverage**.
|
||||
|
||||
## Process
|
||||
|
||||
### 1. Frame the problem space
|
||||
|
||||
Before spawning sub-agents, write a user-facing explanation of the problem space for the chosen candidate:
|
||||
|
||||
- The constraints any new interface would need to satisfy
|
||||
- The dependencies it would rely on, and which category they fall into (see [DEEPENING.md](DEEPENING.md))
|
||||
- A rough illustrative code sketch to ground the constraints — not a proposal, just a way to make the constraints concrete
|
||||
|
||||
Show this to the user, then immediately proceed to Step 2. The user reads and thinks while the sub-agents work in parallel.
|
||||
|
||||
### 2. Spawn sub-agents
|
||||
|
||||
Spawn 3+ sub-agents in parallel using the Agent tool. Each must produce a **radically different** interface for the deepened module.
|
||||
|
||||
Prompt each sub-agent with a separate technical brief (file paths, coupling details, dependency category from [DEEPENING.md](DEEPENING.md), what sits behind the seam). The brief is independent of the user-facing problem-space explanation in Step 1. Give each agent a different design constraint:
|
||||
|
||||
- Agent 1: "Minimize the interface — aim for 1–3 entry points max. Maximise leverage per entry point."
|
||||
- Agent 2: "Maximise flexibility — support many use cases and extension."
|
||||
- Agent 3: "Optimise for the most common caller — make the default case trivial."
|
||||
- Agent 4 (if applicable): "Design around ports & adapters for cross-seam dependencies."
|
||||
|
||||
Include both [LANGUAGE.md](LANGUAGE.md) vocabulary and CONTEXT.md vocabulary in the brief so each sub-agent names things consistently with the architecture language and the project's domain language.
|
||||
|
||||
Each sub-agent outputs:
|
||||
|
||||
1. Interface (types, methods, params — plus invariants, ordering, error modes)
|
||||
2. Usage example showing how callers use it
|
||||
3. What the implementation hides behind the seam
|
||||
4. Dependency strategy and adapters (see [DEEPENING.md](DEEPENING.md))
|
||||
5. Trade-offs — where leverage is high, where it's thin
|
||||
|
||||
### 3. Present and compare
|
||||
|
||||
Present designs sequentially so the user can absorb each one, then compare them in prose. Contrast by **depth** (leverage at the interface), **locality** (where change concentrates), and **seam placement**.
|
||||
|
||||
After comparing, give your own recommendation: which design you think is strongest and why. If elements from different designs would combine well, propose a hybrid. Be opinionated — the user wants a strong read, not a menu.
|
||||
53
.agents/skills/improve-codebase-architecture/LANGUAGE.md
Normal file
53
.agents/skills/improve-codebase-architecture/LANGUAGE.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# Language
|
||||
|
||||
Shared vocabulary for every suggestion this skill makes. Use these terms exactly — don't substitute "component," "service," "API," or "boundary." Consistent language is the whole point.
|
||||
|
||||
## Terms
|
||||
|
||||
**Module**
|
||||
Anything with an interface and an implementation. Deliberately scale-agnostic — applies equally to a function, class, package, or tier-spanning slice.
|
||||
_Avoid_: unit, component, service.
|
||||
|
||||
**Interface**
|
||||
Everything a caller must know to use the module correctly. Includes the type signature, but also invariants, ordering constraints, error modes, required configuration, and performance characteristics.
|
||||
_Avoid_: API, signature (too narrow — those refer only to the type-level surface).
|
||||
|
||||
**Implementation**
|
||||
What's inside a module — its body of code. Distinct from **Adapter**: a thing can be a small adapter with a large implementation (a Postgres repo) or a large adapter with a small implementation (an in-memory fake). Reach for "adapter" when the seam is the topic; "implementation" otherwise.
|
||||
|
||||
**Depth**
|
||||
Leverage at the interface — the amount of behaviour a caller (or test) can exercise per unit of interface they have to learn. A module is **deep** when a large amount of behaviour sits behind a small interface. A module is **shallow** when the interface is nearly as complex as the implementation.
|
||||
|
||||
**Seam** _(from Michael Feathers)_
|
||||
A place where you can alter behaviour without editing in that place. The *location* at which a module's interface lives. Choosing where to put the seam is its own design decision, distinct from what goes behind it.
|
||||
_Avoid_: boundary (overloaded with DDD's bounded context).
|
||||
|
||||
**Adapter**
|
||||
A concrete thing that satisfies an interface at a seam. Describes *role* (what slot it fills), not substance (what's inside).
|
||||
|
||||
**Leverage**
|
||||
What callers get from depth. More capability per unit of interface they have to learn. One implementation pays back across N call sites and M tests.
|
||||
|
||||
**Locality**
|
||||
What maintainers get from depth. Change, bugs, knowledge, and verification concentrate at one place rather than spreading across callers. Fix once, fixed everywhere.
|
||||
|
||||
## Principles
|
||||
|
||||
- **Depth is a property of the interface, not the implementation.** A deep module can be internally composed of small, mockable, swappable parts — they just aren't part of the interface. A module can have **internal seams** (private to its implementation, used by its own tests) as well as the **external seam** at its interface.
|
||||
- **The deletion test.** Imagine deleting the module. If complexity vanishes, the module wasn't hiding anything (it was a pass-through). If complexity reappears across N callers, the module was earning its keep.
|
||||
- **The interface is the test surface.** Callers and tests cross the same seam. If you want to test *past* the interface, the module is probably the wrong shape.
|
||||
- **One adapter means a hypothetical seam. Two adapters means a real one.** Don't introduce a seam unless something actually varies across it.
|
||||
|
||||
## Relationships
|
||||
|
||||
- A **Module** has exactly one **Interface** (the surface it presents to callers and tests).
|
||||
- **Depth** is a property of a **Module**, measured against its **Interface**.
|
||||
- A **Seam** is where a **Module**'s **Interface** lives.
|
||||
- An **Adapter** sits at a **Seam** and satisfies the **Interface**.
|
||||
- **Depth** produces **Leverage** for callers and **Locality** for maintainers.
|
||||
|
||||
## Rejected framings
|
||||
|
||||
- **Depth as ratio of implementation-lines to interface-lines** (Ousterhout): rewards padding the implementation. We use depth-as-leverage instead.
|
||||
- **"Interface" as the TypeScript `interface` keyword or a class's public methods**: too narrow — interface here includes every fact a caller must know.
|
||||
- **"Boundary"**: overloaded with DDD's bounded context. Say **seam** or **interface**.
|
||||
81
.agents/skills/improve-codebase-architecture/SKILL.md
Normal file
81
.agents/skills/improve-codebase-architecture/SKILL.md
Normal file
@@ -0,0 +1,81 @@
|
||||
---
|
||||
name: improve-codebase-architecture
|
||||
description: Find deepening opportunities in a codebase, informed by the domain language in CONTEXT.md and the decisions in docs/adr/. Use when the user wants to improve architecture, find refactoring opportunities, consolidate tightly-coupled modules, or make a codebase more testable and AI-navigable.
|
||||
---
|
||||
|
||||
# Improve Codebase Architecture
|
||||
|
||||
Surface architectural friction and propose **deepening opportunities** — refactors that turn shallow modules into deep ones. The aim is testability and AI-navigability.
|
||||
|
||||
## Glossary
|
||||
|
||||
Use these terms exactly in every suggestion. Consistent language is the point — don't drift into "component," "service," "API," or "boundary." Full definitions in [LANGUAGE.md](LANGUAGE.md).
|
||||
|
||||
- **Module** — anything with an interface and an implementation (function, class, package, slice).
|
||||
- **Interface** — everything a caller must know to use the module: types, invariants, error modes, ordering, config. Not just the type signature.
|
||||
- **Implementation** — the code inside.
|
||||
- **Depth** — leverage at the interface: a lot of behaviour behind a small interface. **Deep** = high leverage. **Shallow** = interface nearly as complex as the implementation.
|
||||
- **Seam** — where an interface lives; a place behaviour can be altered without editing in place. (Use this, not "boundary.")
|
||||
- **Adapter** — a concrete thing satisfying an interface at a seam.
|
||||
- **Leverage** — what callers get from depth.
|
||||
- **Locality** — what maintainers get from depth: change, bugs, knowledge concentrated in one place.
|
||||
|
||||
Key principles (see [LANGUAGE.md](LANGUAGE.md) for the full list):
|
||||
|
||||
- **Deletion test**: imagine deleting the module. If complexity vanishes, it was a pass-through. If complexity reappears across N callers, it was earning its keep.
|
||||
- **The interface is the test surface.**
|
||||
- **One adapter = hypothetical seam. Two adapters = real seam.**
|
||||
|
||||
This skill is _informed_ by the project's domain model. The domain language gives names to good seams; ADRs record decisions the skill should not re-litigate.
|
||||
|
||||
## Process
|
||||
|
||||
### 1. Explore
|
||||
|
||||
Read the project's domain glossary and any ADRs in the area you're touching first.
|
||||
|
||||
Then use the Agent tool with `subagent_type=Explore` to walk the codebase. Don't follow rigid heuristics — explore organically and note where you experience friction:
|
||||
|
||||
- Where does understanding one concept require bouncing between many small modules?
|
||||
- Where are modules **shallow** — interface nearly as complex as the implementation?
|
||||
- Where have pure functions been extracted just for testability, but the real bugs hide in how they're called (no **locality**)?
|
||||
- Where do tightly-coupled modules leak across their seams?
|
||||
- Which parts of the codebase are untested, or hard to test through their current interface?
|
||||
|
||||
Apply the **deletion test** to anything you suspect is shallow: would deleting it concentrate complexity, or just move it? A "yes, concentrates" is the signal you want.
|
||||
|
||||
### 2. Present candidates as an HTML report
|
||||
|
||||
Write a self-contained HTML file to the OS temp directory so nothing lands in the repo. Resolve the temp dir from `$TMPDIR`, falling back to `/tmp` (or `%TEMP%` on Windows), and write to `<tmpdir>/architecture-review-<timestamp>.html` so each run gets a fresh file. Open it for the user — `xdg-open <path>` on Linux, `open <path>` on macOS, `start <path>` on Windows — and tell them the absolute path.
|
||||
|
||||
The report uses **Tailwind via CDN** for layout and styling, and **Mermaid via CDN** for diagrams where a graph/flow/sequence reliably communicates the structure. Mix Mermaid with hand-crafted CSS/SVG visuals — use Mermaid when relationships are graph-shaped (call graphs, dependencies, sequences), and hand-built divs/SVG when you want something more editorial (mass diagrams, cross-sections, collapse animations). Each candidate gets a **before/after visualisation**. Be visual.
|
||||
|
||||
For each candidate, the same template as before, but rendered as a card:
|
||||
|
||||
- **Files** — which files/modules are involved
|
||||
- **Problem** — why the current architecture is causing friction
|
||||
- **Solution** — plain English description of what would change
|
||||
- **Benefits** — explained in terms of locality and leverage, and how tests would improve
|
||||
- **Before / After diagram** — side-by-side, custom-drawn, illustrating the shallowness and the deepening
|
||||
- **Recommendation strength** — one of `Strong`, `Worth exploring`, `Speculative`, rendered as a badge
|
||||
|
||||
End the report with a **Top recommendation** section: which candidate you'd tackle first and why.
|
||||
|
||||
**Use CONTEXT.md vocabulary for the domain, and [LANGUAGE.md](LANGUAGE.md) vocabulary for the architecture.** If `CONTEXT.md` defines "Order," talk about "the Order intake module" — not "the FooBarHandler," and not "the Order service."
|
||||
|
||||
**ADR conflicts**: if a candidate contradicts an existing ADR, only surface it when the friction is real enough to warrant revisiting the ADR. Mark it clearly in the card (e.g. a warning callout: _"contradicts ADR-0007 — but worth reopening because…"_). Don't list every theoretical refactor an ADR forbids.
|
||||
|
||||
See [HTML-REPORT.md](HTML-REPORT.md) for the full HTML scaffold, diagram patterns, and styling guidance.
|
||||
|
||||
Do NOT propose interfaces yet. After the file is written, ask the user: "Which of these would you like to explore?"
|
||||
|
||||
### 3. Grilling loop
|
||||
|
||||
Once the user picks a candidate, drop into a grilling conversation. Walk the design tree with them — constraints, dependencies, the shape of the deepened module, what sits behind the seam, what tests survive.
|
||||
|
||||
Side effects happen inline as decisions crystallize:
|
||||
|
||||
- **Naming a deepened module after a concept not in `CONTEXT.md`?** Add the term to `CONTEXT.md` — same discipline as `/grill-with-docs` (see [CONTEXT-FORMAT.md](../grill-with-docs/CONTEXT-FORMAT.md)). Create the file lazily if it doesn't exist.
|
||||
- **Sharpening a fuzzy term during the conversation?** Update `CONTEXT.md` right there.
|
||||
- **User rejects the candidate with a load-bearing reason?** Offer an ADR, framed as: _"Want me to record this as an ADR so future architecture reviews don't re-suggest it?"_ Only offer when the reason would actually be needed by a future explorer to avoid re-suggesting the same thing — skip ephemeral reasons ("not worth it right now") and self-evident ones. See [ADR-FORMAT.md](../grill-with-docs/ADR-FORMAT.md).
|
||||
- **Want to explore alternative interfaces for the deepened module?** See [INTERFACE-DESIGN.md](INTERFACE-DESIGN.md).
|
||||
79
.agents/skills/prototype/LOGIC.md
Normal file
79
.agents/skills/prototype/LOGIC.md
Normal file
@@ -0,0 +1,79 @@
|
||||
# Logic Prototype
|
||||
|
||||
A tiny interactive terminal app that lets the user drive a state model by hand. Use this when the question is about **business logic, state transitions, or data shape** — the kind of thing that looks reasonable on paper but only feels wrong once you push it through real cases.
|
||||
|
||||
## When this is the right shape
|
||||
|
||||
- "I'm not sure if this state machine handles the edge case where X then Y."
|
||||
- "Does this data model actually let me represent the case where..."
|
||||
- "I want to feel out what the API should look like before writing it."
|
||||
- Anything where the user wants to **press buttons and watch state change**.
|
||||
|
||||
If the question is "what should this look like" — wrong branch. Use [UI.md](UI.md).
|
||||
|
||||
## Process
|
||||
|
||||
### 1. State the question
|
||||
|
||||
Before writing code, write down what state model and what question you're prototyping. One paragraph, in the prototype's README or a comment at the top of the file. A logic prototype that answers the wrong question is pure waste — make the question explicit so it can be checked later, whether the user is watching now or returning to it AFK.
|
||||
|
||||
### 2. Pick the language
|
||||
|
||||
Use whatever the host project uses. If the project has no obvious runtime (e.g. a docs repo), ask.
|
||||
|
||||
Match the project's existing conventions for tooling — don't add a new package manager or runtime just for the prototype.
|
||||
|
||||
### 3. Isolate the logic in a portable module
|
||||
|
||||
Put the actual logic — the bit that's answering the question — behind a small, pure interface that could be lifted out and dropped into the real codebase later. The TUI around it is throwaway; the logic module shouldn't be.
|
||||
|
||||
The right shape depends on the question:
|
||||
|
||||
- **A pure reducer** — `(state, action) => state`. Good when actions are discrete events and state is a single value.
|
||||
- **A state machine** — explicit states and transitions. Good when "which actions are even legal right now" is part of the question.
|
||||
- **A small set of pure functions** over a plain data type. Good when there's no implicit current state — just transformations.
|
||||
- **A class or module with a clear method surface** when the logic genuinely owns ongoing internal state.
|
||||
|
||||
Pick whichever shape best fits the question being asked, *not* whichever is easiest to wire to a TUI. Keep it pure: no I/O, no terminal code, no `console.log` for control flow. The TUI imports it and calls into it; nothing flows the other direction.
|
||||
|
||||
This is what makes the prototype useful past its own lifetime. When the question's been answered, the validated reducer / machine / function set can be lifted into the real module — the TUI shell gets deleted.
|
||||
|
||||
### 4. Build the smallest TUI that exposes the state
|
||||
|
||||
Build it as a **lightweight TUI** — on every tick, clear the screen (`console.clear()` / `print("\033[2J\033[H")` / equivalent) and re-render the whole frame. The user should always see one stable view, not an ever-growing scrollback.
|
||||
|
||||
Each frame has two parts, in this order:
|
||||
|
||||
1. **Current state**, pretty-printed and diff-friendly (one field per line, or formatted JSON). Use **bold** for field names or section headers and **dim** for less important context (timestamps, IDs, derived values). Native ANSI escape codes are fine — `\x1b[1m` bold, `\x1b[2m` dim, `\x1b[0m` reset. No need to pull in a styling library unless one is already in the project.
|
||||
2. **Keyboard shortcuts**, listed at the bottom: `[a] add user [d] delete user [t] tick clock [q] quit`. Bold the key, dim the description, or vice-versa — whatever reads cleanly.
|
||||
|
||||
Behaviour:
|
||||
|
||||
1. **Initialise state** — a single in-memory object/struct. Render the first frame on start.
|
||||
2. **Read one keystroke (or one line)** at a time, dispatch to a handler that mutates state.
|
||||
3. **Re-render** the full frame after every action — don't append, replace.
|
||||
4. **Loop until quit.**
|
||||
|
||||
The whole frame should fit on one screen.
|
||||
|
||||
### 5. Make it runnable in one command
|
||||
|
||||
Add a script to the project's existing task runner (`package.json` scripts, `Makefile`, `justfile`, `pyproject.toml`). The user should run `pnpm run <prototype-name>` or equivalent — never need to remember a path.
|
||||
|
||||
If the host project has no task runner, just put the command at the top of the prototype's README.
|
||||
|
||||
### 6. Hand it over
|
||||
|
||||
Give the user the run command. They'll drive it themselves; the interesting moments are when they say "wait, that shouldn't be possible" or "huh, I assumed X would be different" — those are the bugs in the _idea_, which is the whole point. If they want new actions added, add them. Prototypes evolve.
|
||||
|
||||
### 7. Capture the answer
|
||||
|
||||
When the prototype has done its job, the answer to the question is the only thing worth keeping. If the user is around, ask what it taught them. If not, leave a `NOTES.md` next to the prototype so the answer can be filled in (or filled in by you, if you've watched the session) before the prototype gets deleted.
|
||||
|
||||
## Anti-patterns
|
||||
|
||||
- **Don't add tests.** A prototype that needs tests is no longer a prototype.
|
||||
- **Don't wire it to the real database.** Use an in-memory store unless the question is specifically about persistence.
|
||||
- **Don't generalise.** No "what if we wanted to support X later." The prototype answers one question.
|
||||
- **Don't blur the logic and the TUI together.** If the reducer / state machine references `console.log`, prompts, or terminal escape codes, it's no longer portable. Keep the TUI as a thin shell over a pure module.
|
||||
- **Don't ship the TUI shell into production.** The shell is optimised for being driven by hand from a terminal. The logic module behind it is the bit worth keeping.
|
||||
30
.agents/skills/prototype/SKILL.md
Normal file
30
.agents/skills/prototype/SKILL.md
Normal file
@@ -0,0 +1,30 @@
|
||||
---
|
||||
name: prototype
|
||||
description: Build a throwaway prototype to flesh out a design before committing to it. Routes between two branches — a runnable terminal app for state/business-logic questions, or several radically different UI variations toggleable from one route. Use when the user wants to prototype, sanity-check a data model or state machine, mock up a UI, explore design options, or says "prototype this", "let me play with it", "try a few designs".
|
||||
---
|
||||
|
||||
# Prototype
|
||||
|
||||
A prototype is **throwaway code that answers a question**. The question decides the shape.
|
||||
|
||||
## Pick a branch
|
||||
|
||||
Identify which question is being answered — from the user's prompt, the surrounding code, or by asking if the user is around:
|
||||
|
||||
- **"Does this logic / state model feel right?"** → [LOGIC.md](LOGIC.md). Build a tiny interactive terminal app that pushes the state machine through cases that are hard to reason about on paper.
|
||||
- **"What should this look like?"** → [UI.md](UI.md). Generate several radically different UI variations on a single route, switchable via a URL search param and a floating bottom bar.
|
||||
|
||||
The two branches produce very different artifacts — getting this wrong wastes the whole prototype. If the question is genuinely ambiguous and the user isn't reachable, default to whichever branch better matches the surrounding code (a backend module → logic; a page or component → UI) and state the assumption at the top of the prototype.
|
||||
|
||||
## Rules that apply to both
|
||||
|
||||
1. **Throwaway from day one, and clearly marked as such.** Locate the prototype code close to where it will actually be used (next to the module or page it's prototyping for) so context is obvious — but name it so a casual reader can see it's a prototype, not production. For throwaway UI routes, obey whatever routing convention the project already uses; don't invent a new top-level structure.
|
||||
2. **One command to run.** Whatever the project's existing task runner supports — `pnpm <name>`, `python <path>`, `bun <path>`, etc. The user must be able to start it without thinking.
|
||||
3. **No persistence by default.** State lives in memory. Persistence is the thing the prototype is _checking_, not something it should depend on. If the question explicitly involves a database, hit a scratch DB or a local file with a clear "PROTOTYPE — wipe me" name.
|
||||
4. **Skip the polish.** No tests, no error handling beyond what makes the prototype _runnable_, no abstractions. The point is to learn something fast and then delete it.
|
||||
5. **Surface the state.** After every action (logic) or on every variant switch (UI), print or render the full relevant state so the user can see what changed.
|
||||
6. **Delete or absorb when done.** When the prototype has answered its question, either delete it or fold the validated decision into the real code — don't leave it rotting in the repo.
|
||||
|
||||
## When done
|
||||
|
||||
The _answer_ is the only thing worth keeping from a prototype. Capture it somewhere durable (commit message, ADR, issue, or a `NOTES.md` next to the prototype) along with the question it was answering. If the user is around, that capture is a quick conversation; if not, leave the placeholder so they (or you, on the next pass) can fill in the verdict before deleting the prototype.
|
||||
112
.agents/skills/prototype/UI.md
Normal file
112
.agents/skills/prototype/UI.md
Normal file
@@ -0,0 +1,112 @@
|
||||
# UI Prototype
|
||||
|
||||
Generate **several radically different UI variations** on a single route, switchable from a floating bottom bar. The user flips between variants in the browser, picks one (or steals bits from each), then throws the rest away.
|
||||
|
||||
If the question is about logic/state rather than what something looks like — wrong branch. Use [LOGIC.md](LOGIC.md).
|
||||
|
||||
## When this is the right shape
|
||||
|
||||
- "What should this page look like?"
|
||||
- "I want to see a few options for this dashboard before committing."
|
||||
- "Try a different layout for the settings screen."
|
||||
- Any time the user would otherwise spend a day picking between three vague mockups in their head.
|
||||
|
||||
## Two sub-shapes — strongly prefer sub-shape A
|
||||
|
||||
A UI prototype is much easier to judge when it's **butting up against the rest of the app** — real header, real sidebar, real data, real density. A throwaway route on its own is a vacuum: every variant looks fine in isolation. Default to sub-shape A whenever there's a plausible existing page to host the variants. Only reach for sub-shape B if the prototype genuinely has no nearby home.
|
||||
|
||||
### Sub-shape A — adjustment to an existing page (preferred)
|
||||
|
||||
The route already exists. Variants are rendered **on the same route**, gated by a `?variant=` URL search param. The existing data fetching, params, and auth all stay — only the rendering swaps. This is the default; pick it unless there's a specific reason not to.
|
||||
|
||||
If the prototype is for something that doesn't yet have a page but *would naturally live inside one* (a new section of the dashboard, a new card on the settings screen, a new step in an existing flow) — that's still sub-shape A. Mount the variants inside the host page.
|
||||
|
||||
### Sub-shape B — a new page (last resort)
|
||||
|
||||
Only use this when the thing being prototyped genuinely has no existing page to live inside — e.g. an entirely new top-level surface, or a flow that can't be embedded anywhere sensible.
|
||||
|
||||
Create a **throwaway route** following whatever routing convention the project already uses — don't invent a new top-level structure. Name it so it's obviously a prototype (e.g. include the word `prototype` in the path or filename). Same `?variant=` pattern.
|
||||
|
||||
Before committing to sub-shape B, sanity-check: is there really no existing page this could be embedded in? An empty route hides design problems that a populated one would expose.
|
||||
|
||||
In both sub-shapes the floating bottom bar is identical.
|
||||
|
||||
## Process
|
||||
|
||||
### 1. State the question and pick N
|
||||
|
||||
Default to **3 variants**. More than 5 stops being radically different and starts being noise — cap there.
|
||||
|
||||
Write down the plan in one line, in the prototype's location or a top-of-file comment:
|
||||
|
||||
> "Three variants of the settings page, switchable via `?variant=`, on the existing `/settings` route."
|
||||
|
||||
This works whether the user is here to push back or not.
|
||||
|
||||
### 2. Generate radically different variants
|
||||
|
||||
Draft each variant. Hold each one to:
|
||||
|
||||
- The page's purpose and the data it has access to.
|
||||
- The project's component library / styling system (TailwindCSS, shadcn, MUI, plain CSS, whatever).
|
||||
- A clear exported component name, e.g. `VariantA`, `VariantB`, `VariantC`.
|
||||
|
||||
Variants must be **structurally different** — different layout, different information hierarchy, different primary affordance, not just different colours. Three slightly-tweaked card grids isn't a UI prototype, it's wallpaper. If two drafts come out too similar, redo one with explicit "do not use a card grid" guidance.
|
||||
|
||||
### 3. Wire them together
|
||||
|
||||
Create a single switcher component on the route:
|
||||
|
||||
```tsx
|
||||
// pseudo-code — adapt to the project's framework
|
||||
const variant = searchParams.get('variant') ?? 'A';
|
||||
return (
|
||||
<>
|
||||
{variant === 'A' && <VariantA {...data} />}
|
||||
{variant === 'B' && <VariantB {...data} />}
|
||||
{variant === 'C' && <VariantC {...data} />}
|
||||
<PrototypeSwitcher variants={['A','B','C']} current={variant} />
|
||||
</>
|
||||
);
|
||||
```
|
||||
|
||||
For sub-shape A (existing page): keep all the existing data fetching above the switcher; only the rendered subtree changes per variant.
|
||||
|
||||
For sub-shape B (new page): the throwaway route under `/prototype/<name>` mounts the same switcher.
|
||||
|
||||
### 4. Build the floating switcher
|
||||
|
||||
A small fixed-position bar at the bottom-centre of the screen with three pieces:
|
||||
|
||||
- **Left arrow** — cycles to the previous variant (wraps around).
|
||||
- **Variant label** — shows the current variant key and, if the variant exports a name, that name too. e.g. `B — Sidebar layout`.
|
||||
- **Right arrow** — cycles forward (wraps around).
|
||||
|
||||
Behaviour:
|
||||
|
||||
- Clicking an arrow updates the URL search param (use the framework's router — `router.replace` on Next, `navigate` on React Router, etc) so the variant is shareable and reload-stable.
|
||||
- Keyboard: `←` and `→` arrow keys also cycle. Don't intercept arrow keys when an `<input>`, `<textarea>`, or `[contenteditable]` is focused.
|
||||
- Visually distinct from the page (e.g. high-contrast pill, subtle shadow) so it's obviously not part of the design being evaluated.
|
||||
- Hidden in production builds — gate on `process.env.NODE_ENV !== 'production'` or an equivalent check, so a stray prototype merge can't ship the bar to users.
|
||||
|
||||
Put the switcher in a single shared component so both sub-shapes can reuse it. Locate it wherever shared UI lives in the project.
|
||||
|
||||
### 5. Hand it over
|
||||
|
||||
Surface the URL (and the `?variant=` keys). The user will flip through whenever they get to it. The interesting feedback is usually **"I want the header from B with the sidebar from C"** — that's the actual design they want.
|
||||
|
||||
### 6. Capture the answer and clean up
|
||||
|
||||
Once a variant has won, write down which one and why (commit message, ADR, issue, or a `NOTES.md` next to the prototype if running AFK and the user hasn't responded yet). Then:
|
||||
|
||||
- **Sub-shape A** — delete the losing variants and the switcher; fold the winner into the existing page.
|
||||
- **Sub-shape B** — promote the winning variant to a real route, delete the throwaway route and the switcher.
|
||||
|
||||
Don't leave variant components or the switcher lying around. They rot fast and confuse the next reader.
|
||||
|
||||
## Anti-patterns
|
||||
|
||||
- **Variants that differ only in colour or copy.** That's a tweak, not a prototype. Real variants disagree about structure.
|
||||
- **Sharing too much code between variants.** A shared `<Header>` is fine; a shared `<Layout>` defeats the point. Each variant should be free to throw out the layout.
|
||||
- **Wiring variants to real mutations.** Read-only prototypes are fine. If a variant needs to mutate, point it at a stub — the question is "what should this look like", not "does the backend work".
|
||||
- **Promoting the prototype directly to production.** The variant code was written under prototype constraints (no tests, minimal error handling). Rewrite it properly when you fold it in.
|
||||
121
.agents/skills/setup-matt-pocock-skills/SKILL.md
Normal file
121
.agents/skills/setup-matt-pocock-skills/SKILL.md
Normal file
@@ -0,0 +1,121 @@
|
||||
---
|
||||
name: setup-matt-pocock-skills
|
||||
description: Sets up an `## Agent skills` block in AGENTS.md/CLAUDE.md and `docs/agents/` so the engineering skills know this repo's issue tracker (GitHub or local markdown), triage label vocabulary, and domain doc layout. Run before first use of `to-issues`, `to-prd`, `triage`, `diagnose`, `tdd`, `improve-codebase-architecture`, or `zoom-out` — or if those skills appear to be missing context about the issue tracker, triage labels, or domain docs.
|
||||
disable-model-invocation: true
|
||||
---
|
||||
|
||||
# Setup Matt Pocock's Skills
|
||||
|
||||
Scaffold the per-repo configuration that the engineering skills assume:
|
||||
|
||||
- **Issue tracker** — where issues live (GitHub by default; local markdown is also supported out of the box)
|
||||
- **Triage labels** — the strings used for the five canonical triage roles
|
||||
- **Domain docs** — where `CONTEXT.md` and ADRs live, and the consumer rules for reading them
|
||||
|
||||
This is a prompt-driven skill, not a deterministic script. Explore, present what you found, confirm with the user, then write.
|
||||
|
||||
## Process
|
||||
|
||||
### 1. Explore
|
||||
|
||||
Look at the current repo to understand its starting state. Read whatever exists; don't assume:
|
||||
|
||||
- `git remote -v` and `.git/config` — is this a GitHub repo? Which one?
|
||||
- `AGENTS.md` and `CLAUDE.md` at the repo root — does either exist? Is there already an `## Agent skills` section in either?
|
||||
- `CONTEXT.md` and `CONTEXT-MAP.md` at the repo root
|
||||
- `docs/adr/` and any `src/*/docs/adr/` directories
|
||||
- `docs/agents/` — does this skill's prior output already exist?
|
||||
- `.scratch/` — sign that a local-markdown issue tracker convention is already in use
|
||||
|
||||
### 2. Present findings and ask
|
||||
|
||||
Summarise what's present and what's missing. Then walk the user through the three decisions **one at a time** — present a section, get the user's answer, then move to the next. Don't dump all three at once.
|
||||
|
||||
Assume the user does not know what these terms mean. Each section starts with a short explainer (what it is, why these skills need it, what changes if they pick differently). Then show the choices and the default.
|
||||
|
||||
**Section A — Issue tracker.**
|
||||
|
||||
> Explainer: The "issue tracker" is where issues live for this repo. Skills like `to-issues`, `triage`, `to-prd`, and `qa` read from and write to it — they need to know whether to call `gh issue create`, write a markdown file under `.scratch/`, or follow some other workflow you describe. Pick the place you actually track work for this repo.
|
||||
|
||||
Default posture: these skills were designed for GitHub. If a `git remote` points at GitHub, propose that. If a `git remote` points at GitLab (`gitlab.com` or a self-hosted host), propose GitLab. Otherwise (or if the user prefers), offer:
|
||||
|
||||
- **GitHub** — issues live in the repo's GitHub Issues (uses the `gh` CLI)
|
||||
- **GitLab** — issues live in the repo's GitLab Issues (uses the [`glab`](https://gitlab.com/gitlab-org/cli) CLI)
|
||||
- **Local markdown** — issues live as files under `.scratch/<feature>/` in this repo (good for solo projects or repos without a remote)
|
||||
- **Other** (Jira, Linear, etc.) — ask the user to describe the workflow in one paragraph; the skill will record it as freeform prose
|
||||
|
||||
**Section B — Triage label vocabulary.**
|
||||
|
||||
> Explainer: When the `triage` skill processes an incoming issue, it moves it through a state machine — needs evaluation, waiting on reporter, ready for an AFK agent to pick up, ready for a human, or won't fix. To do that, it needs to apply labels (or the equivalent in your issue tracker) that match strings *you've actually configured*. If your repo already uses different label names (e.g. `bug:triage` instead of `needs-triage`), map them here so the skill applies the right ones instead of creating duplicates.
|
||||
|
||||
The five canonical roles:
|
||||
|
||||
- `needs-triage` — maintainer needs to evaluate
|
||||
- `needs-info` — waiting on reporter
|
||||
- `ready-for-agent` — fully specified, AFK-ready (an agent can pick it up with no human context)
|
||||
- `ready-for-human` — needs human implementation
|
||||
- `wontfix` — will not be actioned
|
||||
|
||||
Default: each role's string equals its name. Ask the user if they want to override any. If their issue tracker has no existing labels, the defaults are fine.
|
||||
|
||||
**Section C — Domain docs.**
|
||||
|
||||
> Explainer: Some skills (`improve-codebase-architecture`, `diagnose`, `tdd`) read a `CONTEXT.md` file to learn the project's domain language, and `docs/adr/` for past architectural decisions. They need to know whether the repo has one global context or multiple (e.g. a monorepo with separate frontend/backend contexts) so they look in the right place.
|
||||
|
||||
Confirm the layout:
|
||||
|
||||
- **Single-context** — one `CONTEXT.md` + `docs/adr/` at the repo root. Most repos are this.
|
||||
- **Multi-context** — `CONTEXT-MAP.md` at the root pointing to per-context `CONTEXT.md` files (typically a monorepo).
|
||||
|
||||
### 3. Confirm and edit
|
||||
|
||||
Show the user a draft of:
|
||||
|
||||
- The `## Agent skills` block to add to whichever of `CLAUDE.md` / `AGENTS.md` is being edited (see step 4 for selection rules)
|
||||
- The contents of `docs/agents/issue-tracker.md`, `docs/agents/triage-labels.md`, `docs/agents/domain.md`
|
||||
|
||||
Let them edit before writing.
|
||||
|
||||
### 4. Write
|
||||
|
||||
**Pick the file to edit:**
|
||||
|
||||
- If `CLAUDE.md` exists, edit it.
|
||||
- Else if `AGENTS.md` exists, edit it.
|
||||
- If neither exists, ask the user which one to create — don't pick for them.
|
||||
|
||||
Never create `AGENTS.md` when `CLAUDE.md` already exists (or vice versa) — always edit the one that's already there.
|
||||
|
||||
If an `## Agent skills` block already exists in the chosen file, update its contents in-place rather than appending a duplicate. Don't overwrite user edits to the surrounding sections.
|
||||
|
||||
The block:
|
||||
|
||||
```markdown
|
||||
## Agent skills
|
||||
|
||||
### Issue tracker
|
||||
|
||||
[one-line summary of where issues are tracked]. See `docs/agents/issue-tracker.md`.
|
||||
|
||||
### Triage labels
|
||||
|
||||
[one-line summary of the label vocabulary]. See `docs/agents/triage-labels.md`.
|
||||
|
||||
### Domain docs
|
||||
|
||||
[one-line summary of layout — "single-context" or "multi-context"]. See `docs/agents/domain.md`.
|
||||
```
|
||||
|
||||
Then write the three docs files using the seed templates in this skill folder as a starting point:
|
||||
|
||||
- [issue-tracker-github.md](./issue-tracker-github.md) — GitHub issue tracker
|
||||
- [issue-tracker-gitlab.md](./issue-tracker-gitlab.md) — GitLab issue tracker
|
||||
- [issue-tracker-local.md](./issue-tracker-local.md) — local-markdown issue tracker
|
||||
- [triage-labels.md](./triage-labels.md) — label mapping
|
||||
- [domain.md](./domain.md) — domain doc consumer rules + layout
|
||||
|
||||
For "other" issue trackers, write `docs/agents/issue-tracker.md` from scratch using the user's description.
|
||||
|
||||
### 5. Done
|
||||
|
||||
Tell the user the setup is complete and which engineering skills will now read from these files. Mention they can edit `docs/agents/*.md` directly later — re-running this skill is only necessary if they want to switch issue trackers or restart from scratch.
|
||||
51
.agents/skills/setup-matt-pocock-skills/domain.md
Normal file
51
.agents/skills/setup-matt-pocock-skills/domain.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Domain Docs
|
||||
|
||||
How the engineering skills should consume this repo's domain documentation when exploring the codebase.
|
||||
|
||||
## Before exploring, read these
|
||||
|
||||
- **`CONTEXT.md`** at the repo root, or
|
||||
- **`CONTEXT-MAP.md`** at the repo root if it exists — it points at one `CONTEXT.md` per context. Read each one relevant to the topic.
|
||||
- **`docs/adr/`** — read ADRs that touch the area you're about to work in. In multi-context repos, also check `src/<context>/docs/adr/` for context-scoped decisions.
|
||||
|
||||
If any of these files don't exist, **proceed silently**. Don't flag their absence; don't suggest creating them upfront. The producer skill (`/grill-with-docs`) creates them lazily when terms or decisions actually get resolved.
|
||||
|
||||
## File structure
|
||||
|
||||
Single-context repo (most repos):
|
||||
|
||||
```
|
||||
/
|
||||
├── CONTEXT.md
|
||||
├── docs/adr/
|
||||
│ ├── 0001-event-sourced-orders.md
|
||||
│ └── 0002-postgres-for-write-model.md
|
||||
└── src/
|
||||
```
|
||||
|
||||
Multi-context repo (presence of `CONTEXT-MAP.md` at the root):
|
||||
|
||||
```
|
||||
/
|
||||
├── CONTEXT-MAP.md
|
||||
├── docs/adr/ ← system-wide decisions
|
||||
└── src/
|
||||
├── ordering/
|
||||
│ ├── CONTEXT.md
|
||||
│ └── docs/adr/ ← context-specific decisions
|
||||
└── billing/
|
||||
├── CONTEXT.md
|
||||
└── docs/adr/
|
||||
```
|
||||
|
||||
## Use the glossary's vocabulary
|
||||
|
||||
When your output names a domain concept (in an issue title, a refactor proposal, a hypothesis, a test name), use the term as defined in `CONTEXT.md`. Don't drift to synonyms the glossary explicitly avoids.
|
||||
|
||||
If the concept you need isn't in the glossary yet, that's a signal — either you're inventing language the project doesn't use (reconsider) or there's a real gap (note it for `/grill-with-docs`).
|
||||
|
||||
## Flag ADR conflicts
|
||||
|
||||
If your output contradicts an existing ADR, surface it explicitly rather than silently overriding:
|
||||
|
||||
> _Contradicts ADR-0007 (event-sourced orders) — but worth reopening because…_
|
||||
@@ -0,0 +1,22 @@
|
||||
# Issue tracker: GitHub
|
||||
|
||||
Issues and PRDs for this repo live as GitHub issues. Use the `gh` CLI for all operations.
|
||||
|
||||
## Conventions
|
||||
|
||||
- **Create an issue**: `gh issue create --title "..." --body "..."`. Use a heredoc for multi-line bodies.
|
||||
- **Read an issue**: `gh issue view <number> --comments`, filtering comments by `jq` and also fetching labels.
|
||||
- **List issues**: `gh issue list --state open --json number,title,body,labels,comments --jq '[.[] | {number, title, body, labels: [.labels[].name], comments: [.comments[].body]}]'` with appropriate `--label` and `--state` filters.
|
||||
- **Comment on an issue**: `gh issue comment <number> --body "..."`
|
||||
- **Apply / remove labels**: `gh issue edit <number> --add-label "..."` / `--remove-label "..."`
|
||||
- **Close**: `gh issue close <number> --comment "..."`
|
||||
|
||||
Infer the repo from `git remote -v` — `gh` does this automatically when run inside a clone.
|
||||
|
||||
## When a skill says "publish to the issue tracker"
|
||||
|
||||
Create a GitHub issue.
|
||||
|
||||
## When a skill says "fetch the relevant ticket"
|
||||
|
||||
Run `gh issue view <number> --comments`.
|
||||
@@ -0,0 +1,23 @@
|
||||
# Issue tracker: GitLab
|
||||
|
||||
Issues and PRDs for this repo live as GitLab issues. Use the [`glab`](https://gitlab.com/gitlab-org/cli) CLI for all operations.
|
||||
|
||||
## Conventions
|
||||
|
||||
- **Create an issue**: `glab issue create --title "..." --description "..."`. Use a heredoc for multi-line descriptions. Pass `--description -` to open an editor.
|
||||
- **Read an issue**: `glab issue view <number> --comments`. Use `-F json` for machine-readable output.
|
||||
- **List issues**: `glab issue list -F json` with appropriate `--label` filters.
|
||||
- **Comment on an issue**: `glab issue note <number> --message "..."`. GitLab calls comments "notes".
|
||||
- **Apply / remove labels**: `glab issue update <number> --label "..."` / `--unlabel "..."`. Multiple labels can be comma-separated or by repeating the flag.
|
||||
- **Close**: `glab issue close <number>`. `glab issue close` does not accept a closing comment, so post the explanation first with `glab issue note <number> --message "..."`, then close.
|
||||
- **Merge requests**: GitLab calls PRs "merge requests". Use `glab mr create`, `glab mr view`, `glab mr note`, etc. — the same shape as `gh pr ...` with `mr` in place of `pr` and `note`/`--message` in place of `comment`/`--body`.
|
||||
|
||||
Infer the repo from `git remote -v` — `glab` does this automatically when run inside a clone.
|
||||
|
||||
## When a skill says "publish to the issue tracker"
|
||||
|
||||
Create a GitLab issue.
|
||||
|
||||
## When a skill says "fetch the relevant ticket"
|
||||
|
||||
Run `glab issue view <number> --comments`.
|
||||
@@ -0,0 +1,19 @@
|
||||
# Issue tracker: Local Markdown
|
||||
|
||||
Issues and PRDs for this repo live as markdown files in `.scratch/`.
|
||||
|
||||
## Conventions
|
||||
|
||||
- One feature per directory: `.scratch/<feature-slug>/`
|
||||
- The PRD is `.scratch/<feature-slug>/PRD.md`
|
||||
- Implementation issues are `.scratch/<feature-slug>/issues/<NN>-<slug>.md`, numbered from `01`
|
||||
- Triage state is recorded as a `Status:` line near the top of each issue file (see `triage-labels.md` for the role strings)
|
||||
- Comments and conversation history append to the bottom of the file under a `## Comments` heading
|
||||
|
||||
## When a skill says "publish to the issue tracker"
|
||||
|
||||
Create a new file under `.scratch/<feature-slug>/` (creating the directory if needed).
|
||||
|
||||
## When a skill says "fetch the relevant ticket"
|
||||
|
||||
Read the file at the referenced path. The user will normally pass the path or the issue number directly.
|
||||
15
.agents/skills/setup-matt-pocock-skills/triage-labels.md
Normal file
15
.agents/skills/setup-matt-pocock-skills/triage-labels.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Triage Labels
|
||||
|
||||
The skills speak in terms of five canonical triage roles. This file maps those roles to the actual label strings used in this repo's issue tracker.
|
||||
|
||||
| Label in mattpocock/skills | Label in our tracker | Meaning |
|
||||
| -------------------------- | -------------------- | ---------------------------------------- |
|
||||
| `needs-triage` | `needs-triage` | Maintainer needs to evaluate this issue |
|
||||
| `needs-info` | `needs-info` | Waiting on reporter for more information |
|
||||
| `ready-for-agent` | `ready-for-agent` | Fully specified, ready for an AFK agent |
|
||||
| `ready-for-human` | `ready-for-human` | Requires human implementation |
|
||||
| `wontfix` | `wontfix` | Will not be actioned |
|
||||
|
||||
When a skill mentions a role (e.g. "apply the AFK-ready triage label"), use the corresponding label string from this table.
|
||||
|
||||
Edit the right-hand column to match whatever vocabulary you actually use.
|
||||
109
.agents/skills/tdd/SKILL.md
Normal file
109
.agents/skills/tdd/SKILL.md
Normal file
@@ -0,0 +1,109 @@
|
||||
---
|
||||
name: tdd
|
||||
description: Test-driven development with red-green-refactor loop. Use when user wants to build features or fix bugs using TDD, mentions "red-green-refactor", wants integration tests, or asks for test-first development.
|
||||
---
|
||||
|
||||
# Test-Driven Development
|
||||
|
||||
## Philosophy
|
||||
|
||||
**Core principle**: Tests should verify behavior through public interfaces, not implementation details. Code can change entirely; tests shouldn't.
|
||||
|
||||
**Good tests** are integration-style: they exercise real code paths through public APIs. They describe _what_ the system does, not _how_ it does it. A good test reads like a specification - "user can checkout with valid cart" tells you exactly what capability exists. These tests survive refactors because they don't care about internal structure.
|
||||
|
||||
**Bad tests** are coupled to implementation. They mock internal collaborators, test private methods, or verify through external means (like querying a database directly instead of using the interface). The warning sign: your test breaks when you refactor, but behavior hasn't changed. If you rename an internal function and tests fail, those tests were testing implementation, not behavior.
|
||||
|
||||
See [tests.md](tests.md) for examples and [mocking.md](mocking.md) for mocking guidelines.
|
||||
|
||||
## Anti-Pattern: Horizontal Slices
|
||||
|
||||
**DO NOT write all tests first, then all implementation.** This is "horizontal slicing" - treating RED as "write all tests" and GREEN as "write all code."
|
||||
|
||||
This produces **crap tests**:
|
||||
|
||||
- Tests written in bulk test _imagined_ behavior, not _actual_ behavior
|
||||
- You end up testing the _shape_ of things (data structures, function signatures) rather than user-facing behavior
|
||||
- Tests become insensitive to real changes - they pass when behavior breaks, fail when behavior is fine
|
||||
- You outrun your headlights, committing to test structure before understanding the implementation
|
||||
|
||||
**Correct approach**: Vertical slices via tracer bullets. One test → one implementation → repeat. Each test responds to what you learned from the previous cycle. Because you just wrote the code, you know exactly what behavior matters and how to verify it.
|
||||
|
||||
```
|
||||
WRONG (horizontal):
|
||||
RED: test1, test2, test3, test4, test5
|
||||
GREEN: impl1, impl2, impl3, impl4, impl5
|
||||
|
||||
RIGHT (vertical):
|
||||
RED→GREEN: test1→impl1
|
||||
RED→GREEN: test2→impl2
|
||||
RED→GREEN: test3→impl3
|
||||
...
|
||||
```
|
||||
|
||||
## Workflow
|
||||
|
||||
### 1. Planning
|
||||
|
||||
When exploring the codebase, use the project's domain glossary so that test names and interface vocabulary match the project's language, and respect ADRs in the area you're touching.
|
||||
|
||||
Before writing any code:
|
||||
|
||||
- [ ] Confirm with user what interface changes are needed
|
||||
- [ ] Confirm with user which behaviors to test (prioritize)
|
||||
- [ ] Identify opportunities for [deep modules](deep-modules.md) (small interface, deep implementation)
|
||||
- [ ] Design interfaces for [testability](interface-design.md)
|
||||
- [ ] List the behaviors to test (not implementation steps)
|
||||
- [ ] Get user approval on the plan
|
||||
|
||||
Ask: "What should the public interface look like? Which behaviors are most important to test?"
|
||||
|
||||
**You can't test everything.** Confirm with the user exactly which behaviors matter most. Focus testing effort on critical paths and complex logic, not every possible edge case.
|
||||
|
||||
### 2. Tracer Bullet
|
||||
|
||||
Write ONE test that confirms ONE thing about the system:
|
||||
|
||||
```
|
||||
RED: Write test for first behavior → test fails
|
||||
GREEN: Write minimal code to pass → test passes
|
||||
```
|
||||
|
||||
This is your tracer bullet - proves the path works end-to-end.
|
||||
|
||||
### 3. Incremental Loop
|
||||
|
||||
For each remaining behavior:
|
||||
|
||||
```
|
||||
RED: Write next test → fails
|
||||
GREEN: Minimal code to pass → passes
|
||||
```
|
||||
|
||||
Rules:
|
||||
|
||||
- One test at a time
|
||||
- Only enough code to pass current test
|
||||
- Don't anticipate future tests
|
||||
- Keep tests focused on observable behavior
|
||||
|
||||
### 4. Refactor
|
||||
|
||||
After all tests pass, look for [refactor candidates](refactoring.md):
|
||||
|
||||
- [ ] Extract duplication
|
||||
- [ ] Deepen modules (move complexity behind simple interfaces)
|
||||
- [ ] Apply SOLID principles where natural
|
||||
- [ ] Consider what new code reveals about existing code
|
||||
- [ ] Run tests after each refactor step
|
||||
|
||||
**Never refactor while RED.** Get to GREEN first.
|
||||
|
||||
## Checklist Per Cycle
|
||||
|
||||
```
|
||||
[ ] Test describes behavior, not implementation
|
||||
[ ] Test uses public interface only
|
||||
[ ] Test would survive internal refactor
|
||||
[ ] Code is minimal for this test
|
||||
[ ] No speculative features added
|
||||
```
|
||||
33
.agents/skills/tdd/deep-modules.md
Normal file
33
.agents/skills/tdd/deep-modules.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# Deep Modules
|
||||
|
||||
From "A Philosophy of Software Design":
|
||||
|
||||
**Deep module** = small interface + lots of implementation
|
||||
|
||||
```
|
||||
┌─────────────────────┐
|
||||
│ Small Interface │ ← Few methods, simple params
|
||||
├─────────────────────┤
|
||||
│ │
|
||||
│ │
|
||||
│ Deep Implementation│ ← Complex logic hidden
|
||||
│ │
|
||||
│ │
|
||||
└─────────────────────┘
|
||||
```
|
||||
|
||||
**Shallow module** = large interface + little implementation (avoid)
|
||||
|
||||
```
|
||||
┌─────────────────────────────────┐
|
||||
│ Large Interface │ ← Many methods, complex params
|
||||
├─────────────────────────────────┤
|
||||
│ Thin Implementation │ ← Just passes through
|
||||
└─────────────────────────────────┘
|
||||
```
|
||||
|
||||
When designing interfaces, ask:
|
||||
|
||||
- Can I reduce the number of methods?
|
||||
- Can I simplify the parameters?
|
||||
- Can I hide more complexity inside?
|
||||
31
.agents/skills/tdd/interface-design.md
Normal file
31
.agents/skills/tdd/interface-design.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Interface Design for Testability
|
||||
|
||||
Good interfaces make testing natural:
|
||||
|
||||
1. **Accept dependencies, don't create them**
|
||||
|
||||
```typescript
|
||||
// Testable
|
||||
function processOrder(order, paymentGateway) {}
|
||||
|
||||
// Hard to test
|
||||
function processOrder(order) {
|
||||
const gateway = new StripeGateway();
|
||||
}
|
||||
```
|
||||
|
||||
2. **Return results, don't produce side effects**
|
||||
|
||||
```typescript
|
||||
// Testable
|
||||
function calculateDiscount(cart): Discount {}
|
||||
|
||||
// Hard to test
|
||||
function applyDiscount(cart): void {
|
||||
cart.total -= discount;
|
||||
}
|
||||
```
|
||||
|
||||
3. **Small surface area**
|
||||
- Fewer methods = fewer tests needed
|
||||
- Fewer params = simpler test setup
|
||||
59
.agents/skills/tdd/mocking.md
Normal file
59
.agents/skills/tdd/mocking.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# When to Mock
|
||||
|
||||
Mock at **system boundaries** only:
|
||||
|
||||
- External APIs (payment, email, etc.)
|
||||
- Databases (sometimes - prefer test DB)
|
||||
- Time/randomness
|
||||
- File system (sometimes)
|
||||
|
||||
Don't mock:
|
||||
|
||||
- Your own classes/modules
|
||||
- Internal collaborators
|
||||
- Anything you control
|
||||
|
||||
## Designing for Mockability
|
||||
|
||||
At system boundaries, design interfaces that are easy to mock:
|
||||
|
||||
**1. Use dependency injection**
|
||||
|
||||
Pass external dependencies in rather than creating them internally:
|
||||
|
||||
```typescript
|
||||
// Easy to mock
|
||||
function processPayment(order, paymentClient) {
|
||||
return paymentClient.charge(order.total);
|
||||
}
|
||||
|
||||
// Hard to mock
|
||||
function processPayment(order) {
|
||||
const client = new StripeClient(process.env.STRIPE_KEY);
|
||||
return client.charge(order.total);
|
||||
}
|
||||
```
|
||||
|
||||
**2. Prefer SDK-style interfaces over generic fetchers**
|
||||
|
||||
Create specific functions for each external operation instead of one generic function with conditional logic:
|
||||
|
||||
```typescript
|
||||
// GOOD: Each function is independently mockable
|
||||
const api = {
|
||||
getUser: (id) => fetch(`/users/${id}`),
|
||||
getOrders: (userId) => fetch(`/users/${userId}/orders`),
|
||||
createOrder: (data) => fetch('/orders', { method: 'POST', body: data }),
|
||||
};
|
||||
|
||||
// BAD: Mocking requires conditional logic inside the mock
|
||||
const api = {
|
||||
fetch: (endpoint, options) => fetch(endpoint, options),
|
||||
};
|
||||
```
|
||||
|
||||
The SDK approach means:
|
||||
- Each mock returns one specific shape
|
||||
- No conditional logic in test setup
|
||||
- Easier to see which endpoints a test exercises
|
||||
- Type safety per endpoint
|
||||
10
.agents/skills/tdd/refactoring.md
Normal file
10
.agents/skills/tdd/refactoring.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Refactor Candidates
|
||||
|
||||
After TDD cycle, look for:
|
||||
|
||||
- **Duplication** → Extract function/class
|
||||
- **Long methods** → Break into private helpers (keep tests on public interface)
|
||||
- **Shallow modules** → Combine or deepen
|
||||
- **Feature envy** → Move logic to where data lives
|
||||
- **Primitive obsession** → Introduce value objects
|
||||
- **Existing code** the new code reveals as problematic
|
||||
61
.agents/skills/tdd/tests.md
Normal file
61
.agents/skills/tdd/tests.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# Good and Bad Tests
|
||||
|
||||
## Good Tests
|
||||
|
||||
**Integration-style**: Test through real interfaces, not mocks of internal parts.
|
||||
|
||||
```typescript
|
||||
// GOOD: Tests observable behavior
|
||||
test("user can checkout with valid cart", async () => {
|
||||
const cart = createCart();
|
||||
cart.add(product);
|
||||
const result = await checkout(cart, paymentMethod);
|
||||
expect(result.status).toBe("confirmed");
|
||||
});
|
||||
```
|
||||
|
||||
Characteristics:
|
||||
|
||||
- Tests behavior users/callers care about
|
||||
- Uses public API only
|
||||
- Survives internal refactors
|
||||
- Describes WHAT, not HOW
|
||||
- One logical assertion per test
|
||||
|
||||
## Bad Tests
|
||||
|
||||
**Implementation-detail tests**: Coupled to internal structure.
|
||||
|
||||
```typescript
|
||||
// BAD: Tests implementation details
|
||||
test("checkout calls paymentService.process", async () => {
|
||||
const mockPayment = jest.mock(paymentService);
|
||||
await checkout(cart, payment);
|
||||
expect(mockPayment.process).toHaveBeenCalledWith(cart.total);
|
||||
});
|
||||
```
|
||||
|
||||
Red flags:
|
||||
|
||||
- Mocking internal collaborators
|
||||
- Testing private methods
|
||||
- Asserting on call counts/order
|
||||
- Test breaks when refactoring without behavior change
|
||||
- Test name describes HOW not WHAT
|
||||
- Verifying through external means instead of interface
|
||||
|
||||
```typescript
|
||||
// BAD: Bypasses interface to verify
|
||||
test("createUser saves to database", async () => {
|
||||
await createUser({ name: "Alice" });
|
||||
const row = await db.query("SELECT * FROM users WHERE name = ?", ["Alice"]);
|
||||
expect(row).toBeDefined();
|
||||
});
|
||||
|
||||
// GOOD: Verifies through interface
|
||||
test("createUser makes user retrievable", async () => {
|
||||
const user = await createUser({ name: "Alice" });
|
||||
const retrieved = await getUser(user.id);
|
||||
expect(retrieved.name).toBe("Alice");
|
||||
});
|
||||
```
|
||||
35
.agents/skills/teach/GLOSSARY-FORMAT.md
Normal file
35
.agents/skills/teach/GLOSSARY-FORMAT.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# GLOSSARY.md Format
|
||||
|
||||
`GLOSSARY.md` is the canonical language for this teaching workspace. All explainers, exercises, and learning records should adhere to its terminology. Building it is itself part of learning: compressing a concept into a tight definition is evidence the user understands it.
|
||||
|
||||
## Structure
|
||||
|
||||
```md
|
||||
# {Topic} Glossary
|
||||
|
||||
{One or two sentence description of the topic this glossary covers.}
|
||||
|
||||
## Terms
|
||||
|
||||
**Hypertrophy**:
|
||||
Muscle growth driven by mechanical tension and metabolic stress over repeated training sessions.
|
||||
_Avoid_: Bulking, getting big
|
||||
|
||||
**Progressive overload**:
|
||||
Systematically increasing the demand on a muscle over time — via load, volume, or intensity.
|
||||
_Avoid_: Pushing harder, levelling up
|
||||
|
||||
**RPE (Rate of Perceived Exertion)**:
|
||||
A 1–10 self-rating of how hard a set felt, where 10 is failure and 8 means two reps left in the tank.
|
||||
_Avoid_: Effort score, intensity rating
|
||||
```
|
||||
|
||||
## Rules
|
||||
|
||||
- **Add a term only when the user understands it.** The glossary is a record of compressed knowledge, not a dictionary the user reads to learn. If the user has just been introduced to a concept, wait until they can use it correctly before promoting it here.
|
||||
- **Be opinionated.** When several words exist for the same concept, pick the best one and list the rest as aliases to avoid. This is how language compresses.
|
||||
- **Keep definitions tight.** One or two sentences. Define what the term IS, not what it does or how to do it.
|
||||
- **Use the glossary's own terms inside definitions.** Once a term is in the glossary, prefer it everywhere — including inside other definitions. This is what makes complex terms easier to grasp later.
|
||||
- **Group under subheadings** when natural clusters emerge (e.g. `## Anatomy`, `## Programming`). A flat list is fine when terms cohere.
|
||||
- **Flag ambiguities explicitly.** If a term is used loosely in the wider field, note the resolution: "In this workspace, 'set' always means a working set — warm-ups are tracked separately."
|
||||
- **Revise as understanding deepens.** A definition the user wrote in week one may be wrong by week six. Update in place; do not leave stale entries.
|
||||
46
.agents/skills/teach/LEARNING-RECORD-FORMAT.md
Normal file
46
.agents/skills/teach/LEARNING-RECORD-FORMAT.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# Learning Record Format
|
||||
|
||||
Learning records live in `./learning-records/` and use sequential numbering: `0001-slug.md`, `0002-slug.md`, etc. Create the directory lazily — only when the first record is written.
|
||||
|
||||
They are the teaching equivalent of ADRs: they capture non-obvious lessons, key insights, and stated prior knowledge that will steer future sessions. They are used to calculate the zone of proximal development.
|
||||
|
||||
## Template
|
||||
|
||||
```md
|
||||
# {Short title of what was learned or established}
|
||||
|
||||
{1-3 sentences: what was learned (or what prior knowledge was established), and why it matters for future sessions.}
|
||||
```
|
||||
|
||||
That is the whole format. A learning record can be a single paragraph. The value is recording _that_ this is now known and _why_ it changes what to teach next — not in filling out sections.
|
||||
|
||||
## Optional sections
|
||||
|
||||
Only include these when they add genuine value. Most records won't need them.
|
||||
|
||||
- **Status** frontmatter (`active | superseded by LR-NNNN`) — useful when an earlier understanding turns out to be wrong and is replaced.
|
||||
- **Evidence** — how the user demonstrated the understanding (a question answered, an exercise completed, prior experience cited). Useful when the claim might be revisited.
|
||||
- **Implications** — what this unlocks or rules out for future sessions. Worth recording when non-obvious.
|
||||
|
||||
## Numbering
|
||||
|
||||
Scan `./learning-records/` for the highest existing number and increment by one.
|
||||
|
||||
## When to write a learning record
|
||||
|
||||
Write one when any of these is true:
|
||||
|
||||
1. **The user demonstrated genuine understanding of something non-trivial** — not just exposure, but evidence they can use the concept correctly. This sets a new floor for what to teach next.
|
||||
2. **The user disclosed prior knowledge** — "I already know X." Record it so future sessions don't re-teach it. Also record the _depth_ claimed.
|
||||
3. **A misconception was corrected** — the user previously believed something wrong and now sees why. These are high-value: they predict future stumbling blocks for related topics.
|
||||
4. **The mission shifted in response to learning** — the user discovered they cared about something different than they thought. Cross-link to [[MISSION.md]] and update it.
|
||||
|
||||
### What does _not_ qualify
|
||||
|
||||
- Material that was merely covered. Coverage is not learning. Wait for evidence.
|
||||
- Anything already captured tersely in [[GLOSSARY.md]] as a term definition. Don't duplicate.
|
||||
- Session-by-session activity logs. Learning records are not a journal — they are decision-grade insights.
|
||||
|
||||
## Supersession
|
||||
|
||||
When a later record contradicts an earlier one (the user's understanding deepened or corrected), mark the old record `Status: superseded by LR-NNNN` rather than deleting it. The history of how understanding evolved is itself useful signal.
|
||||
31
.agents/skills/teach/MISSION-FORMAT.md
Normal file
31
.agents/skills/teach/MISSION-FORMAT.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# MISSION.md Format
|
||||
|
||||
`MISSION.md` lives at the workspace root. It captures the _reason_ the user is learning this topic. Every teaching decision — what to teach next, which resources to surface, which exercises to design — should trace back to this document.
|
||||
|
||||
## Template
|
||||
|
||||
```md
|
||||
# Mission: {Topic}
|
||||
|
||||
## Why
|
||||
{1-3 sentences. The concrete real-world goal the user is chasing. What changes in their life or work when they have this skill? Avoid abstract framings like "to understand X" — push for the underlying outcome.}
|
||||
|
||||
## Success looks like
|
||||
- {A specific, observable thing the user will be able to do}
|
||||
- {Another specific thing}
|
||||
- {…}
|
||||
|
||||
## Constraints
|
||||
- {Time, budget, prior commitments, learning preferences, anything that bounds the approach}
|
||||
|
||||
## Out of scope
|
||||
- {Adjacent topics the user explicitly does not want to chase right now — protects the zone of proximal development}
|
||||
```
|
||||
|
||||
## Rules
|
||||
|
||||
- **One mission per workspace.** If the user wants to learn two unrelated things, that is two workspaces.
|
||||
- **Concrete over abstract.** "Run a half marathon by October" beats "get fitter." "Ship a Rust CLI to my team" beats "learn Rust."
|
||||
- **Push back on vagueness.** If the user cannot articulate why, interview them before writing anything. A bad mission is worse than no mission.
|
||||
- **Revise when reality shifts.** Missions change. When the user's goal moves, update this file — don't leave a stale mission steering future sessions.
|
||||
- **Keep it short.** If `MISSION.md` runs past a screen, it has stopped being a compass and started being a plan.
|
||||
32
.agents/skills/teach/RESOURCES-FORMAT.md
Normal file
32
.agents/skills/teach/RESOURCES-FORMAT.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# RESOURCES.md Format
|
||||
|
||||
`RESOURCES.md` is the curated set of trusted sources for this topic. Knowledge for explainers should be drawn from here, not from parametric guesses. Wisdom comes from the communities listed here.
|
||||
|
||||
## Structure
|
||||
|
||||
```md
|
||||
# {Topic} Resources
|
||||
|
||||
## Knowledge
|
||||
|
||||
- [Book: _The Science and Practice of Strength Training_ — Zatsiorsky & Kraemer](https://example.com)
|
||||
Foundational text on programming and adaptation. Use for: anything to do with periodisation, recovery, intensity zones.
|
||||
- [Article: "How Much Should I Train?" — Greg Nuckols (Stronger By Science)](https://example.com)
|
||||
Evidence-based review of volume landmarks. Use for: weekly set targets per muscle group.
|
||||
|
||||
## Wisdom (Communities)
|
||||
|
||||
- [r/weightroom](https://reddit.com/r/weightroom)
|
||||
High-signal subreddit, moderated against bro-science. Use for: programme critique, plateau troubleshooting.
|
||||
- Local: Tuesday strength class at {gym name}
|
||||
Use for: real-time coaching feedback on lifts.
|
||||
```
|
||||
|
||||
## Rules
|
||||
|
||||
- **High-trust only.** Prefer primary sources, recognised experts, peer-reviewed work, and communities with strong moderation. If a resource is marketing dressed as education, leave it out.
|
||||
- **Annotate every entry.** A bare link is useless in three months. Add one line: what it covers and when to reach for it.
|
||||
- **Group by Knowledge / Wisdom.** Mirrors the philosophy in [SKILL.md](./SKILL.md). It is fine for a resource to appear in only one group.
|
||||
- **Surface gaps explicitly.** If no good resource exists for an area the mission needs, write a `## Gaps` section listing what is missing. This drives future search.
|
||||
- **Prune ruthlessly.** A resource that turned out to be wrong, shallow, or off-mission should be removed, not buried. Better five sharp sources than thirty mediocre ones.
|
||||
- **Record community preferences.** If the user has opted out of joining communities, note it here so future sessions don't keep proposing them.
|
||||
131
.agents/skills/teach/SKILL.md
Normal file
131
.agents/skills/teach/SKILL.md
Normal file
@@ -0,0 +1,131 @@
|
||||
---
|
||||
name: teach
|
||||
description: Teach the user a new skill or concept, within this workspace.
|
||||
disable-model-invocation: true
|
||||
argument-hint: "What would you like to learn about?"
|
||||
---
|
||||
|
||||
The user has asked you to teach them something. This is a stateful request - they intend to learn the topic over multiple sessions.
|
||||
|
||||
## Teaching Workspace
|
||||
|
||||
Treat the current directory as a teaching workspace. The state of their learning is captured in this directory in several files:
|
||||
|
||||
- `MISSION.md`: A document capturing the _reason_ the user is interested in the topic. This should be used to ground all teaching. Use the format in [MISSION-FORMAT.md](./MISSION-FORMAT.md).
|
||||
- `./reference/*.html`: A directory of reference materials. These are the compressed learnings from the lessons - cheat sheets, reference algorithms, syntax, yoga poses, glossaries. They are the raw units of learning. They should be beautiful documents which print out well, and are designed for quick reference.
|
||||
- `RESOURCES.md`: A list of resources which can be explored to ground your teaching in contextual knowledge, or to acquire knowledge and wisdom. Use the format in [RESOURCES-FORMAT.md](./RESOURCES-FORMAT.md).
|
||||
- `./learning-records/*.md`: A directory of learning records, which capture what the user has learned. These are loosely equivalent to architectural decision records in software development - they capture non-obvious lessons and key insights that may need to be revised later, or drive future sessions. These should be used to calculate the zone of proximal development. They are titled `0001-<dash-case-name>.md`, where the number increments each time. Use the format in [LEARNING-RECORD-FORMAT.md](./LEARNING-RECORD-FORMAT.md).
|
||||
- `./lessons/*.html`: A directory of lessons. A **lesson** is a single, self-contained HTML output that teaches one tightly-scoped thing tied to the mission. This is the primary unit of teaching in this workspace.
|
||||
- `NOTES.md`: A scratchpad for you to jot down user preferences, or working notes.
|
||||
|
||||
## Philosophy
|
||||
|
||||
To learn at a deep level, the user needs three things:
|
||||
|
||||
- **Knowledge**, captured from high-quality, high-trust resources
|
||||
- **Skills**, acquired through highly-relevant interactive lessons devised by you, based on the knowledge
|
||||
- **Wisdom**, which comes from interacting with other learners and practitioners
|
||||
|
||||
Before the `RESOURCES.md` is well-populated, your focus should be to find high-quality resources which will help the user acquire knowledge. Never trust your parametric knowledge.
|
||||
|
||||
Some topics may require more skills than knowledge. Learning more about theoretical physics might be more knowledge-based. For yoga, more skills-based.
|
||||
|
||||
### Fluency vs Storage Strength
|
||||
|
||||
You should be careful to split between two types of learning:
|
||||
|
||||
- **Fluency strength**: in-the-moment retrieval of knowledge
|
||||
- **Storage strength**: long-term retention of knowledge
|
||||
|
||||
Fluency can give the user an illusory sense of mastery, but storage strength is the real goal. Try to design lessons which build long-term retention by desirable difficulty:
|
||||
|
||||
- Using retrieval practice (recall from memory)
|
||||
- Spacing (distributing practice over time)
|
||||
- Interleaving (mixing up different but related topics in practice - for skills practice only)
|
||||
|
||||
## Lessons
|
||||
|
||||
A lesson is the main thing you produce — the unit in which knowledge and skills reach the user. Each lesson is one self-contained HTML file, saved to `./lessons/` and titled `0001-<dash-case-name>.html` where the number increments each time.
|
||||
|
||||
A lesson should be **beautiful** — clean, readable typography and layout — since the user will return to these later to review. Think Tufte.
|
||||
|
||||
The lesson should be short, and completable very quickly. Learners' working memory is very small, and we need to stay within it. But each lesson should give the user a single tangible win that they can build on. It should be directly tied to the mission, and should be in the user's zone of proximal development.
|
||||
|
||||
If possible, open the lesson file for the user by running a CLI command.
|
||||
|
||||
Each lesson should link via HTML anchors to other lessons and reference documents.
|
||||
|
||||
Each lesson should recommend a primary source for the user to read or watch. This should be the most high-quality, high-trust resource you found on the topic.
|
||||
|
||||
Each lesson should contain a reminder to ask followup questions to the agent. The agent is their teacher, and can assist with anything that's unclear.
|
||||
|
||||
## The Mission
|
||||
|
||||
Every lesson should be tied into the mission - the reason that the user is interested in learning about the topic.
|
||||
|
||||
If the user is unclear about the mission, or the `MISSION.md` is not populated, your first job should be to question the user on why they want to learn this.
|
||||
|
||||
Failing to understand the mission will mean knowledge acquisition is not grounded in real-world goals. Lessons will feel too abstract. You will have no way of judging what the user should do next.
|
||||
|
||||
Missions may change as the user develops more skills and knowledge. This is normal - make sure to update the `MISSION.md` and add a learning record to capture the change. Confirm with the user before changing the mission.
|
||||
|
||||
## Zone Of Proximal Development
|
||||
|
||||
Each lesson, the user should always feel as if they are being challenged 'just enough'.
|
||||
|
||||
The user may specify an exact thing they want to learn. If they don't, figure out their zone of proximal development by:
|
||||
|
||||
- Reading their `learning-records`
|
||||
- Figuring out the right thing to teach them based on their mission
|
||||
- Teach the most relevant thing that fits in their zone of proximal development
|
||||
|
||||
## Knowledge
|
||||
|
||||
Lessons should be designed around a skill the user is going to learn. The knowledge in the lesson should be only what's required to acquire that skill. You teach the knowledge first, then get the user to practice the skills via an interactive feedback loop.
|
||||
|
||||
Knowledge should first be gathered from trusted resources. Use `RESOURCES.md` to keep track of them. Lessons should be littered with citations - links to external resources to back up any claim made. This increases the trustworthiness of the lesson.
|
||||
|
||||
For acquiring knowledge, difficulty is the enemy. It eats working memory you need for understanding.
|
||||
|
||||
## Skills
|
||||
|
||||
If knowledge is all about acquisition, skills are about durability and flexibility. Make the knowledge stick.
|
||||
|
||||
For skill acquisition, difficulty is the tool. Effortful retrieval is what builds storage strength. Skills should be taught through interactive lessons. There are several tools at your disposal:
|
||||
|
||||
- Interactive lessons, using quizzes and light in-browser tasks
|
||||
- Lessons which guide the user through a list of real-world steps to take (for instance, yoga poses)
|
||||
|
||||
Each of these should be based on a **feedback loop**, where the user receives feedback on their performance. This feedback loop should be as tight as possible, giving feedback immediately - and ideally automatically.
|
||||
|
||||
For quizzes, each answer should be exactly the same number of words (and characters, if possible). Don't give the user any clues about the answer through formatting.
|
||||
|
||||
## Acquiring Wisdom
|
||||
|
||||
Wisdom comes from true real-world interaction - testing your skills outside the learning environment.
|
||||
|
||||
When the user asks a question that appears to require wisdom, your default posture should be to attempt to answer - but to ultimately delegate to a **community**.
|
||||
|
||||
A community is a place (online or offline) where the user can test their skills in the real world. This might be a forum, a subreddit, a real-world class (budget permitting) or a local interest group.
|
||||
|
||||
You should attempt to find high-reputation communities the user can join. If the user expresses a preference that they don't want to join a community, respect it.
|
||||
|
||||
## Reference Documents
|
||||
|
||||
While creating lessons, you should also create reference documents. Lessons can reference these documents - they are useful for tracking raw units of knowledge useful across lessons.
|
||||
|
||||
Lessons will rarely be revisited later - reference documents will be. They should be the compressed essence of the lesson, in a format designed for quick reference.
|
||||
|
||||
Some learning topics lend themselves to reference:
|
||||
|
||||
- Syntax and code snippets for programming
|
||||
- Algorithms and flowcharts for processes
|
||||
- Yoga poses and sequences for yoga
|
||||
- Exercises and routines for fitness
|
||||
- Glossaries for any topic with its own nomenclature
|
||||
|
||||
Glossaries, in particular, are an essential reference. Once one is created, it should be adhered to in every lesson.
|
||||
|
||||
## `NOTES.md`
|
||||
|
||||
The user will sometimes express preferences of how they want to be taught, or things you should keep in mind. This is the place to record those preferences, so you can refer back to them when designing lessons or working with the user.
|
||||
83
.agents/skills/to-issues/SKILL.md
Normal file
83
.agents/skills/to-issues/SKILL.md
Normal file
@@ -0,0 +1,83 @@
|
||||
---
|
||||
name: to-issues
|
||||
description: Break a plan, spec, or PRD into independently-grabbable issues on the project issue tracker using tracer-bullet vertical slices. Use when user wants to convert a plan into issues, create implementation tickets, or break down work into issues.
|
||||
---
|
||||
|
||||
# To Issues
|
||||
|
||||
Break a plan into independently-grabbable issues using vertical slices (tracer bullets).
|
||||
|
||||
The issue tracker and triage label vocabulary should have been provided to you — run `/setup-matt-pocock-skills` if not.
|
||||
|
||||
## Process
|
||||
|
||||
### 1. Gather context
|
||||
|
||||
Work from whatever is already in the conversation context. If the user passes an issue reference (issue number, URL, or path) as an argument, fetch it from the issue tracker and read its full body and comments.
|
||||
|
||||
### 2. Explore the codebase (optional)
|
||||
|
||||
If you have not already explored the codebase, do so to understand the current state of the code. Issue titles and descriptions should use the project's domain glossary vocabulary, and respect ADRs in the area you're touching.
|
||||
|
||||
### 3. Draft vertical slices
|
||||
|
||||
Break the plan into **tracer bullet** issues. Each issue is a thin vertical slice that cuts through ALL integration layers end-to-end, NOT a horizontal slice of one layer.
|
||||
|
||||
Slices may be 'HITL' or 'AFK'. HITL slices require human interaction, such as an architectural decision or a design review. AFK slices can be implemented and merged without human interaction. Prefer AFK over HITL where possible.
|
||||
|
||||
<vertical-slice-rules>
|
||||
- Each slice delivers a narrow but COMPLETE path through every layer (schema, API, UI, tests)
|
||||
- A completed slice is demoable or verifiable on its own
|
||||
- Prefer many thin slices over few thick ones
|
||||
</vertical-slice-rules>
|
||||
|
||||
### 4. Quiz the user
|
||||
|
||||
Present the proposed breakdown as a numbered list. For each slice, show:
|
||||
|
||||
- **Title**: short descriptive name
|
||||
- **Type**: HITL / AFK
|
||||
- **Blocked by**: which other slices (if any) must complete first
|
||||
- **User stories covered**: which user stories this addresses (if the source material has them)
|
||||
|
||||
Ask the user:
|
||||
|
||||
- Does the granularity feel right? (too coarse / too fine)
|
||||
- Are the dependency relationships correct?
|
||||
- Should any slices be merged or split further?
|
||||
- Are the correct slices marked as HITL and AFK?
|
||||
|
||||
Iterate until the user approves the breakdown.
|
||||
|
||||
### 5. Publish the issues to the issue tracker
|
||||
|
||||
For each approved slice, publish a new issue to the issue tracker. Use the issue body template below. These issues are considered ready for AFK agents, so publish them with the correct triage label unless instructed otherwise.
|
||||
|
||||
Publish issues in dependency order (blockers first) so you can reference real issue identifiers in the "Blocked by" field.
|
||||
|
||||
<issue-template>
|
||||
## Parent
|
||||
|
||||
A reference to the parent issue on the issue tracker (if the source was an existing issue, otherwise omit this section).
|
||||
|
||||
## What to build
|
||||
|
||||
A concise description of this vertical slice. Describe the end-to-end behavior, not layer-by-layer implementation.
|
||||
|
||||
Avoid specific file paths or code snippets — they go stale fast. Exception: if a prototype produced a snippet that encodes a decision more precisely than prose can (state machine, reducer, schema, type shape), inline it here and note briefly that it came from a prototype. Trim to the decision-rich parts — not a working demo, just the important bits.
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [ ] Criterion 1
|
||||
- [ ] Criterion 2
|
||||
- [ ] Criterion 3
|
||||
|
||||
## Blocked by
|
||||
|
||||
- A reference to the blocking ticket (if any)
|
||||
|
||||
Or "None - can start immediately" if no blockers.
|
||||
|
||||
</issue-template>
|
||||
|
||||
Do NOT close or modify any parent issue.
|
||||
74
.agents/skills/to-prd/SKILL.md
Normal file
74
.agents/skills/to-prd/SKILL.md
Normal file
@@ -0,0 +1,74 @@
|
||||
---
|
||||
name: to-prd
|
||||
description: Turn the current conversation context into a PRD and publish it to the project issue tracker. Use when user wants to create a PRD from the current context.
|
||||
---
|
||||
|
||||
This skill takes the current conversation context and codebase understanding and produces a PRD. Do NOT interview the user — just synthesize what you already know.
|
||||
|
||||
The issue tracker and triage label vocabulary should have been provided to you — run `/setup-matt-pocock-skills` if not.
|
||||
|
||||
## Process
|
||||
|
||||
1. Explore the repo to understand the current state of the codebase, if you haven't already. Use the project's domain glossary vocabulary throughout the PRD, and respect any ADRs in the area you're touching.
|
||||
|
||||
2. Sketch out the seams at which you're going to test the feature. Existing seams should be preferred to new ones. Use the highest seam possible. If new seams are needed, propose them at the highest point you can.
|
||||
|
||||
Check with the user that these seams match their expectations.
|
||||
|
||||
3. Write the PRD using the template below, then publish it to the project issue tracker. Apply the `ready-for-agent` triage label - no need for additional triage.
|
||||
|
||||
<prd-template>
|
||||
|
||||
## Problem Statement
|
||||
|
||||
The problem that the user is facing, from the user's perspective.
|
||||
|
||||
## Solution
|
||||
|
||||
The solution to the problem, from the user's perspective.
|
||||
|
||||
## User Stories
|
||||
|
||||
A LONG, numbered list of user stories. Each user story should be in the format of:
|
||||
|
||||
1. As an <actor>, I want a <feature>, so that <benefit>
|
||||
|
||||
<user-story-example>
|
||||
1. As a mobile bank customer, I want to see balance on my accounts, so that I can make better informed decisions about my spending
|
||||
</user-story-example>
|
||||
|
||||
This list of user stories should be extremely extensive and cover all aspects of the feature.
|
||||
|
||||
## Implementation Decisions
|
||||
|
||||
A list of implementation decisions that were made. This can include:
|
||||
|
||||
- The modules that will be built/modified
|
||||
- The interfaces of those modules that will be modified
|
||||
- Technical clarifications from the developer
|
||||
- Architectural decisions
|
||||
- Schema changes
|
||||
- API contracts
|
||||
- Specific interactions
|
||||
|
||||
Do NOT include specific file paths or code snippets. They may end up being outdated very quickly.
|
||||
|
||||
Exception: if a prototype produced a snippet that encodes a decision more precisely than prose can (state machine, reducer, schema, type shape), inline it within the relevant decision and note briefly that it came from a prototype. Trim to the decision-rich parts — not a working demo, just the important bits.
|
||||
|
||||
## Testing Decisions
|
||||
|
||||
A list of testing decisions that were made. Include:
|
||||
|
||||
- A description of what makes a good test (only test external behavior, not implementation details)
|
||||
- Which modules will be tested
|
||||
- Prior art for the tests (i.e. similar types of tests in the codebase)
|
||||
|
||||
## Out of Scope
|
||||
|
||||
A description of the things that are out of scope for this PRD.
|
||||
|
||||
## Further Notes
|
||||
|
||||
Any further notes about the feature.
|
||||
|
||||
</prd-template>
|
||||
168
.agents/skills/triage/AGENT-BRIEF.md
Normal file
168
.agents/skills/triage/AGENT-BRIEF.md
Normal file
@@ -0,0 +1,168 @@
|
||||
# Writing Agent Briefs
|
||||
|
||||
An agent brief is a structured comment posted on a GitHub issue when it moves to `ready-for-agent`. It is the authoritative specification that an AFK agent will work from. The original issue body and discussion are context — the agent brief is the contract.
|
||||
|
||||
## Principles
|
||||
|
||||
### Durability over precision
|
||||
|
||||
The issue may sit in `ready-for-agent` for days or weeks. The codebase will change in the meantime. Write the brief so it stays useful even as files are renamed, moved, or refactored.
|
||||
|
||||
- **Do** describe interfaces, types, and behavioral contracts
|
||||
- **Do** name specific types, function signatures, or config shapes that the agent should look for or modify
|
||||
- **Don't** reference file paths — they go stale
|
||||
- **Don't** reference line numbers
|
||||
- **Don't** assume the current implementation structure will remain the same
|
||||
|
||||
### Behavioral, not procedural
|
||||
|
||||
Describe **what** the system should do, not **how** to implement it. The agent will explore the codebase fresh and make its own implementation decisions.
|
||||
|
||||
- **Good:** "The `SkillConfig` type should accept an optional `schedule` field of type `CronExpression`"
|
||||
- **Bad:** "Open src/types/skill.ts and add a schedule field on line 42"
|
||||
- **Good:** "When a user runs `/triage` with no arguments, they should see a summary of issues needing attention"
|
||||
- **Bad:** "Add a switch statement in the main handler function"
|
||||
|
||||
### Complete acceptance criteria
|
||||
|
||||
The agent needs to know when it's done. Every agent brief must have concrete, testable acceptance criteria. Each criterion should be independently verifiable.
|
||||
|
||||
- **Good:** "Running `gh issue list --label needs-triage` returns issues that have been through initial classification"
|
||||
- **Bad:** "Triage should work correctly"
|
||||
|
||||
### Explicit scope boundaries
|
||||
|
||||
State what is out of scope. This prevents the agent from gold-plating or making assumptions about adjacent features.
|
||||
|
||||
## Template
|
||||
|
||||
```markdown
|
||||
## Agent Brief
|
||||
|
||||
**Category:** bug / enhancement
|
||||
**Summary:** one-line description of what needs to happen
|
||||
|
||||
**Current behavior:**
|
||||
Describe what happens now. For bugs, this is the broken behavior.
|
||||
For enhancements, this is the status quo the feature builds on.
|
||||
|
||||
**Desired behavior:**
|
||||
Describe what should happen after the agent's work is complete.
|
||||
Be specific about edge cases and error conditions.
|
||||
|
||||
**Key interfaces:**
|
||||
- `TypeName` — what needs to change and why
|
||||
- `functionName()` return type — what it currently returns vs what it should return
|
||||
- Config shape — any new configuration options needed
|
||||
|
||||
**Acceptance criteria:**
|
||||
- [ ] Specific, testable criterion 1
|
||||
- [ ] Specific, testable criterion 2
|
||||
- [ ] Specific, testable criterion 3
|
||||
|
||||
**Out of scope:**
|
||||
- Thing that should NOT be changed or addressed in this issue
|
||||
- Adjacent feature that might seem related but is separate
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Good agent brief (bug)
|
||||
|
||||
```markdown
|
||||
## Agent Brief
|
||||
|
||||
**Category:** bug
|
||||
**Summary:** Skill description truncation drops mid-word, producing broken output
|
||||
|
||||
**Current behavior:**
|
||||
When a skill description exceeds 1024 characters, it is truncated at exactly
|
||||
1024 characters regardless of word boundaries. This produces descriptions
|
||||
that end mid-word (e.g. "Use when the user wants to confi").
|
||||
|
||||
**Desired behavior:**
|
||||
Truncation should break at the last word boundary before 1024 characters
|
||||
and append "..." to indicate truncation.
|
||||
|
||||
**Key interfaces:**
|
||||
- The `SkillMetadata` type's `description` field — no type change needed,
|
||||
but the validation/processing logic that populates it needs to respect
|
||||
word boundaries
|
||||
- Any function that reads SKILL.md frontmatter and extracts the description
|
||||
|
||||
**Acceptance criteria:**
|
||||
- [ ] Descriptions under 1024 chars are unchanged
|
||||
- [ ] Descriptions over 1024 chars are truncated at the last word boundary
|
||||
before 1024 chars
|
||||
- [ ] Truncated descriptions end with "..."
|
||||
- [ ] The total length including "..." does not exceed 1024 chars
|
||||
|
||||
**Out of scope:**
|
||||
- Changing the 1024 char limit itself
|
||||
- Multi-line description support
|
||||
```
|
||||
|
||||
### Good agent brief (enhancement)
|
||||
|
||||
```markdown
|
||||
## Agent Brief
|
||||
|
||||
**Category:** enhancement
|
||||
**Summary:** Add `.out-of-scope/` directory support for tracking rejected feature requests
|
||||
|
||||
**Current behavior:**
|
||||
When a feature request is rejected, the issue is closed with a `wontfix` label
|
||||
and a comment. There is no persistent record of the decision or reasoning.
|
||||
Future similar requests require the maintainer to recall or search for the
|
||||
prior discussion.
|
||||
|
||||
**Desired behavior:**
|
||||
Rejected feature requests should be documented in `.out-of-scope/<concept>.md`
|
||||
files that capture the decision, reasoning, and links to all issues that
|
||||
requested the feature. When triaging new issues, these files should be
|
||||
checked for matches.
|
||||
|
||||
**Key interfaces:**
|
||||
- Markdown file format in `.out-of-scope/` — each file should have a
|
||||
`# Concept Name` heading, a `**Decision:**` line, a `**Reason:**` line,
|
||||
and a `**Prior requests:**` list with issue links
|
||||
- The triage workflow should read all `.out-of-scope/*.md` files early
|
||||
and match incoming issues against them by concept similarity
|
||||
|
||||
**Acceptance criteria:**
|
||||
- [ ] Closing a feature as wontfix creates/updates a file in `.out-of-scope/`
|
||||
- [ ] The file includes the decision, reasoning, and link to the closed issue
|
||||
- [ ] If a matching `.out-of-scope/` file already exists, the new issue is
|
||||
appended to its "Prior requests" list rather than creating a duplicate
|
||||
- [ ] During triage, existing `.out-of-scope/` files are checked and surfaced
|
||||
when a new issue matches a prior rejection
|
||||
|
||||
**Out of scope:**
|
||||
- Automated matching (human confirms the match)
|
||||
- Reopening previously rejected features
|
||||
- Bug reports (only enhancement rejections go to `.out-of-scope/`)
|
||||
```
|
||||
|
||||
### Bad agent brief
|
||||
|
||||
```markdown
|
||||
## Agent Brief
|
||||
|
||||
**Summary:** Fix the triage bug
|
||||
|
||||
**What to do:**
|
||||
The triage thing is broken. Look at the main file and fix it.
|
||||
The function around line 150 has the issue.
|
||||
|
||||
**Files to change:**
|
||||
- src/triage/handler.ts (line 150)
|
||||
- src/types.ts (line 42)
|
||||
```
|
||||
|
||||
This is bad because:
|
||||
- No category
|
||||
- Vague description ("the triage thing is broken")
|
||||
- References file paths and line numbers that will go stale
|
||||
- No acceptance criteria
|
||||
- No scope boundaries
|
||||
- No description of current vs desired behavior
|
||||
101
.agents/skills/triage/OUT-OF-SCOPE.md
Normal file
101
.agents/skills/triage/OUT-OF-SCOPE.md
Normal file
@@ -0,0 +1,101 @@
|
||||
# Out-of-Scope Knowledge Base
|
||||
|
||||
The `.out-of-scope/` directory in a repo stores persistent records of rejected feature requests. It serves two purposes:
|
||||
|
||||
1. **Institutional memory** — why a feature was rejected, so the reasoning isn't lost when the issue is closed
|
||||
2. **Deduplication** — when a new issue comes in that matches a prior rejection, the skill can surface the previous decision instead of re-litigating it
|
||||
|
||||
## Directory structure
|
||||
|
||||
```
|
||||
.out-of-scope/
|
||||
├── dark-mode.md
|
||||
├── plugin-system.md
|
||||
└── graphql-api.md
|
||||
```
|
||||
|
||||
One file per **concept**, not per issue. Multiple issues requesting the same thing are grouped under one file.
|
||||
|
||||
## File format
|
||||
|
||||
The file should be written in a relaxed, readable style — more like a short design document than a database entry. Use paragraphs, code samples, and examples to make the reasoning clear and useful to someone encountering it for the first time.
|
||||
|
||||
```markdown
|
||||
# Dark Mode
|
||||
|
||||
This project does not support dark mode or user-facing theming.
|
||||
|
||||
## Why this is out of scope
|
||||
|
||||
The rendering pipeline assumes a single color palette defined in
|
||||
`ThemeConfig`. Supporting multiple themes would require:
|
||||
|
||||
- A theme context provider wrapping the entire component tree
|
||||
- Per-component theme-aware style resolution
|
||||
- A persistence layer for user theme preferences
|
||||
|
||||
This is a significant architectural change that doesn't align with the
|
||||
project's focus on content authoring. Theming is a concern for downstream
|
||||
consumers who embed or redistribute the output.
|
||||
|
||||
```ts
|
||||
// The current ThemeConfig interface is not designed for runtime switching:
|
||||
interface ThemeConfig {
|
||||
colors: ColorPalette; // single palette, resolved at build time
|
||||
fonts: FontStack;
|
||||
}
|
||||
```
|
||||
|
||||
## Prior requests
|
||||
|
||||
- #42 — "Add dark mode support"
|
||||
- #87 — "Night theme for accessibility"
|
||||
- #134 — "Dark theme option"
|
||||
```
|
||||
|
||||
### Naming the file
|
||||
|
||||
Use a short, descriptive kebab-case name for the concept: `dark-mode.md`, `plugin-system.md`, `graphql-api.md`. The name should be recognizable enough that someone browsing the directory understands what was rejected without opening the file.
|
||||
|
||||
### Writing the reason
|
||||
|
||||
The reason should be substantive — not "we don't want this" but why. Good reasons reference:
|
||||
|
||||
- Project scope or philosophy ("This project focuses on X; theming is a downstream concern")
|
||||
- Technical constraints ("Supporting this would require Y, which conflicts with our Z architecture")
|
||||
- Strategic decisions ("We chose to use A instead of B because...")
|
||||
|
||||
The reason should be durable. Avoid referencing temporary circumstances ("we're too busy right now") — those aren't real rejections, they're deferrals.
|
||||
|
||||
## When to check `.out-of-scope/`
|
||||
|
||||
During triage (Step 1: Gather context), read all files in `.out-of-scope/`. When evaluating a new issue:
|
||||
|
||||
- Check if the request matches an existing out-of-scope concept
|
||||
- Matching is by concept similarity, not keyword — "night theme" matches `dark-mode.md`
|
||||
- If there's a match, surface it to the maintainer: "This is similar to `.out-of-scope/dark-mode.md` — we rejected this before because [reason]. Do you still feel the same way?"
|
||||
|
||||
The maintainer may:
|
||||
|
||||
- **Confirm** — the new issue gets added to the existing file's "Prior requests" list, then closed
|
||||
- **Reconsider** — the out-of-scope file gets deleted or updated, and the issue proceeds through normal triage
|
||||
- **Disagree** — the issues are related but distinct, proceed with normal triage
|
||||
|
||||
## When to write to `.out-of-scope/`
|
||||
|
||||
Only when an **enhancement** (not a bug) is rejected as `wontfix`. The flow:
|
||||
|
||||
1. Maintainer decides a feature request is out of scope
|
||||
2. Check if a matching `.out-of-scope/` file already exists
|
||||
3. If yes: append the new issue to the "Prior requests" list
|
||||
4. If no: create a new file with the concept name, decision, reason, and first prior request
|
||||
5. Post a comment on the issue explaining the decision and mentioning the `.out-of-scope/` file
|
||||
6. Close the issue with the `wontfix` label
|
||||
|
||||
## Updating or removing out-of-scope files
|
||||
|
||||
If the maintainer changes their mind about a previously rejected concept:
|
||||
|
||||
- Delete the `.out-of-scope/` file
|
||||
- The skill does not need to reopen old issues — they're historical records
|
||||
- The new issue that triggered the reconsideration proceeds through normal triage
|
||||
103
.agents/skills/triage/SKILL.md
Normal file
103
.agents/skills/triage/SKILL.md
Normal file
@@ -0,0 +1,103 @@
|
||||
---
|
||||
name: triage
|
||||
description: Triage issues through a state machine driven by triage roles. Use when user wants to create an issue, triage issues, review incoming bugs or feature requests, prepare issues for an AFK agent, or manage issue workflow.
|
||||
---
|
||||
|
||||
# Triage
|
||||
|
||||
Move issues on the project issue tracker through a small state machine of triage roles.
|
||||
|
||||
Every comment or issue posted to the issue tracker during triage **must** start with this disclaimer:
|
||||
|
||||
```
|
||||
> *This was generated by AI during triage.*
|
||||
```
|
||||
|
||||
## Reference docs
|
||||
|
||||
- [AGENT-BRIEF.md](AGENT-BRIEF.md) — how to write durable agent briefs
|
||||
- [OUT-OF-SCOPE.md](OUT-OF-SCOPE.md) — how the `.out-of-scope/` knowledge base works
|
||||
|
||||
## Roles
|
||||
|
||||
Two **category** roles:
|
||||
|
||||
- `bug` — something is broken
|
||||
- `enhancement` — new feature or improvement
|
||||
|
||||
Five **state** roles:
|
||||
|
||||
- `needs-triage` — maintainer needs to evaluate
|
||||
- `needs-info` — waiting on reporter for more information
|
||||
- `ready-for-agent` — fully specified, ready for an AFK agent
|
||||
- `ready-for-human` — needs human implementation
|
||||
- `wontfix` — will not be actioned
|
||||
|
||||
Every triaged issue should carry exactly one category role and one state role. If state roles conflict, flag it and ask the maintainer before doing anything else.
|
||||
|
||||
These are canonical role names — the actual label strings used in the issue tracker may differ. The mapping should have been provided to you - run `/setup-matt-pocock-skills` if not.
|
||||
|
||||
State transitions: an unlabeled issue normally goes to `needs-triage` first; from there it moves to `needs-info`, `ready-for-agent`, `ready-for-human`, or `wontfix`. `needs-info` returns to `needs-triage` once the reporter replies. The maintainer can override at any time — flag transitions that look unusual and ask before proceeding.
|
||||
|
||||
## Invocation
|
||||
|
||||
The maintainer invokes `/triage` and describes what they want in natural language. Interpret the request and act. Examples:
|
||||
|
||||
- "Show me anything that needs my attention"
|
||||
- "Let's look at #42"
|
||||
- "Move #42 to ready-for-agent"
|
||||
- "What's ready for agents to pick up?"
|
||||
|
||||
## Show what needs attention
|
||||
|
||||
Query the issue tracker and present three buckets, oldest first:
|
||||
|
||||
1. **Unlabeled** — never triaged.
|
||||
2. **`needs-triage`** — evaluation in progress.
|
||||
3. **`needs-info` with reporter activity since the last triage notes** — needs re-evaluation.
|
||||
|
||||
Show counts and a one-line summary per issue. Let the maintainer pick.
|
||||
|
||||
## Triage a specific issue
|
||||
|
||||
1. **Gather context.** Read the full issue (body, comments, labels, reporter, dates). Parse any prior triage notes so you don't re-ask resolved questions. Explore the codebase using the project's domain glossary, respecting ADRs in the area. Read `.out-of-scope/*.md` and surface any prior rejection that resembles this issue.
|
||||
|
||||
2. **Recommend.** Tell the maintainer your category and state recommendation with reasoning, plus a brief codebase summary relevant to the issue. Wait for direction.
|
||||
|
||||
3. **Reproduce (bugs only).** Before any grilling, attempt reproduction: read the reporter's steps, trace the relevant code, run tests or commands. Report what happened — successful repro with code path, failed repro, or insufficient detail (a strong `needs-info` signal). A confirmed repro makes a much stronger agent brief.
|
||||
|
||||
4. **Grill (if needed).** If the issue needs fleshing out, run a `/grill-with-docs` session.
|
||||
|
||||
5. **Apply the outcome:**
|
||||
- `ready-for-agent` — post an agent brief comment ([AGENT-BRIEF.md](AGENT-BRIEF.md)).
|
||||
- `ready-for-human` — same structure as an agent brief, but note why it can't be delegated (judgment calls, external access, design decisions, manual testing).
|
||||
- `needs-info` — post triage notes (template below).
|
||||
- `wontfix` (bug) — polite explanation, then close.
|
||||
- `wontfix` (enhancement) — write to `.out-of-scope/`, link to it from a comment, then close ([OUT-OF-SCOPE.md](OUT-OF-SCOPE.md)).
|
||||
- `needs-triage` — apply the role. Optional comment if there's partial progress.
|
||||
|
||||
## Quick state override
|
||||
|
||||
If the maintainer says "move #42 to ready-for-agent", trust them and apply the role directly. Confirm what you're about to do (role changes, comment, close), then act. Skip grilling. If moving to `ready-for-agent` without a grilling session, ask whether they want to write an agent brief.
|
||||
|
||||
## Needs-info template
|
||||
|
||||
```markdown
|
||||
## Triage Notes
|
||||
|
||||
**What we've established so far:**
|
||||
|
||||
- point 1
|
||||
- point 2
|
||||
|
||||
**What we still need from you (@reporter):**
|
||||
|
||||
- question 1
|
||||
- question 2
|
||||
```
|
||||
|
||||
Capture everything resolved during grilling under "established so far" so the work isn't lost. Questions must be specific and actionable, not "please provide more info".
|
||||
|
||||
## Resuming a previous session
|
||||
|
||||
If prior triage notes exist on the issue, read them, check whether the reporter has answered any outstanding questions, and present an updated picture before continuing. Don't re-ask resolved questions.
|
||||
117
.agents/skills/write-a-skill/SKILL.md
Normal file
117
.agents/skills/write-a-skill/SKILL.md
Normal file
@@ -0,0 +1,117 @@
|
||||
---
|
||||
name: write-a-skill
|
||||
description: Create new agent skills with proper structure, progressive disclosure, and bundled resources. Use when user wants to create, write, or build a new skill.
|
||||
---
|
||||
|
||||
# Writing Skills
|
||||
|
||||
## Process
|
||||
|
||||
1. **Gather requirements** - ask user about:
|
||||
- What task/domain does the skill cover?
|
||||
- What specific use cases should it handle?
|
||||
- Does it need executable scripts or just instructions?
|
||||
- Any reference materials to include?
|
||||
|
||||
2. **Draft the skill** - create:
|
||||
- SKILL.md with concise instructions
|
||||
- Additional reference files if content exceeds 500 lines
|
||||
- Utility scripts if deterministic operations needed
|
||||
|
||||
3. **Review with user** - present draft and ask:
|
||||
- Does this cover your use cases?
|
||||
- Anything missing or unclear?
|
||||
- Should any section be more/less detailed?
|
||||
|
||||
## Skill Structure
|
||||
|
||||
```
|
||||
skill-name/
|
||||
├── SKILL.md # Main instructions (required)
|
||||
├── REFERENCE.md # Detailed docs (if needed)
|
||||
├── EXAMPLES.md # Usage examples (if needed)
|
||||
└── scripts/ # Utility scripts (if needed)
|
||||
└── helper.js
|
||||
```
|
||||
|
||||
## SKILL.md Template
|
||||
|
||||
```md
|
||||
---
|
||||
name: skill-name
|
||||
description: Brief description of capability. Use when [specific triggers].
|
||||
---
|
||||
|
||||
# Skill Name
|
||||
|
||||
## Quick start
|
||||
|
||||
[Minimal working example]
|
||||
|
||||
## Workflows
|
||||
|
||||
[Step-by-step processes with checklists for complex tasks]
|
||||
|
||||
## Advanced features
|
||||
|
||||
[Link to separate files: See [REFERENCE.md](REFERENCE.md)]
|
||||
```
|
||||
|
||||
## Description Requirements
|
||||
|
||||
The description is **the only thing your agent sees** when deciding which skill to load. It's surfaced in the system prompt alongside all other installed skills. Your agent reads these descriptions and picks the relevant skill based on the user's request.
|
||||
|
||||
**Goal**: Give your agent just enough info to know:
|
||||
|
||||
1. What capability this skill provides
|
||||
2. When/why to trigger it (specific keywords, contexts, file types)
|
||||
|
||||
**Format**:
|
||||
|
||||
- Max 1024 chars
|
||||
- Write in third person
|
||||
- First sentence: what it does
|
||||
- Second sentence: "Use when [specific triggers]"
|
||||
|
||||
**Good example**:
|
||||
|
||||
```
|
||||
Extract text and tables from PDF files, fill forms, merge documents. Use when working with PDF files or when user mentions PDFs, forms, or document extraction.
|
||||
```
|
||||
|
||||
**Bad example**:
|
||||
|
||||
```
|
||||
Helps with documents.
|
||||
```
|
||||
|
||||
The bad example gives your agent no way to distinguish this from other document skills.
|
||||
|
||||
## When to Add Scripts
|
||||
|
||||
Add utility scripts when:
|
||||
|
||||
- Operation is deterministic (validation, formatting)
|
||||
- Same code would be generated repeatedly
|
||||
- Errors need explicit handling
|
||||
|
||||
Scripts save tokens and improve reliability vs generated code.
|
||||
|
||||
## When to Split Files
|
||||
|
||||
Split into separate files when:
|
||||
|
||||
- SKILL.md exceeds 100 lines
|
||||
- Content has distinct domains (finance vs sales schemas)
|
||||
- Advanced features are rarely needed
|
||||
|
||||
## Review Checklist
|
||||
|
||||
After drafting, verify:
|
||||
|
||||
- [ ] Description includes triggers ("Use when...")
|
||||
- [ ] SKILL.md under 100 lines
|
||||
- [ ] No time-sensitive info
|
||||
- [ ] Consistent terminology
|
||||
- [ ] Concrete examples included
|
||||
- [ ] References one level deep
|
||||
7
.agents/skills/zoom-out/SKILL.md
Normal file
7
.agents/skills/zoom-out/SKILL.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
name: zoom-out
|
||||
description: Tell the agent to zoom out and give broader context or a higher-level perspective. Use when you're unfamiliar with a section of code or need to understand how it fits into the bigger picture.
|
||||
disable-model-invocation: true
|
||||
---
|
||||
|
||||
I don't know this area of code well. Go up a layer of abstraction. Give me a map of all the relevant modules and callers, using the project's domain glossary vocabulary.
|
||||
1
.claude/skills/caveman
Symbolic link
1
.claude/skills/caveman
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/caveman
|
||||
1
.claude/skills/diagnose
Symbolic link
1
.claude/skills/diagnose
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/diagnose
|
||||
1
.claude/skills/grill-me
Symbolic link
1
.claude/skills/grill-me
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/grill-me
|
||||
1
.claude/skills/grill-with-docs
Symbolic link
1
.claude/skills/grill-with-docs
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/grill-with-docs
|
||||
1
.claude/skills/handoff
Symbolic link
1
.claude/skills/handoff
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/handoff
|
||||
1
.claude/skills/improve-codebase-architecture
Symbolic link
1
.claude/skills/improve-codebase-architecture
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/improve-codebase-architecture
|
||||
1
.claude/skills/prototype
Symbolic link
1
.claude/skills/prototype
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/prototype
|
||||
1
.claude/skills/setup-matt-pocock-skills
Symbolic link
1
.claude/skills/setup-matt-pocock-skills
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/setup-matt-pocock-skills
|
||||
1
.claude/skills/tdd
Symbolic link
1
.claude/skills/tdd
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/tdd
|
||||
1
.claude/skills/teach
Symbolic link
1
.claude/skills/teach
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/teach
|
||||
1
.claude/skills/to-issues
Symbolic link
1
.claude/skills/to-issues
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/to-issues
|
||||
1
.claude/skills/to-prd
Symbolic link
1
.claude/skills/to-prd
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/to-prd
|
||||
1
.claude/skills/triage
Symbolic link
1
.claude/skills/triage
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/triage
|
||||
1
.claude/skills/write-a-skill
Symbolic link
1
.claude/skills/write-a-skill
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/write-a-skill
|
||||
1
.claude/skills/zoom-out
Symbolic link
1
.claude/skills/zoom-out
Symbolic link
@@ -0,0 +1 @@
|
||||
../../.agents/skills/zoom-out
|
||||
198
.codex/skills/export-datasource/SKILL.md
Normal file
198
.codex/skills/export-datasource/SKILL.md
Normal file
@@ -0,0 +1,198 @@
|
||||
---
|
||||
name: export-datasource
|
||||
description: Project-specific guide for implementing, modifying, or reviewing export data sources in junhong_cmp_fiber. Use when Codex needs to add a new export scene, extend filters or dynamic columns, register an export scene, change export task query behavior, or explain/debug the DataSource-based export system.
|
||||
---
|
||||
|
||||
# Export Datasource
|
||||
|
||||
Use this skill to work on this project's DataSource-based export system. It covers developer-facing export scene implementation, not one-off manual export operations.
|
||||
|
||||
## First Reads
|
||||
|
||||
Before editing export code, read the relevant current files:
|
||||
|
||||
- `internal/exporter/datasource.go`
|
||||
- `internal/exporter/query_params.go`
|
||||
- `internal/exporter/filter_helpers.go`
|
||||
- `internal/exporter/registry.go`
|
||||
- Existing scene closest to the new scene:
|
||||
- `internal/exporter/device_scene.go`
|
||||
- `internal/exporter/iot_card_scene.go`
|
||||
- For request/scene validation:
|
||||
- `internal/model/dto/export_task_dto.go`
|
||||
- `pkg/constants/constants.go`
|
||||
- For behavior across worker stages:
|
||||
- `internal/task/export_dispatch.go`
|
||||
- `internal/task/export_shard.go`
|
||||
- `internal/task/export_finalize.go`
|
||||
|
||||
Read `references/export-scene-template.md` when adding a new scene or when a concrete code skeleton is useful.
|
||||
|
||||
## Mental Model
|
||||
|
||||
The framework owns async execution, sharding, file generation, OSS upload, and download URLs. A scene implementation owns only data semantics:
|
||||
|
||||
```go
|
||||
type DataSource interface {
|
||||
Scene() string
|
||||
Count(ctx context.Context, params ExportParams) (int, error)
|
||||
Headers(ctx context.Context, params ExportParams) ([]string, error)
|
||||
Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error)
|
||||
}
|
||||
```
|
||||
|
||||
Execution flow:
|
||||
|
||||
1. Admin API creates `tb_export_task` and enqueues `export:dispatch`.
|
||||
2. Dispatch parses `query_json.filters` plus permission snapshot into `ExportParams`.
|
||||
3. Dispatch calls `Headers` once and stores `query_json.resolved_headers`.
|
||||
4. Dispatch calls `Count` and creates `tb_export_shard_task` rows with `shard_offset` and `shard_limit`.
|
||||
5. Shard calls `Fetch`, writes headerless CSV shard files, and uploads them.
|
||||
6. Finalize downloads shard CSV files in shard order, writes one header row, uploads final CSV or converts CSV to XLSX.
|
||||
|
||||
Do not reintroduce keyset cursor logic. New scenes must use offset/limit through `Fetch`.
|
||||
|
||||
## Implementation Workflow
|
||||
|
||||
1. Add a scene constant in `pkg/constants/constants.go`.
|
||||
2. Update `internal/model/dto/export_task_dto.go` validation and descriptions for `scene`.
|
||||
3. Implement `internal/exporter/<scene>_scene.go` with `DataSource`.
|
||||
4. Register the source in `NewDefaultRegistry`.
|
||||
5. Update `IsSupportedScene` if it is used by the current code path.
|
||||
6. Add or update migrations only if the exported domain needs schema/index changes. Do not change export task tables unless the framework contract changes.
|
||||
7. Build and manually verify. This repository forbids automated tests unless the user explicitly requests them.
|
||||
|
||||
## DataSource Rules
|
||||
|
||||
- `Scene` must return the constant, not a string literal.
|
||||
- `Count` and `Fetch` must apply the same filters and permission scope.
|
||||
- `Headers` defines the exact column contract for the whole task. Dispatch stores it once; shards and finalize reuse it.
|
||||
- `Fetch` must return rows aligned to `Headers`; the framework pads/truncates as a fallback, but the source should be correct.
|
||||
- `Fetch` must use stable ordering, normally `ORDER BY id ASC`.
|
||||
- Return string values only. Format time as `2006-01-02 15:04:05` unless the surrounding scene establishes another convention.
|
||||
- Use GORM only. Do not use `database/sql`.
|
||||
- Keep SQL parameters bound through GORM placeholders. Do not concatenate user-controlled values into SQL.
|
||||
- Keep comments, logs, errors, and documentation in Chinese per project rules.
|
||||
|
||||
## Filters And Permissions
|
||||
|
||||
Incoming task query shape:
|
||||
|
||||
```json
|
||||
{
|
||||
"filters": {
|
||||
"status": 1,
|
||||
"shop_id": 1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`ParseExportParams` exposes:
|
||||
|
||||
- `Filters`: `query_json.filters`
|
||||
- `ScopeShopIDs`: shop permission snapshot captured at task creation
|
||||
- `UserType`: creator user type snapshot
|
||||
|
||||
Apply permission scope inside each DataSource:
|
||||
|
||||
```go
|
||||
query = applyExportShopScope(query, params, "shop_id")
|
||||
```
|
||||
|
||||
For aliased queries:
|
||||
|
||||
```go
|
||||
query = applyExportShopScope(query, params, "o.shop_id")
|
||||
```
|
||||
|
||||
Use helpers from `filter_helpers.go`:
|
||||
|
||||
- `filterInt`
|
||||
- `filterUint`
|
||||
- `filterString`
|
||||
- `filterBool`
|
||||
- `filterTime`
|
||||
- `formatOptionalUint`
|
||||
- `formatOptionalTime`
|
||||
|
||||
Do not read live permissions from context inside a DataSource. Export tasks must use the permission snapshot stored at creation time.
|
||||
|
||||
## Dynamic Columns
|
||||
|
||||
Use dynamic headers only when the task parameters or data require it. If `Headers` changes based on filters, `Fetch` must produce the same shape for every shard under the same `ExportParams`.
|
||||
|
||||
Example pattern:
|
||||
|
||||
```go
|
||||
headers := []string{"ID", "ICCID", "状态"}
|
||||
if filterBool(params.Filters, "with_package") {
|
||||
headers = append(headers, "套餐名称", "套餐状态")
|
||||
}
|
||||
return headers, nil
|
||||
```
|
||||
|
||||
## Join Queries
|
||||
|
||||
JOIN-based exports are allowed. Keep these constraints:
|
||||
|
||||
- Preserve one output row per intended exported entity unless the scene explicitly exports detail rows.
|
||||
- If a JOIN can multiply rows, make `Count` match the exported row semantics exactly.
|
||||
- Use table aliases consistently in filters and scope columns.
|
||||
- Prefer explicit `Select` into a local row struct for multi-table exports.
|
||||
- Keep `Order`, `Limit`, and `Offset` on the final query used by `Fetch`.
|
||||
|
||||
## User-Facing API Notes
|
||||
|
||||
Current API group:
|
||||
|
||||
- `POST /api/admin/export-tasks`
|
||||
- `GET /api/admin/export-tasks`
|
||||
- `GET /api/admin/export-tasks/:id`
|
||||
- `POST /api/admin/export-tasks/:id/cancel`
|
||||
|
||||
Creation request:
|
||||
|
||||
```json
|
||||
{
|
||||
"scene": "iot_card",
|
||||
"format": "csv",
|
||||
"query": {
|
||||
"filters": {
|
||||
"shop_id": 1,
|
||||
"with_package": true
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Formats are `csv` and `xlsx`. Final download URLs are returned from task detail after completion.
|
||||
|
||||
## Verification
|
||||
|
||||
This project forbids automated tests and `_test.go` files unless the user explicitly asks for tests. Use manual verification:
|
||||
|
||||
```bash
|
||||
go build ./internal/exporter/...
|
||||
go build ./internal/task/...
|
||||
go build ./...
|
||||
```
|
||||
|
||||
Then create an export task through the API and inspect:
|
||||
|
||||
- `tb_export_task.query_json` contains original `filters` and generated `resolved_headers`.
|
||||
- `tb_export_task.total_rows` matches the filtered query.
|
||||
- `tb_export_shard_task.shard_offset` and `shard_limit` are filled.
|
||||
- Shards reach success and final task reaches completed status.
|
||||
- Downloaded file has one header row, expected row count, correct filtering, and valid CSV/XLSX format.
|
||||
|
||||
Use PostgreSQL MCP/manual SQL for data validation when needed.
|
||||
|
||||
## Common Mistakes
|
||||
|
||||
- Updating only `Fetch` and forgetting `Count`, causing wrong shard planning.
|
||||
- Returning dynamic rows whose column count does not match `Headers`.
|
||||
- Filtering by requested `shop_id` without also applying `ScopeShopIDs`.
|
||||
- Using context/user middleware inside worker DataSource logic.
|
||||
- Registering the source but forgetting DTO `oneof`, so API rejects the new scene.
|
||||
- Writing XLSX shard files. Shards should be CSV; finalize handles final format.
|
||||
- Adding automated tests despite the repository ban.
|
||||
4
.codex/skills/export-datasource/agents/openai.yaml
Normal file
4
.codex/skills/export-datasource/agents/openai.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
interface:
|
||||
display_name: "导出数据源开发"
|
||||
short_description: "指导新增和维护项目导出数据源场景"
|
||||
default_prompt: "Use $export-datasource to add a new export scene for this project."
|
||||
@@ -0,0 +1,222 @@
|
||||
# Export Scene Template
|
||||
|
||||
Use this reference when adding a new export scene.
|
||||
|
||||
## Minimal Checklist
|
||||
|
||||
- Add `ExportTaskSceneXxx` in `pkg/constants/constants.go`.
|
||||
- Add the scene to `CreateExportTaskRequest.Scene` and `ListExportTaskRequest.Scene` validation/description.
|
||||
- Create `internal/exporter/<scene>_scene.go`.
|
||||
- Register `NewXxxDataSource(db)` in `internal/exporter/registry.go`.
|
||||
- Update `IsSupportedScene`.
|
||||
- Run `gofmt` on changed Go files.
|
||||
- Run `go build ./internal/exporter/...`, `go build ./internal/task/...`, and `go build ./...`.
|
||||
- Manually create a task and validate database rows plus downloaded file.
|
||||
|
||||
## Skeleton
|
||||
|
||||
```go
|
||||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// XxxDataSource Xxx 导出数据源。
|
||||
type XxxDataSource struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewXxxDataSource 创建 Xxx 导出数据源。
|
||||
func NewXxxDataSource(db *gorm.DB) *XxxDataSource {
|
||||
return &XxxDataSource{db: db}
|
||||
}
|
||||
|
||||
// Scene 返回导出场景编码。
|
||||
func (s *XxxDataSource) Scene() string {
|
||||
return constants.ExportTaskSceneXxx
|
||||
}
|
||||
|
||||
// Count 统计 Xxx 导出行数。
|
||||
func (s *XxxDataSource) Count(ctx context.Context, params ExportParams) (int, error) {
|
||||
var total int64
|
||||
query := s.applyFilters(s.db.WithContext(ctx).Model(&model.Xxx{}), params)
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(total), nil
|
||||
}
|
||||
|
||||
// Headers 返回 Xxx 导出表头。
|
||||
func (s *XxxDataSource) Headers(ctx context.Context, params ExportParams) ([]string, error) {
|
||||
return []string{"ID", "名称", "状态", "店铺ID", "创建时间"}, nil
|
||||
}
|
||||
|
||||
// Fetch 按 offset/limit 查询 Xxx 导出数据。
|
||||
func (s *XxxDataSource) Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error) {
|
||||
if limit <= 0 {
|
||||
return [][]string{}, nil
|
||||
}
|
||||
|
||||
var items []model.Xxx
|
||||
query := s.applyFilters(s.db.WithContext(ctx).Model(&model.Xxx{}), params).
|
||||
Order("id ASC").
|
||||
Limit(limit).
|
||||
Offset(offset)
|
||||
if err := query.Find(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows := make([][]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
rows = append(rows, []string{
|
||||
strconv.FormatUint(uint64(item.ID), 10),
|
||||
item.Name,
|
||||
strconv.Itoa(item.Status),
|
||||
formatOptionalUint(item.ShopID),
|
||||
item.CreatedAt.Format(exportTimeLayout),
|
||||
})
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (s *XxxDataSource) applyFilters(query *gorm.DB, params ExportParams) *gorm.DB {
|
||||
query = applyExportShopScope(query, params, "shop_id")
|
||||
|
||||
if status, ok := filterInt(params.Filters, "status"); ok {
|
||||
query = query.Where("status = ?", status)
|
||||
}
|
||||
if shopID, ok := filterUint(params.Filters, "shop_id"); ok {
|
||||
query = query.Where("shop_id = ?", shopID)
|
||||
}
|
||||
if start, ok := filterTime(params.Filters, "created_at_start"); ok {
|
||||
query = query.Where("created_at >= ?", start)
|
||||
}
|
||||
if end, ok := filterTime(params.Filters, "created_at_end"); ok {
|
||||
query = query.Where("created_at <= ?", end)
|
||||
}
|
||||
return query
|
||||
}
|
||||
```
|
||||
|
||||
## JOIN Skeleton
|
||||
|
||||
Use this shape for multi-table exports:
|
||||
|
||||
```go
|
||||
type xxxExportRow struct {
|
||||
ID uint
|
||||
Name string
|
||||
Status int
|
||||
ShopID *uint
|
||||
ExtraName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (s *XxxDataSource) Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error) {
|
||||
if limit <= 0 {
|
||||
return [][]string{}, nil
|
||||
}
|
||||
|
||||
var items []xxxExportRow
|
||||
query := s.applyFilters(s.db.WithContext(ctx).Table("tb_xxx AS x"), params, "x.").
|
||||
Select(`
|
||||
x.id,
|
||||
x.name,
|
||||
x.status,
|
||||
x.shop_id,
|
||||
COALESCE(e.name, '') AS extra_name,
|
||||
x.created_at
|
||||
`).
|
||||
Joins("LEFT JOIN tb_extra AS e ON e.xxx_id = x.id AND e.deleted_at IS NULL").
|
||||
Where("x.deleted_at IS NULL").
|
||||
Order("x.id ASC").
|
||||
Limit(limit).
|
||||
Offset(offset)
|
||||
if err := query.Scan(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows := make([][]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
rows = append(rows, []string{
|
||||
strconv.FormatUint(uint64(item.ID), 10),
|
||||
item.Name,
|
||||
strconv.Itoa(item.Status),
|
||||
formatOptionalUint(item.ShopID),
|
||||
item.ExtraName,
|
||||
item.CreatedAt.Format(exportTimeLayout),
|
||||
})
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (s *XxxDataSource) applyFilters(query *gorm.DB, params ExportParams, prefix string) *gorm.DB {
|
||||
query = applyExportShopScope(query, params, prefix+"shop_id")
|
||||
if status, ok := filterInt(params.Filters, "status"); ok {
|
||||
query = query.Where(prefix+"status = ?", status)
|
||||
}
|
||||
return query
|
||||
}
|
||||
```
|
||||
|
||||
If the JOIN multiplies rows, update `Count` to count the same exported row set. Do not count only the base table unless `Fetch` also returns one row per base record.
|
||||
|
||||
## Registry Patch
|
||||
|
||||
```go
|
||||
func NewDefaultRegistry(db *gorm.DB) *Registry {
|
||||
return NewRegistry(
|
||||
NewDeviceDataSource(db),
|
||||
NewIotCardDataSource(db),
|
||||
NewXxxDataSource(db),
|
||||
)
|
||||
}
|
||||
|
||||
func IsSupportedScene(scene string) bool {
|
||||
switch scene {
|
||||
case constants.ExportTaskSceneDevice,
|
||||
constants.ExportTaskSceneIotCard,
|
||||
constants.ExportTaskSceneXxx:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Manual API Smoke
|
||||
|
||||
```bash
|
||||
curl -X POST 'http://localhost:端口/api/admin/export-tasks' \
|
||||
-H 'Authorization: Bearer <token>' \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{
|
||||
"scene": "xxx",
|
||||
"format": "csv",
|
||||
"query": {
|
||||
"filters": {
|
||||
"status": 1
|
||||
}
|
||||
}
|
||||
}'
|
||||
```
|
||||
|
||||
Check database:
|
||||
|
||||
```sql
|
||||
SELECT id, scene, format, status, total_rows, total_shards, query_json
|
||||
FROM tb_export_task
|
||||
WHERE id = <task_id>;
|
||||
|
||||
SELECT shard_no, status, shard_offset, shard_limit, row_count, file_key
|
||||
FROM tb_export_shard_task
|
||||
WHERE task_id = <task_id>
|
||||
ORDER BY shard_no;
|
||||
```
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -107,3 +107,6 @@ docs/admin-openapi.yaml
|
||||
.omx/state/sessions/omx-1777517489966-oo3g37/notify-hook-state.json
|
||||
.omx/state/tmux-extended-keys/private-tmp-tmux-501-default.json
|
||||
.aider*
|
||||
|
||||
# /teach skill 的个人学习工作区,不进入项目提交历史
|
||||
.claude/teach-workspace/
|
||||
|
||||
126
.scratch/customer-binding-architecture/PRD.md
Normal file
126
.scratch/customer-binding-architecture/PRD.md
Normal file
@@ -0,0 +1,126 @@
|
||||
Status: ready-for-agent
|
||||
|
||||
# PRD:客户绑定体系重构与资产归属验证统一
|
||||
|
||||
## Problem Statement
|
||||
|
||||
C 端用户在使用没有虚拟号的 IoT 卡时,整个业务流程完全失效:登录后无法实名认证、无法购买套餐、无法操作设备。根本原因是系统的客户绑定机制依赖虚拟号(`virtual_no`)作为唯一标识,而虚拟号在 IoT 卡上是可选字段,线上有 12,000+ 张无虚拟号的卡且确认会流向 C 端用户。
|
||||
|
||||
与此同时,换货流程在旧卡有虚拟号、新卡无虚拟号时会被错误拦截,即使旧卡实际上没有任何客户绑定记录,换货依然报错"新资产无法承接客户绑定"。
|
||||
|
||||
从设计层面看,`tb_personal_customer_iccid` 表(按 ICCID 绑定)早已建好,Store 层也实现完整,但从未被任何 Service 或 Handler 接入。同时,"判断某张卡/设备是否属于某个客户"这个核心安全规则,散落在 6 个文件中以 4 种不同方式实现,部分实现缺少对已禁用绑定记录的过滤,存在安全缺口。
|
||||
|
||||
## Solution
|
||||
|
||||
建立统一的 **CustomerBinding 模块**,封装客户与资产之间的绑定创建和归属验证逻辑,屏蔽"有虚拟号走 `tb_personal_customer_device` / 无虚拟号走 `tb_personal_customer_iccid`"的路由细节。所有调用方只需传入资产信息,不感知底层用了哪张表。
|
||||
|
||||
在此基础上,修复换货服务中校验与执行逻辑混淆的 bug,并将资产解析和 IoT 卡状态字段整理为后续阶段任务。
|
||||
|
||||
## User Stories
|
||||
|
||||
### C 端用户(无虚拟号的卡)
|
||||
|
||||
1. 作为一个持有无虚拟号 IoT 卡的 C 端用户,我想在首次登录时成功绑定我的卡,以便后续能正常使用所有 C 端功能。
|
||||
2. 作为一个持有无虚拟号 IoT 卡的 C 端用户,我想在绑定卡后能正常提交实名认证,以便激活卡的完整功能。
|
||||
3. 作为一个持有无虚拟号 IoT 卡的 C 端用户,我想购买流量套餐,以便为我的卡充值续费。
|
||||
4. 作为一个持有无虚拟号 IoT 卡的 C 端用户,我想在换货后仍能访问新卡并正常操作,以便换货不影响我的使用体验。
|
||||
|
||||
### C 端用户(有虚拟号的卡)
|
||||
|
||||
5. 作为一个持有有虚拟号 IoT 卡的 C 端用户,我的所有现有功能不受本次改动影响,以便升级对我透明无感知。
|
||||
6. 作为一个持有有虚拟号 IoT 卡的 C 端用户,当我的绑定关系被禁用后,我不能通过该绑定关系访问资产,以便系统安全边界得到保障。
|
||||
|
||||
### 后台运营人员(换货)
|
||||
|
||||
7. 作为后台运营人员,我想将一张有虚拟号的旧卡换货为一张无虚拟号的新卡,并且换货能正常完成,以便不再因无虚拟号而被系统错误拦截。
|
||||
8. 作为后台运营人员,当旧卡有客户绑定记录、新卡无虚拟号时,我希望系统在换货完成后自动将客户绑定迁移到新卡的 ICCID,以便客户换货后仍能访问新卡。
|
||||
9. 作为后台运营人员,当旧卡没有任何客户绑定记录时,无论旧卡是否有虚拟号,换货都应该正常完成,以便换货流程不被无意义的条件拦截。
|
||||
|
||||
### 系统(数据一致性)
|
||||
|
||||
10. 作为系统,当客户首次绑定一张无虚拟号的卡时,应正确将该卡的 `asset_status` 从在库更新为已销售,以便轮询系统能感知到该卡有真实用户在使用。
|
||||
11. 作为系统,当客户的绑定关系被禁用(`status=0`)后,归属验证应拒绝该客户访问对应资产,以便被撤销的绑定不再赋予访问权限。
|
||||
|
||||
## Implementation Decisions
|
||||
|
||||
### 1. 新增 CustomerBinding 模块
|
||||
|
||||
在 `internal/service/` 下建立 `customer_binding` 包,对外暴露两个核心接口:
|
||||
|
||||
- `Bind(ctx, tx, customerID, assetType, assetID)` — 创建或更新客户与资产的绑定关系;对于卡资产,有虚拟号写 `tb_personal_customer_device`,无虚拟号写 `tb_personal_customer_iccid`
|
||||
- `OwnsAsset(ctx, customerID, assetType, assetID) bool` — 验证客户是否持有该资产的有效绑定(`status=1`);按资产类型路由到对应绑定表查询
|
||||
- `Migrate(ctx, tx, oldAsset, newAsset)` — 换货时迁移绑定关系;处理四种组合(有→有、有→无、无→有、无→无)
|
||||
|
||||
`OwnsAsset` 内部统一强制 `status=1` 过滤,解决当前部分实现缺少此过滤的安全缺口。
|
||||
|
||||
### 2. 绑定路由规则(卡资产)
|
||||
|
||||
| 旧卡 | 新卡 | Migrate 行为 |
|
||||
|------|------|-------------|
|
||||
| 有虚拟号,有 pcd 绑定 | 有虚拟号 | 更新 pcd 记录的 virtual_no |
|
||||
| 有虚拟号,有 pcd 绑定 | 无虚拟号 | 禁用旧 pcd 记录 + 创建 pci ICCID 绑定 |
|
||||
| 有虚拟号,无绑定 | 无虚拟号 | 跳过(无需迁移) |
|
||||
| 无虚拟号 | 任意 | 迁移 pci 记录(若存在)到新卡 ICCID |
|
||||
|
||||
### 3. 接入 CustomerBinding 的调用点
|
||||
|
||||
以下调用点需要替换为 CustomerBinding 模块:
|
||||
|
||||
- `client_auth/service.go: bindAsset()` — 使用 `CustomerBinding.Bind()`
|
||||
- `client_auth/service.go: resolveAssetBindingKey()` — 废弃,逻辑内聚到 CustomerBinding
|
||||
- `handler/app/client_realname.go: ExistsByCustomerAndDevice()` — 替换为 `CustomerBinding.OwnsAsset()`
|
||||
- `handler/app/client_device.go: ExistsByCustomerAndDevice()` — 替换为 `CustomerBinding.OwnsAsset()`
|
||||
- `handler/app/client_wallet.go: isCustomerOwnAsset()` — 替换为 `CustomerBinding.OwnsAsset()`
|
||||
- `handler/app/client_asset.go: isCustomerOwnAsset()` — 替换为 `CustomerBinding.OwnsAsset()`
|
||||
- `service/client_order/service.go: checkAssetOwnership()` — 替换为 `CustomerBinding.OwnsAsset()`
|
||||
- `service/exchange/service.go: switchCustomerBindingWithTx()` — 替换为 `CustomerBinding.Migrate()`
|
||||
|
||||
### 4. 修复 switchCustomerBindingWithTx 的校验混淆
|
||||
|
||||
当前 `switchCustomerBindingWithTx`(执行函数)在执行阶段重复做了 `ensureNewAssetBindingAvailableWithTx`(校验函数)的判断,且条件更严(不查实际记录数,只看 key 是否为空)。
|
||||
|
||||
修复方式:将 `switchCustomerBindingWithTx` 改为调用 `CustomerBinding.Migrate()`,移除函数内的 `newKey==""` 拦截逻辑。`ensureNewAssetBindingAvailableWithTx` 同步更新:当 `newKey==""` 时,不再拦截,因为 `Migrate()` 已能正确处理此情形。
|
||||
|
||||
### 5. asset_status 首销标记修复
|
||||
|
||||
`bindAsset` 中通过 `firstEverBind`(查 `virtual_no=""` 的记录数)来判断是否首次绑定并触发 `markAssetAsSold()`。无虚拟号的卡因为 key 为空,导致此逻辑失效,`asset_status` 永远停在在库。
|
||||
|
||||
修复方式:在 `CustomerBinding.Bind()` 内,对 IoT 卡分别按各自绑定表查询首绑状态,再触发 `markAssetAsSold()`。
|
||||
|
||||
### 6. 不引入新的数据库表或字段
|
||||
|
||||
`tb_personal_customer_iccid` 表和 `PersonalCustomerICCIDStore` 已经存在且完整,本次只是接入,不需要迁移或 schema 变更。
|
||||
|
||||
### 7. 现有 personal_customer_device 数据不迁移
|
||||
|
||||
有虚拟号的卡现有绑定数据保持不变,继续走 `tb_personal_customer_device` 路径。只有无虚拟号的卡新登录时才写 `tb_personal_customer_iccid`。
|
||||
|
||||
## Testing Decisions
|
||||
|
||||
本项目禁止自动化测试,使用 PostgreSQL MCP 和 Postman/curl 手动验证。
|
||||
|
||||
**验证重点场景:**
|
||||
|
||||
1. **无虚拟号卡首次登录**:用无虚拟号卡的 ICCID 登录后,`tb_personal_customer_iccid` 应出现绑定记录,`tb_iot_card.asset_status` 应变为 2(已销售)
|
||||
2. **无虚拟号卡归属验证**:登录后调用实名认证接口,应返回成功而非"无权限"
|
||||
3. **无虚拟号卡购买套餐**:购买套餐接口应正常通过归属验证
|
||||
4. **有虚拟号换无虚拟号(旧卡有绑定)**:换货完成后,`tb_personal_customer_device` 旧记录 status 应为 0,`tb_personal_customer_iccid` 应出现新卡 ICCID 的绑定记录
|
||||
5. **有虚拟号换无虚拟号(旧卡无绑定)**:换货应正常完成,无报错,不写入任何绑定记录
|
||||
6. **有虚拟号换有虚拟号(回归)**:现有流程不受影响,pcd 表记录的 virtual_no 正确更新到新卡
|
||||
7. **禁用绑定后归属验证**:将某条 pcd 或 pci 记录 status 设为 0,对应客户调用归属验证应返回无权限
|
||||
8. **有虚拟号卡回归(全流程)**:确认有虚拟号卡的登录、实名、购买套餐流程无任何变化
|
||||
|
||||
## Out of Scope
|
||||
|
||||
- **IoT 卡状态字段整理**(`status` / `asset_status` / `activation_status` 语义重叠问题):影响面广,单独规划
|
||||
- **资产解析函数统一**(12 个 resolve 变体合并):不影响当前 bug,单独规划
|
||||
- **`tb_personal_customer_iccid` 换货迁移的历史数据回填**:历史上无虚拟号卡的用户无绑定记录,不做回填,用户需重新登录
|
||||
- **`tb_personal_customer_device` 中 `virtual_no=""` 的历史垃圾数据清理**:需先确认实际数量(SQL 查询),作为独立数据修复任务
|
||||
|
||||
## Further Notes
|
||||
|
||||
- 线上有 12,000+ 张无虚拟号的 IoT 卡,这是合法业务数据,不是数据质量问题
|
||||
- `tb_personal_customer_iccid` 的 Store 已有完整的 CRUD 实现(Create、ExistsByCustomerAndICCID、GetByCustomerID、CreateOrUpdateLastUsed 等),无需重写
|
||||
- 换货 bug 的直接触发路径:`completeExchangeWithTx → switchCustomerBindingWithTx`,在执行阶段因 `newKey==""` 报错,即使旧卡实际无任何客户绑定记录
|
||||
- 建议在上线前执行:`SELECT COUNT(*) FROM tb_personal_customer_device WHERE (virtual_no IS NULL OR virtual_no = '') AND deleted_at IS NULL` 确认是否有历史脏数据需要清理
|
||||
- **临时线上补丁(知悉)**:2026-06-18 已将线上 12,000+ 张无虚拟号的卡手动补充了虚拟号作为应急措施,以保障线上可用性。数据回滚由业务方自行处理,不在本次实现范围内。
|
||||
@@ -0,0 +1,50 @@
|
||||
Status: done
|
||||
|
||||
# CustomerBinding 模块骨架 + 有虚拟号路径替换(纯重构)
|
||||
|
||||
## What to build
|
||||
|
||||
建立 `internal/service/customer_binding` 包,对外暴露 `Bind()` 和 `OwnsAsset()` 两个接口,将现有有虚拟号路径的逻辑迁入。同时将代码库中 6 处归属验证调用点统一替换为新接口。
|
||||
|
||||
**本切片是纯重构,不改变任何现有行为。**
|
||||
|
||||
### Bind(ctx, tx, customerID, assetType, assetID)
|
||||
|
||||
封装创建客户与资产绑定记录的逻辑,当前只实现有虚拟号路径:
|
||||
- IoT 卡有虚拟号 → 写 `tb_personal_customer_device`(与现在 `bindAsset` 行为一致)
|
||||
- 设备 → 写 `tb_personal_customer_device`(设备必然有虚拟号)
|
||||
- 首次绑定时触发 `markAssetAsSold()`(firstEverBind 逻辑保持不变)
|
||||
|
||||
### OwnsAsset(ctx, customerID, assetType, assetID) bool
|
||||
|
||||
封装归属验证逻辑,当前只实现有虚拟号路径:
|
||||
- 查 `tb_personal_customer_device WHERE virtual_no = ? AND status = 1`
|
||||
- 内部统一强制 `status = 1` 过滤(修复现有部分实现缺少此过滤的安全缺口)
|
||||
|
||||
### 替换 6 处调用点
|
||||
|
||||
以下调用点替换为 `CustomerBinding` 的方法:
|
||||
|
||||
| 调用点 | 替换目标 |
|
||||
|--------|---------|
|
||||
| `client_auth/service.go: bindAsset()` | `CustomerBinding.Bind()` |
|
||||
| `handler/app/client_realname.go:92` | `CustomerBinding.OwnsAsset()` |
|
||||
| `handler/app/client_device.go:82` | `CustomerBinding.OwnsAsset()` |
|
||||
| `handler/app/client_wallet.go: isCustomerOwnAsset()` | `CustomerBinding.OwnsAsset()` |
|
||||
| `handler/app/client_asset.go: isCustomerOwnAsset()` | `CustomerBinding.OwnsAsset()` |
|
||||
| `service/client_order/service.go: checkAssetOwnership()` | `CustomerBinding.OwnsAsset()` |
|
||||
|
||||
exchange service 的 `customerOwnsAsset()` 在切片 3 中处理。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [ ] `internal/service/customer_binding` 包存在,对外暴露 `Bind` 和 `OwnsAsset`
|
||||
- [ ] 有虚拟号的 IoT 卡登录后,`tb_personal_customer_device` 正常写入绑定记录(与现在一致)
|
||||
- [ ] 设备登录后,`tb_personal_customer_device` 正常写入绑定记录(与现在一致)
|
||||
- [ ] 被禁用(status=0)的绑定记录不能通过 `OwnsAsset` 验证(修复安全缺口)
|
||||
- [ ] 6 处调用点全部替换完毕,原有私有方法(resolveAssetBindingKey、isCustomerOwnAsset 等)可删除
|
||||
- [ ] 有虚拟号卡的实名认证、购买套餐、设备操作全链路与现在行为一致
|
||||
|
||||
## Blocked by
|
||||
|
||||
None - 可立即开始
|
||||
@@ -0,0 +1,52 @@
|
||||
Status: done
|
||||
|
||||
# 无虚拟号单卡 C 端完整链路
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/customer-binding-architecture/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
扩展 `CustomerBinding` 模块,使无虚拟号的独立 IoT 卡(单卡)能够完整走通 C 端业务流程:登录绑定、实名认证、购买套餐。
|
||||
|
||||
**仅影响 IoT 卡(单卡)。设备资产必然有虚拟号,不在本切片处理范围内。**
|
||||
|
||||
### 扩展 Bind()
|
||||
|
||||
当资产为 IoT 卡且 `virtual_no` 为空时:
|
||||
- 写 `tb_personal_customer_iccid`(按 ICCID 绑定),而非 `tb_personal_customer_device`
|
||||
- `PersonalCustomerICCIDStore` 已有完整实现,直接注入使用
|
||||
|
||||
当资产为 IoT 卡且 `virtual_no` 不为空时:行为与切片 1 一致,不变。
|
||||
|
||||
### 修复 firstEverBind + markAssetAsSold
|
||||
|
||||
当前 `firstEverBind` 通过查 `tb_personal_customer_device WHERE virtual_no = ""` 来判断是否首次绑定,对无虚拟号的卡完全失效(所有无虚拟号的卡共用同一个空 key)。
|
||||
|
||||
修复方式:在 `Bind()` 内,按路径分别判断首绑:
|
||||
- 有虚拟号路径:查 `tb_personal_customer_device WHERE virtual_no = ?`
|
||||
- 无虚拟号路径:查 `tb_personal_customer_iccid WHERE iccid = ?`
|
||||
|
||||
首次绑定后正常触发 `markAssetAsSold()`,将卡的 `asset_status` 从在库(1)更新为已销售(2)。
|
||||
|
||||
### 扩展 OwnsAsset()
|
||||
|
||||
当资产为 IoT 卡时:
|
||||
- 若卡有 `virtual_no`:查 `tb_personal_customer_device`(与切片 1 一致)
|
||||
- 若卡无 `virtual_no`:查 `tb_personal_customer_iccid WHERE iccid = ? AND status = 1`
|
||||
|
||||
当资产为设备时:行为不变,只查 `tb_personal_customer_device`。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [ ] 无虚拟号的单卡 C 端登录后,`tb_personal_customer_iccid` 中出现对应的绑定记录
|
||||
- [ ] 无虚拟号单卡首次登录后,`tb_iot_card.asset_status` 变为 2(已销售)
|
||||
- [ ] 无虚拟号单卡登录后,实名认证接口返回成功(不报"无权限")
|
||||
- [ ] 无虚拟号单卡登录后,购买套餐接口归属验证通过
|
||||
- [ ] 有虚拟号卡的全部行为与切片 1 完成后保持一致(无回归)
|
||||
- [ ] 设备资产的全部行为不受影响
|
||||
|
||||
## Blocked by
|
||||
|
||||
`.scratch/customer-binding-architecture/issues/01-customer-binding-module-refactor.md`
|
||||
@@ -0,0 +1,45 @@
|
||||
Status: done
|
||||
|
||||
# 换货绑定迁移修复
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/customer-binding-architecture/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
在 `CustomerBinding` 模块上实现 `Migrate(ctx, tx, oldAsset, newAsset)` 方法,替换换货服务中现有的 `switchCustomerBindingWithTx` 和 `ensureNewAssetBindingAvailableWithTx` 逻辑,修复有虚拟号旧卡换无虚拟号新卡时被误拦截的 bug。
|
||||
|
||||
### Migrate(ctx, tx, oldAsset, newAsset)
|
||||
|
||||
处理四种虚拟号组合,仅涉及 IoT 卡(设备必然有虚拟号,只有有→有一种情形):
|
||||
|
||||
| 旧卡 | 新卡 | 行为 |
|
||||
|------|------|------|
|
||||
| 有虚拟号,pcd 有绑定 | 有虚拟号 | 更新 pcd 记录的 virtual_no 为新卡虚拟号 |
|
||||
| 有虚拟号,pcd 有绑定 | 无虚拟号 | 禁用旧 pcd 记录(status=0)+ 创建新 pci ICCID 绑定 |
|
||||
| 有虚拟号,pcd 无绑定 | 无虚拟号 | 跳过,无需迁移 |
|
||||
| 无虚拟号,pci 有绑定 | 任意 | 迁移 pci 记录到新卡 ICCID(或新卡虚拟号路径) |
|
||||
| 任意 | 任意(无绑定) | 跳过 |
|
||||
|
||||
### 修复换货服务
|
||||
|
||||
- `switchCustomerBindingWithTx` 替换为调用 `CustomerBinding.Migrate()`,移除函数内 `newKey == ""` 的误拦截逻辑
|
||||
- `ensureNewAssetBindingAvailableWithTx` 更新:当新卡无虚拟号时,不再拦截换货,因为 `Migrate()` 已能正确处理此情形(无绑定时跳过,有绑定时迁移到 pci)
|
||||
|
||||
### 根本 bug 说明
|
||||
|
||||
原 `switchCustomerBindingWithTx` 在执行阶段做了 `if newKey == "" { return error }` 的检查,但没有先确认旧资产是否实际存在绑定记录。旧卡有虚拟号但无任何客户绑定时,也会被误拦截报错"新资产无法承接客户绑定"。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [ ] 旧卡有虚拟号 + 有客户绑定,换货为有虚拟号新卡:pcd 记录的 virtual_no 正确更新为新卡虚拟号
|
||||
- [ ] 旧卡有虚拟号 + 有客户绑定,换货为无虚拟号新卡:旧 pcd 记录 status 变为 0,pci 表出现新卡 ICCID 的绑定记录
|
||||
- [ ] 旧卡有虚拟号 + 无客户绑定,换货为无虚拟号新卡:换货正常完成,不报错,不写入任何绑定记录
|
||||
- [ ] 旧卡无虚拟号,换货为任意新卡:换货正常完成
|
||||
- [ ] 有虚拟号换有虚拟号(原有场景):行为与修复前一致,无回归
|
||||
- [ ] 换货完成后,客户通过新卡(无论有无虚拟号)能正常通过归属验证
|
||||
|
||||
## Blocked by
|
||||
|
||||
`.scratch/customer-binding-architecture/issues/02-no-virtual-no-card-cend-flow.md`
|
||||
143
.scratch/enterprise-auth-and-asset-enhancements/PRD.md
Normal file
143
.scratch/enterprise-auth-and-asset-enhancements/PRD.md
Normal file
@@ -0,0 +1,143 @@
|
||||
Status: ready-for-agent
|
||||
|
||||
# PRD:企业授权增强 & 资产列表扩展
|
||||
|
||||
## Problem Statement
|
||||
|
||||
平台运营人员在管理企业授权和查看资产状态时面临以下痛点:
|
||||
|
||||
1. **企业维度检索缺失**:卡列表和设备列表无法按企业维度过滤,也无法在列表中直接看到某张卡/设备被授权给了哪个企业,需要跨页面跳转查询。
|
||||
2. **企业授权操作繁琐**:授权和收回接口只接受精确 ICCID/虚拟号列表,但前端列表有分页,量大时需一个个选,无法按批次号、号段等条件一次性批量操作。
|
||||
3. **换货历史不可见**:经历过换货的旧卡/设备,在列表和详情中没有任何标记,运营无法判断当前资产是否为"换货遗留"状态或曾经历过换货后被重新激活(转新)的资产。
|
||||
4. **主钱包流水和退款列表检索维度不足**:无法直接通过资产标识(ICCID 或虚拟号)快速定位某个资产相关的流水和退款记录。
|
||||
5. **卡网络状态过滤缺失**:standalone 卡列表无法按网络状态(开机/停机)过滤,批量运维操作不便。
|
||||
|
||||
## Solution
|
||||
|
||||
对六个业务场景进行针对性扩展:
|
||||
|
||||
1. 在 standalone 卡列表和设备列表中新增企业维度过滤及企业信息返回字段
|
||||
2. 将企业授权/收回接口升级为支持 list/range/filter 三(两)模式选资产
|
||||
3. 在 standalone 卡列表和设备列表响应中暴露换货业务状态和资产世代编号
|
||||
4. 主钱包流水列表新增资产标识精确检索
|
||||
5. 退款列表新增资产标识精确检索
|
||||
6. standalone 卡列表新增网络状态过滤
|
||||
|
||||
同时修复卡企业授权表的数据一致性问题(补唯一约束,与设备授权保持一致)。
|
||||
|
||||
## User Stories
|
||||
|
||||
1. 作为平台运营,我希望在 standalone 卡列表中输入企业 ID 进行过滤,以便快速找到已授权给该企业的所有卡
|
||||
2. 作为平台运营,我希望在 standalone 卡列表中使用"是否授权企业"开关过滤,以便快速区分已授权和未授权的卡库存
|
||||
3. 作为平台运营,我希望在 standalone 卡列表中直接看到每张卡被授权给哪个企业(企业ID和名称),以便不用跳转到企业详情页
|
||||
4. 作为平台运营,我希望在设备列表中输入企业 ID 进行过滤,以便快速找到已授权给该企业的所有设备
|
||||
5. 作为平台运营,我希望在设备列表中使用"是否授权企业"开关过滤,以便区分已授权和未授权的设备
|
||||
6. 作为平台运营,我希望在设备列表中直接看到每台设备被授权给哪个企业,以便快速掌握设备归属
|
||||
7. 作为平台运营,我希望在授权卡给企业时能通过 ICCID 号段范围一次性选取,以便高效授权大批量卡
|
||||
8. 作为平台运营,我希望在授权卡给企业时能通过批次号、运营商等筛选条件选取,以便按业务维度批量授权
|
||||
9. 作为平台运营,我希望在收回企业卡授权时同样支持 list/range/filter 三种模式,以便与授权操作保持一致的操作体验
|
||||
10. 作为平台运营,我希望在授权设备给企业时能通过虚拟号模糊搜索和批次号筛选一次性选取,以便批量操作设备
|
||||
11. 作为平台运营,我希望在收回企业设备授权时支持 list/filter 两种模式,以便批量收回
|
||||
12. 作为平台运营,我希望在 standalone 卡列表中看到每张卡的业务状态(`asset_status`),以便识别已换货但尚未转新的"死档"卡
|
||||
13. 作为平台运营,我希望在 standalone 卡列表中看到每张卡的世代编号(`generation`),以便识别曾换货后转新重入库的卡
|
||||
14. 作为平台运营,我希望在设备列表中看到每台设备的业务状态和世代编号,以便同样掌握设备的换货历史
|
||||
15. 作为平台运营,我希望在主钱包流水列表中输入 ICCID 或虚拟号精确检索,以便快速定位某资产的所有充值/扣款记录
|
||||
16. 作为平台运营,我希望在退款列表中输入 ICCID 或虚拟号精确检索,以便快速找到某资产相关的所有退款申请
|
||||
17. 作为平台运营,我希望在 standalone 卡列表中按网络状态(开机/停机)过滤,以便批量处理特定网络状态的卡
|
||||
|
||||
## Implementation Decisions
|
||||
|
||||
### 数据一致性修复(前置)
|
||||
|
||||
- **卡企业授权唯一约束**:在 `tb_enterprise_card_authorization` 表上补充部分唯一索引,约束同一张卡在同一时间只能有一条有效授权记录(`WHERE revoked_at IS NULL AND deleted_at IS NULL`),与设备授权表的 `uq_active_device_auth` 约束保持一致
|
||||
- **service 层校验补充**:卡授权 service 在执行授权前,需增加"卡是否已授权给其他企业"的校验,目前只校验同一企业重复授权
|
||||
|
||||
### 需求1:standalone 卡列表 + 设备列表企业字段
|
||||
|
||||
**Request 新增过滤条件**(`ListStandaloneIotCardRequest` 和 `ListDeviceRequest`):
|
||||
- `authorized_enterprise_id *uint`:按企业ID过滤,只匹配当前有效授权(`revoked_at IS NULL`)
|
||||
- `is_authorized_to_enterprise *bool`:true=已授权给某企业,false=未授权任何企业
|
||||
|
||||
**Response 新增字段**(`StandaloneIotCardResponse` 和 `DeviceResponse`):
|
||||
- `authorized_enterprise_id *uint`:未授权时为 null
|
||||
- `authorized_enterprise_name string`:未授权时为空字符串
|
||||
|
||||
Store 层需通过 JOIN 或子查询 `tb_enterprise_card_authorization` / `tb_enterprise_device_authorization` 表获取企业ID,再批量查企业名称
|
||||
|
||||
### 需求2:企业授权/收回接口升级为多模式
|
||||
|
||||
参照 `AllocateStandaloneCardsRequest` / `RecallStandaloneCardsRequest` 的 `selection_type` 三模式设计:
|
||||
|
||||
**allocate-cards / recall-cards**(三模式:list / range / filter):
|
||||
- `list`:`ICCIDs []string`,精确 ICCID 列表
|
||||
- `range`:`ICCIDStart string` + `ICCIDEnd string`,号段范围
|
||||
- `filter`:筛选条件
|
||||
- allocate-cards filter 字段:`ICCID`(模糊)、`BatchNo`、`CarrierID`、`ShopID`/`ShopIDs`
|
||||
- recall-cards filter 字段:`ICCID`(模糊)、`BatchNo`、`CarrierID`
|
||||
|
||||
**allocate-devices / recall-devices**(两模式:list / filter,不支持 range):
|
||||
- `list`:`DeviceNos []string`,设备虚拟号列表
|
||||
- `filter`:筛选条件
|
||||
- allocate-devices filter 字段:`VirtualNo`(模糊)、`BatchNo`、`ShopID`
|
||||
- recall-devices filter 字段:`VirtualNo`(模糊)、`BatchNo`
|
||||
|
||||
原有接口的请求结构需要破坏性变更(DTO 重构),需与前端联调确认过渡方案
|
||||
|
||||
### 需求3:换货状态字段
|
||||
|
||||
**`StandaloneIotCardResponse` 和 `DeviceResponse` 新增字段**:
|
||||
- `asset_status int`:业务状态(1=在库, 2=已销售, 3=已换货, 4=已停用)
|
||||
- `asset_status_name string`:对应中文名称
|
||||
- `generation int`:资产世代编号(初始值1,每次换货+转新后+1)
|
||||
|
||||
语义约定:
|
||||
- `asset_status = 3`:该资产已换货且尚未执行"旧资产转新",是永久性死档标记
|
||||
- `generation > 1`:该资产历史上曾执行过换货+转新,目前仍在流通
|
||||
|
||||
### 需求4:主钱包流水资产标识检索
|
||||
|
||||
`MainWalletTransactionListRequest` 新增:
|
||||
- `AssetIdentifier string`:精确匹配 `asset_identifier` 快照字段(ICCID 或虚拟号),空字符串时不过滤
|
||||
|
||||
### 需求5:退款列表资产标识检索
|
||||
|
||||
`RefundListRequest` 新增:
|
||||
- `AssetIdentifier string`:精确匹配 `asset_identifier` 快照字段(ICCID 或虚拟号),空字符串时不过滤
|
||||
|
||||
### 需求6:standalone 卡列表网络状态过滤
|
||||
|
||||
`ListStandaloneIotCardRequest` 新增:
|
||||
- `NetworkStatus *int`:网络状态过滤(0=停机, 1=开机),不传则不过滤
|
||||
|
||||
Store 层 `applyStandaloneFilters` 函数中补充 `network_status = ?` 条件
|
||||
|
||||
## Testing Decisions
|
||||
|
||||
本项目禁止自动化测试,验证方式为:
|
||||
|
||||
- **数据库验证**:通过 PostgreSQL MCP 工具验证授权表唯一约束是否生效、字段值是否正确写入
|
||||
- **接口验证**:通过 curl/Postman 对各接口进行手动联调,重点覆盖以下场景:
|
||||
- 企业 ID 过滤:确认只返回有效授权(revoked_at IS NULL)的卡/设备
|
||||
- 是否授权企业过滤:true/false 结果集互补且无交集
|
||||
- 授权时尝试将同一张卡授权给第二个企业,确认被拒绝
|
||||
- allocate-cards 三种 selection_type 均能正确选取目标卡
|
||||
- allocate-devices 两种 selection_type 均能正确选取目标设备
|
||||
- asset_status=3 的卡在列表中可见且字段正确
|
||||
- generation 字段在换货+转新后正确递增
|
||||
- 主钱包流水 asset_identifier 精确匹配不漏不多
|
||||
- 退款 asset_identifier 精确匹配不漏不多
|
||||
- 网络状态过滤 0/1 结果集互补
|
||||
|
||||
## Out of Scope
|
||||
|
||||
- 企业卡/设备授权列表接口本身的改造(本次只改授权/收回操作接口)
|
||||
- `AllocateCardsPreviewReq`(预览接口)是否同步升级为多模式(待后续评估)
|
||||
- standalone 卡列表和设备列表的导出功能是否同步支持新增过滤条件
|
||||
- 换货历史的完整时间线展示(仅暴露字段,不新增详情接口)
|
||||
- `generation` 字段的过滤能力(暂只返回,不支持作为检索条件)
|
||||
|
||||
## Further Notes
|
||||
|
||||
- 需求1 的企业名称需要 N+1 防护:通过授权查询批量拿到 enterprise_id 后,用 IN 一次性查企业名称,不能逐条查
|
||||
- 需求2 的 filter 模式对于 allocate-cards 场景,卡的范围天然受操作者权限约束(代理用户只能授权自己店铺的卡),Store 层已有 `middleware.ApplyShopFilter` 处理,filter 模式需要同样受此约束
|
||||
- 卡唯一约束迁移执行前,需确认现有数据中是否存在一张卡同时有多条 `revoked_at IS NULL` 的记录,若有需先清理
|
||||
@@ -0,0 +1,28 @@
|
||||
Status: ready-for-human
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/enterprise-auth-and-asset-enhancements/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
修复卡企业授权的数据一致性问题,使其与设备授权保持一致:一张卡在同一时间只能授权给一个企业。
|
||||
|
||||
分两步:
|
||||
|
||||
**第一步:数据库迁移**
|
||||
在 `tb_enterprise_card_authorization` 表上新增部分唯一索引,约束同一张卡不能同时存在两条有效授权记录(`revoked_at IS NULL AND deleted_at IS NULL`)。迁移执行前需先查询是否存在脏数据(同一 card_id 有多条 revoked_at IS NULL 的记录),若有需在迁移脚本中先行清理。
|
||||
|
||||
**第二步:service 层校验补充**
|
||||
卡授权 service(`BatchAuthorize`)在执行授权前,增加"目标卡是否已授权给其他企业"的校验。目前代码只跳过已授权给同一企业的卡,不阻止授权给第二个企业。新逻辑:若目标卡已存在有效授权(`revoked_at IS NULL`)且授权对象不是当前企业,则将该卡加入失败列表并给出明确错误原因(如"卡已授权给其他企业,请先收回")。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] `tb_enterprise_card_authorization` 表存在部分唯一索引,约束 `(card_id) WHERE revoked_at IS NULL AND deleted_at IS NULL`
|
||||
- [x] 尝试将同一张卡授权给第二个企业时,接口返回失败,错误信息明确说明"已授权给其他企业"
|
||||
- [x] 已撤回(revoked_at 有值)的历史记录不受唯一约束影响,可正常查询
|
||||
- [x] 同一张卡授权给同一企业时仍返回"已授权给该企业"的原有错误,行为不变
|
||||
|
||||
## Blocked by
|
||||
|
||||
None - can start immediately
|
||||
@@ -0,0 +1,22 @@
|
||||
Status: ready-for-human
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/enterprise-auth-and-asset-enhancements/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
在 `/api/admin/iot-cards/standalone` 接口的查询条件中新增网络状态过滤。
|
||||
|
||||
Request DTO 新增可选字段 `network_status *int`(0=停机,1=开机),不传时不过滤。Store 层 `applyStandaloneFilters` 函数补充对应的 `network_status = ?` WHERE 条件。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] 传入 `network_status=0` 时只返回停机的卡
|
||||
- [x] 传入 `network_status=1` 时只返回开机的卡
|
||||
- [x] 不传 `network_status` 时返回全部(与改动前行为一致)
|
||||
- [x] 网络状态过滤可与现有其他过滤条件(ICCID、运营商等)叠加使用
|
||||
|
||||
## Blocked by
|
||||
|
||||
None - can start immediately
|
||||
@@ -0,0 +1,34 @@
|
||||
Status: ready-for-human
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/enterprise-auth-and-asset-enhancements/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
在 standalone 卡列表响应和设备列表响应中暴露换货相关字段,使运营人员能直接在列表中识别资产的换货历史。
|
||||
|
||||
**`StandaloneIotCardResponse` 新增三个字段:**
|
||||
- `asset_status int`:业务状态(1=在库, 2=已销售, 3=已换货, 4=已停用)
|
||||
- `asset_status_name string`:对应中文名称
|
||||
- `generation int`:资产世代编号(初始值1,每次换货+旧资产转新后 +1)
|
||||
|
||||
**`DeviceResponse` 同步新增相同三个字段。**
|
||||
|
||||
语义约定(写入注释和 description):
|
||||
- `asset_status = 3`:该资产已换货且尚未执行"旧资产转新",不再流通
|
||||
- `generation > 1`:该资产历史上曾执行过换货后转新,目前仍在使用
|
||||
|
||||
Service 层组装响应时直接从 Model 取值,`asset_status_name` 通过 constants 中的方法转换。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] `/api/admin/iot-cards/standalone` 响应中每条记录包含 `asset_status`、`asset_status_name`、`generation` 三个字段
|
||||
- [x] `/api/admin/devices` 响应中每条记录包含相同三个字段
|
||||
- [x] 已换货未转新的卡/设备返回 `asset_status=3`、`asset_status_name="已换货"`
|
||||
- [x] 经过换货转新重入库的卡/设备返回 `generation=2`(或更高)且 `asset_status=1`
|
||||
- [x] 普通未经历换货的资产返回 `generation=1`
|
||||
|
||||
## Blocked by
|
||||
|
||||
None - can start immediately
|
||||
@@ -0,0 +1,23 @@
|
||||
Status: ready-for-human
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/enterprise-auth-and-asset-enhancements/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
在 `/api/admin/shops/{shop_id}/main-wallet/transactions` 接口的查询条件中新增资产标识精确检索。
|
||||
|
||||
`MainWalletTransactionListRequest` 新增可选字段 `asset_identifier string`,非空时对 `asset_identifier` 列做精确匹配(`= ?`,不做模糊匹配)。`asset_identifier` 字段存储的是下单时资产的标识符快照(ICCID 或虚拟号),空字符串时不过滤。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] 传入有效的 ICCID 时只返回该卡相关的主钱包流水
|
||||
- [x] 传入有效的设备虚拟号时只返回该设备相关的主钱包流水
|
||||
- [x] 传入不存在的标识符时返回空列表(total=0),不报错
|
||||
- [x] 不传 `asset_identifier` 时行为与改动前完全一致
|
||||
- [x] 可与 `transaction_type`、`start_date`、`end_date` 等现有条件叠加使用
|
||||
|
||||
## Blocked by
|
||||
|
||||
None - can start immediately
|
||||
@@ -0,0 +1,23 @@
|
||||
Status: ready-for-human
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/enterprise-auth-and-asset-enhancements/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
在 `/api/admin/refunds` 接口的查询条件中新增资产标识精确检索。
|
||||
|
||||
`RefundListRequest` 新增可选字段 `asset_identifier string`,非空时对退款记录的 `asset_identifier` 列做精确匹配(`= ?`)。该字段存储的是下单时资产的标识符快照(ICCID 或虚拟号),空字符串时不过滤。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] 传入有效的 ICCID 时只返回该卡相关的退款申请
|
||||
- [x] 传入有效的设备虚拟号时只返回该设备相关的退款申请
|
||||
- [x] 传入不存在的标识符时返回空列表(total=0),不报错
|
||||
- [x] 不传 `asset_identifier` 时行为与改动前完全一致
|
||||
- [x] 可与 `status`、`order_id`、`shop_id` 等现有条件叠加使用
|
||||
|
||||
## Blocked by
|
||||
|
||||
None - can start immediately
|
||||
@@ -0,0 +1,33 @@
|
||||
Status: ready-for-human
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/enterprise-auth-and-asset-enhancements/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
在 `/api/admin/iot-cards/standalone` 接口中新增企业维度过滤条件,并在响应中返回当前有效授权的企业信息。
|
||||
|
||||
**Request 新增过滤条件(`ListStandaloneIotCardRequest`):**
|
||||
- `authorized_enterprise_id *uint`:按企业ID过滤,只匹配 `tb_enterprise_card_authorization` 中 `revoked_at IS NULL` 的有效授权
|
||||
- `is_authorized_to_enterprise *bool`:true=只返回当前已授权给某企业的卡,false=只返回未授权任何企业的卡
|
||||
|
||||
**Response 新增字段(`StandaloneIotCardResponse`):**
|
||||
- `authorized_enterprise_id *uint`:当前有效授权的企业ID,未授权时为 null
|
||||
- `authorized_enterprise_name string`:对应企业名称,未授权时为空字符串
|
||||
|
||||
Store 层在 `applyStandaloneFilters` 中增加对 `authorized_enterprise_id` 和 `is_authorized_to_enterprise` 的处理,通过子查询 `tb_enterprise_card_authorization`(`revoked_at IS NULL AND deleted_at IS NULL`)实现过滤。响应组装时批量查询企业名称(IN 查询),不逐条查询。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] 传入 `authorized_enterprise_id` 时只返回当前有效授权给该企业的卡
|
||||
- [x] 传入 `is_authorized_to_enterprise=true` 时只返回已授权给某企业的卡
|
||||
- [x] 传入 `is_authorized_to_enterprise=false` 时只返回未授权任何企业的卡
|
||||
- [x] 已撤回的历史授权不影响过滤结果(已撤回视为未授权)
|
||||
- [x] 响应中每张卡包含 `authorized_enterprise_id` 和 `authorized_enterprise_name`,未授权卡对应字段为 null/空字符串
|
||||
- [x] 企业名称通过批量查询获取,不触发 N+1 查询
|
||||
- [x] 新增过滤条件可与现有条件(ICCID、运营商、店铺等)叠加使用
|
||||
|
||||
## Blocked by
|
||||
|
||||
- `issues/01-card-enterprise-auth-unique-constraint.md`
|
||||
@@ -0,0 +1,32 @@
|
||||
Status: ready-for-human
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/enterprise-auth-and-asset-enhancements/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
在 `/api/admin/devices` 接口中新增企业维度过滤条件,并在响应中返回当前有效授权的企业信息。
|
||||
|
||||
**Request 新增过滤条件(`ListDeviceRequest`):**
|
||||
- `authorized_enterprise_id *uint`:按企业ID过滤,只匹配 `tb_enterprise_device_authorization` 中 `revoked_at IS NULL` 的有效授权
|
||||
- `is_authorized_to_enterprise *bool`:true=只返回当前已授权给某企业的设备,false=只返回未授权任何企业的设备
|
||||
|
||||
**Response 新增字段(`DeviceResponse`):**
|
||||
- `authorized_enterprise_id *uint`:当前有效授权的企业ID,未授权时为 null
|
||||
- `authorized_enterprise_name string`:对应企业名称,未授权时为空字符串
|
||||
|
||||
Store 层通过子查询或 JOIN `tb_enterprise_device_authorization` 实现过滤。响应组装时批量查询企业名称(IN 查询),不逐条查询。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] 传入 `authorized_enterprise_id` 时只返回当前有效授权给该企业的设备
|
||||
- [x] 传入 `is_authorized_to_enterprise=true` 时只返回已授权给某企业的设备
|
||||
- [x] 传入 `is_authorized_to_enterprise=false` 时只返回未授权任何企业的设备
|
||||
- [x] 已撤回的历史授权不影响过滤结果(已撤回视为未授权)
|
||||
- [x] 响应中每台设备包含 `authorized_enterprise_id` 和 `authorized_enterprise_name`,未授权设备对应字段为 null/空字符串
|
||||
- [x] 企业名称通过批量查询获取,不触发 N+1 查询
|
||||
|
||||
## Blocked by
|
||||
|
||||
None - can start immediately
|
||||
@@ -0,0 +1,42 @@
|
||||
Status: ready-for-human
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/enterprise-auth-and-asset-enhancements/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
将企业卡授权和收回接口升级为支持 list/range/filter 三种模式批量选取卡,替代原来只接受精确 ICCID 列表的方式。
|
||||
|
||||
**涉及接口:**
|
||||
- `POST /api/admin/enterprises/{id}/allocate-cards`
|
||||
- `POST /api/admin/enterprises/{id}/recall-cards`
|
||||
|
||||
**`AllocateCardsReq` 重构为:**
|
||||
- `selection_type string`(必填,`list`、`range` 或 `filter`)
|
||||
- list 模式:`iccids []string`(ICCID 列表,最多1000个)
|
||||
- range 模式:`iccid_start string` + `iccid_end string`(号段范围)
|
||||
- filter 模式过滤字段:`iccid string`(模糊)、`batch_no string`、`carrier_id *uint`、`shop_id *uint`、`shop_ids []uint`
|
||||
- `remark string`(备注,所有模式均可选)
|
||||
|
||||
**`RecallCardsReq` 重构为:**
|
||||
- `selection_type string`(必填,`list`、`range` 或 `filter`)
|
||||
- list 模式:`iccids []string`
|
||||
- range 模式:`iccid_start string` + `iccid_end string`
|
||||
- filter 模式过滤字段:`iccid string`(模糊)、`batch_no string`、`carrier_id *uint`
|
||||
|
||||
filter 和 range 模式下,allocate 操作的候选卡集受操作者权限约束(代理用户只能授权自己店铺的卡),与 `applyStandaloneFilters` 中的 `ApplyShopFilter` 逻辑保持一致。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] `allocate-cards` 接口接受 `selection_type=list` + `iccids`,行为与改动前一致
|
||||
- [x] `allocate-cards` 接口接受 `selection_type=range` + 号段,批量授权号段内所有匹配的卡
|
||||
- [x] `allocate-cards` 接口接受 `selection_type=filter` + 过滤条件,批量授权所有匹配的卡
|
||||
- [x] `recall-cards` 接口同样支持三种模式,分别正确收回对应卡的企业授权
|
||||
- [x] filter/range 模式下代理用户只能操作自己店铺的卡,超出范围的卡进入失败列表
|
||||
- [x] 任何模式下尝试授权"已授权给其他企业"的卡,该卡进入失败列表并附带原因
|
||||
- [x] 响应中 `success_count`、`fail_count`、`failed_items` 准确反映实际执行结果
|
||||
|
||||
## Blocked by
|
||||
|
||||
- `issues/01-card-enterprise-auth-unique-constraint.md`
|
||||
@@ -0,0 +1,38 @@
|
||||
Status: ready-for-human
|
||||
|
||||
## Parent
|
||||
|
||||
`.scratch/enterprise-auth-and-asset-enhancements/PRD.md`
|
||||
|
||||
## What to build
|
||||
|
||||
将企业设备授权和收回接口升级为支持 list/filter 两种模式批量选取设备,替代原来只接受精确设备号列表的方式。
|
||||
|
||||
**涉及接口:**
|
||||
- `POST /api/admin/enterprises/{id}/allocate-devices`
|
||||
- `POST /api/admin/enterprises/{id}/recall-devices`
|
||||
|
||||
**`AllocateDevicesReq` 重构为:**
|
||||
- `selection_type string`(必填,`list` 或 `filter`)
|
||||
- list 模式:`device_nos []string`(设备虚拟号列表)
|
||||
- filter 模式过滤字段:`virtual_no string`(模糊)、`batch_no string`、`shop_id *uint`
|
||||
|
||||
**`RecallDevicesReq` 重构为:**
|
||||
- `selection_type string`(必填,`list` 或 `filter`)
|
||||
- list 模式:`device_nos []string`
|
||||
- filter 模式过滤字段:`virtual_no string`(模糊)、`batch_no string`
|
||||
|
||||
filter 模式下,allocate 操作的候选设备集受操作者权限约束(代理用户只能授权自己店铺的设备)。filter 模式命中的设备数量无硬性上限,但单次事务处理建议分批,超大批次需记录日志。
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [x] `allocate-devices` 接口接受 `selection_type=list` + `device_nos` 列表,行为与改动前一致
|
||||
- [x] `allocate-devices` 接口接受 `selection_type=filter` + 过滤条件,批量授权所有匹配设备
|
||||
- [x] `recall-devices` 接口接受 `selection_type=list` + `device_nos` 列表,行为与改动前一致
|
||||
- [x] `recall-devices` 接口接受 `selection_type=filter` + 过滤条件,批量收回所有匹配设备
|
||||
- [x] filter 模式下代理用户只能操作自己店铺的设备,超出范围的设备进入失败列表
|
||||
- [x] 响应中 `success_count`、`fail_count`、`failed_items` 准确反映实际执行结果
|
||||
|
||||
## Blocked by
|
||||
|
||||
None - can start immediately
|
||||
14
CLAUDE.md
14
CLAUDE.md
@@ -338,3 +338,17 @@ queueClient.EnqueueTask(ctx, constants.TaskTypeXxx, payloadBytes)
|
||||
|
||||
|
||||
**详细规范和 OpenSpec 工作流请查看**: `@/openspec/AGENTS.md`
|
||||
|
||||
## Agent skills
|
||||
|
||||
### Issue tracker
|
||||
|
||||
Issue 和 PRD 以本地 Markdown 文件形式存放在 `.scratch/<feature-slug>/` 下。详见 `docs/agents/issue-tracker.md`。
|
||||
|
||||
### Triage labels
|
||||
|
||||
使用默认的英文 Triage 标签词汇(`needs-triage` / `needs-info` / `ready-for-agent` / `ready-for-human` / `wontfix`)。详见 `docs/agents/triage-labels.md`。
|
||||
|
||||
### Domain docs
|
||||
|
||||
单 Context 布局——根目录的 `CONTEXT.md` + `docs/adr/`(按需懒创建),与现有的 `openspec/` 提案工作流并存。详见 `docs/agents/domain.md`。
|
||||
|
||||
19
CONTEXT.md
Normal file
19
CONTEXT.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# 领域术语表
|
||||
|
||||
## 资产(Asset)
|
||||
|
||||
**IoT 卡 / IotCard**:物联网流量卡,以 ICCID 为唯一标识。分为独立卡(`is_standalone=true`,未绑定设备)和设备卡(绑定在设备上)。
|
||||
|
||||
**设备 / Device**:硬件设备,以虚拟号(VirtualNo)为业务标识,可插卡使用。
|
||||
|
||||
**资产世代 / Generation**:`generation` 字段记录卡/设备经历"换货+旧资产转新"操作的次数。初始值为 1;每完成一次换货且旧资产执行"转新"后加 1。`generation > 1` 说明该资产曾作为旧资产经历过换货并被重新投入使用。
|
||||
|
||||
**资产业务状态 / AssetStatus**:`asset_status` 字段表示资产在 CMP 内部的业务生命周期(1=在库, 2=已销售, 3=已换货, 4=已停用)。换货完成时旧资产置为 3;执行"旧资产转新"后重置为 1,同时 generation+1。
|
||||
|
||||
## 企业授权(Enterprise Authorization)
|
||||
|
||||
**卡企业授权**:将独立卡授权给企业使用的操作。**业务约束:一张卡在同一时间只能授权给一个企业**(与设备授权保持一致,数据库通过部分唯一索引强制约束)。
|
||||
|
||||
**有效授权**:`revoked_at IS NULL AND deleted_at IS NULL` 的授权记录。撤回授权后(`revoked_at` 有值)视为历史记录,不计入当前授权关系。
|
||||
|
||||
**设备企业授权**:将设备(含其绑定卡)授权给企业使用的操作。同一台设备同一时间只能授权给一个企业(`uq_active_device_auth` 唯一约束)。
|
||||
File diff suppressed because it is too large
Load Diff
@@ -250,7 +250,7 @@ curl -G "https://open.example.com/api/open/v1/cards/status" \
|
||||
- `total_flow_mb`:套餐总流量,单位 MB。
|
||||
- `valid_days`:套餐有效天数。
|
||||
|
||||
钱包购买接口一次只允许一个 `package_code`,`card_nos` 最多 100 张卡。每张卡独立创建钱包订单,允许部分成功,失败卡会返回独立错误码和原因。
|
||||
钱包购买接口一次只允许一个 `package_code`,`card_nos` 最多 100 张卡。每张卡独立创建钱包订单,允许部分成功,失败卡会返回独立错误码和原因。任一卡购买失败时,外层 `code`/`msg` 返回首条失败卡的错误码和原因,同时 `data.failed_cards` 保留失败明细,`data.orders` 保留已成功订单;接入方应以 `success_count`、`failed_count`、`orders`、`failed_cards` 判断单卡结果,避免整批重试造成重复下单。
|
||||
|
||||
## 5. 错误码
|
||||
|
||||
|
||||
40
docs/agents/domain.md
Normal file
40
docs/agents/domain.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# 领域文档
|
||||
|
||||
工程类 Skill(`improve-codebase-architecture`、`diagnose`、`tdd` 等)在探索代码库前应如何消费本仓库的领域文档。
|
||||
|
||||
## 探索前请先阅读
|
||||
|
||||
- 根目录的 **`CONTEXT.md`**(单 Context 布局)
|
||||
- 根目录的 **`docs/adr/`** —— 阅读与当前改动相关的 ADR
|
||||
|
||||
如果这些文件还不存在,**直接跳过,不要提示缺失,也不要主动建议现在就创建它们**。这两个文件由 `/grill-with-docs` 在术语或决定真正确定下来时按需创建。
|
||||
|
||||
## 与 openspec/ 的关系
|
||||
|
||||
本仓库已经使用 `openspec/`(`openspec/specs/` 存放规范、`openspec/changes/` 存放变更提案)作为正式的提案与规范工作流,详见根目录 `CLAUDE.md` 的「OpenSpec 工作流」一节。`CONTEXT.md` / `docs/adr/` 是补充性质的轻量级领域词汇表和架构决定记录,不会替代 openspec 流程:
|
||||
|
||||
- 涉及接口/数据库等实际变更 → 走 openspec 提案
|
||||
- 记录领域词汇、命名约定、历史架构权衡 → 写入 `CONTEXT.md` / `docs/adr/`
|
||||
|
||||
## 文件结构(单 Context)
|
||||
|
||||
```
|
||||
/
|
||||
├── CONTEXT.md
|
||||
├── docs/adr/
|
||||
│ ├── 0001-xxx.md
|
||||
│ └── 0002-xxx.md
|
||||
└── openspec/
|
||||
```
|
||||
|
||||
## 使用词汇表中的术语
|
||||
|
||||
当输出内容涉及某个领域概念时(issue 标题、重构提案、假设、测试名称),使用 `CONTEXT.md` 中定义的术语,不要漂移到词汇表明确避免使用的同义词。
|
||||
|
||||
如果你需要的概念还不在词汇表中,这是一个信号——可能是你在发明项目并未使用的语言(应重新考虑),也可能是真实存在的空白(记录下来留给 `/grill-with-docs`)。
|
||||
|
||||
## 标记与 ADR 的冲突
|
||||
|
||||
如果你的输出与某条已有 ADR 冲突,必须显式指出,而不是悄悄覆盖:
|
||||
|
||||
> 与 ADR-0007(事件溯源订单)冲突——但值得重新讨论,原因是……
|
||||
19
docs/agents/issue-tracker.md
Normal file
19
docs/agents/issue-tracker.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Issue 追踪方式:本地 Markdown
|
||||
|
||||
本仓库的 Issue 和 PRD 以 Markdown 文件形式存放在 `.scratch/` 目录下。
|
||||
|
||||
## 约定
|
||||
|
||||
- 每个功能一个目录:`.scratch/<feature-slug>/`
|
||||
- PRD 文件:`.scratch/<feature-slug>/PRD.md`
|
||||
- 实现类 Issue:`.scratch/<feature-slug>/issues/<NN>-<slug>.md`,从 `01` 开始编号
|
||||
- Triage 状态记录在每个 Issue 文件顶部的 `Status:` 行(状态字符串见 `triage-labels.md`)
|
||||
- 评论和讨论记录追加在文件底部的 `## Comments` 标题下
|
||||
|
||||
## 当某个 Skill 说"发布到 issue tracker"时
|
||||
|
||||
在 `.scratch/<feature-slug>/` 下创建一个新文件(目录不存在则先创建)。
|
||||
|
||||
## 当某个 Skill 说"获取对应的 ticket"时
|
||||
|
||||
读取所引用路径对应的文件。用户通常会直接给出文件路径或 Issue 编号。
|
||||
15
docs/agents/triage-labels.md
Normal file
15
docs/agents/triage-labels.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Triage 标签
|
||||
|
||||
这些 Skill 用五个统一的 Triage 角色来表达状态。本文件将这些角色映射到本仓库实际使用的标签字符串。
|
||||
|
||||
| Skill 中的角色 | 本仓库使用的标签 | 含义 |
|
||||
| ----------------- | ------------------ | -------------------------------- |
|
||||
| `needs-triage` | `needs-triage` | 需要维护者评估该 issue |
|
||||
| `needs-info` | `needs-info` | 等待提交者补充信息 |
|
||||
| `ready-for-agent` | `ready-for-agent` | 已完整描述,AFK Agent 可直接处理 |
|
||||
| `ready-for-human` | `ready-for-human` | 需要人工实现 |
|
||||
| `wontfix` | `wontfix` | 不予处理 |
|
||||
|
||||
由于本仓库使用本地 Markdown 作为 issue tracker,这些标签以每个 issue 文件顶部的 `Status: <label>` 行体现(见 `issue-tracker.md`)。
|
||||
|
||||
当某个 Skill 提到某个角色时(例如"打上 AFK-ready 的 triage 标签"),使用本表中对应的标签字符串。
|
||||
94
docs/polling-system/轮询配置二维条件重构计划.md
Normal file
94
docs/polling-system/轮询配置二维条件重构计划.md
Normal file
@@ -0,0 +1,94 @@
|
||||
# 轮询配置二维条件重构计划
|
||||
|
||||
## 背景
|
||||
|
||||
当前轮询配置使用 `card_condition` 单字段表达卡的轮询条件,但业务上实际存在两个独立维度:
|
||||
|
||||
- 实名状态:未实名、已实名
|
||||
- 卡状态:开机、停机
|
||||
|
||||
单字段枚举会把两个维度压成互斥状态,导致“未实名 + 停机”这类卡只能命中一个条件,配置语义不直观,也容易出现高低频策略互相覆盖。
|
||||
|
||||
## 目标
|
||||
|
||||
将轮询配置从单一 `card_condition` 改为二维条件匹配:
|
||||
|
||||
- `realname_condition`:`any`、`not_real_name`、`real_name`
|
||||
- `network_condition`:`any`、`online`、`offline`
|
||||
|
||||
配置匹配规则调整为:
|
||||
|
||||
```text
|
||||
实名条件匹配 AND 卡状态条件匹配 AND 卡类型/运营商等条件匹配
|
||||
```
|
||||
|
||||
一张卡允许同时命中多条配置;每个任务类型仍按现有 `priority ASC` 选择第一条非空 interval。
|
||||
|
||||
## 推荐语义
|
||||
|
||||
| 条件字段 | 值 | 语义 |
|
||||
| --- | --- | --- |
|
||||
| `realname_condition` | `any` | 不限制实名状态 |
|
||||
| `realname_condition` | `not_real_name` | `real_name_status != 1` |
|
||||
| `realname_condition` | `real_name` | `real_name_status = 1` |
|
||||
| `network_condition` | `any` | 不限制开停机状态 |
|
||||
| `network_condition` | `online` | `network_status = 1` |
|
||||
| `network_condition` | `offline` | `network_status = 0` |
|
||||
|
||||
## 迁移映射
|
||||
|
||||
为了平滑迁移旧配置,可按以下规则回填新字段:
|
||||
|
||||
| 旧 `card_condition` | 新 `realname_condition` | 新 `network_condition` |
|
||||
| --- | --- | --- |
|
||||
| 空 | `any` | `any` |
|
||||
| `not_real_name` | `not_real_name` | `online` |
|
||||
| `real_name` | `real_name` | `any` |
|
||||
| `suspended` | `any` | `offline` |
|
||||
| `activated` | `real_name` | `online` |
|
||||
|
||||
## 示例配置
|
||||
|
||||
未实名卡高频实名:
|
||||
|
||||
```text
|
||||
realname_condition = not_real_name
|
||||
network_condition = any
|
||||
realname_check_interval = 120
|
||||
其他 interval = NULL
|
||||
```
|
||||
|
||||
停机卡高频套餐和卡状态:
|
||||
|
||||
```text
|
||||
realname_condition = any
|
||||
network_condition = offline
|
||||
package_check_interval = 120
|
||||
card_status_check_interval = 120
|
||||
其他 interval = NULL
|
||||
```
|
||||
|
||||
已实名开机卡低频实名、常规套餐和卡状态:
|
||||
|
||||
```text
|
||||
realname_condition = real_name
|
||||
network_condition = online
|
||||
realname_check_interval = 86400
|
||||
package_check_interval = 300
|
||||
card_status_check_interval = 3600
|
||||
```
|
||||
|
||||
## 实施步骤
|
||||
|
||||
1. 新增数据库字段 `realname_condition` 和 `network_condition`,保留旧字段用于迁移兼容。
|
||||
2. 回填历史配置的新字段值。
|
||||
3. 调整 DTO、Handler、Service、文档生成器和 OpenAPI 描述。
|
||||
4. 调整 `PollingConfigManager` 的匹配逻辑,按二维条件筛选配置。
|
||||
5. 验证典型组合:未实名开机、未实名停机、已实名开机、已实名停机。
|
||||
6. 确认线上配置完成迁移后,再考虑废弃旧 `card_condition` 字段。
|
||||
|
||||
## 风险
|
||||
|
||||
- `real_name` 条件生效后,现有灰度配置可能覆盖大量已实名卡,需要上线前确认优先级和 interval。
|
||||
- 多条配置同时命中是预期行为,但需要用 priority 明确每个任务类型的最终 interval。
|
||||
- 迁移期间需要保持旧配置兼容,避免配置未更新时轮询队列为空。
|
||||
@@ -1,7 +1,7 @@
|
||||
# 奇成数据迁移方案
|
||||
|
||||
> 目标:把奇成(旧 MySQL `kyhl` 库)的存量卡和设备迁移到新系统(PostgreSQL),输出 SQL 脚本供线上手动执行。
|
||||
> 范围:当前态资产 + 当前生效套餐 + 累计已用流量 + 代理佣金总值。**不迁移历史明细**。
|
||||
> 范围:当前态资产 + 当前生效正式套餐 + 未生效待生效正式套餐 + 累计已用流量 + 代理佣金总值。**不迁移历史明细、已过期套餐和加油包**。
|
||||
|
||||
---
|
||||
|
||||
@@ -14,12 +14,10 @@
|
||||
│ resources/devices.csv│
|
||||
└──────────┬───────────┘
|
||||
│
|
||||
我们维护映射配置
|
||||
我们维护唯一决策配置
|
||||
┌──────────▼───────────┐
|
||||
│ config/carrier.yaml │
|
||||
│ config/series.yaml │
|
||||
│ config/package.yaml │
|
||||
│ config/shop.yaml │
|
||||
│ config/mapping.yaml │
|
||||
│ 归属/槽位/套餐映射 │
|
||||
└──────────┬───────────┘
|
||||
│
|
||||
┌──────────▼───────────┐
|
||||
@@ -45,9 +43,11 @@
|
||||
```
|
||||
|
||||
**关键约束**
|
||||
- 两个脚本都**不直接写入**线上库,只生成 `.sql` 文件
|
||||
- 两个脚本都**不直接写入**线上库,只生成 `.sql` 文件和审核 CSV
|
||||
- `config/mapping.yaml` 是归属、设备当前槽位、套餐来源槽位和套餐迁移范围的决策源;奇成只提供历史事实
|
||||
- 脚本对奇成线上库**只 SELECT**,DB 连接层强制只读事务(`SET TRANSACTION READ ONLY`)
|
||||
- 所有 INSERT 使用 `ON CONFLICT DO NOTHING`,支持重跑
|
||||
- `errors.csv` 非空时不执行 SQL,先修正配置/输入后重跑
|
||||
|
||||
---
|
||||
|
||||
@@ -84,13 +84,14 @@
|
||||
| `sim_iccid_2` | 否 | 插槽2的 iccid | `89860623...` |
|
||||
| `sim_iccid_3` | 否 | 插槽3的 iccid | |
|
||||
| `sim_iccid_4` | 否 | 插槽4的 iccid | |
|
||||
| `current_slot` | 否 | 当前使用的插槽位置(1-4),用于 `tb_device_sim_binding.is_current` | `1` |
|
||||
| `target_shop_code` | 否 | 目标店铺编码。**留空 = 进平台库存** | `SHOP20260428103424CZZQ` |
|
||||
| `current_slot` | 否 | 当前使用的插槽位置(1-4),用于 `tb_device_sim_binding.is_current`。空值按 `mapping.yaml` 批量规则或覆盖项填充 | `2` |
|
||||
| `package_source_slot` | 否 | 设备套餐来源槽位(1-4)。只迁该槽位卡的奇成正式套餐为设备套餐 | `2` |
|
||||
|
||||
**注意**
|
||||
- 设备上的卡,其 `cards.csv` 中 `target_shop_code` 必须留空(一致性校验由脚本检查)
|
||||
- `cards.csv` 必须先到位,`devices.csv` 的 `sim_iccid_*` 才能被校验
|
||||
- 业务方分别准备这两份是合理的,奇成 `tbl_card_relate`(主卡+副卡1+副卡2)的关系业务方应已掌握
|
||||
- 槽位解析优先级:`devices.csv` 行级配置 > `mapping.yaml.overrides.devices` > `mapping.yaml.ownership_rules.device` 批量默认值
|
||||
- `current_slot` 或 `package_source_slot` 对应槽位无卡时写入 `errors.csv` 并阻断该设备相关 SQL
|
||||
|
||||
---
|
||||
|
||||
@@ -123,9 +124,37 @@
|
||||
|
||||
## 四、我们维护的映射配置
|
||||
|
||||
迁移前**必须**在新系统建好店铺、运营商、套餐系列、套餐,然后填好以下映射。
|
||||
迁移前**必须**在新系统建好店铺、运营商、套餐系列、套餐,然后填好 `config/mapping.yaml`。该文件是本次迁移的唯一决策源。
|
||||
|
||||
### 4.1 `config/carrier_mapping.yaml`
|
||||
### 4.1 归属、槽位和套餐规则
|
||||
|
||||
```yaml
|
||||
ownership_rules:
|
||||
default_target_shop_code: KWTX
|
||||
device:
|
||||
mode: default_shop
|
||||
current_slot: 2
|
||||
package_source_slot: 2
|
||||
standalone_card:
|
||||
mode: default_shop
|
||||
|
||||
package_rules:
|
||||
migrate_statuses: [active, pending]
|
||||
|
||||
overrides:
|
||||
devices:
|
||||
- virtual_no: "862639073940258"
|
||||
target_shop_code: OTHER
|
||||
current_slot: 1
|
||||
package_source_slot: 1
|
||||
cards:
|
||||
- iccid: "89861590172420360956"
|
||||
target_shop_code: OTHER
|
||||
```
|
||||
|
||||
归属规则:独立卡按 `overrides.cards` 或 `ownership_rules.standalone_card` 决定;设备按 `overrides.devices` 或 `ownership_rules.device` 决定;设备上的卡跟随设备。奇成 `agent_id` 不再作为默认归属来源。
|
||||
|
||||
### 4.2 `config/mapping.yaml` 中的 carriers
|
||||
|
||||
```yaml
|
||||
# 奇成的运营商名称(业务方在 cards.csv 中填的)→ 新系统 carrier_id + type
|
||||
@@ -141,7 +170,7 @@ carriers:
|
||||
# ... 业务方提供的所有运营商都要列出
|
||||
```
|
||||
|
||||
### 4.2 `config/series_mapping.yaml`
|
||||
### 4.3 `config/mapping.yaml` 中的 series
|
||||
|
||||
```yaml
|
||||
# 奇成套餐系列名 → 新系统 series_id
|
||||
@@ -153,7 +182,7 @@ series:
|
||||
target_series_id: 6
|
||||
```
|
||||
|
||||
### 4.3 `config/package_mapping.yaml`
|
||||
### 4.4 `config/mapping.yaml` 中的 packages
|
||||
|
||||
```yaml
|
||||
# 奇成套餐名 → 新系统 package_id(cards.csv 的 package_name 直接对照这里)
|
||||
@@ -166,7 +195,7 @@ packages:
|
||||
|
||||
> 注:奇成的 `tbl_set_meal` 套餐表非常大,**不需要全部映射**,只映射本次迁移涉及的套餐(脚本预扫描 cards.csv 收集 package_name 集合,缺失的列出来让我们补)。
|
||||
|
||||
### 4.4 `config/shop_mapping.yaml`(可选别名)
|
||||
### 4.5 店铺编码
|
||||
|
||||
```yaml
|
||||
# 如果业务方愿意填 shop_code,本文件可以省略
|
||||
@@ -318,16 +347,15 @@ FROM c;
|
||||
```
|
||||
1. 读 cards.csv 得到 iccid 列表
|
||||
2. 连奇成(只读事务)批量查:
|
||||
- tbl_card_life WHERE iccid_mark IN (...) AND status=1 ORDER BY expire_date DESC
|
||||
→ 每张卡取最新生效的套餐:meal_id, meal_name, start_date, expire_date
|
||||
- tbl_card WHERE iccid_mark IN (...) → total_bytes_cnt (GB)
|
||||
- tbl_agent_commission_account → 按 agent_id 聚合 can_draw_amount, total_commision_amount
|
||||
- tbl_agent,根据 cards.csv 中 agent 信息映射出 shop_id
|
||||
3. 对每张卡:
|
||||
a. 套餐映射:legacy_meal_name → package_id(从 yaml 或业务方 csv)
|
||||
b. 生成伪订单 SQL(order_no = "MIG-CARD-" + iccid 后8位 + 时间戳)
|
||||
c. 生成 tb_package_usage SQL(关联伪订单 + 计算 snapshot 字段)
|
||||
d. 生成 UPDATE tb_iot_card SET data_usage_mb = ... (把 GB 转 MB)
|
||||
- `tbl_card_life` 当前生效正式套餐和 `tbl_next_month_card_life` 次月待生效套餐,保留 legacy 套餐 ID、套餐名、类型、状态、开始时间、到期时间和稳定排序键
|
||||
- `tbl_card.total_bytes_cnt` → 反算新系统真用量 MB
|
||||
- `tbl_agent_commission_account` / `tbl_agent` → 仅用于代理钱包初始化
|
||||
3. 对每个独立卡或设备套餐来源槽位:
|
||||
a. 套餐映射:`legacy_meal_id` → `target_package_id`(来自 `mapping.yaml.packages`)
|
||||
b. active 套餐写 `tb_package_usage.status=1`,pending 套餐写 `status=0` 并按稳定顺序写 `priority`
|
||||
c. 生成伪订单 SQL(order_no = `MIG-<ICCID>-<priority>`)
|
||||
d. 生成 `tb_package_usage` SQL(关联伪订单 + 计算 snapshot 字段)
|
||||
e. 生成 `UPDATE tb_iot_card SET data_usage_mb = ...`(真用量 MB)
|
||||
4. 对每个 shop(按代理映射出来的):
|
||||
a. 生成 INSERT/UPDATE tb_agent_wallet(总值)
|
||||
b. 生成一条 tb_agent_wallet_transaction(type=initial_migration, amount=总值, remark=源奇成代理ID)
|
||||
@@ -343,7 +371,10 @@ output/
|
||||
step2_03_card_data_usage_update.sql # 卡累计流量 UPDATE
|
||||
step2_04_agent_wallet_init.sql # 代理钱包总值
|
||||
step2_05_agent_wallet_transactions.sql # 迁移流水
|
||||
warnings.csv # 警告:找不到当前生效套餐、无法映射的代理等
|
||||
step2_06_asset_series_update.sql # 资产套餐系列回填
|
||||
package_resolution.csv # 套餐迁移审核文件
|
||||
errors.csv # 套餐映射缺失、多 active、槽位缺失等阻断错误
|
||||
warnings.csv # 用量反算和代理钱包警告
|
||||
summary.txt
|
||||
```
|
||||
|
||||
@@ -357,7 +388,7 @@ WITH new_order AS (
|
||||
total_amount, payment_method, payment_status, paid_at,
|
||||
source, generation, creator, updater, created_at, updated_at
|
||||
)
|
||||
SELECT 'MIG-CARD-' || RIGHT('89852000263338772439', 8) || '-' || TO_CHAR(NOW(), 'YYYYMMDDHH24MISS'),
|
||||
SELECT 'MIG-89852000263338772439-1',
|
||||
'single_card', 'personal', 0, c.id,
|
||||
0, 'offline', 2, NOW(),
|
||||
'migration', 1, 0, 0, NOW(), NOW()
|
||||
@@ -489,9 +520,9 @@ scripts/migration/
|
||||
| 1 | 业务方填的 carrier_name / package_name 与奇成不一致 → 映射失败 | 脚本预扫描,把所有未匹配项写到 unmatched.csv 让业务方修正 |
|
||||
| 2 | 奇成的 iccid 有 18/21/22 位脏数据 | 脚本第三节规则剔除,进 errors.csv |
|
||||
| 3 | 同一 iccid 被业务方填多次 | 脚本去重 + 警告 |
|
||||
| 4 | 设备上的卡在 cards.csv 里又写了 target_shop_code | 一致性校验,进 errors.csv |
|
||||
| 5 | 奇成 tbl_card_life 一张卡有多条 status=1 记录 | 取 expire_date 最大的那条,warnings.csv 记录 |
|
||||
| 6 | 套餐 ID 映射不全 | 脚本预扫描,缺的列出来,业务方/我们补全 yaml |
|
||||
| 4 | 设备 `current_slot` 或 `package_source_slot` 指向空槽位 | 写入 errors.csv,阻断该设备资产或套餐 SQL |
|
||||
| 5 | 同一资产存在多个 active 正式套餐 | 写入 errors.csv,阻断该资产套餐 SQL,人工裁决后修正 |
|
||||
| 6 | 套餐 ID 映射不全 | `package_resolution.csv` 记录,errors.csv 阻断对应套餐 SQL |
|
||||
| 7 | tb_iot_card.is_standalone 由触发器维护 | 脚本写入时设 true,触发器会因为后续 binding 自动转 false |
|
||||
| 8 | 代理→shop 映射不明确 | 需要业务方提供 agent_id → shop_code 映射表,作为 config/agent_shop_mapping.yaml |
|
||||
| 9 | 线上库 sequence 与测试库不同 | 用 CTE + RETURNING 模式,不硬编码 id |
|
||||
@@ -526,7 +557,7 @@ mappings:
|
||||
|
||||
| 字段 | 值 | 说明 |
|
||||
|------|-----|------|
|
||||
| `order_no` | `MIG-CARD-<iccid后8位>-<时间戳>` / `MIG-DEV-<vno>-<时间戳>` | 前缀就是迁移数据的唯一识别标记 |
|
||||
| `order_no` | `MIG-<ICCID>-<priority>` | 前缀就是迁移数据的唯一识别标记 |
|
||||
| `source` | `'admin'`(复用现有) | 不新增 `migration` 取值 |
|
||||
| `buyer_type` | `'personal'` | 与 buyer_id=0 配对 |
|
||||
| `buyer_id` | `0` | 与赠送/平台自营一致 |
|
||||
|
||||
8
go.mod
8
go.mod
@@ -5,7 +5,7 @@ go 1.25.0
|
||||
require (
|
||||
github.com/ArtisanCloud/PowerWeChat/v3 v3.4.38
|
||||
github.com/aws/aws-sdk-go v1.55.5
|
||||
github.com/bytedance/sonic v1.14.2
|
||||
github.com/bytedance/sonic v1.15.2
|
||||
github.com/go-playground/validator/v10 v10.28.0
|
||||
github.com/gofiber/fiber/v2 v2.52.9
|
||||
github.com/gofiber/storage/redis/v3 v3.4.1
|
||||
@@ -14,6 +14,7 @@ require (
|
||||
github.com/hibiken/asynq v0.25.1
|
||||
github.com/jackc/pgx/v5 v5.7.6
|
||||
github.com/redis/go-redis/v9 v9.17.3
|
||||
github.com/smartwalle/alipay/v3 v3.2.29
|
||||
github.com/spf13/viper v1.21.0
|
||||
github.com/swaggest/openapi-go v0.2.60
|
||||
github.com/valyala/fasthttp v1.66.0
|
||||
@@ -24,6 +25,7 @@ require (
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
gorm.io/datatypes v1.2.7
|
||||
gorm.io/driver/mysql v1.5.6
|
||||
gorm.io/driver/postgres v1.6.0
|
||||
gorm.io/gorm v1.31.1
|
||||
)
|
||||
@@ -35,7 +37,7 @@ require (
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
|
||||
github.com/andybalholm/brotli v1.2.0 // indirect
|
||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||
github.com/bytedance/sonic/loader v0.4.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.5.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||
@@ -69,7 +71,6 @@ require (
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
github.com/rogpeppe/go-internal v1.13.1 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/smartwalle/alipay/v3 v3.2.29 // indirect
|
||||
github.com/smartwalle/ncrypto v1.0.4 // indirect
|
||||
github.com/smartwalle/ngx v1.1.0 // indirect
|
||||
github.com/smartwalle/nsign v1.0.9 // indirect
|
||||
@@ -98,5 +99,4 @@ require (
|
||||
golang.org/x/time v0.14.0 // indirect
|
||||
google.golang.org/protobuf v1.36.10 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gorm.io/driver/mysql v1.5.6 // indirect
|
||||
)
|
||||
|
||||
4
go.sum
4
go.sum
@@ -28,8 +28,12 @@ github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M
|
||||
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/sonic v1.14.2 h1:k1twIoe97C1DtYUo+fZQy865IuHia4PR5RPiuGPPIIE=
|
||||
github.com/bytedance/sonic v1.14.2/go.mod h1:T80iDELeHiHKSc0C9tubFygiuXoGzrkjKzX2quAx980=
|
||||
github.com/bytedance/sonic v1.15.2 h1:90H+rcF/FwLXwfB1cudOLq/je83n683Utf4Cbp0xHCo=
|
||||
github.com/bytedance/sonic v1.15.2/go.mod h1:mT2NbXunuaEbnZ+mRIX/vYqKISmgEuHFDI4UzmKx2SA=
|
||||
github.com/bytedance/sonic/loader v0.4.0 h1:olZ7lEqcxtZygCK9EKYKADnpQoYkRQxaeY2NYzevs+o=
|
||||
github.com/bytedance/sonic/loader v0.4.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/bytedance/sonic/loader v0.5.1 h1:Ygpfa9zwRCCKSlrp5bBP/b/Xzc3VxsAW+5NIYXrOOpI=
|
||||
github.com/bytedance/sonic/loader v0.5.1/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
|
||||
@@ -17,7 +17,6 @@ import (
|
||||
|
||||
func initHandlers(svc *services, deps *Dependencies) *Handlers {
|
||||
validate := validator.New()
|
||||
personalCustomerDeviceStore := postgres.NewPersonalCustomerDeviceStore(deps.DB)
|
||||
assetWalletStore := postgres.NewAssetWalletStore(deps.DB, deps.Redis)
|
||||
packageStore := postgres.NewPackageStore(deps.DB)
|
||||
shopPackageAllocationStore := postgres.NewShopPackageAllocationStore(deps.DB)
|
||||
@@ -56,7 +55,7 @@ func initHandlers(svc *services, deps *Dependencies) *Handlers {
|
||||
rechargeOrderStore,
|
||||
paymentStore,
|
||||
assetWalletStore,
|
||||
personalCustomerDeviceStore,
|
||||
svc.CustomerBinding,
|
||||
personalCustomerOpenIDStore,
|
||||
personalCustomerStore,
|
||||
personalCustomerPhoneStore,
|
||||
@@ -78,12 +77,12 @@ func initHandlers(svc *services, deps *Dependencies) *Handlers {
|
||||
Permission: admin.NewPermissionHandler(svc.Permission),
|
||||
PersonalCustomer: app.NewPersonalCustomerHandler(svc.PersonalCustomer, deps.Logger),
|
||||
ClientAuth: app.NewClientAuthHandler(svc.ClientAuth, deps.Logger),
|
||||
ClientAsset: app.NewClientAssetHandler(svc.Asset, personalCustomerDeviceStore, assetWalletStore, packageStore, shopPackageAllocationStore, iotCardStore, deviceStore, deps.DB, deps.Logger),
|
||||
ClientWallet: app.NewClientWalletHandler(svc.Asset, personalCustomerDeviceStore, assetWalletStore, assetWalletTransactionStore, rechargeOrderStore, paymentStore, svc.Recharge, personalCustomerOpenIDStore, svc.WechatConfig, deps.Redis, deps.Logger, deps.DB, iotCardStore, deviceStore),
|
||||
ClientAsset: app.NewClientAssetHandler(svc.Asset, svc.CustomerBinding, assetWalletStore, packageStore, shopPackageAllocationStore, iotCardStore, deviceStore, deps.DB, deps.Logger),
|
||||
ClientWallet: app.NewClientWalletHandler(svc.Asset, svc.CustomerBinding, assetWalletStore, assetWalletTransactionStore, rechargeOrderStore, paymentStore, svc.Recharge, personalCustomerOpenIDStore, svc.WechatConfig, deps.Redis, deps.Logger, deps.DB, iotCardStore, deviceStore),
|
||||
ClientOrder: app.NewClientOrderHandler(clientOrderService, deps.Logger),
|
||||
ClientExchange: app.NewClientExchangeHandler(svc.Exchange),
|
||||
ClientRealname: app.NewClientRealnameHandler(svc.Asset, personalCustomerDeviceStore, iotCardStore, deviceSimBindingStore, carrierStore, deps.GatewayClient, deps.Logger, svc.PollingManualTrigger),
|
||||
ClientDevice: app.NewClientDeviceHandler(svc.Asset, personalCustomerDeviceStore, deviceStore, deviceSimBindingStore, iotCardStore, deps.GatewayClient, deps.Logger),
|
||||
ClientRealname: app.NewClientRealnameHandler(svc.Asset, svc.CustomerBinding, iotCardStore, deviceSimBindingStore, carrierStore, deps.GatewayClient, deps.Logger, svc.PollingManualTrigger),
|
||||
ClientDevice: app.NewClientDeviceHandler(svc.Asset, svc.CustomerBinding, deviceStore, deviceSimBindingStore, iotCardStore, deps.GatewayClient, deps.Logger),
|
||||
ClientRechargeOrder: app.NewClientRechargeOrderHandler(rechargeOrderStore, paymentStore, deps.Logger),
|
||||
Shop: admin.NewShopHandler(svc.Shop),
|
||||
ShopRole: admin.NewShopRoleHandler(svc.Shop),
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
authSvc "github.com/break/junhong_cmp_fiber/internal/service/auth"
|
||||
carrierSvc "github.com/break/junhong_cmp_fiber/internal/service/carrier"
|
||||
clientAuthSvc "github.com/break/junhong_cmp_fiber/internal/service/client_auth"
|
||||
customerBindingSvc "github.com/break/junhong_cmp_fiber/internal/service/customer_binding"
|
||||
commissionCalculationSvc "github.com/break/junhong_cmp_fiber/internal/service/commission_calculation"
|
||||
commissionStatsSvc "github.com/break/junhong_cmp_fiber/internal/service/commission_stats"
|
||||
commissionWithdrawalSvc "github.com/break/junhong_cmp_fiber/internal/service/commission_withdrawal"
|
||||
@@ -109,9 +110,13 @@ type services struct {
|
||||
TrafficQuery *trafficSvc.QueryService
|
||||
OperationPassword *operationPasswordSvc.Service
|
||||
AgentOpenAPI *agentOpenAPISvc.Service
|
||||
CustomerBinding *customerBindingSvc.Service
|
||||
}
|
||||
|
||||
func initServices(s *stores, deps *Dependencies) *services {
|
||||
// CustomerBinding 模块依赖最少,最先初始化
|
||||
customerBinding := customerBindingSvc.New(deps.DB, s.IotCard, s.Device)
|
||||
|
||||
purchaseValidation := purchaseValidationSvc.New(deps.DB, s.IotCard, s.Device, s.Package, s.ShopPackageAllocation)
|
||||
accountAudit := accountAuditSvc.NewService(s.AccountOperationLog)
|
||||
assetAudit := assetAuditSvc.NewService(s.AssetOperationLog, deps.DB)
|
||||
@@ -172,6 +177,8 @@ func initServices(s *stores, deps *Dependencies) *services {
|
||||
iotCard.SetRealnameActivator(packageActivation)
|
||||
iotCard.SetStopResumeService(stopResumeService)
|
||||
iotCard.SetDeviceSimBindingStore(s.DeviceSimBinding)
|
||||
iotCard.SetEnterpriseCardAuthStore(s.EnterpriseCardAuthorization)
|
||||
iotCard.SetEnterpriseStore(s.Enterprise)
|
||||
iotCard.SetRedisClient(deps.Redis)
|
||||
device := deviceSvc.New(
|
||||
deps.DB,
|
||||
@@ -187,12 +194,14 @@ func initServices(s *stores, deps *Dependencies) *services {
|
||||
deps.GatewayClient,
|
||||
s.AssetIdentifier,
|
||||
assetAudit,
|
||||
s.EnterpriseDeviceAuthorization,
|
||||
s.Enterprise,
|
||||
)
|
||||
operationPassword := operationPasswordSvc.New(deps.Redis)
|
||||
shopCommission := shopCommissionSvc.New(s.Shop, s.Account, s.AgentWallet, s.CommissionWithdrawalRequest, s.CommissionWithdrawalSetting, s.CommissionRecord, s.AgentWalletTransaction, deps.DB, deps.Logger)
|
||||
packageService := packageSvc.New(s.Package, s.PackageSeries, s.ShopPackageAllocation, s.ShopSeriesAllocation)
|
||||
orderService := orderSvc.New(deps.DB, deps.Redis, s.Order, s.OrderItem, s.AgentWallet, s.AssetWallet, s.Payment, purchaseValidation, s.ShopPackageAllocation, s.ShopSeriesAllocation, s.IotCard, s.Device, s.PackageSeries, s.PackageUsage, s.Package, wechatConfig, deps.WechatPayment, paymentLoader, deps.QueueClient, deps.Logger, s.AssetIdentifier, s.PersonalCustomer, s.PersonalCustomerPhone)
|
||||
assetService := assetSvc.New(deps.DB, s.Device, s.IotCard, s.PackageUsage, s.Package, s.PackageSeries, s.DeviceSimBinding, s.Shop, deps.Redis, iotCard, deps.GatewayClient, s.AssetIdentifier, s.Order, s.OrderItem, s.ExchangeOrder)
|
||||
assetService := assetSvc.New(deps.DB, s.Device, s.IotCard, s.PackageUsage, s.Package, s.PackageSeries, s.DeviceSimBinding, s.Shop, deps.Redis, iotCard, deps.GatewayClient, s.AssetIdentifier, s.Order, s.OrderItem, s.ExchangeOrder, assetAudit)
|
||||
agentOpenAPI := agentOpenAPISvc.New(assetService, packageService, orderService, shopCommission, stopResumeService, device, s.IotCard, s.PackageUsage, s.Package, s.PackageSeries, s.AgentWallet, s.DeviceSimBinding, s.Device)
|
||||
|
||||
return &services{
|
||||
@@ -206,7 +215,6 @@ func initServices(s *stores, deps *Dependencies) *services {
|
||||
deps.DB,
|
||||
s.PersonalCustomerOpenID,
|
||||
s.PersonalCustomer,
|
||||
s.PersonalCustomerDevice,
|
||||
s.PersonalCustomerPhone,
|
||||
s.IotCard,
|
||||
s.Device,
|
||||
@@ -215,6 +223,7 @@ func initServices(s *stores, deps *Dependencies) *services {
|
||||
deps.JWTManager,
|
||||
deps.Redis,
|
||||
deps.Logger,
|
||||
customerBinding,
|
||||
),
|
||||
Shop: shopSvc.New(s.Shop, s.Account, s.ShopRole, s.Role, s.AccountRole, s.AgentWallet),
|
||||
Auth: authSvc.New(s.Account, s.AccountRole, s.RolePermission, s.Permission, s.Shop, deps.TokenManager, deps.Logger),
|
||||
@@ -260,11 +269,11 @@ func initServices(s *stores, deps *Dependencies) *services {
|
||||
CommissionStats: commissionStatsSvc.New(s.ShopSeriesCommissionStats),
|
||||
PurchaseValidation: purchaseValidation,
|
||||
Order: orderService,
|
||||
Exchange: exchangeSvc.New(deps.DB, s.ExchangeOrder, s.IotCard, s.Device, s.AssetWallet, s.AssetWalletTransaction, s.PackageUsage, s.PackageUsageDailyRecord, s.ResourceTag, s.PersonalCustomerDevice, deps.Logger),
|
||||
Exchange: exchangeSvc.New(deps.DB, s.ExchangeOrder, s.IotCard, s.Device, s.AssetWallet, s.AssetWalletTransaction, s.PackageUsage, s.PackageUsageDailyRecord, s.ResourceTag, customerBinding, deps.Logger),
|
||||
Recharge: rechargeSvc.New(deps.DB, s.AssetWallet, s.AssetWalletTransaction, s.IotCard, s.Device, s.ShopSeriesAllocation, s.PackageSeries, s.CommissionRecord, wechatConfig, paymentLoader, deps.Logger),
|
||||
PollingConfig: pollingSvc.NewConfigService(s.PollingConfig, deps.Redis, deps.Logger),
|
||||
PollingConcurrency: pollingSvc.NewConcurrencyService(s.PollingConcurrencyConfig, deps.Redis),
|
||||
PollingMonitoring: pollingSvc.NewMonitoringService(deps.Redis),
|
||||
PollingMonitoring: pollingSvc.NewMonitoringServiceWithQueueMgr(deps.Redis, pollingQueueMgr, deps.Logger),
|
||||
PollingAlert: pollingSvc.NewAlertService(s.PollingAlertRule, s.PollingAlertHistory, deps.Redis, deps.Logger),
|
||||
PollingCleanup: pollingSvc.NewCleanupService(s.DataCleanupConfig, s.DataCleanupLog, deps.Logger),
|
||||
PollingManualTrigger: pollingSvc.NewManualTriggerService(s.PollingManualTriggerLog, s.IotCard, deps.Redis, deps.Logger),
|
||||
@@ -304,5 +313,6 @@ func initServices(s *stores, deps *Dependencies) *services {
|
||||
s.AssetWallet,
|
||||
deps.Logger,
|
||||
),
|
||||
CustomerBinding: customerBinding,
|
||||
}
|
||||
}
|
||||
|
||||
22
internal/exporter/datasource.go
Normal file
22
internal/exporter/datasource.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package exporter
|
||||
|
||||
import "context"
|
||||
|
||||
// DataSource 导出场景数据源接口。
|
||||
// 场景实现只负责统计、表头和按 offset/limit 拉取数据,分片调度由导出框架统一处理。
|
||||
type DataSource interface {
|
||||
Scene() string
|
||||
Count(ctx context.Context, params ExportParams) (int, error)
|
||||
Headers(ctx context.Context, params ExportParams) ([]string, error)
|
||||
Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error)
|
||||
}
|
||||
|
||||
// ExportParams 导出任务执行参数。
|
||||
// Filters 来自任务 query_json.filters,权限字段来自任务创建时的快照。
|
||||
type ExportParams struct {
|
||||
Filters map[string]any
|
||||
ScopeShopIDs []uint
|
||||
UserType int
|
||||
CreatorShopID *uint
|
||||
ResolvedHeaders []string
|
||||
}
|
||||
@@ -2,97 +2,448 @@ package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// DeviceSceneStrategy 设备导出场景策略。
|
||||
type DeviceSceneStrategy struct {
|
||||
const (
|
||||
deviceExportBaseHeaderCount = 6
|
||||
deviceExportCardGroupSize = 5
|
||||
deviceExportTailHeaderCount = 5
|
||||
deviceExportMinCardGroups = 1
|
||||
)
|
||||
|
||||
// DeviceDataSource 设备导出数据源。
|
||||
type DeviceDataSource struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewDeviceSceneStrategy 创建设备场景策略。
|
||||
func NewDeviceSceneStrategy(db *gorm.DB) *DeviceSceneStrategy {
|
||||
return &DeviceSceneStrategy{db: db}
|
||||
// NewDeviceDataSource 创建设备导出数据源。
|
||||
func NewDeviceDataSource(db *gorm.DB) *DeviceDataSource {
|
||||
return &DeviceDataSource{db: db}
|
||||
}
|
||||
|
||||
// Scene 场景编码。
|
||||
func (s *DeviceSceneStrategy) Scene() string {
|
||||
// Scene 返回导出场景编码。
|
||||
func (s *DeviceDataSource) Scene() string {
|
||||
return constants.ExportTaskSceneDevice
|
||||
}
|
||||
|
||||
// Headers 导出表头。
|
||||
func (s *DeviceSceneStrategy) Headers() []string {
|
||||
return []string{"ID", "设备虚拟号", "设备名称", "设备型号", "设备类型", "状态", "店铺ID", "创建时间"}
|
||||
// Count 统计设备导出行数。
|
||||
func (s *DeviceDataSource) Count(ctx context.Context, params ExportParams) (int, error) {
|
||||
var total int64
|
||||
query := s.applyFilters(s.baseQuery(ctx), params)
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(total), nil
|
||||
}
|
||||
|
||||
// NextShardBoundary 获取下一段分片边界。
|
||||
func (s *DeviceSceneStrategy) NextShardBoundary(ctx context.Context, task *model.ExportTask, afterID uint64, limit int) (*ShardBoundary, error) {
|
||||
if limit <= 0 {
|
||||
limit = constants.ExportDefaultShardSize
|
||||
}
|
||||
|
||||
var ids []uint64
|
||||
query := s.db.WithContext(ctx).
|
||||
Model(&model.Device{}).
|
||||
Where("id > ?", afterID).
|
||||
Order("id ASC").
|
||||
Limit(limit)
|
||||
query = applyShopScopeForTask(query, task)
|
||||
|
||||
if err := query.Pluck("id", &ids).Error; err != nil {
|
||||
// Headers 返回设备导出表头。
|
||||
func (s *DeviceDataSource) Headers(ctx context.Context, params ExportParams) ([]string, error) {
|
||||
cardGroups, err := s.resolveCardGroupCount(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &ShardBoundary{
|
||||
StartID: afterID,
|
||||
EndID: ids[len(ids)-1],
|
||||
RowCount: len(ids),
|
||||
}, nil
|
||||
return buildDeviceExportHeaders(cardGroups), nil
|
||||
}
|
||||
|
||||
// QueryRows 查询分片内数据行。
|
||||
func (s *DeviceSceneStrategy) QueryRows(ctx context.Context, task *model.ExportTask, startID, endID uint64) ([][]string, error) {
|
||||
if endID == 0 {
|
||||
// Fetch 按 offset/limit 查询设备导出数据。
|
||||
func (s *DeviceDataSource) Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error) {
|
||||
if limit <= 0 {
|
||||
return [][]string{}, nil
|
||||
}
|
||||
|
||||
var devices []model.Device
|
||||
query := s.db.WithContext(ctx).
|
||||
Model(&model.Device{}).
|
||||
Where("id > ? AND id <= ?", startID, endID).
|
||||
Order("id ASC")
|
||||
query = applyShopScopeForTask(query, task)
|
||||
cardGroups := cardGroupCountFromHeaders(params.ResolvedHeaders)
|
||||
if cardGroups <= 0 {
|
||||
resolved, err := s.resolveCardGroupCount(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cardGroups = resolved
|
||||
}
|
||||
|
||||
if err := query.Find(&devices).Error; err != nil {
|
||||
devices, err := s.fetchDevices(ctx, params, offset, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(devices) == 0 {
|
||||
return [][]string{}, nil
|
||||
}
|
||||
|
||||
deviceIDs := make([]uint, 0, len(devices))
|
||||
for _, item := range devices {
|
||||
deviceIDs = append(deviceIDs, item.ID)
|
||||
}
|
||||
|
||||
cardMap, err := s.fetchBoundCards(ctx, deviceIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
packageMap, err := s.fetchActivePackages(ctx, deviceIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows := make([][]string, 0, len(devices))
|
||||
for _, item := range devices {
|
||||
shopID := ""
|
||||
if item.ShopID != nil {
|
||||
shopID = strconv.FormatUint(uint64(*item.ShopID), 10)
|
||||
}
|
||||
|
||||
rows = append(rows, []string{
|
||||
strconv.FormatUint(uint64(item.ID), 10),
|
||||
item.VirtualNo,
|
||||
item.DeviceName,
|
||||
item.DeviceModel,
|
||||
item.DeviceType,
|
||||
strconv.Itoa(item.Status),
|
||||
shopID,
|
||||
item.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
rows = append(rows, buildDeviceExportRow(item, cardMap[item.ID], packageMap[item.ID], cardGroups))
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (s *DeviceDataSource) baseQuery(ctx context.Context) *gorm.DB {
|
||||
return s.db.WithContext(ctx).
|
||||
Table("tb_device AS d").
|
||||
Where("d.deleted_at IS NULL")
|
||||
}
|
||||
|
||||
// applyFilters 应用与 /api/admin/devices 一致的筛选条件。
|
||||
// 导出任务运行在 Worker 中,权限只能使用任务创建时保存的店铺范围快照。
|
||||
func (s *DeviceDataSource) applyFilters(query *gorm.DB, params ExportParams) *gorm.DB {
|
||||
query = applyExportShopScope(query, params, "d.shop_id")
|
||||
|
||||
if virtualNo, ok := filterString(params.Filters, "virtual_no"); ok {
|
||||
query = query.Where("d.virtual_no LIKE ?", "%"+virtualNo+"%")
|
||||
}
|
||||
if imei, ok := filterString(params.Filters, "imei"); ok {
|
||||
query = query.Where("d.imei LIKE ?", "%"+imei+"%")
|
||||
}
|
||||
if keyword, ok := filterString(params.Filters, "keyword"); ok {
|
||||
like := "%" + keyword + "%"
|
||||
query = query.Where("d.virtual_no LIKE ? OR d.imei LIKE ?", like, like)
|
||||
}
|
||||
if deviceName, ok := filterString(params.Filters, "device_name"); ok {
|
||||
query = query.Where("d.device_name LIKE ?", "%"+deviceName+"%")
|
||||
}
|
||||
if status, ok := filterInt(params.Filters, "status"); ok && status > 0 {
|
||||
query = query.Where("d.status = ?", status)
|
||||
}
|
||||
if activationStatus, ok := filterInt(params.Filters, "activation_status"); ok {
|
||||
query = s.applyActivationStatusFilter(query, activationStatus)
|
||||
}
|
||||
if shopIDs, ok := filterUintSlice(params.Filters, "shop_ids"); ok {
|
||||
if len(shopIDs) == 0 {
|
||||
query = query.Where("1 = 0")
|
||||
} else {
|
||||
query = query.Where("d.shop_id IN ?", shopIDs)
|
||||
}
|
||||
} else if shopID, ok := filterInt(params.Filters, "shop_id"); ok {
|
||||
if shopID == 0 {
|
||||
query = query.Where("1 = 0")
|
||||
} else if shopID > 0 {
|
||||
query = query.Where("d.shop_id = ?", shopID)
|
||||
}
|
||||
}
|
||||
if batchNo, ok := filterString(params.Filters, "batch_no"); ok {
|
||||
query = query.Where("d.batch_no = ?", batchNo)
|
||||
}
|
||||
if deviceType, ok := filterString(params.Filters, "device_type"); ok {
|
||||
query = query.Where("d.device_type = ?", deviceType)
|
||||
}
|
||||
if manufacturer, ok := filterString(params.Filters, "manufacturer"); ok {
|
||||
query = query.Where("d.manufacturer LIKE ?", "%"+manufacturer+"%")
|
||||
}
|
||||
if start, ok := filterTime(params.Filters, "created_at_start"); ok {
|
||||
query = query.Where("d.created_at >= ?", start)
|
||||
}
|
||||
if end, ok := filterTime(params.Filters, "created_at_end"); ok {
|
||||
query = query.Where("d.created_at <= ?", end)
|
||||
}
|
||||
if seriesID, ok := filterUint(params.Filters, "series_id"); ok {
|
||||
query = query.Where("d.series_id = ?", seriesID)
|
||||
}
|
||||
if hasActivePackage, ok := filterOptionalBool(params.Filters, "has_active_package"); ok {
|
||||
query = s.applyHasActivePackageFilter(query, hasActivePackage)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
// applyHasActivePackageFilter 按设备是否有生效中的主套餐过滤。
|
||||
func (s *DeviceDataSource) applyHasActivePackageFilter(query *gorm.DB, hasActive bool) *gorm.DB {
|
||||
subQuery := `SELECT 1 FROM tb_package_usage AS pu
|
||||
WHERE pu.device_id = d.id
|
||||
AND pu.status = ?
|
||||
AND pu.master_usage_id IS NULL
|
||||
AND pu.deleted_at IS NULL`
|
||||
if hasActive {
|
||||
return query.Where("EXISTS ("+subQuery+")", constants.PackageUsageStatusActive)
|
||||
}
|
||||
return query.Where("NOT EXISTS ("+subQuery+")", constants.PackageUsageStatusActive)
|
||||
}
|
||||
|
||||
func (s *DeviceDataSource) applyActivationStatusFilter(query *gorm.DB, activationStatus int) *gorm.DB {
|
||||
activeCondition := `EXISTS (
|
||||
SELECT 1
|
||||
FROM tb_package_usage AS pu
|
||||
JOIN tb_device_sim_binding AS b
|
||||
ON b.device_id = pu.device_id
|
||||
AND b.bind_status = ?
|
||||
AND b.deleted_at IS NULL
|
||||
JOIN tb_iot_card AS c
|
||||
ON c.id = b.iot_card_id
|
||||
AND c.real_name_status = ?
|
||||
AND c.deleted_at IS NULL
|
||||
WHERE pu.device_id = d.id
|
||||
AND pu.status = ?
|
||||
AND pu.master_usage_id IS NULL
|
||||
AND pu.deleted_at IS NULL
|
||||
)`
|
||||
args := []any{
|
||||
constants.BindStatusBound,
|
||||
constants.RealNameStatusVerified,
|
||||
constants.PackageUsageStatusActive,
|
||||
}
|
||||
if activationStatus == constants.ActivationStatusActive {
|
||||
return query.Where(activeCondition, args...)
|
||||
}
|
||||
if activationStatus == constants.ActivationStatusInactive {
|
||||
return query.Where("NOT "+activeCondition, args...)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func (s *DeviceDataSource) resolveCardGroupCount(ctx context.Context, params ExportParams) (int, error) {
|
||||
var count int64
|
||||
query := s.applyFilters(s.baseQuery(ctx), params).
|
||||
Select("COALESCE(MAX(bound_cards.card_count), 0)").
|
||||
Joins(`
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT COUNT(*) AS card_count
|
||||
FROM tb_device_sim_binding AS b
|
||||
WHERE b.device_id = d.id
|
||||
AND b.bind_status = ?
|
||||
AND b.deleted_at IS NULL
|
||||
) AS bound_cards ON TRUE
|
||||
`, constants.BindStatusBound)
|
||||
if err := query.Scan(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if count < deviceExportMinCardGroups {
|
||||
return deviceExportMinCardGroups, nil
|
||||
}
|
||||
return int(count), nil
|
||||
}
|
||||
|
||||
func (s *DeviceDataSource) fetchDevices(ctx context.Context, params ExportParams, offset, limit int) ([]deviceExportRow, error) {
|
||||
var devices []deviceExportRow
|
||||
query := s.applyFilters(s.baseQuery(ctx), params).
|
||||
Joins("LEFT JOIN tb_shop AS sh ON sh.id = d.shop_id").
|
||||
Joins("LEFT JOIN tb_package_series AS ps ON ps.id = d.series_id AND ps.deleted_at IS NULL").
|
||||
Joins(`
|
||||
LEFT JOIN tb_asset_wallet AS aw
|
||||
ON aw.resource_type = ?
|
||||
AND aw.resource_id = d.id
|
||||
AND aw.deleted_at IS NULL
|
||||
`, constants.AssetWalletResourceTypeDevice).
|
||||
Select(`
|
||||
d.id,
|
||||
d.virtual_no,
|
||||
d.imei,
|
||||
d.device_name,
|
||||
d.device_model,
|
||||
COALESCE(sh.shop_name, '') AS shop_name,
|
||||
COALESCE(ps.series_name, '') AS series_name,
|
||||
COALESCE(aw.balance, 0) AS wallet_balance
|
||||
`).
|
||||
Order("d.id ASC").
|
||||
Limit(limit).
|
||||
Offset(offset)
|
||||
if err := query.Scan(&devices).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
func (s *DeviceDataSource) fetchBoundCards(ctx context.Context, deviceIDs []uint) (map[uint][]deviceExportCardRow, error) {
|
||||
result := make(map[uint][]deviceExportCardRow, len(deviceIDs))
|
||||
if len(deviceIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var cards []deviceExportCardRow
|
||||
if err := s.db.WithContext(ctx).
|
||||
Table("tb_device_sim_binding AS b").
|
||||
Select(`
|
||||
b.device_id,
|
||||
b.slot_position,
|
||||
c.iccid,
|
||||
c.msisdn,
|
||||
c.carrier_name,
|
||||
c.current_month_usage_mb,
|
||||
c.network_status
|
||||
`).
|
||||
Joins("JOIN tb_iot_card AS c ON c.id = b.iot_card_id AND c.deleted_at IS NULL").
|
||||
Where("b.device_id IN ?", deviceIDs).
|
||||
Where("b.bind_status = ?", constants.BindStatusBound).
|
||||
Where("b.deleted_at IS NULL").
|
||||
Order("b.device_id ASC, b.slot_position ASC, b.id ASC").
|
||||
Scan(&cards).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, card := range cards {
|
||||
result[card.DeviceID] = append(result[card.DeviceID], card)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *DeviceDataSource) fetchActivePackages(ctx context.Context, deviceIDs []uint) (map[uint]deviceExportPackageRow, error) {
|
||||
result := make(map[uint]deviceExportPackageRow, len(deviceIDs))
|
||||
if len(deviceIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var packages []deviceExportPackageRow
|
||||
if err := s.db.WithContext(ctx).
|
||||
Table("tb_package_usage AS pu").
|
||||
Select(`
|
||||
pu.device_id,
|
||||
COALESCE(NULLIF(pu.package_name, ''), p.package_name, '') AS package_name,
|
||||
pu.activated_at,
|
||||
pu.expires_at
|
||||
`).
|
||||
Joins("LEFT JOIN tb_package AS p ON p.id = pu.package_id AND p.deleted_at IS NULL").
|
||||
Where("pu.device_id IN ?", deviceIDs).
|
||||
Where("pu.status = ?", constants.PackageUsageStatusActive).
|
||||
Where("pu.master_usage_id IS NULL").
|
||||
Where("pu.deleted_at IS NULL").
|
||||
Order("pu.device_id ASC, pu.priority ASC, pu.activated_at ASC, pu.id ASC").
|
||||
Scan(&packages).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, item := range packages {
|
||||
if _, exists := result[item.DeviceID]; exists {
|
||||
continue
|
||||
}
|
||||
result[item.DeviceID] = item
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type deviceExportRow struct {
|
||||
ID uint `gorm:"column:id"`
|
||||
VirtualNo string `gorm:"column:virtual_no"`
|
||||
IMEI string `gorm:"column:imei"`
|
||||
DeviceName string `gorm:"column:device_name"`
|
||||
DeviceModel string `gorm:"column:device_model"`
|
||||
ShopName string `gorm:"column:shop_name"`
|
||||
SeriesName string `gorm:"column:series_name"`
|
||||
WalletBalance int64 `gorm:"column:wallet_balance"`
|
||||
}
|
||||
|
||||
type deviceExportCardRow struct {
|
||||
DeviceID uint `gorm:"column:device_id"`
|
||||
SlotPosition int `gorm:"column:slot_position"`
|
||||
ICCID string `gorm:"column:iccid"`
|
||||
MSISDN string `gorm:"column:msisdn"`
|
||||
CarrierName string `gorm:"column:carrier_name"`
|
||||
CurrentMonthUsageMB float64 `gorm:"column:current_month_usage_mb"`
|
||||
NetworkStatus int `gorm:"column:network_status"`
|
||||
}
|
||||
|
||||
type deviceExportPackageRow struct {
|
||||
DeviceID uint `gorm:"column:device_id"`
|
||||
PackageName string `gorm:"column:package_name"`
|
||||
ActivatedAt *time.Time `gorm:"column:activated_at"`
|
||||
ExpiresAt *time.Time `gorm:"column:expires_at"`
|
||||
}
|
||||
|
||||
func buildDeviceExportHeaders(cardGroups int) []string {
|
||||
if cardGroups < deviceExportMinCardGroups {
|
||||
cardGroups = deviceExportMinCardGroups
|
||||
}
|
||||
|
||||
headers := []string{"设备号", "IMEI", "设备名称", "店铺名称", "套餐系列", "设备型号"}
|
||||
for i := 1; i <= cardGroups; i++ {
|
||||
headers = append(headers,
|
||||
fmt.Sprintf("ICCID%d", i),
|
||||
fmt.Sprintf("接入号%d", i),
|
||||
fmt.Sprintf("ICCID%d对应的运营商", i),
|
||||
fmt.Sprintf("ICCID%d使用的流量", i),
|
||||
fmt.Sprintf("卡状态%d", i),
|
||||
)
|
||||
}
|
||||
headers = append(headers,
|
||||
"设备合计",
|
||||
"套餐的生效时间",
|
||||
"套餐的到期时间",
|
||||
"当前套餐",
|
||||
"钱包余额",
|
||||
)
|
||||
return headers
|
||||
}
|
||||
|
||||
func buildDeviceExportRow(item deviceExportRow, cards []deviceExportCardRow, pkg deviceExportPackageRow, cardGroups int) []string {
|
||||
if cardGroups < deviceExportMinCardGroups {
|
||||
cardGroups = deviceExportMinCardGroups
|
||||
}
|
||||
|
||||
row := []string{
|
||||
item.VirtualNo,
|
||||
item.IMEI,
|
||||
item.DeviceName,
|
||||
item.ShopName,
|
||||
item.SeriesName,
|
||||
item.DeviceModel,
|
||||
}
|
||||
|
||||
totalUsage := 0.0
|
||||
for i := 0; i < cardGroups; i++ {
|
||||
if i >= len(cards) {
|
||||
row = append(row, "", "", "", "", "")
|
||||
continue
|
||||
}
|
||||
card := cards[i]
|
||||
totalUsage += card.CurrentMonthUsageMB
|
||||
row = append(row,
|
||||
card.ICCID,
|
||||
card.MSISDN,
|
||||
card.CarrierName,
|
||||
formatMB(card.CurrentMonthUsageMB),
|
||||
constants.GetNetworkStatusName(card.NetworkStatus),
|
||||
)
|
||||
}
|
||||
// 如果表头列数被历史任务固定得比当前绑定卡少,仍保证合计覆盖当前查到的全部卡。
|
||||
for i := cardGroups; i < len(cards); i++ {
|
||||
totalUsage += cards[i].CurrentMonthUsageMB
|
||||
}
|
||||
|
||||
row = append(row,
|
||||
formatMB(totalUsage),
|
||||
formatOptionalTime(pkg.ActivatedAt),
|
||||
formatOptionalTime(pkg.ExpiresAt),
|
||||
pkg.PackageName,
|
||||
formatMoneyYuan(item.WalletBalance),
|
||||
)
|
||||
return row
|
||||
}
|
||||
|
||||
func cardGroupCountFromHeaders(headers []string) int {
|
||||
if len(headers) < deviceExportBaseHeaderCount+deviceExportTailHeaderCount {
|
||||
return 0
|
||||
}
|
||||
cardColumnCount := len(headers) - deviceExportBaseHeaderCount - deviceExportTailHeaderCount
|
||||
if cardColumnCount <= 0 || cardColumnCount%deviceExportCardGroupSize != 0 {
|
||||
return 0
|
||||
}
|
||||
return cardColumnCount / deviceExportCardGroupSize
|
||||
}
|
||||
|
||||
func formatMB(value float64) string {
|
||||
return strconv.FormatFloat(value, 'f', 2, 64)
|
||||
}
|
||||
|
||||
func formatMoneyYuan(cents int64) string {
|
||||
sign := ""
|
||||
if cents < 0 {
|
||||
sign = "-"
|
||||
cents = -cents
|
||||
}
|
||||
return fmt.Sprintf("%s%d.%02d", sign, cents/100, cents%100)
|
||||
}
|
||||
|
||||
270
internal/exporter/filter_helpers.go
Normal file
270
internal/exporter/filter_helpers.go
Normal file
@@ -0,0 +1,270 @@
|
||||
package exporter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
const exportTimeLayout = "2006-01-02 15:04:05"
|
||||
|
||||
func applyExportShopScope(query *gorm.DB, params ExportParams, column string) *gorm.DB {
|
||||
switch params.UserType {
|
||||
case constants.UserTypeSuperAdmin, constants.UserTypePlatform:
|
||||
return query
|
||||
case constants.UserTypeAgent:
|
||||
if len(params.ScopeShopIDs) == 0 {
|
||||
return query.Where("1 = 0")
|
||||
}
|
||||
return query.Where(column+" IN ?", params.ScopeShopIDs)
|
||||
default:
|
||||
return query.Where("1 = 0")
|
||||
}
|
||||
}
|
||||
|
||||
func filterInt(filters map[string]any, key string) (int, bool) {
|
||||
value, ok := filters[key]
|
||||
if !ok || value == nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
case int:
|
||||
return v, true
|
||||
case int64:
|
||||
return int(v), true
|
||||
case uint:
|
||||
return int(v), true
|
||||
case uint64:
|
||||
return int(v), true
|
||||
case float64:
|
||||
return int(v), true
|
||||
case string:
|
||||
if strings.TrimSpace(v) == "" {
|
||||
return 0, false
|
||||
}
|
||||
parsed, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return parsed, true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
func filterUint(filters map[string]any, key string) (uint, bool) {
|
||||
value, ok := filterInt(filters, key)
|
||||
if !ok || value <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return uint(value), true
|
||||
}
|
||||
|
||||
func filterString(filters map[string]any, key string) (string, bool) {
|
||||
value, ok := filters[key]
|
||||
if !ok || value == nil {
|
||||
return "", false
|
||||
}
|
||||
text := strings.TrimSpace(fmt.Sprint(value))
|
||||
if text == "" {
|
||||
return "", false
|
||||
}
|
||||
return text, true
|
||||
}
|
||||
|
||||
func filterBool(filters map[string]any, key string) bool {
|
||||
value, ok := filters[key]
|
||||
if !ok || value == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
case bool:
|
||||
return v
|
||||
case string:
|
||||
parsed, err := strconv.ParseBool(strings.TrimSpace(v))
|
||||
return err == nil && parsed
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// filterOptionalBool 解析可选布尔筛选项。
|
||||
// 返回第二个值标识调用方是否明确传入该筛选项,避免 false 被误判为未传。
|
||||
func filterOptionalBool(filters map[string]any, key string) (bool, bool) {
|
||||
value, ok := filters[key]
|
||||
if !ok || value == nil {
|
||||
return false, false
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
case bool:
|
||||
return v, true
|
||||
case string:
|
||||
if strings.TrimSpace(v) == "" {
|
||||
return false, false
|
||||
}
|
||||
parsed, err := strconv.ParseBool(strings.TrimSpace(v))
|
||||
if err != nil {
|
||||
return false, false
|
||||
}
|
||||
return parsed, true
|
||||
case int:
|
||||
return v != 0, true
|
||||
case int64:
|
||||
return v != 0, true
|
||||
case uint:
|
||||
return v != 0, true
|
||||
case uint64:
|
||||
return v != 0, true
|
||||
case float64:
|
||||
return v != 0, true
|
||||
default:
|
||||
return false, false
|
||||
}
|
||||
}
|
||||
|
||||
// filterUintSlice 解析前端 JSON 或查询参数中的 uint 数组筛选项。
|
||||
// 空数组按未传处理;数组存在但过滤后为空时由调用方决定是否返回空结果。
|
||||
func filterUintSlice(filters map[string]any, key string) ([]uint, bool) {
|
||||
value, ok := filters[key]
|
||||
if !ok || value == nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
case []uint:
|
||||
if len(v) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
return normalizeUintSlice(v), true
|
||||
case []int:
|
||||
if len(v) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
result := make([]uint, 0, len(v))
|
||||
for _, item := range v {
|
||||
if item > 0 {
|
||||
result = append(result, uint(item))
|
||||
}
|
||||
}
|
||||
return normalizeUintSlice(result), true
|
||||
case []float64:
|
||||
if len(v) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
result := make([]uint, 0, len(v))
|
||||
for _, item := range v {
|
||||
if item > 0 {
|
||||
result = append(result, uint(item))
|
||||
}
|
||||
}
|
||||
return normalizeUintSlice(result), true
|
||||
case []any:
|
||||
if len(v) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
result := make([]uint, 0, len(v))
|
||||
for _, item := range v {
|
||||
switch typed := item.(type) {
|
||||
case int:
|
||||
if typed > 0 {
|
||||
result = append(result, uint(typed))
|
||||
}
|
||||
case int64:
|
||||
if typed > 0 {
|
||||
result = append(result, uint(typed))
|
||||
}
|
||||
case uint:
|
||||
if typed > 0 {
|
||||
result = append(result, typed)
|
||||
}
|
||||
case uint64:
|
||||
if typed > 0 {
|
||||
result = append(result, uint(typed))
|
||||
}
|
||||
case float64:
|
||||
if typed > 0 {
|
||||
result = append(result, uint(typed))
|
||||
}
|
||||
case string:
|
||||
if parsed, err := strconv.ParseUint(strings.TrimSpace(typed), 10, 64); err == nil && parsed > 0 {
|
||||
result = append(result, uint(parsed))
|
||||
}
|
||||
}
|
||||
}
|
||||
return normalizeUintSlice(result), true
|
||||
case string:
|
||||
if strings.TrimSpace(v) == "" {
|
||||
return nil, false
|
||||
}
|
||||
parts := strings.Split(v, ",")
|
||||
result := make([]uint, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
parsed, err := strconv.ParseUint(strings.TrimSpace(part), 10, 64)
|
||||
if err == nil && parsed > 0 {
|
||||
result = append(result, uint(parsed))
|
||||
}
|
||||
}
|
||||
return normalizeUintSlice(result), true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
// normalizeUintSlice 去除无效值和重复 ID,保留原始顺序。
|
||||
func normalizeUintSlice(values []uint) []uint {
|
||||
seen := make(map[uint]struct{}, len(values))
|
||||
result := make([]uint, 0, len(values))
|
||||
for _, value := range values {
|
||||
if value == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[value]; ok {
|
||||
continue
|
||||
}
|
||||
seen[value] = struct{}{}
|
||||
result = append(result, value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func filterTime(filters map[string]any, key string) (time.Time, bool) {
|
||||
text, ok := filterString(filters, key)
|
||||
if !ok {
|
||||
return time.Time{}, false
|
||||
}
|
||||
|
||||
layouts := []string{
|
||||
time.RFC3339,
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02",
|
||||
}
|
||||
for _, layout := range layouts {
|
||||
parsed, err := time.ParseInLocation(layout, text, time.Local)
|
||||
if err == nil {
|
||||
return parsed, true
|
||||
}
|
||||
}
|
||||
return time.Time{}, false
|
||||
}
|
||||
|
||||
func formatOptionalUint(value *uint) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return strconv.FormatUint(uint64(*value), 10)
|
||||
}
|
||||
|
||||
func formatOptionalTime(value *time.Time) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return value.Format(exportTimeLayout)
|
||||
}
|
||||
@@ -2,97 +2,219 @@ package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// IotCardSceneStrategy IoT 卡导出场景策略。
|
||||
type IotCardSceneStrategy struct {
|
||||
// IotCardDataSource IoT 卡导出数据源。
|
||||
type IotCardDataSource struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewIotCardSceneStrategy 创建 IoT 卡场景策略。
|
||||
func NewIotCardSceneStrategy(db *gorm.DB) *IotCardSceneStrategy {
|
||||
return &IotCardSceneStrategy{db: db}
|
||||
// NewIotCardDataSource 创建 IoT 卡导出数据源。
|
||||
func NewIotCardDataSource(db *gorm.DB) *IotCardDataSource {
|
||||
return &IotCardDataSource{db: db}
|
||||
}
|
||||
|
||||
// Scene 场景编码。
|
||||
func (s *IotCardSceneStrategy) Scene() string {
|
||||
// Scene 返回导出场景编码。
|
||||
func (s *IotCardDataSource) Scene() string {
|
||||
return constants.ExportTaskSceneIotCard
|
||||
}
|
||||
|
||||
// Headers 导出表头。
|
||||
func (s *IotCardSceneStrategy) Headers() []string {
|
||||
return []string{"ID", "ICCID", "MSISDN", "虚拟号", "运营商名称", "状态", "店铺ID", "创建时间"}
|
||||
// Count 统计 IoT 卡导出行数。
|
||||
func (s *IotCardDataSource) Count(ctx context.Context, params ExportParams) (int, error) {
|
||||
var total int64
|
||||
query := s.applyFilters(ctx, s.baseQuery(ctx), params)
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(total), nil
|
||||
}
|
||||
|
||||
// NextShardBoundary 获取下一段分片边界。
|
||||
func (s *IotCardSceneStrategy) NextShardBoundary(ctx context.Context, task *model.ExportTask, afterID uint64, limit int) (*ShardBoundary, error) {
|
||||
// Headers 返回 IoT 卡导出表头。
|
||||
func (s *IotCardDataSource) Headers(ctx context.Context, params ExportParams) ([]string, error) {
|
||||
return []string{"ICCID", "MSISDN", "绑定设备虚拟号", "运营商", "店铺名称", "绑定设备名称", "是否实名", "实名时间", "网络状态"}, nil
|
||||
}
|
||||
|
||||
// Fetch 按 offset/limit 查询 IoT 卡导出数据。
|
||||
func (s *IotCardDataSource) Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error) {
|
||||
if limit <= 0 {
|
||||
limit = constants.ExportDefaultShardSize
|
||||
}
|
||||
|
||||
var ids []uint64
|
||||
query := s.db.WithContext(ctx).
|
||||
Model(&model.IotCard{}).
|
||||
Where("id > ?", afterID).
|
||||
Order("id ASC").
|
||||
Limit(limit)
|
||||
query = applyShopScopeForTask(query, task)
|
||||
|
||||
if err := query.Pluck("id", &ids).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &ShardBoundary{
|
||||
StartID: afterID,
|
||||
EndID: ids[len(ids)-1],
|
||||
RowCount: len(ids),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// QueryRows 查询分片内数据行。
|
||||
func (s *IotCardSceneStrategy) QueryRows(ctx context.Context, task *model.ExportTask, startID, endID uint64) ([][]string, error) {
|
||||
if endID == 0 {
|
||||
return [][]string{}, nil
|
||||
}
|
||||
|
||||
var cards []model.IotCard
|
||||
query := s.db.WithContext(ctx).
|
||||
Model(&model.IotCard{}).
|
||||
Where("id > ? AND id <= ?", startID, endID).
|
||||
Order("id ASC")
|
||||
query = applyShopScopeForTask(query, task)
|
||||
|
||||
if err := query.Find(&cards).Error; err != nil {
|
||||
var items []iotCardExportRow
|
||||
query := s.applyFilters(ctx, s.baseQuery(ctx), params).
|
||||
Select(`
|
||||
c.iccid,
|
||||
c.msisdn,
|
||||
c.device_virtual_no,
|
||||
c.carrier_name,
|
||||
COALESCE(sh.shop_name, '') AS shop_name,
|
||||
COALESCE(d.device_name, '') AS device_name,
|
||||
c.real_name_status,
|
||||
c.first_realname_at,
|
||||
c.network_status
|
||||
`).
|
||||
Order("c.id ASC").
|
||||
Limit(limit).
|
||||
Offset(offset)
|
||||
if err := query.Scan(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows := make([][]string, 0, len(cards))
|
||||
for _, item := range cards {
|
||||
shopID := ""
|
||||
if item.ShopID != nil {
|
||||
shopID = strconv.FormatUint(uint64(*item.ShopID), 10)
|
||||
}
|
||||
|
||||
rows := make([][]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
rows = append(rows, []string{
|
||||
strconv.FormatUint(uint64(item.ID), 10),
|
||||
item.ICCID,
|
||||
item.MSISDN,
|
||||
item.VirtualNo,
|
||||
item.DeviceVirtualNo,
|
||||
item.CarrierName,
|
||||
strconv.Itoa(item.Status),
|
||||
shopID,
|
||||
item.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
item.ShopName,
|
||||
item.DeviceName,
|
||||
formatRealNameVerified(item.RealNameStatus),
|
||||
formatOptionalTime(item.FirstRealnameAt),
|
||||
constants.GetNetworkStatusName(item.NetworkStatus),
|
||||
})
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// baseQuery 构造卡导出的基础查询。
|
||||
// 绑定设备名称从当前有效绑定中取一条,避免多绑定历史记录放大导出行数。
|
||||
func (s *IotCardDataSource) baseQuery(ctx context.Context) *gorm.DB {
|
||||
return s.db.WithContext(ctx).
|
||||
Table("tb_iot_card AS c").
|
||||
Joins("LEFT JOIN tb_shop AS sh ON sh.id = c.shop_id").
|
||||
Joins(`
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT d.device_name
|
||||
FROM tb_device_sim_binding AS b
|
||||
JOIN tb_device AS d ON d.id = b.device_id AND d.deleted_at IS NULL
|
||||
WHERE b.iot_card_id = c.id
|
||||
AND b.bind_status = ?
|
||||
AND b.deleted_at IS NULL
|
||||
ORDER BY b.is_current DESC, b.id DESC
|
||||
LIMIT 1
|
||||
) AS d ON TRUE
|
||||
`, constants.BindStatusBound).
|
||||
Where("c.deleted_at IS NULL")
|
||||
}
|
||||
|
||||
// applyFilters 应用与 /api/admin/iot-cards/standalone 一致的筛选条件。
|
||||
// 导出任务运行在 Worker 中,权限只能使用任务创建时保存的店铺范围快照。
|
||||
func (s *IotCardDataSource) applyFilters(ctx context.Context, query *gorm.DB, params ExportParams) *gorm.DB {
|
||||
query = applyExportShopScope(query, params, "c.shop_id")
|
||||
|
||||
if isStandalone, ok := filterOptionalBool(params.Filters, "is_standalone"); ok {
|
||||
query = query.Where("c.is_standalone = ?", isStandalone)
|
||||
}
|
||||
if status, ok := filterInt(params.Filters, "status"); ok && status > 0 {
|
||||
query = query.Where("c.status = ?", status)
|
||||
}
|
||||
if carrierID, ok := filterUint(params.Filters, "carrier_id"); ok {
|
||||
query = query.Where("c.carrier_id = ?", carrierID)
|
||||
}
|
||||
if shopIDs, ok := filterUintSlice(params.Filters, "shop_ids"); ok {
|
||||
if len(shopIDs) == 0 {
|
||||
query = query.Where("1 = 0")
|
||||
} else {
|
||||
query = query.Where("c.shop_id IN ?", shopIDs)
|
||||
}
|
||||
} else if shopID, ok := filterInt(params.Filters, "shop_id"); ok {
|
||||
if shopID <= 0 {
|
||||
query = query.Where("1 = 0")
|
||||
} else {
|
||||
query = query.Where("c.shop_id = ?", shopID)
|
||||
}
|
||||
}
|
||||
if iccid, ok := filterString(params.Filters, "iccid"); ok {
|
||||
query = query.Where("c.iccid LIKE ?", "%"+iccid+"%")
|
||||
}
|
||||
if msisdn, ok := filterString(params.Filters, "msisdn"); ok {
|
||||
query = query.Where("c.msisdn LIKE ?", "%"+msisdn+"%")
|
||||
}
|
||||
if virtualNo, ok := filterString(params.Filters, "virtual_no"); ok {
|
||||
query = query.Where("c.virtual_no LIKE ?", "%"+virtualNo+"%")
|
||||
}
|
||||
if keyword, ok := filterString(params.Filters, "keyword"); ok {
|
||||
query = query.Where("c.iccid LIKE ? OR c.virtual_no LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
if batchNo, ok := filterString(params.Filters, "batch_no"); ok {
|
||||
query = query.Where("c.batch_no = ?", batchNo)
|
||||
}
|
||||
if packageID, ok := filterUint(params.Filters, "package_id"); ok {
|
||||
query = query.Where("c.id IN (?)",
|
||||
s.db.WithContext(ctx).Table("tb_package_usage").
|
||||
Select("iot_card_id").
|
||||
Where("package_id = ? AND deleted_at IS NULL", packageID))
|
||||
}
|
||||
if isDistributed, ok := filterOptionalBool(params.Filters, "is_distributed"); ok {
|
||||
if isDistributed {
|
||||
query = query.Where("c.shop_id IS NOT NULL")
|
||||
} else {
|
||||
query = query.Where("c.shop_id IS NULL")
|
||||
}
|
||||
}
|
||||
if iccidStart, ok := filterString(params.Filters, "iccid_start"); ok {
|
||||
query = query.Where("c.iccid >= ?", iccidStart)
|
||||
}
|
||||
if iccidEnd, ok := filterString(params.Filters, "iccid_end"); ok {
|
||||
query = query.Where("c.iccid <= ?", iccidEnd)
|
||||
}
|
||||
if isReplaced, ok := filterOptionalBool(params.Filters, "is_replaced"); ok {
|
||||
subQuery := s.db.WithContext(ctx).Table("tb_exchange_order").
|
||||
Select("old_asset_id").
|
||||
Where("old_asset_type = ? AND status IN ? AND deleted_at IS NULL",
|
||||
constants.ExchangeAssetTypeIotCard,
|
||||
[]int{constants.ExchangeStatusShipped, constants.ExchangeStatusCompleted},
|
||||
)
|
||||
if isReplaced {
|
||||
query = query.Where("c.id IN (?)", subQuery)
|
||||
} else {
|
||||
query = query.Where("c.id NOT IN (?)", subQuery)
|
||||
}
|
||||
}
|
||||
if seriesID, ok := filterUint(params.Filters, "series_id"); ok {
|
||||
query = query.Where("c.series_id = ?", seriesID)
|
||||
}
|
||||
if carrierName, ok := filterString(params.Filters, "carrier_name"); ok {
|
||||
query = query.Where("c.carrier_name LIKE ?", "%"+carrierName+"%")
|
||||
}
|
||||
if hasActivePackage, ok := filterOptionalBool(params.Filters, "has_active_package"); ok {
|
||||
subQuery := s.db.WithContext(ctx).Table("tb_package_usage AS pu").
|
||||
Select("1").
|
||||
Where("pu.iot_card_id = c.id AND pu.status = ? AND pu.master_usage_id IS NULL AND pu.deleted_at IS NULL", constants.PackageUsageStatusActive)
|
||||
if hasActivePackage {
|
||||
query = query.Where("EXISTS (?)", subQuery)
|
||||
} else {
|
||||
query = query.Where("NOT EXISTS (?)", subQuery)
|
||||
}
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
type iotCardExportRow struct {
|
||||
ICCID string `gorm:"column:iccid"`
|
||||
MSISDN string `gorm:"column:msisdn"`
|
||||
DeviceVirtualNo string `gorm:"column:device_virtual_no"`
|
||||
CarrierName string `gorm:"column:carrier_name"`
|
||||
ShopName string `gorm:"column:shop_name"`
|
||||
DeviceName string `gorm:"column:device_name"`
|
||||
RealNameStatus int `gorm:"column:real_name_status"`
|
||||
FirstRealnameAt *time.Time `gorm:"column:first_realname_at"`
|
||||
NetworkStatus int `gorm:"column:network_status"`
|
||||
}
|
||||
|
||||
func formatRealNameVerified(status int) string {
|
||||
switch status {
|
||||
case constants.RealNameStatusVerified:
|
||||
return "是"
|
||||
case constants.RealNameStatusNotVerified:
|
||||
return "否"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
599
internal/exporter/order_scene.go
Normal file
599
internal/exporter/order_scene.go
Normal file
@@ -0,0 +1,599 @@
|
||||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
const (
|
||||
orderExportBaseHeaderCount = 11
|
||||
orderExportCardGroupSize = 2
|
||||
orderExportMinCardGroups = 1
|
||||
)
|
||||
|
||||
// OrderDataSource 订单导出数据源。
|
||||
type OrderDataSource struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewOrderDataSource 创建订单导出数据源。
|
||||
func NewOrderDataSource(db *gorm.DB) *OrderDataSource {
|
||||
return &OrderDataSource{db: db}
|
||||
}
|
||||
|
||||
// Scene 返回导出场景编码。
|
||||
func (s *OrderDataSource) Scene() string {
|
||||
return constants.ExportTaskSceneOrder
|
||||
}
|
||||
|
||||
// Count 统计订单导出行数。
|
||||
func (s *OrderDataSource) Count(ctx context.Context, params ExportParams) (int, error) {
|
||||
query, err := s.applyFilters(ctx, s.baseQuery(ctx), params)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(total), nil
|
||||
}
|
||||
|
||||
// Headers 返回订单导出表头。
|
||||
func (s *OrderDataSource) Headers(ctx context.Context, params ExportParams) ([]string, error) {
|
||||
cardGroups, err := s.resolveCardGroupCount(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buildOrderExportHeaders(cardGroups), nil
|
||||
}
|
||||
|
||||
// Fetch 按 offset/limit 查询订单导出数据。
|
||||
func (s *OrderDataSource) Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error) {
|
||||
if limit <= 0 {
|
||||
return [][]string{}, nil
|
||||
}
|
||||
|
||||
cardGroups := orderCardGroupCountFromHeaders(params.ResolvedHeaders)
|
||||
if cardGroups <= 0 {
|
||||
resolved, err := s.resolveCardGroupCount(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cardGroups = resolved
|
||||
}
|
||||
|
||||
orders, err := s.fetchOrders(ctx, params, offset, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(orders) == 0 {
|
||||
return [][]string{}, nil
|
||||
}
|
||||
|
||||
orderIDs := make([]uint, 0, len(orders))
|
||||
deviceIDs := make([]uint, 0, len(orders))
|
||||
for _, item := range orders {
|
||||
orderIDs = append(orderIDs, item.ID)
|
||||
if item.DeviceID != nil {
|
||||
deviceIDs = append(deviceIDs, *item.DeviceID)
|
||||
}
|
||||
}
|
||||
|
||||
itemMap, err := s.fetchOrderItems(ctx, orderIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cardMap, err := s.fetchBoundCards(ctx, deviceIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paymentMap, err := s.fetchPreferredPayments(ctx, orderIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows := make([][]string, 0, len(orders))
|
||||
for _, item := range orders {
|
||||
var cards []orderExportCardRow
|
||||
if item.DeviceID != nil {
|
||||
cards = cardMap[*item.DeviceID]
|
||||
}
|
||||
rows = append(rows, buildOrderExportRow(item, itemMap[item.ID], paymentMap[item.ID], cards, cardGroups))
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) baseQuery(ctx context.Context) *gorm.DB {
|
||||
return s.db.WithContext(ctx).
|
||||
Table("tb_order AS o").
|
||||
Where("o.deleted_at IS NULL")
|
||||
}
|
||||
|
||||
// applyFilters 应用与 /api/admin/orders 当前实际行为一致的筛选条件。
|
||||
// is_expired 在接口 Service 中未进入 Store 筛选,这里按当前接口行为忽略。
|
||||
func (s *OrderDataSource) applyFilters(ctx context.Context, query *gorm.DB, params ExportParams) (*gorm.DB, error) {
|
||||
query = s.applyOrderScope(query, params)
|
||||
|
||||
if params.UserType == constants.UserTypeAgent {
|
||||
if params.CreatorShopID != nil {
|
||||
query = query.Where("o.buyer_type = ? AND o.buyer_id = ?", model.BuyerTypeAgent, *params.CreatorShopID)
|
||||
} else {
|
||||
query = query.Where("o.buyer_type = ?", model.BuyerTypeAgent)
|
||||
}
|
||||
}
|
||||
if paymentStatus, ok := filterInt(params.Filters, "payment_status"); ok {
|
||||
query = query.Where("o.payment_status = ?", paymentStatus)
|
||||
}
|
||||
if paymentMethod, ok := filterString(params.Filters, "payment_method"); ok {
|
||||
query = query.Where("o.payment_method = ?", paymentMethod)
|
||||
}
|
||||
if orderType, ok := filterString(params.Filters, "order_type"); ok {
|
||||
query = query.Where("o.order_type = ?", orderType)
|
||||
}
|
||||
if orderNo, ok := filterString(params.Filters, "order_no"); ok {
|
||||
query = query.Where("o.order_no = ?", orderNo)
|
||||
}
|
||||
if purchaseRole, ok := filterString(params.Filters, "purchase_role"); ok {
|
||||
query = query.Where("o.purchase_role = ?", purchaseRole)
|
||||
}
|
||||
if sellerShopID, ok := filterUint(params.Filters, "seller_shop_id"); ok {
|
||||
query = query.Where("o.seller_shop_id = ?", sellerShopID)
|
||||
}
|
||||
if start, ok := filterTime(params.Filters, "start_time"); ok {
|
||||
query = query.Where("o.created_at >= ?", start)
|
||||
}
|
||||
if end, ok := filterTime(params.Filters, "end_time"); ok {
|
||||
query = query.Where("o.created_at <= ?", end)
|
||||
}
|
||||
if buyerPhone, ok := filterString(params.Filters, "buyer_phone"); ok {
|
||||
query = query.Where("o.buyer_phone = ?", buyerPhone)
|
||||
}
|
||||
if identifier, ok := filterString(params.Filters, "identifier"); ok {
|
||||
iotCardID, deviceID, err := s.resolveOrderListAssetIdentifier(ctx, params, identifier)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
assetQuery := s.db.Where("o.asset_identifier = ?", identifier)
|
||||
if iotCardID != nil {
|
||||
assetQuery = assetQuery.Or("o.iot_card_id = ?", *iotCardID)
|
||||
}
|
||||
if deviceID != nil {
|
||||
assetQuery = assetQuery.Or("o.device_id = ?", *deviceID)
|
||||
}
|
||||
query = query.Where(assetQuery)
|
||||
}
|
||||
return query, nil
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) applyOrderScope(query *gorm.DB, params ExportParams) *gorm.DB {
|
||||
switch params.UserType {
|
||||
case constants.UserTypeSuperAdmin, constants.UserTypePlatform:
|
||||
return query
|
||||
case constants.UserTypeAgent:
|
||||
if len(params.ScopeShopIDs) == 0 {
|
||||
return query.Where("1 = 0")
|
||||
}
|
||||
return query.Where(
|
||||
s.db.Where("o.buyer_type = ? AND o.buyer_id IN ?", model.BuyerTypeAgent, params.ScopeShopIDs).
|
||||
Or("o.seller_shop_id IN ?", params.ScopeShopIDs).
|
||||
Or(s.db.Where("o.seller_shop_id IS NULL AND o.operator_type = ? AND o.operator_id IN ?", model.OperatorAccountTypeAgent, params.ScopeShopIDs)),
|
||||
)
|
||||
default:
|
||||
return query.Where("1 = 0")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) resolveCardGroupCount(ctx context.Context, params ExportParams) (int, error) {
|
||||
filtered, err := s.applyFilters(ctx, s.baseQuery(ctx), params)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var count int64
|
||||
query := filtered.
|
||||
Select("COALESCE(MAX(bound_cards.card_count), 0)").
|
||||
Joins(`
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT COUNT(*) AS card_count
|
||||
FROM tb_device_sim_binding AS b
|
||||
WHERE b.device_id = o.device_id
|
||||
AND b.bind_status = ?
|
||||
AND b.deleted_at IS NULL
|
||||
) AS bound_cards ON TRUE
|
||||
`, constants.BindStatusBound)
|
||||
if err := query.Scan(&count).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if count < orderExportMinCardGroups {
|
||||
return orderExportMinCardGroups, nil
|
||||
}
|
||||
return int(count), nil
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) fetchOrders(ctx context.Context, params ExportParams, offset, limit int) ([]orderExportRow, error) {
|
||||
var orders []orderExportRow
|
||||
filtered, err := s.applyFilters(ctx, s.baseQuery(ctx), params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := filtered.
|
||||
Joins("LEFT JOIN tb_shop AS sh ON sh.id = o.seller_shop_id").
|
||||
Select(`
|
||||
o.id,
|
||||
o.order_no,
|
||||
o.asset_identifier,
|
||||
o.seller_cost_price,
|
||||
o.total_amount,
|
||||
o.actual_paid_amount,
|
||||
o.payment_status,
|
||||
o.payment_method,
|
||||
o.created_at,
|
||||
o.device_id,
|
||||
COALESCE(sh.shop_name, '') AS shop_name
|
||||
`).
|
||||
Order("o.id DESC").
|
||||
Limit(limit).
|
||||
Offset(offset)
|
||||
if err := query.Scan(&orders).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) fetchOrderItems(ctx context.Context, orderIDs []uint) (map[uint][]orderExportItemRow, error) {
|
||||
result := make(map[uint][]orderExportItemRow, len(orderIDs))
|
||||
if len(orderIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var items []orderExportItemRow
|
||||
if err := s.db.WithContext(ctx).
|
||||
Table("tb_order_item").
|
||||
Select("order_id, package_name, quantity").
|
||||
Where("order_id IN ?", orderIDs).
|
||||
Where("deleted_at IS NULL").
|
||||
Order("order_id ASC, id ASC").
|
||||
Scan(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
result[item.OrderID] = append(result[item.OrderID], item)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) fetchBoundCards(ctx context.Context, deviceIDs []uint) (map[uint][]orderExportCardRow, error) {
|
||||
result := make(map[uint][]orderExportCardRow, len(deviceIDs))
|
||||
deviceIDs = normalizeUintSlice(deviceIDs)
|
||||
if len(deviceIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var cards []orderExportCardRow
|
||||
if err := s.db.WithContext(ctx).
|
||||
Table("tb_device_sim_binding AS b").
|
||||
Select(`
|
||||
b.device_id,
|
||||
b.slot_position,
|
||||
c.msisdn,
|
||||
c.iccid
|
||||
`).
|
||||
Joins("JOIN tb_iot_card AS c ON c.id = b.iot_card_id AND c.deleted_at IS NULL").
|
||||
Where("b.device_id IN ?", deviceIDs).
|
||||
Where("b.bind_status = ?", constants.BindStatusBound).
|
||||
Where("b.deleted_at IS NULL").
|
||||
Order("b.device_id ASC, b.slot_position ASC, b.id ASC").
|
||||
Scan(&cards).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, card := range cards {
|
||||
result[card.DeviceID] = append(result[card.DeviceID], card)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) fetchPreferredPayments(ctx context.Context, orderIDs []uint) (map[uint]orderExportPaymentRow, error) {
|
||||
result := make(map[uint]orderExportPaymentRow, len(orderIDs))
|
||||
if len(orderIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
var payments []orderExportPaymentRow
|
||||
if err := s.db.WithContext(ctx).
|
||||
Table("tb_payment").
|
||||
Select("order_id, third_party_trade_no, status").
|
||||
Where("order_id IN ?", orderIDs).
|
||||
Where("order_type = ?", model.PaymentOrderTypePackage).
|
||||
Where("deleted_at IS NULL").
|
||||
Order("order_id ASC, CASE WHEN status = 1 THEN 0 ELSE 1 END ASC, created_at DESC, id DESC").
|
||||
Scan(&payments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, payment := range payments {
|
||||
if _, exists := result[payment.OrderID]; exists {
|
||||
continue
|
||||
}
|
||||
result[payment.OrderID] = payment
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) resolveOrderListAssetIdentifier(ctx context.Context, params ExportParams, identifier string) (*uint, *uint, error) {
|
||||
if identifier == "" {
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
var registry orderExportAssetIdentifierRow
|
||||
registryErr := s.db.WithContext(ctx).
|
||||
Table("tb_asset_identifier").
|
||||
Select("asset_type, asset_id").
|
||||
Where("identifier = ?", identifier).
|
||||
First(®istry).Error
|
||||
if registryErr != nil && registryErr != gorm.ErrRecordNotFound {
|
||||
return nil, nil, registryErr
|
||||
}
|
||||
if registryErr == nil {
|
||||
switch registry.AssetType {
|
||||
case model.AssetTypeIotCard:
|
||||
if s.assetVisible(ctx, params, "tb_iot_card", registry.AssetID) {
|
||||
cardID := registry.AssetID
|
||||
return &cardID, nil, nil
|
||||
}
|
||||
case model.AssetTypeDevice:
|
||||
if s.assetVisible(ctx, params, "tb_device", registry.AssetID) {
|
||||
deviceID := registry.AssetID
|
||||
return nil, &deviceID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if deviceID, err := s.resolveDeviceIDByIdentifier(ctx, params, identifier); err != nil {
|
||||
return nil, nil, err
|
||||
} else if deviceID != nil {
|
||||
return nil, deviceID, nil
|
||||
}
|
||||
|
||||
cardID, err := s.resolveCardIDByIdentifier(ctx, params, identifier)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return cardID, nil, nil
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) assetVisible(ctx context.Context, params ExportParams, table string, id uint) bool {
|
||||
var count int64
|
||||
query := s.db.WithContext(ctx).Table(table).Where("id = ? AND deleted_at IS NULL", id)
|
||||
if params.UserType == constants.UserTypeAgent {
|
||||
if len(params.ScopeShopIDs) == 0 {
|
||||
return false
|
||||
}
|
||||
query = query.Where("shop_id IN ?", params.ScopeShopIDs)
|
||||
}
|
||||
return query.Count(&count).Error == nil && count > 0
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) resolveDeviceIDByIdentifier(ctx context.Context, params ExportParams, identifier string) (*uint, error) {
|
||||
var row struct {
|
||||
ID uint `gorm:"column:id"`
|
||||
}
|
||||
query := s.db.WithContext(ctx).
|
||||
Table("tb_device").
|
||||
Select("id").
|
||||
Where("deleted_at IS NULL").
|
||||
Where("virtual_no = ? OR imei = ? OR sn = ?", identifier, identifier, identifier)
|
||||
if params.UserType == constants.UserTypeAgent {
|
||||
if len(params.ScopeShopIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
query = query.Where("shop_id IN ?", params.ScopeShopIDs)
|
||||
}
|
||||
err := query.First(&row).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &row.ID, nil
|
||||
}
|
||||
|
||||
func (s *OrderDataSource) resolveCardIDByIdentifier(ctx context.Context, params ExportParams, identifier string) (*uint, error) {
|
||||
conditions := []string{"virtual_no = ?", "msisdn = ?", "iccid = ?"}
|
||||
args := []any{identifier, identifier, identifier}
|
||||
if len(identifier) == 19 {
|
||||
conditions = append(conditions, "iccid_19 = ?")
|
||||
args = append(args, identifier)
|
||||
} else if len(identifier) == 20 {
|
||||
conditions = append(conditions, "iccid_20 = ?")
|
||||
args = append(args, identifier)
|
||||
}
|
||||
|
||||
var row struct {
|
||||
ID uint `gorm:"column:id"`
|
||||
}
|
||||
query := s.db.WithContext(ctx).
|
||||
Table("tb_iot_card").
|
||||
Select("id").
|
||||
Where("deleted_at IS NULL").
|
||||
Where(strings.Join(conditions, " OR "), args...)
|
||||
if params.UserType == constants.UserTypeAgent {
|
||||
if len(params.ScopeShopIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
query = query.Where("shop_id IN ?", params.ScopeShopIDs)
|
||||
}
|
||||
err := query.First(&row).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &row.ID, nil
|
||||
}
|
||||
|
||||
type orderExportRow struct {
|
||||
ID uint `gorm:"column:id"`
|
||||
OrderNo string `gorm:"column:order_no"`
|
||||
ShopName string `gorm:"column:shop_name"`
|
||||
AssetIdentifier string `gorm:"column:asset_identifier"`
|
||||
SellerCostPrice int64 `gorm:"column:seller_cost_price"`
|
||||
TotalAmount int64 `gorm:"column:total_amount"`
|
||||
ActualPaidAmount *int64 `gorm:"column:actual_paid_amount"`
|
||||
PaymentStatus int `gorm:"column:payment_status"`
|
||||
PaymentMethod string `gorm:"column:payment_method"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
DeviceID *uint `gorm:"column:device_id"`
|
||||
}
|
||||
|
||||
type orderExportItemRow struct {
|
||||
OrderID uint `gorm:"column:order_id"`
|
||||
PackageName string `gorm:"column:package_name"`
|
||||
Quantity int `gorm:"column:quantity"`
|
||||
}
|
||||
|
||||
type orderExportPaymentRow struct {
|
||||
OrderID uint `gorm:"column:order_id"`
|
||||
ThirdPartyTradeNo string `gorm:"column:third_party_trade_no"`
|
||||
Status int `gorm:"column:status"`
|
||||
}
|
||||
|
||||
type orderExportCardRow struct {
|
||||
DeviceID uint `gorm:"column:device_id"`
|
||||
SlotPosition int `gorm:"column:slot_position"`
|
||||
MSISDN string `gorm:"column:msisdn"`
|
||||
ICCID string `gorm:"column:iccid"`
|
||||
}
|
||||
|
||||
type orderExportAssetIdentifierRow struct {
|
||||
AssetType string `gorm:"column:asset_type"`
|
||||
AssetID uint `gorm:"column:asset_id"`
|
||||
}
|
||||
|
||||
func buildOrderExportHeaders(cardGroups int) []string {
|
||||
if cardGroups < orderExportMinCardGroups {
|
||||
cardGroups = orderExportMinCardGroups
|
||||
}
|
||||
|
||||
headers := []string{
|
||||
"订单号",
|
||||
"店铺",
|
||||
"虚拟号",
|
||||
"套餐名称",
|
||||
"成本价",
|
||||
"下单金额",
|
||||
"支付状态",
|
||||
"支付方式",
|
||||
"数量",
|
||||
"下单时间",
|
||||
"第三方单号",
|
||||
}
|
||||
for i := 1; i <= cardGroups; i++ {
|
||||
headers = append(headers,
|
||||
fmt.Sprintf("接入号%d", i),
|
||||
fmt.Sprintf("ICCID%d", i),
|
||||
)
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
func buildOrderExportRow(item orderExportRow, items []orderExportItemRow, payment orderExportPaymentRow, cards []orderExportCardRow, cardGroups int) []string {
|
||||
if cardGroups < orderExportMinCardGroups {
|
||||
cardGroups = orderExportMinCardGroups
|
||||
}
|
||||
|
||||
row := []string{
|
||||
item.OrderNo,
|
||||
item.ShopName,
|
||||
item.AssetIdentifier,
|
||||
joinOrderPackageNames(items),
|
||||
formatMoneyYuan(item.SellerCostPrice),
|
||||
formatMoneyYuan(orderActualPaidAmount(item)),
|
||||
constants.GetOrderPaymentStatusName(item.PaymentStatus),
|
||||
formatOrderPaymentMethod(item.PaymentMethod),
|
||||
formatOrderQuantity(items),
|
||||
item.CreatedAt.Format(exportTimeLayout),
|
||||
payment.ThirdPartyTradeNo,
|
||||
}
|
||||
for i := 0; i < cardGroups; i++ {
|
||||
if i >= len(cards) {
|
||||
row = append(row, "", "")
|
||||
continue
|
||||
}
|
||||
row = append(row, cards[i].MSISDN, cards[i].ICCID)
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
func orderCardGroupCountFromHeaders(headers []string) int {
|
||||
if len(headers) < orderExportBaseHeaderCount {
|
||||
return 0
|
||||
}
|
||||
cardColumnCount := len(headers) - orderExportBaseHeaderCount
|
||||
if cardColumnCount <= 0 || cardColumnCount%orderExportCardGroupSize != 0 {
|
||||
return 0
|
||||
}
|
||||
return cardColumnCount / orderExportCardGroupSize
|
||||
}
|
||||
|
||||
func joinOrderPackageNames(items []orderExportItemRow) string {
|
||||
if len(items) == 0 {
|
||||
return ""
|
||||
}
|
||||
names := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
if item.PackageName != "" {
|
||||
names = append(names, item.PackageName)
|
||||
}
|
||||
}
|
||||
return strings.Join(names, ",")
|
||||
}
|
||||
|
||||
func formatOrderQuantity(items []orderExportItemRow) string {
|
||||
if len(items) == 0 {
|
||||
return "0"
|
||||
}
|
||||
total := 0
|
||||
for _, item := range items {
|
||||
total += item.Quantity
|
||||
}
|
||||
return fmt.Sprintf("%d", total)
|
||||
}
|
||||
|
||||
func orderActualPaidAmount(item orderExportRow) int64 {
|
||||
if item.ActualPaidAmount != nil {
|
||||
return *item.ActualPaidAmount
|
||||
}
|
||||
if item.PaymentStatus == model.PaymentStatusPaid {
|
||||
return item.TotalAmount
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func formatOrderPaymentMethod(method string) string {
|
||||
switch method {
|
||||
case model.PaymentMethodWallet:
|
||||
return "钱包支付"
|
||||
case model.PaymentMethodWechat:
|
||||
return "微信支付"
|
||||
case model.PaymentMethodAlipay:
|
||||
return "支付宝支付"
|
||||
case model.PaymentMethodOffline:
|
||||
return "线下支付"
|
||||
default:
|
||||
return method
|
||||
}
|
||||
}
|
||||
62
internal/exporter/query_params.go
Normal file
62
internal/exporter/query_params.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package exporter
|
||||
|
||||
import (
|
||||
"github.com/bytedance/sonic"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
)
|
||||
|
||||
// ParseExportParams 从导出任务快照解析数据源执行参数。
|
||||
func ParseExportParams(task *model.ExportTask) ExportParams {
|
||||
params := ExportParams{Filters: make(map[string]any)}
|
||||
if task == nil {
|
||||
return params
|
||||
}
|
||||
|
||||
params.ScopeShopIDs = append(params.ScopeShopIDs, task.ScopeShopIDs...)
|
||||
params.UserType = task.CreatorUserType
|
||||
params.CreatorShopID = task.CreatorShopID
|
||||
|
||||
var raw map[string]any
|
||||
if len(task.QueryJSON) == 0 {
|
||||
return params
|
||||
}
|
||||
if err := sonic.Unmarshal(task.QueryJSON, &raw); err != nil {
|
||||
return params
|
||||
}
|
||||
|
||||
if filters, ok := raw["filters"].(map[string]any); ok {
|
||||
params.Filters = filters
|
||||
}
|
||||
params.ResolvedHeaders = headersFromRawQuery(raw)
|
||||
return params
|
||||
}
|
||||
|
||||
// ResolvedHeadersFromTask 从任务 query_json 读取 dispatch 阶段固定的表头。
|
||||
func ResolvedHeadersFromTask(task *model.ExportTask) []string {
|
||||
if task == nil || len(task.QueryJSON) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var raw map[string]any
|
||||
if err := sonic.Unmarshal(task.QueryJSON, &raw); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return headersFromRawQuery(raw)
|
||||
}
|
||||
|
||||
func headersFromRawQuery(raw map[string]any) []string {
|
||||
values, ok := raw["resolved_headers"].([]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
headers := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
if text, ok := value.(string); ok {
|
||||
headers = append(headers, text)
|
||||
}
|
||||
}
|
||||
return headers
|
||||
}
|
||||
@@ -1,71 +1,55 @@
|
||||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// ShardBoundary 分片边界信息。
|
||||
type ShardBoundary struct {
|
||||
StartID uint64
|
||||
EndID uint64
|
||||
RowCount int
|
||||
}
|
||||
|
||||
// SceneStrategy 导出场景策略接口。
|
||||
type SceneStrategy interface {
|
||||
Scene() string
|
||||
Headers() []string
|
||||
NextShardBoundary(ctx context.Context, task *model.ExportTask, afterID uint64, limit int) (*ShardBoundary, error)
|
||||
QueryRows(ctx context.Context, task *model.ExportTask, startID, endID uint64) ([][]string, error)
|
||||
}
|
||||
|
||||
// Registry 场景策略注册中心。
|
||||
// Registry 导出数据源注册中心。
|
||||
type Registry struct {
|
||||
strategies map[string]SceneStrategy
|
||||
sources map[string]DataSource
|
||||
}
|
||||
|
||||
// NewRegistry 创建场景策略注册中心。
|
||||
func NewRegistry(strategies ...SceneStrategy) *Registry {
|
||||
m := make(map[string]SceneStrategy, len(strategies))
|
||||
for _, strategy := range strategies {
|
||||
if strategy == nil {
|
||||
// NewRegistry 创建导出数据源注册中心。
|
||||
func NewRegistry(sources ...DataSource) *Registry {
|
||||
m := make(map[string]DataSource, len(sources))
|
||||
for _, source := range sources {
|
||||
if source == nil {
|
||||
continue
|
||||
}
|
||||
m[strategy.Scene()] = strategy
|
||||
m[source.Scene()] = source
|
||||
}
|
||||
return &Registry{strategies: m}
|
||||
return &Registry{sources: m}
|
||||
}
|
||||
|
||||
// NewDefaultRegistry 创建默认场景策略注册中心。
|
||||
// NewDefaultRegistry 创建默认导出数据源注册中心。
|
||||
func NewDefaultRegistry(db *gorm.DB) *Registry {
|
||||
return NewRegistry(
|
||||
NewDeviceSceneStrategy(db),
|
||||
NewIotCardSceneStrategy(db),
|
||||
NewDeviceDataSource(db),
|
||||
NewIotCardDataSource(db),
|
||||
NewOrderDataSource(db),
|
||||
)
|
||||
}
|
||||
|
||||
// Get 获取指定场景策略。
|
||||
func (r *Registry) Get(scene string) (SceneStrategy, bool) {
|
||||
strategy, ok := r.strategies[scene]
|
||||
return strategy, ok
|
||||
// Get 获取指定场景数据源。
|
||||
func (r *Registry) Get(scene string) (DataSource, bool) {
|
||||
source, ok := r.sources[scene]
|
||||
return source, ok
|
||||
}
|
||||
|
||||
// IsSupported 判断场景是否支持。
|
||||
func (r *Registry) IsSupported(scene string) bool {
|
||||
_, ok := r.strategies[scene]
|
||||
_, ok := r.sources[scene]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Scenes 返回当前支持的场景列表。
|
||||
func (r *Registry) Scenes() []string {
|
||||
result := make([]string, 0, len(r.strategies))
|
||||
for scene := range r.strategies {
|
||||
result := make([]string, 0, len(r.sources))
|
||||
for scene := range r.sources {
|
||||
result = append(result, scene)
|
||||
}
|
||||
sort.Strings(result)
|
||||
@@ -75,7 +59,7 @@ func (r *Registry) Scenes() []string {
|
||||
// IsSupportedScene 判断是否为受支持的场景。
|
||||
func IsSupportedScene(scene string) bool {
|
||||
switch scene {
|
||||
case constants.ExportTaskSceneDevice, constants.ExportTaskSceneIotCard:
|
||||
case constants.ExportTaskSceneDevice, constants.ExportTaskSceneIotCard, constants.ExportTaskSceneOrder:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
||||
@@ -1,26 +1 @@
|
||||
package exporter
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
func applyShopScopeForTask(query *gorm.DB, task *model.ExportTask) *gorm.DB {
|
||||
if task == nil {
|
||||
return query.Where("1 = 0")
|
||||
}
|
||||
|
||||
switch task.CreatorUserType {
|
||||
case constants.UserTypeSuperAdmin, constants.UserTypePlatform:
|
||||
return query
|
||||
case constants.UserTypeAgent:
|
||||
if len(task.ScopeShopIDs) == 0 {
|
||||
return query.Where("1 = 0")
|
||||
}
|
||||
return query.Where("shop_id IN ?", []uint(task.ScopeShopIDs))
|
||||
default:
|
||||
return query.Where("1 = 0")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTimeout = 30 * time.Second
|
||||
defaultTimeout = 60 * time.Second
|
||||
maxIdleConns = 100
|
||||
maxIdleConnsPerHost = 10
|
||||
idleConnTimeout = 90 * time.Second
|
||||
|
||||
@@ -3,6 +3,7 @@ package admin
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
@@ -162,6 +163,63 @@ func (h *AssetHandler) CurrentPackage(c *fiber.Ctx) error {
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
// UpdatePackageExpiresAt 修改资产套餐过期时间
|
||||
// PATCH /api/admin/assets/:identifier/packages/:package_usage_id/expires-at
|
||||
func (h *AssetHandler) UpdatePackageExpiresAt(c *fiber.Ctx) error {
|
||||
if err := ensureAssetPackageAdjuster(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
asset, packageUsageID, err := h.resolveAssetPackageUsagePath(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var req dto.UpdateAssetPackageExpiresAtRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
expiresAt, err := parseAssetPackageTime(req.ExpiresAt)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeInvalidParam, "过期时间格式无效")
|
||||
}
|
||||
|
||||
result, err := h.assetService.UpdatePackageExpiresAt(c.UserContext(), asset.AssetType, asset.AssetID, assetIdentifierForAudit(asset), packageUsageID, expiresAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
// UpdatePackageUsage 修改资产套餐已用量
|
||||
// PATCH /api/admin/assets/:identifier/packages/:package_usage_id/used-data
|
||||
func (h *AssetHandler) UpdatePackageUsage(c *fiber.Ctx) error {
|
||||
if err := ensureAssetPackageAdjuster(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
asset, packageUsageID, err := h.resolveAssetPackageUsagePath(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var req dto.UpdateAssetPackageUsageRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
if req.DataUsageMB == nil || *req.DataUsageMB < 0 {
|
||||
return errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
result, err := h.assetService.UpdatePackageUsage(c.UserContext(), asset.AssetType, asset.AssetID, assetIdentifierForAudit(asset), packageUsageID, *req.DataUsageMB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return response.Success(c, result)
|
||||
}
|
||||
|
||||
// Stop 停机(设备批量停机,单卡停机)
|
||||
// POST /api/admin/assets/:identifier/stop
|
||||
func (h *AssetHandler) Stop(c *fiber.Ctx) error {
|
||||
@@ -440,3 +498,57 @@ func resolveCallerAccountType(c *fiber.Ctx) string {
|
||||
}
|
||||
return "agent"
|
||||
}
|
||||
|
||||
func (h *AssetHandler) resolveAssetPackageUsagePath(c *fiber.Ctx) (*dto.AssetResolveResponse, uint, error) {
|
||||
asset, err := h.resolveByIdentifier(c)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
packageUsageID, parseErr := strconv.ParseUint(c.Params("package_usage_id"), 10, 64)
|
||||
if parseErr != nil || packageUsageID == 0 {
|
||||
return nil, 0, errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
return asset, uint(packageUsageID), nil
|
||||
}
|
||||
|
||||
func ensureAssetPackageAdjuster(c *fiber.Ctx) error {
|
||||
if middleware.GetUserIDFromContext(c.UserContext()) != 41 && middleware.GetUserIDFromContext(c.UserContext()) != 127 {
|
||||
return errors.New(errors.CodeForbidden, "无权限操作该资源或资源不存在")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseAssetPackageTime(value string) (time.Time, error) {
|
||||
raw := strings.TrimSpace(value)
|
||||
if raw == "" {
|
||||
return time.Time{}, errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
layouts := []string{
|
||||
time.RFC3339,
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02T15:04:05",
|
||||
"2006-01-02",
|
||||
}
|
||||
for _, layout := range layouts {
|
||||
if t, err := time.ParseInLocation(layout, raw, time.Local); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
return time.Time{}, errors.New(errors.CodeInvalidParam)
|
||||
}
|
||||
|
||||
func assetIdentifierForAudit(asset *dto.AssetResolveResponse) string {
|
||||
if asset == nil {
|
||||
return ""
|
||||
}
|
||||
if asset.AssetType == "card" && strings.TrimSpace(asset.ICCID) != "" {
|
||||
return asset.ICCID
|
||||
}
|
||||
if strings.TrimSpace(asset.VirtualNo) != "" {
|
||||
return asset.VirtualNo
|
||||
}
|
||||
return asset.Identifier
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
asset "github.com/break/junhong_cmp_fiber/internal/service/asset"
|
||||
customerBinding "github.com/break/junhong_cmp_fiber/internal/service/customer_binding"
|
||||
packagepkg "github.com/break/junhong_cmp_fiber/internal/service/package"
|
||||
"github.com/break/junhong_cmp_fiber/internal/service/packageprice"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
@@ -27,7 +28,7 @@ import (
|
||||
// 提供 B1~B4 资产信息、可购套餐、套餐历史、手动刷新接口
|
||||
type ClientAssetHandler struct {
|
||||
assetService *asset.Service
|
||||
personalDeviceStore *postgres.PersonalCustomerDeviceStore
|
||||
customerBinding *customerBinding.Service
|
||||
assetWalletStore *postgres.AssetWalletStore
|
||||
packageStore *postgres.PackageStore
|
||||
shopPackageAllocationStore *postgres.ShopPackageAllocationStore
|
||||
@@ -40,7 +41,7 @@ type ClientAssetHandler struct {
|
||||
// NewClientAssetHandler 创建 C 端资产信息处理器
|
||||
func NewClientAssetHandler(
|
||||
assetService *asset.Service,
|
||||
personalDeviceStore *postgres.PersonalCustomerDeviceStore,
|
||||
binding *customerBinding.Service,
|
||||
assetWalletStore *postgres.AssetWalletStore,
|
||||
packageStore *postgres.PackageStore,
|
||||
shopPackageAllocationStore *postgres.ShopPackageAllocationStore,
|
||||
@@ -51,7 +52,7 @@ func NewClientAssetHandler(
|
||||
) *ClientAssetHandler {
|
||||
return &ClientAssetHandler{
|
||||
assetService: assetService,
|
||||
personalDeviceStore: personalDeviceStore,
|
||||
customerBinding: binding,
|
||||
assetWalletStore: assetWalletStore,
|
||||
packageStore: packageStore,
|
||||
shopPackageAllocationStore: shopPackageAllocationStore,
|
||||
@@ -102,7 +103,7 @@ func (h *ClientAssetHandler) resolveAssetFromIdentifier(c *fiber.Ctx, identifier
|
||||
return nil, err
|
||||
}
|
||||
|
||||
owned, ownErr := h.isCustomerOwnAsset(skipPermissionCtx, customerID, assetInfo.VirtualNo)
|
||||
owned, ownErr := h.customerBinding.OwnsAsset(skipPermissionCtx, customerID, assetInfo.AssetType, assetInfo.AssetID)
|
||||
if ownErr != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, ownErr, "查询资产归属失败")
|
||||
}
|
||||
@@ -365,23 +366,24 @@ func (h *ClientAssetHandler) GetPackageHistory(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
list = append(list, dto.AssetPackageResponse{
|
||||
PackageUsageID: usage.ID,
|
||||
PackageID: usage.PackageID,
|
||||
PackageName: pkgName,
|
||||
PackageType: pkgType,
|
||||
UsageType: usage.UsageType,
|
||||
Status: usage.Status,
|
||||
StatusName: packageStatusName(usage.Status),
|
||||
RealTotalMB: metrics.RealTotalMB,
|
||||
RealUsedMB: metrics.RealUsedMB,
|
||||
VirtualTotalMB: metrics.VirtualTotalMB,
|
||||
VirtualUsedMB: metrics.VirtualUsedMB,
|
||||
ReductionPct: metrics.ReductionPct,
|
||||
ActivatedAt: usage.ActivatedAt,
|
||||
ExpiresAt: usage.ExpiresAt,
|
||||
MasterUsageID: usage.MasterUsageID,
|
||||
Priority: usage.Priority,
|
||||
CreatedAt: usage.CreatedAt,
|
||||
PackageUsageID: usage.ID,
|
||||
PackageID: usage.PackageID,
|
||||
PackageName: pkgName,
|
||||
PackageType: pkgType,
|
||||
UsageType: usage.UsageType,
|
||||
Status: usage.Status,
|
||||
StatusName: packageStatusName(usage.Status),
|
||||
RealTotalMB: metrics.RealTotalMB,
|
||||
RealUsedMB: metrics.RealUsedMB,
|
||||
VirtualTotalMB: metrics.VirtualTotalMB,
|
||||
VirtualUsedMB: metrics.VirtualUsedMB,
|
||||
ReductionPct: metrics.ReductionPct,
|
||||
EnableVirtualData: usage.EnableVirtualDataSnapshot,
|
||||
ActivatedAt: usage.ActivatedAt,
|
||||
ExpiresAt: usage.ExpiresAt,
|
||||
MasterUsageID: usage.MasterUsageID,
|
||||
Priority: usage.Priority,
|
||||
CreatedAt: usage.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -421,21 +423,6 @@ func (h *ClientAssetHandler) RefreshAsset(c *fiber.Ctx) error {
|
||||
return response.Success(c, resp)
|
||||
}
|
||||
|
||||
func (h *ClientAssetHandler) isCustomerOwnAsset(ctx context.Context, customerID uint, virtualNo string) (bool, error) {
|
||||
records, err := h.personalDeviceStore.GetByCustomerID(ctx, customerID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, record := range records {
|
||||
if record == nil {
|
||||
continue
|
||||
}
|
||||
if record.Status == constants.StatusEnabled && record.VirtualNo == virtualNo {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (h *ClientAssetHandler) getAssetGeneration(ctx context.Context, assetType string, assetID uint) (int, error) {
|
||||
switch assetType {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/break/junhong_cmp_fiber/internal/middleware"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
assetSvc "github.com/break/junhong_cmp_fiber/internal/service/asset"
|
||||
customerBinding "github.com/break/junhong_cmp_fiber/internal/service/customer_binding"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/logger"
|
||||
@@ -27,7 +28,7 @@ type deviceAssetInfo struct {
|
||||
// 提供设备卡列表、重启、恢复出厂、WiFi 配置、切卡等操作
|
||||
type ClientDeviceHandler struct {
|
||||
assetService *assetSvc.Service
|
||||
personalDeviceStore *postgres.PersonalCustomerDeviceStore
|
||||
customerBinding *customerBinding.Service
|
||||
deviceStore *postgres.DeviceStore
|
||||
deviceSimBindingStore *postgres.DeviceSimBindingStore
|
||||
iotCardStore *postgres.IotCardStore
|
||||
@@ -38,7 +39,7 @@ type ClientDeviceHandler struct {
|
||||
// NewClientDeviceHandler 创建 C 端设备能力处理器
|
||||
func NewClientDeviceHandler(
|
||||
assetService *assetSvc.Service,
|
||||
personalDeviceStore *postgres.PersonalCustomerDeviceStore,
|
||||
binding *customerBinding.Service,
|
||||
deviceStore *postgres.DeviceStore,
|
||||
deviceSimBindingStore *postgres.DeviceSimBindingStore,
|
||||
iotCardStore *postgres.IotCardStore,
|
||||
@@ -47,7 +48,7 @@ func NewClientDeviceHandler(
|
||||
) *ClientDeviceHandler {
|
||||
return &ClientDeviceHandler{
|
||||
assetService: assetService,
|
||||
personalDeviceStore: personalDeviceStore,
|
||||
customerBinding: binding,
|
||||
deviceStore: deviceStore,
|
||||
deviceSimBindingStore: deviceSimBindingStore,
|
||||
iotCardStore: iotCardStore,
|
||||
@@ -79,11 +80,11 @@ func (h *ClientDeviceHandler) validateDeviceAsset(c *fiber.Ctx, identifier strin
|
||||
}
|
||||
|
||||
// 校验个人客户对该设备的所有权
|
||||
owns, err := h.personalDeviceStore.ExistsByCustomerAndDevice(ctx, customerID, asset.VirtualNo)
|
||||
owns, err := h.customerBinding.OwnsAsset(ctx, customerID, asset.AssetType, asset.AssetID)
|
||||
if err != nil {
|
||||
h.logger.Error("校验设备所有权失败",
|
||||
zap.Uint("customer_id", customerID),
|
||||
zap.String("virtual_no", asset.VirtualNo),
|
||||
zap.Uint("asset_id", asset.AssetID),
|
||||
zap.Error(err))
|
||||
return nil, errors.New(errors.CodeInternalError)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
assetService "github.com/break/junhong_cmp_fiber/internal/service/asset"
|
||||
customerBinding "github.com/break/junhong_cmp_fiber/internal/service/customer_binding"
|
||||
pollingSvc "github.com/break/junhong_cmp_fiber/internal/service/polling"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/config"
|
||||
@@ -29,7 +30,7 @@ var clientRealnameValidator = validator.New()
|
||||
// ClientRealnameHandler C 端实名认证处理器
|
||||
type ClientRealnameHandler struct {
|
||||
assetService *assetService.Service
|
||||
personalDeviceStore *postgres.PersonalCustomerDeviceStore
|
||||
customerBinding *customerBinding.Service
|
||||
iotCardStore *postgres.IotCardStore
|
||||
deviceSimBindingStore *postgres.DeviceSimBindingStore
|
||||
carrierStore *postgres.CarrierStore
|
||||
@@ -41,7 +42,7 @@ type ClientRealnameHandler struct {
|
||||
// NewClientRealnameHandler 创建 C 端实名认证处理器
|
||||
func NewClientRealnameHandler(
|
||||
assetSvc *assetService.Service,
|
||||
personalDeviceStore *postgres.PersonalCustomerDeviceStore,
|
||||
binding *customerBinding.Service,
|
||||
iotCardStore *postgres.IotCardStore,
|
||||
deviceSimBindingStore *postgres.DeviceSimBindingStore,
|
||||
carrierStore *postgres.CarrierStore,
|
||||
@@ -51,7 +52,7 @@ func NewClientRealnameHandler(
|
||||
) *ClientRealnameHandler {
|
||||
return &ClientRealnameHandler{
|
||||
assetService: assetSvc,
|
||||
personalDeviceStore: personalDeviceStore,
|
||||
customerBinding: binding,
|
||||
iotCardStore: iotCardStore,
|
||||
deviceSimBindingStore: deviceSimBindingStore,
|
||||
carrierStore: carrierStore,
|
||||
@@ -89,11 +90,11 @@ func (h *ClientRealnameHandler) GetRealnameLink(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
// 4. 验证资产归属(个人客户必须绑定过该资产)
|
||||
owned, err := h.personalDeviceStore.ExistsByCustomerAndDevice(ctx, customerID, asset.VirtualNo)
|
||||
owned, err := h.customerBinding.OwnsAsset(ctx, customerID, asset.AssetType, asset.AssetID)
|
||||
if err != nil {
|
||||
logger.GetAppLogger().Error("查询资产归属失败",
|
||||
zap.Uint("customer_id", customerID),
|
||||
zap.String("virtual_no", asset.VirtualNo),
|
||||
zap.Uint("asset_id", asset.AssetID),
|
||||
zap.Error(err))
|
||||
return errors.New(errors.CodeInternalError, "查询资产归属失败")
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
asset "github.com/break/junhong_cmp_fiber/internal/service/asset"
|
||||
customerBinding "github.com/break/junhong_cmp_fiber/internal/service/customer_binding"
|
||||
rechargeSvc "github.com/break/junhong_cmp_fiber/internal/service/recharge"
|
||||
wechatConfigSvc "github.com/break/junhong_cmp_fiber/internal/service/wechat_config"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
@@ -31,7 +32,7 @@ import (
|
||||
// 提供 C1~C5 钱包详情、流水、充值前校验、充值下单、充值记录接口
|
||||
type ClientWalletHandler struct {
|
||||
assetService *asset.Service
|
||||
personalDeviceStore *postgres.PersonalCustomerDeviceStore
|
||||
customerBinding *customerBinding.Service
|
||||
walletStore *postgres.AssetWalletStore
|
||||
transactionStore *postgres.AssetWalletTransactionStore
|
||||
rechargeOrderStore *postgres.RechargeOrderStore
|
||||
@@ -49,7 +50,7 @@ type ClientWalletHandler struct {
|
||||
// NewClientWalletHandler 创建 C 端钱包处理器
|
||||
func NewClientWalletHandler(
|
||||
assetService *asset.Service,
|
||||
personalDeviceStore *postgres.PersonalCustomerDeviceStore,
|
||||
binding *customerBinding.Service,
|
||||
walletStore *postgres.AssetWalletStore,
|
||||
transactionStore *postgres.AssetWalletTransactionStore,
|
||||
rechargeOrderStore *postgres.RechargeOrderStore,
|
||||
@@ -65,7 +66,7 @@ func NewClientWalletHandler(
|
||||
) *ClientWalletHandler {
|
||||
return &ClientWalletHandler{
|
||||
assetService: assetService,
|
||||
personalDeviceStore: personalDeviceStore,
|
||||
customerBinding: binding,
|
||||
walletStore: walletStore,
|
||||
transactionStore: transactionStore,
|
||||
rechargeOrderStore: rechargeOrderStore,
|
||||
@@ -544,7 +545,7 @@ func (h *ClientWalletHandler) resolveAssetFromIdentifier(c *fiber.Ctx, identifie
|
||||
return nil, err
|
||||
}
|
||||
|
||||
owned, ownErr := h.isCustomerOwnAsset(skipPermissionCtx, customerID, assetInfo.VirtualNo)
|
||||
owned, ownErr := h.customerBinding.OwnsAsset(skipPermissionCtx, customerID, assetInfo.AssetType, assetInfo.AssetID)
|
||||
if ownErr != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, ownErr, "查询资产归属失败")
|
||||
}
|
||||
@@ -572,21 +573,6 @@ func (h *ClientWalletHandler) resolveAssetFromIdentifier(c *fiber.Ctx, identifie
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *ClientWalletHandler) isCustomerOwnAsset(ctx context.Context, customerID uint, virtualNo string) (bool, error) {
|
||||
records, err := h.personalDeviceStore.GetByCustomerID(ctx, customerID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, record := range records {
|
||||
if record == nil {
|
||||
continue
|
||||
}
|
||||
if record.Status == constants.StatusEnabled && record.VirtualNo == virtualNo {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (h *ClientWalletHandler) getAssetGeneration(ctx context.Context, assetType string, assetID uint) (int, error) {
|
||||
switch assetType {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user