Commit 21c9bd5
feat: independent currency system + polymorphic RewardEntry editor (#30)
* feat(server): currency schema + activity binding + drop is_currency
Introduces three new tables for the currency subsystem:
- currencies: org-scoped catalog (id, alias, name, icon, sort_order,
is_active, activity_id, activity_node_id, metadata)
- currency_wallets: per-user balance, unique (org, user, currency_id)
+ version lock, enabling ON CONFLICT DO UPDATE under neon-http's
no-transactions constraint
- currency_ledger: immutable audit trail mirroring item_grant_logs
Also binds activity_id / activity_node_id on the three definition-layer
tables (item_definitions, currencies, entity_blueprints) as soft links
matching the established check_in_configs / shop_products pattern —
lets the activity service's cleanup path archive activity-scoped
content by the same mechanism.
Finally drops item_definitions.is_currency (currencies are now a
first-class table, not an item flag) and swaps the
storage_box_deposits.currency_definition_id FK from item_definitions
to currencies so deposits can only reference real currencies.
Migrations 0028..0031 cover the four schema moves. Applied to local
Postgres and Neon dev branch.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(server): currency module — CRUD + wallet + ledger + routes
New modules/currency/ with the standard service/routes/validators/
errors split. The service is a single `createCurrencyService(deps)`
factory (deps: Pick<AppDeps, "db">) exposing:
- Definition CRUD (list with activityId filter, get by id or alias,
create / update / delete, assertAllExist batch check for other
modules)
- grant(...) — per-entry loop using ON CONFLICT DO UPDATE on the
unique (org, user, currency_id) index; writes a ledger row per
grant
- deduct(...) — conditional UPDATE with WHERE balance >= amount; a
zero-row return becomes CurrencyInsufficientBalance, which makes
overdrafts impossible under concurrency without transactions
- getBalance / getWallets / listLedger (cursor-paged)
Admin routes mounted at /api/currency and client routes at
/api/client/currency. Matches the item module surface so the admin
UI can lift the same patterns.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(server): RewardEntry gains "currency" type + dispatcher plumbing
RewardType = "item" | "entity" | "currency". lib/rewards.ts grows a
RewardCurrencySvc interface and makes `currencySvc` a non-optional
field on RewardServices — that way a consumer module that forgets to
inject currencyService fails at compile time, never silently drops
currency rewards at runtime.
grantRewards and deductCosts each get a new filterByType("currency")
branch that batches entries into a single currencySvc.grant/deduct
call, identical in shape to the item branch.
All 11 module validators that store RewardEntry[] in jsonb columns
have their zod discriminator expanded from ["item", "entity"] to
["item", "entity", "currency"] so the new type is accepted end-to-end.
modules/level/client-routes.ts had a handful of inline type literals
with the same narrow union — widened alongside.
No behavior change for existing item-only or mixed item+entity
rewards. Currency entries now flow through the dispatcher untouched.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(server): bind activityId to item + entity definitions; clean up isCurrency
Item and entity CRUD now persist and filter by activity_id /
activity_node_id. listDefinitions / listBlueprints accept
`activityId: string | null` — passing `null` filters for permanent
entries, a string filters to that activity. Standard openapi
validators updated on all three layers (validators / service / routes
serializer).
Removes the last references to `isCurrency` on the item side:
- createDefinition / updateDefinition no longer write the column
- CreateDefinitionInput / UpdateDefinitionInput / response shapes
stripped of isCurrency
- routes serializer stops emitting the field
The column itself was dropped in the schema commit above; this
cleans up the module-layer callers.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* refactor(server): wire storage-box + exchange + task + level to currencyService
storage-box:
- createStorageBoxService factory takes `currencySvc: CurrencyService`
in place of the old `itemSvc: ItemService`
- deposit path: currencySvc.deduct (was itemSvc.deductItems)
- withdraw path: currencySvc.grant (was itemSvc.grantItems)
- validateAcceptedCurrencies now queries the `currencies` table, not
`item_definitions.isCurrency`
- openapi description & tests updated; service.test.ts rewrites
"make currency" fixture to use currencyService.createDefinition
exchange:
- Factory accepts both itemSvc and currencySvc; internal helpers
`deductOne(entry)` / `grantOne(entry)` route per-RewardEntry by
type, so a cost/reward list can mix item + currency. Rollback
path dispatches by the same helpers, preserving exchange's
per-entry rollback semantics (batching via deductCosts would
have collapsed the "which ones already succeeded" granularity).
task + level:
- Barrel wires `currencySvc: currencyService` alongside the
existing itemSvc / entitySvc; mock RewardServices in their
tests grows the same field (required by the type).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(admin): currency management page + ActivityPicker on item/entity forms
New admin currency module:
- lib/types/currency.ts — CurrencyDefinition, WalletView, LedgerEntry,
grant/deduct payload types
- hooks/use-currency.ts — list/get/create/update/delete; useUserWallets,
useUserBalance; useGrantCurrency / useDeductCurrency; useCurrencyLedger
with cursor-paged filter
- components/currency/{DefinitionForm,DefinitionTable,LedgerTable}.tsx
- routes/_dashboard/currency/{index,create,$currencyId}.tsx —
definitions tab + ledger tab (filter by endUserId / currencyId),
create page, detail/edit page with inline DefinitionForm
- AppSidebar gains a "货币 / Currency" nav entry (Coins icon)
Definition-layer forms on neighboring modules:
- item/DefinitionForm: drops the isCurrency Switch (currencies live in
their own table now); wires ActivityPicker against activityId
- item/DefinitionTable: drops the "货币" badge derivation
- entity/BlueprintForm: wires ActivityPicker
Wiring fallout:
- hooks/use-item drops useCurrencies (moved to use-currency)
- lib/types/item + entity: types updated for activityId / activityNodeId
and remove isCurrency
- storage-box components (StorageBoxConfigForm, StorageBoxDepositLookup)
re-point `useCurrencies` import from `use-item` to `use-currency`
(same call site, different hook source)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(admin): shared RewardEntry editor — type/currency/item/entity picker
Every cost/reward field server-side stores a polymorphic
RewardEntry[] = {type, id, count}, but the admin side was fragmented:
half the modules used a legacy {definitionId, quantity} shape (shop,
exchange, check-in, collection, task, cdkey, mail, lottery,
dialogue), and level / leaderboard / activity each defined their own
narrow RewardEntry that didn't include "currency". This commit
unifies all of them.
New shared infrastructure:
- lib/types/rewards.ts — canonical RewardEntry + RewardType
- hooks/use-reward-catalog.ts — parallel fetch of items +
currencies + entity blueprints with a per-type index +
resolveLabel helper
- components/rewards/RewardEntryEditor.tsx — `[type ▾][target ▾]
[count #][🗑]` row-based editor. Second dropdown populates from
useRewardCatalog by the currently-selected type; switching type
clears the target id. Optional `allowedTypes` prop for callers
that want to restrict (e.g. deduct-only fields hiding "entity").
Migrated forms:
- shop ProductForm (cost + reward) + StageForm (reward)
- exchange OptionForm (cost + reward) — deleted its local
handwritten ItemEntryEditor
- collection MilestoneForm (reward) — drops its `itemDefinitions`
prop; caller routes (albumId/index.tsx) drop the pass-through
- check-in RewardForm — single reward list
- mail MessageForm — single reward list
- lottery PrizeForm — rewards moved from single item-only dropdown
to full editor (can now reward currency or entity)
- dialogue ScriptEditor (onEnter + option rewards)
- cdkey/create — deleted its local handwritten editor
Polymorphic display:
- components/item/ItemRewardRow accepts `entry: RewardEntry` and
resolves name/icon via useRewardCatalog (keeps the legacy
{definitionId, quantity} props for call-sites that haven't
migrated — they're treated as type="item")
- 4 display sites now pass `entry` instead of two separate props:
check-in/RewardsSection, exchange/ExecutePanel, mail/$messageId,
collection/$albumId (the old "name × quantity" text rendering
was also upgraded to use ItemRewardRow).
Types unified:
- 9 lib/types/*.ts switch `ItemEntry[]` to `RewardEntry[]` and
import from ./rewards. item.ts keeps its legacy ItemEntry shape
intact — that one is for grant/deduct admin API only (goes
straight to itemService, doesn't flow through RewardEntry).
- level/leaderboard/activity had each defined a local
RewardEntry = {type: "item"|"entity",...} — now re-export the
canonical one so they automatically pick up "currency".
i18n:
- reward_type_item / currency / entity
- reward_entry_empty_hint / reward_entry_pick_target / reward_entry_add
zh + en.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 19cf34a commit 21c9bd5
102 files changed
Lines changed: 66366 additions & 773 deletions
File tree
- apps
- admin
- messages
- src
- components
- check-in
- collection
- currency
- dialogue
- entity
- exchange
- item
- lottery
- mail
- rewards
- shop
- storage-box
- hooks
- lib/types
- routes/_dashboard
- cdkey
- collection/$albumId
- currency
- exchange/$configId/options/$optionId
- mail/$messageId
- server
- drizzle
- meta
- src
- lib
- modules
- activity
- cdkey
- check-in
- collection
- currency
- entity
- exchange
- item
- leaderboard
- level
- lottery
- mail
- shop
- storage-box
- task
- schema
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
975 | 975 | | |
976 | 976 | | |
977 | 977 | | |
978 | | - | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
979 | 1012 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
975 | 975 | | |
976 | 976 | | |
977 | 977 | | |
978 | | - | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
979 | 1012 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| 50 | + | |
49 | 51 | | |
50 | 52 | | |
51 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | 3 | | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | 8 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 9 | + | |
23 | 10 | | |
24 | 11 | | |
25 | 12 | | |
| |||
34 | 21 | | |
35 | 22 | | |
36 | 23 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | 24 | | |
41 | | - | |
| 25 | + | |
42 | 26 | | |
43 | 27 | | |
44 | | - | |
| 28 | + | |
45 | 29 | | |
46 | 30 | | |
47 | 31 | | |
| |||
53 | 37 | | |
54 | 38 | | |
55 | 39 | | |
56 | | - | |
57 | | - | |
| 40 | + | |
| 41 | + | |
58 | 42 | | |
59 | 43 | | |
60 | 44 | | |
61 | | - | |
| 45 | + | |
62 | 46 | | |
63 | 47 | | |
64 | 48 | | |
| |||
73 | 57 | | |
74 | 58 | | |
75 | 59 | | |
76 | | - | |
77 | | - | |
78 | | - | |
| 60 | + | |
79 | 61 | | |
80 | 62 | | |
81 | 63 | | |
82 | 64 | | |
83 | 65 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
145 | 71 | | |
146 | 72 | | |
147 | 73 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
| 111 | + | |
117 | 112 | | |
118 | 113 | | |
119 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
30 | 29 | | |
31 | 30 | | |
32 | 31 | | |
| |||
37 | 36 | | |
38 | 37 | | |
39 | 38 | | |
40 | | - | |
41 | 39 | | |
42 | 40 | | |
43 | 41 | | |
| |||
50 | 48 | | |
51 | 49 | | |
52 | 50 | | |
53 | | - | |
| 51 | + | |
54 | 52 | | |
55 | 53 | | |
56 | 54 | | |
| |||
67 | 65 | | |
68 | 66 | | |
69 | 67 | | |
70 | | - | |
| 68 | + | |
71 | 69 | | |
72 | 70 | | |
73 | 71 | | |
| |||
213 | 211 | | |
214 | 212 | | |
215 | 213 | | |
216 | | - | |
| 214 | + | |
217 | 215 | | |
218 | 216 | | |
219 | 217 | | |
220 | | - | |
221 | 218 | | |
222 | 219 | | |
223 | 220 | | |
| |||
0 commit comments