@zhin.js/plugin-ledger 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/commands/ledger-add/index.js +6 -4
- package/commands/ledger-add/index.ts +6 -4
- package/commands/ledger-delete/index.js +6 -4
- package/commands/ledger-delete/index.ts +6 -4
- package/commands/ledger-list/index.js +8 -6
- package/commands/ledger-list/index.ts +8 -6
- package/commands/ledger-month/index.js +5 -3
- package/commands/ledger-month/index.ts +5 -3
- package/commands/ledger-report/index.js +11 -11
- package/commands/ledger-report/index.ts +10 -12
- package/commands/ledger-stats/index.js +8 -6
- package/commands/ledger-stats/index.ts +8 -6
- package/commands/ledger-today/index.js +5 -3
- package/commands/ledger-today/index.ts +5 -3
- package/commands/ledger-week/index.js +5 -3
- package/commands/ledger-week/index.ts +5 -3
- package/lib/config.js +3 -3
- package/lib/config.js.map +1 -1
- package/lib/entry-input.d.ts +3 -2
- package/lib/entry-input.d.ts.map +1 -1
- package/lib/entry-input.js +11 -9
- package/lib/entry-input.js.map +1 -1
- package/lib/i18n.d.ts +2 -0
- package/lib/i18n.d.ts.map +1 -0
- package/lib/i18n.js +200 -0
- package/lib/i18n.js.map +1 -0
- package/lib/identity.d.ts +3 -1
- package/lib/identity.d.ts.map +1 -1
- package/lib/identity.js +5 -2
- package/lib/identity.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/money.d.ts +2 -1
- package/lib/money.d.ts.map +1 -1
- package/lib/money.js +6 -5
- package/lib/money.js.map +1 -1
- package/lib/period.d.ts +4 -3
- package/lib/period.d.ts.map +1 -1
- package/lib/period.js +8 -16
- package/lib/period.js.map +1 -1
- package/lib/report-format.d.ts +5 -4
- package/lib/report-format.d.ts.map +1 -1
- package/lib/report-format.js +25 -24
- package/lib/report-format.js.map +1 -1
- package/lib/service.d.ts +1 -1
- package/lib/service.d.ts.map +1 -1
- package/lib/service.js +59 -25
- package/lib/service.js.map +1 -1
- package/lib/store.d.ts +9 -1
- package/lib/store.d.ts.map +1 -1
- package/lib/store.js +32 -3
- package/lib/store.js.map +1 -1
- package/package.json +7 -7
- package/schema.json +15 -0
- package/src/config.ts +3 -3
- package/src/entry-input.ts +12 -9
- package/src/i18n.ts +200 -0
- package/src/identity.ts +6 -2
- package/src/index.ts +1 -1
- package/src/money.ts +7 -5
- package/src/period.ts +7 -13
- package/src/report-format.ts +26 -24
- package/src/service.ts +59 -26
- package/src/store.ts +33 -3
package/src/i18n.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { createMessages } from 'zhin.js/i18n';
|
|
2
|
+
|
|
3
|
+
export const messages = createMessages({
|
|
4
|
+
"actor": {
|
|
5
|
+
"zh-CN": "记账需要在真实会话中由具体用户发送",
|
|
6
|
+
"en": "Ledger commands require a real user in a conversation."
|
|
7
|
+
},
|
|
8
|
+
"usage": {
|
|
9
|
+
"zh-CN": "格式:<类别> <金额> [备注],例如:午饭 18.5 和同事",
|
|
10
|
+
"en": "Format: <category> <amount> [note], for example: Lunch 18.5 With colleagues"
|
|
11
|
+
},
|
|
12
|
+
"order": {
|
|
13
|
+
"zh-CN": "类别要写在金额前面。{usage}",
|
|
14
|
+
"en": "Put the category before the amount. {usage}"
|
|
15
|
+
},
|
|
16
|
+
"amountError": {
|
|
17
|
+
"zh-CN": "{reason}。{usage}",
|
|
18
|
+
"en": "{reason} {usage}"
|
|
19
|
+
},
|
|
20
|
+
"categoryNumber": {
|
|
21
|
+
"zh-CN": "类别不能是数字。{usage}",
|
|
22
|
+
"en": "The category cannot be a number. {usage}"
|
|
23
|
+
},
|
|
24
|
+
"categoryLimit": {
|
|
25
|
+
"zh-CN": "类别最多 {max} 个字",
|
|
26
|
+
"en": "Category limit: {max} characters."
|
|
27
|
+
},
|
|
28
|
+
"noteLimit": {
|
|
29
|
+
"zh-CN": "备注最多 {max} 个字",
|
|
30
|
+
"en": "Note limit: {max} characters."
|
|
31
|
+
},
|
|
32
|
+
"negative": {
|
|
33
|
+
"zh-CN": "金额不能为负数",
|
|
34
|
+
"en": "Amount cannot be negative."
|
|
35
|
+
},
|
|
36
|
+
"amountFormat": {
|
|
37
|
+
"zh-CN": "金额格式不正确,请写成 18 或 18.50(最多两位小数)",
|
|
38
|
+
"en": "Use an amount such as 18 or 18.50, with at most two decimal places."
|
|
39
|
+
},
|
|
40
|
+
"zero": {
|
|
41
|
+
"zh-CN": "金额必须大于 0",
|
|
42
|
+
"en": "Amount must be greater than zero."
|
|
43
|
+
},
|
|
44
|
+
"amountMax": {
|
|
45
|
+
"zh-CN": "单笔金额不能超过 ¥1000000.00",
|
|
46
|
+
"en": "A single entry cannot exceed ¥1000000.00."
|
|
47
|
+
},
|
|
48
|
+
"period.today": {
|
|
49
|
+
"zh-CN": "今日",
|
|
50
|
+
"en": "Today"
|
|
51
|
+
},
|
|
52
|
+
"period.week": {
|
|
53
|
+
"zh-CN": "本周",
|
|
54
|
+
"en": "This week"
|
|
55
|
+
},
|
|
56
|
+
"period.month": {
|
|
57
|
+
"zh-CN": "本月",
|
|
58
|
+
"en": "This month"
|
|
59
|
+
},
|
|
60
|
+
"period.all": {
|
|
61
|
+
"zh-CN": "全部",
|
|
62
|
+
"en": "All time"
|
|
63
|
+
},
|
|
64
|
+
"allRecords": {
|
|
65
|
+
"zh-CN": "全部记录",
|
|
66
|
+
"en": "All records"
|
|
67
|
+
},
|
|
68
|
+
"periodInput": {
|
|
69
|
+
"zh-CN": "时间范围只能是:今日、本周、本月、全部",
|
|
70
|
+
"en": "Choose a period: today, week, month, all."
|
|
71
|
+
},
|
|
72
|
+
"actionInput": {
|
|
73
|
+
"zh-CN": "操作只能是:开启、关闭、状态",
|
|
74
|
+
"en": "Choose an action: on, off, status."
|
|
75
|
+
},
|
|
76
|
+
"added": {
|
|
77
|
+
"zh-CN": "✅ 已记账 #{seq} · {category} {amount}{note}",
|
|
78
|
+
"en": "✅ Recorded #{seq} · {category} {amount}{note}"
|
|
79
|
+
},
|
|
80
|
+
"notFound": {
|
|
81
|
+
"zh-CN": "没有找到你的账目 #{seq}(只能删除自己的记账,请查看账单编号)",
|
|
82
|
+
"en": "Your entry #{seq} was not found. You can only delete your own entries; check the entry list for its ID."
|
|
83
|
+
},
|
|
84
|
+
"deleted": {
|
|
85
|
+
"zh-CN": "🗑️ 已删除 #{seq} · {category} {amount}",
|
|
86
|
+
"en": "🗑️ Deleted #{seq} · {category} {amount}"
|
|
87
|
+
},
|
|
88
|
+
"spending": {
|
|
89
|
+
"zh-CN": "{period}消费",
|
|
90
|
+
"en": "{period} spending"
|
|
91
|
+
},
|
|
92
|
+
"statistics": {
|
|
93
|
+
"zh-CN": "分类统计",
|
|
94
|
+
"en": "Category statistics"
|
|
95
|
+
},
|
|
96
|
+
"entryList": {
|
|
97
|
+
"zh-CN": "{period}账单",
|
|
98
|
+
"en": "{period} entries"
|
|
99
|
+
},
|
|
100
|
+
"privateSent": {
|
|
101
|
+
"zh-CN": "🔒 {title}已私信发送给你",
|
|
102
|
+
"en": "🔒 {title} was sent to you privately."
|
|
103
|
+
},
|
|
104
|
+
"privateFallback": {
|
|
105
|
+
"zh-CN": "{text}\n(群聊中金额已隐藏,私聊我可查看完整金额)",
|
|
106
|
+
"en": "{text}\n(Amounts are hidden in public chats. Message me privately to view them.)"
|
|
107
|
+
},
|
|
108
|
+
"reportEnabled": {
|
|
109
|
+
"zh-CN": "记账日报已开启:每天 {time}({timezone})私信发送当天消费",
|
|
110
|
+
"en": "Daily reports are enabled: today’s spending is sent privately at {time} ({timezone})."
|
|
111
|
+
},
|
|
112
|
+
"reportDisabled": {
|
|
113
|
+
"zh-CN": "记账日报未开启。",
|
|
114
|
+
"en": "Daily reports are disabled."
|
|
115
|
+
},
|
|
116
|
+
"reportOff": {
|
|
117
|
+
"zh-CN": "已关闭记账日报",
|
|
118
|
+
"en": "Daily reports disabled."
|
|
119
|
+
},
|
|
120
|
+
"noPrivate": {
|
|
121
|
+
"zh-CN": "当前环境不支持主动私信,无法开启记账日报",
|
|
122
|
+
"en": "This environment cannot send proactive private messages; daily reports cannot be enabled."
|
|
123
|
+
},
|
|
124
|
+
"reportOn": {
|
|
125
|
+
"zh-CN": "已开启记账日报:每天 {time}({timezone})私信发送当天消费。",
|
|
126
|
+
"en": "Daily reports enabled: today’s spending is sent privately at {time} ({timezone})."
|
|
127
|
+
},
|
|
128
|
+
"cannotPrivate": {
|
|
129
|
+
"zh-CN": "无法私信你,请先私聊我后再开启记账日报。",
|
|
130
|
+
"en": "I could not message you privately. Message me privately before enabling daily reports."
|
|
131
|
+
},
|
|
132
|
+
"reportOnPrivate": {
|
|
133
|
+
"zh-CN": "🔒 已开启记账日报,确认消息已私信发送给你",
|
|
134
|
+
"en": "🔒 Daily reports enabled; confirmation was sent to you privately."
|
|
135
|
+
},
|
|
136
|
+
"report": {
|
|
137
|
+
"zh-CN": "🌙 记账日报\n{text}",
|
|
138
|
+
"en": "🌙 Daily expense report\n{text}"
|
|
139
|
+
},
|
|
140
|
+
"commandHint": {
|
|
141
|
+
"zh-CN": "发送「{command}」。",
|
|
142
|
+
"en": "Send \"{command}\"."
|
|
143
|
+
},
|
|
144
|
+
"emptySummary": {
|
|
145
|
+
"zh-CN": "📒 {period}还没有记账({range})",
|
|
146
|
+
"en": "📒 {period}: no expenses recorded ({range})."
|
|
147
|
+
},
|
|
148
|
+
"summaryHeader": {
|
|
149
|
+
"zh-CN": "📒 {period}消费({range})",
|
|
150
|
+
"en": "📒 {period} spending ({range})"
|
|
151
|
+
},
|
|
152
|
+
"total": {
|
|
153
|
+
"zh-CN": "合计 {amount} · {count} 笔",
|
|
154
|
+
"en": "Total {amount} · {count} entries"
|
|
155
|
+
},
|
|
156
|
+
"categoryFull": {
|
|
157
|
+
"zh-CN": "· {category} {amount}({count} 笔,{share}%)",
|
|
158
|
+
"en": "· {category} {amount} ({count} entries, {share}%)"
|
|
159
|
+
},
|
|
160
|
+
"categoryMasked": {
|
|
161
|
+
"zh-CN": "· {category} {amount}({count} 笔)",
|
|
162
|
+
"en": "· {category} {amount} ({count} entries)"
|
|
163
|
+
},
|
|
164
|
+
"emptyStats": {
|
|
165
|
+
"zh-CN": "📊 {period}没有可统计的记账({range})",
|
|
166
|
+
"en": "📊 {period}: no entries to analyze ({range})."
|
|
167
|
+
},
|
|
168
|
+
"statsHeader": {
|
|
169
|
+
"zh-CN": "📊 分类统计 · {period}({range})",
|
|
170
|
+
"en": "📊 Category statistics · {period} ({range})"
|
|
171
|
+
},
|
|
172
|
+
"statsFull": {
|
|
173
|
+
"zh-CN": "· {category}:合计 {amount},{count} 笔,平均 {average},最大 {max},占比 {share}%",
|
|
174
|
+
"en": "· {category}: total {amount}, {count} entries, average {average}, largest {max}, share {share}%"
|
|
175
|
+
},
|
|
176
|
+
"statsMasked": {
|
|
177
|
+
"zh-CN": "· {category}:{count} 笔,金额已隐藏",
|
|
178
|
+
"en": "· {category}: {count} entries, amounts hidden"
|
|
179
|
+
},
|
|
180
|
+
"categoryCount": {
|
|
181
|
+
"zh-CN": "共 {count} 个类别",
|
|
182
|
+
"en": "{count} categories"
|
|
183
|
+
},
|
|
184
|
+
"categoryCountTotal": {
|
|
185
|
+
"zh-CN": "共 {count} 个类别 · 合计 {amount}",
|
|
186
|
+
"en": "{count} categories · total {amount}"
|
|
187
|
+
},
|
|
188
|
+
"emptyList": {
|
|
189
|
+
"zh-CN": "🧾 {period}没有账目({range})",
|
|
190
|
+
"en": "🧾 {period}: no entries ({range})."
|
|
191
|
+
},
|
|
192
|
+
"listHeader": {
|
|
193
|
+
"zh-CN": "🧾 {period}账单({range})",
|
|
194
|
+
"en": "🧾 {period} entries ({range})"
|
|
195
|
+
},
|
|
196
|
+
"more": {
|
|
197
|
+
"zh-CN": "……另有 {count} 笔未显示",
|
|
198
|
+
"en": "… {count} more entries not shown"
|
|
199
|
+
}
|
|
200
|
+
});
|
package/src/identity.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { resolveLocale, type Locale } from 'zhin.js/i18n';
|
|
2
|
+
import { messages as t } from './i18n.js';
|
|
1
3
|
import { createDeliveryTarget, type DeliveryTarget } from 'zhin.js';
|
|
2
4
|
import type { CommandMessage } from 'zhin.js/command';
|
|
3
5
|
|
|
@@ -5,6 +7,7 @@ import type { CommandMessage } from 'zhin.js/command';
|
|
|
5
7
|
export interface LedgerCaller {
|
|
6
8
|
/** Stable per-user key: adapter + endpoint (bot account) + sender id. */
|
|
7
9
|
readonly ownerKey: string;
|
|
10
|
+
readonly locale?: Locale;
|
|
8
11
|
readonly senderId: string;
|
|
9
12
|
/** The conversation the command came from. */
|
|
10
13
|
readonly conversation: DeliveryTarget;
|
|
@@ -21,9 +24,9 @@ export function ledgerOwnerKey(adapter: string, endpointId: string, senderId: st
|
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
/** Builds the caller from a command message; commands without a real sender are refused. */
|
|
24
|
-
export function ledgerCaller(input: CommandMessage | undefined): LedgerCaller {
|
|
27
|
+
export function ledgerCaller(input: CommandMessage | undefined, locale: Locale = resolveLocale(input?.locale)): LedgerCaller {
|
|
25
28
|
const senderId = input?.sender?.id;
|
|
26
|
-
if (!input || !senderId) throw new LedgerInputError('
|
|
29
|
+
if (!input || !senderId) throw new LedgerInputError(t(locale, 'actor'));
|
|
27
30
|
const conversation = createDeliveryTarget(input);
|
|
28
31
|
const endpoint = conversation.endpoint;
|
|
29
32
|
const isPrivate = conversation.kind === 'private';
|
|
@@ -33,6 +36,7 @@ export function ledgerCaller(input: CommandMessage | undefined): LedgerCaller {
|
|
|
33
36
|
return {
|
|
34
37
|
ownerKey: ledgerOwnerKey(endpoint.adapter, endpoint.id, senderId),
|
|
35
38
|
senderId,
|
|
39
|
+
locale,
|
|
36
40
|
conversation,
|
|
37
41
|
privateTarget,
|
|
38
42
|
isPrivate,
|
package/src/index.ts
CHANGED
|
@@ -26,4 +26,4 @@ export {
|
|
|
26
26
|
} from './report-format.js';
|
|
27
27
|
export { ledgerServiceToken } from './runtime.js';
|
|
28
28
|
export { LedgerService, type DailyReportAction, type LedgerServiceOptions } from './service.js';
|
|
29
|
-
export { defineLedgerTables, LedgerStore, ENTRY_TABLE, USER_TABLE, type LedgerEntry, type LedgerUserRecord } from './store.js';
|
|
29
|
+
export { defineLedgerTables, LedgerStore, ENTRY_TABLE, USER_TABLE, PRIVATE_TARGET_TABLE, type LedgerEntry, type LedgerUserRecord } from './store.js';
|
package/src/money.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { resolveLocale, type Locale } from 'zhin.js/i18n';
|
|
2
|
+
import { messages as t } from './i18n.js';
|
|
1
3
|
/**
|
|
2
4
|
* Amounts are stored as integer cents (分) and parsed from text without floating point,
|
|
3
5
|
* so 0.1 + 0.2 style rounding errors cannot enter the ledger.
|
|
@@ -28,17 +30,17 @@ function invalid(reason: string): AmountParseResult {
|
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/** Parses `18`, `18.5`, `18.50`, `¥18.5` or `18.5元` into cents; rejects negatives, zero, exponents and more than two decimals. */
|
|
31
|
-
export function parseAmountToCents(raw: string): AmountParseResult {
|
|
33
|
+
export function parseAmountToCents(raw: string, locale?: Locale): AmountParseResult {
|
|
32
34
|
const normalized = toHalfWidth(raw.trim());
|
|
33
35
|
const withoutCurrency = normalized.replace(CURRENCY_PREFIX, '').replace(CURRENCY_SUFFIX, '').trim();
|
|
34
|
-
if (withoutCurrency.startsWith('-')) return invalid('
|
|
36
|
+
if (withoutCurrency.startsWith('-')) return invalid(t(locale, 'negative'));
|
|
35
37
|
const match = AMOUNT_PATTERN.exec(withoutCurrency);
|
|
36
|
-
if (!match) return invalid(
|
|
38
|
+
if (!match) return invalid(t(locale, 'amountFormat'));
|
|
37
39
|
const wholeYuan = Number(match[1]);
|
|
38
40
|
const fractionDigits = (match[2] ?? '').padEnd(2, '0');
|
|
39
41
|
const cents = wholeYuan * 100 + Number(fractionDigits);
|
|
40
|
-
if (cents === 0) return invalid(
|
|
41
|
-
if (cents > MAX_ENTRY_CENTS) return invalid(
|
|
42
|
+
if (cents === 0) return invalid(t(locale, 'zero'));
|
|
43
|
+
if (cents > MAX_ENTRY_CENTS) return invalid(t(locale, 'amountMax'));
|
|
42
44
|
return { ok: true, cents };
|
|
43
45
|
}
|
|
44
46
|
|
package/src/period.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { resolveLocale, type Locale } from 'zhin.js/i18n';
|
|
2
|
+
import { messages as t } from './i18n.js';
|
|
1
3
|
/**
|
|
2
4
|
* Calendar periods evaluated in the configured IANA timezone. Entries are compared by their
|
|
3
5
|
* local calendar date (YYYY-MM-DD), so daylight-saving shifts never move an entry into another day.
|
|
@@ -7,21 +9,13 @@ export type LedgerPeriod = 'today' | 'week' | 'month' | 'all';
|
|
|
7
9
|
|
|
8
10
|
const DAY_MS = 86_400_000;
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
today: '今日',
|
|
12
|
-
week: '本周',
|
|
13
|
-
month: '本月',
|
|
14
|
-
all: '全部',
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export function periodLabel(period: LedgerPeriod): string {
|
|
18
|
-
return PERIOD_LABELS[period];
|
|
19
|
-
}
|
|
12
|
+
export function periodLabel(period: LedgerPeriod, locale?: Locale): string { return t(locale, `period.${period}`); }
|
|
20
13
|
|
|
21
14
|
/** Maps a user-typed period word to a period; undefined when the word is not recognised. */
|
|
22
|
-
export function parsePeriodWord(word: string | undefined, fallback: LedgerPeriod): LedgerPeriod | undefined {
|
|
15
|
+
export function parsePeriodWord(word: string | undefined, fallback: LedgerPeriod, locale?: Locale): LedgerPeriod | undefined {
|
|
23
16
|
if (word === undefined || word.trim() === '') return fallback;
|
|
24
17
|
const normalized = word.trim();
|
|
18
|
+
if (resolveLocale(locale) === 'en') return ['today', 'week', 'month', 'all'].includes(normalized.toLowerCase()) ? normalized.toLowerCase() as LedgerPeriod : undefined;
|
|
25
19
|
if (normalized === '今日' || normalized === '今天') return 'today';
|
|
26
20
|
if (normalized === '本周' || normalized === '这周') return 'week';
|
|
27
21
|
if (normalized === '本月' || normalized === '这个月') return 'month';
|
|
@@ -89,7 +83,7 @@ export function isInPeriod(entryMs: number, nowMs: number, period: LedgerPeriod,
|
|
|
89
83
|
}
|
|
90
84
|
|
|
91
85
|
/** Human-readable range for a period header, e.g. `2026-10-19 ~ 2026-10-25`. */
|
|
92
|
-
export function describePeriodRange(nowMs: number, period: LedgerPeriod, timeZone: string): string {
|
|
86
|
+
export function describePeriodRange(nowMs: number, period: LedgerPeriod, timeZone: string, locale?: Locale): string {
|
|
93
87
|
const today = localDateKey(nowMs, timeZone);
|
|
94
88
|
if (period === 'today') return today;
|
|
95
89
|
if (period === 'month') return today.slice(0, 7);
|
|
@@ -97,5 +91,5 @@ export function describePeriodRange(nowMs: number, period: LedgerPeriod, timeZon
|
|
|
97
91
|
const start = weekStartKey(today);
|
|
98
92
|
return `${start} ~ ${weekEndKey(start)}`;
|
|
99
93
|
}
|
|
100
|
-
return '
|
|
94
|
+
return t(locale, 'allRecords');
|
|
101
95
|
}
|
package/src/report-format.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { resolveLocale, type Locale } from 'zhin.js/i18n';
|
|
2
|
+
import { messages as t } from './i18n.js';
|
|
1
3
|
import { formatCents, MASKED_AMOUNT } from './money.js';
|
|
2
4
|
import { describePeriodRange, localDateKey, periodLabel, type LedgerPeriod } from './period.js';
|
|
3
5
|
import type { LedgerEntry } from './store.js';
|
|
@@ -39,12 +41,12 @@ export function totalsByCategory(entries: readonly LedgerEntry[]): CategoryTotal
|
|
|
39
41
|
return totals;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
export function summarize(entries: readonly LedgerEntry[], period: LedgerPeriod, nowMs: number, timeZone: string): PeriodSummary {
|
|
44
|
+
export function summarize(entries: readonly LedgerEntry[], period: LedgerPeriod, nowMs: number, timeZone: string, locale?: Locale): PeriodSummary {
|
|
43
45
|
let totalCents = 0;
|
|
44
46
|
for (const entry of entries) totalCents += entry.amountCents;
|
|
45
47
|
return {
|
|
46
48
|
period,
|
|
47
|
-
range: describePeriodRange(nowMs, period, timeZone),
|
|
49
|
+
range: describePeriodRange(nowMs, period, timeZone, locale),
|
|
48
50
|
totalCents,
|
|
49
51
|
count: entries.length,
|
|
50
52
|
categories: totalsByCategory(entries),
|
|
@@ -58,56 +60,56 @@ function sharePercent(partCents: number, totalCents: number): string {
|
|
|
58
60
|
return `${Math.floor(tenths / 10)}.${tenths % 10}`;
|
|
59
61
|
}
|
|
60
62
|
|
|
61
|
-
export function formatPeriodSummary(summary: PeriodSummary): LedgerReply {
|
|
62
|
-
const label = periodLabel(summary.period);
|
|
63
|
+
export function formatPeriodSummary(summary: PeriodSummary, locale?: Locale): LedgerReply {
|
|
64
|
+
const label = periodLabel(summary.period, locale);
|
|
63
65
|
if (summary.count === 0) {
|
|
64
|
-
const empty =
|
|
66
|
+
const empty = t(locale, 'emptySummary', { period: label, range: summary.range });
|
|
65
67
|
return { full: empty, masked: empty };
|
|
66
68
|
}
|
|
67
|
-
const header =
|
|
68
|
-
const fullLines = [header,
|
|
69
|
-
const maskedLines = [header,
|
|
69
|
+
const header = t(locale, 'summaryHeader', { period: label, range: summary.range });
|
|
70
|
+
const fullLines = [header, t(locale, 'total', { amount: formatCents(summary.totalCents), count: summary.count })];
|
|
71
|
+
const maskedLines = [header, t(locale, 'total', { amount: MASKED_AMOUNT, count: summary.count })];
|
|
70
72
|
for (const category of summary.categories) {
|
|
71
73
|
const share = sharePercent(category.totalCents, summary.totalCents);
|
|
72
|
-
fullLines.push(
|
|
73
|
-
maskedLines.push(
|
|
74
|
+
fullLines.push(t(locale, 'categoryFull', { category: category.category, amount: formatCents(category.totalCents), count: category.count, share }));
|
|
75
|
+
maskedLines.push(t(locale, 'categoryMasked', { category: category.category, amount: MASKED_AMOUNT, count: category.count }));
|
|
74
76
|
}
|
|
75
77
|
return { full: fullLines.join('\n'), masked: maskedLines.join('\n') };
|
|
76
78
|
}
|
|
77
79
|
|
|
78
|
-
export function formatCategoryStatistics(summary: PeriodSummary): LedgerReply {
|
|
79
|
-
const label = periodLabel(summary.period);
|
|
80
|
+
export function formatCategoryStatistics(summary: PeriodSummary, locale?: Locale): LedgerReply {
|
|
81
|
+
const label = periodLabel(summary.period, locale);
|
|
80
82
|
if (summary.count === 0) {
|
|
81
|
-
const empty =
|
|
83
|
+
const empty = t(locale, 'emptyStats', { period: label, range: summary.range });
|
|
82
84
|
return { full: empty, masked: empty };
|
|
83
85
|
}
|
|
84
|
-
const header =
|
|
86
|
+
const header = t(locale, 'statsHeader', { period: label, range: summary.range });
|
|
85
87
|
const fullLines = [header];
|
|
86
88
|
const maskedLines = [header];
|
|
87
89
|
for (const category of summary.categories) {
|
|
88
90
|
const averageCents = Math.round(category.totalCents / category.count);
|
|
89
91
|
const share = sharePercent(category.totalCents, summary.totalCents);
|
|
90
|
-
fullLines.push(
|
|
91
|
-
maskedLines.push(
|
|
92
|
+
fullLines.push(t(locale, 'statsFull', { category: category.category, amount: formatCents(category.totalCents), count: category.count, average: formatCents(averageCents), max: formatCents(category.largestCents), share }));
|
|
93
|
+
maskedLines.push(t(locale, 'statsMasked', { category: category.category, count: category.count }));
|
|
92
94
|
}
|
|
93
|
-
fullLines.push(
|
|
94
|
-
maskedLines.push(
|
|
95
|
+
fullLines.push(t(locale, 'categoryCountTotal', { count: summary.categories.length, amount: formatCents(summary.totalCents) }));
|
|
96
|
+
maskedLines.push(t(locale, 'categoryCount', { count: summary.categories.length }));
|
|
95
97
|
return { full: fullLines.join('\n'), masked: maskedLines.join('\n') };
|
|
96
98
|
}
|
|
97
99
|
|
|
98
100
|
export const MAX_LISTED_ENTRIES = 20;
|
|
99
101
|
|
|
100
102
|
/** Newest first, with `#id` so users can find what to delete. */
|
|
101
|
-
export function formatEntryList(entries: readonly LedgerEntry[], period: LedgerPeriod, nowMs: number, timeZone: string): LedgerReply {
|
|
102
|
-
const label = periodLabel(period);
|
|
103
|
-
const range = describePeriodRange(nowMs, period, timeZone);
|
|
103
|
+
export function formatEntryList(entries: readonly LedgerEntry[], period: LedgerPeriod, nowMs: number, timeZone: string, locale?: Locale): LedgerReply {
|
|
104
|
+
const label = periodLabel(period, locale);
|
|
105
|
+
const range = describePeriodRange(nowMs, period, timeZone, locale);
|
|
104
106
|
if (entries.length === 0) {
|
|
105
|
-
const empty =
|
|
107
|
+
const empty = t(locale, 'emptyList', { period: label, range });
|
|
106
108
|
return { full: empty, masked: empty };
|
|
107
109
|
}
|
|
108
110
|
const newestFirst = [...entries].sort((left, right) => right.createdAt - left.createdAt || right.seq - left.seq);
|
|
109
111
|
const shown = newestFirst.slice(0, MAX_LISTED_ENTRIES);
|
|
110
|
-
const header =
|
|
112
|
+
const header = t(locale, 'listHeader', { period: label, range });
|
|
111
113
|
const fullLines = [header];
|
|
112
114
|
const maskedLines = [header];
|
|
113
115
|
for (const entry of shown) {
|
|
@@ -117,7 +119,7 @@ export function formatEntryList(entries: readonly LedgerEntry[], period: LedgerP
|
|
|
117
119
|
maskedLines.push(`#${entry.seq} ${date} ${entry.category} ${MASKED_AMOUNT}`);
|
|
118
120
|
}
|
|
119
121
|
if (newestFirst.length > shown.length) {
|
|
120
|
-
const more =
|
|
122
|
+
const more = t(locale, 'more', { count: newestFirst.length - shown.length });
|
|
121
123
|
fullLines.push(more);
|
|
122
124
|
maskedLines.push(more);
|
|
123
125
|
}
|
package/src/service.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolveLocale, type Locale } from 'zhin.js/i18n';
|
|
2
|
+
import { messages as t } from './i18n.js';
|
|
3
|
+
import { getLogger, type DeliveryTarget, type OutboundHost, type PluginDatabaseHost } from 'zhin.js';
|
|
2
4
|
import type { ResolvedLedgerConfig } from './config.js';
|
|
3
5
|
import { parseEntryInput } from './entry-input.js';
|
|
4
6
|
import type { LedgerCaller } from './identity.js';
|
|
@@ -36,6 +38,8 @@ export class LedgerService {
|
|
|
36
38
|
readonly #now: () => number;
|
|
37
39
|
/** Serialises writes per user so two quick `记` commands never share an `#id`. */
|
|
38
40
|
readonly #userQueues = new Map<string, Promise<unknown>>();
|
|
41
|
+
/** Private targets already saved this process, to skip redundant writes. */
|
|
42
|
+
readonly #knownPrivate = new Map<string, string>();
|
|
39
43
|
|
|
40
44
|
constructor(database: PluginDatabaseHost, outbound: OutboundHost | undefined, config: ResolvedLedgerConfig, options: LedgerServiceOptions = {}) {
|
|
41
45
|
this.#store = new LedgerStore(database);
|
|
@@ -55,8 +59,31 @@ export class LedgerService {
|
|
|
55
59
|
return next;
|
|
56
60
|
}
|
|
57
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Some platforms (Discord, Lark, Slack, ...) address a private chat by a conversation id that is
|
|
64
|
+
* not the user id. When the user talks to the bot in private we save that real conversation and
|
|
65
|
+
* use it for later private deliveries; until then the user id is the best available guess.
|
|
66
|
+
*/
|
|
67
|
+
async #rememberPrivate(caller: LedgerCaller): Promise<void> {
|
|
68
|
+
if (!caller.isPrivate) return;
|
|
69
|
+
const raw = JSON.stringify(caller.conversation);
|
|
70
|
+
if (this.#knownPrivate.get(caller.ownerKey) === raw) return;
|
|
71
|
+
try {
|
|
72
|
+
await this.#store.setPrivateTarget(caller.ownerKey, caller.conversation);
|
|
73
|
+
this.#knownPrivate.set(caller.ownerKey, raw);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
logger.warn(`saving private target failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async #privateTargetOf(caller: LedgerCaller): Promise<DeliveryTarget> {
|
|
80
|
+
if (caller.isPrivate) return caller.conversation;
|
|
81
|
+
return (await this.#store.getPrivateTarget(caller.ownerKey)) ?? caller.privateTarget;
|
|
82
|
+
}
|
|
83
|
+
|
|
58
84
|
async addEntry(caller: LedgerCaller, text: string): Promise<string> {
|
|
59
|
-
|
|
85
|
+
await this.#rememberPrivate(caller);
|
|
86
|
+
const parsed = parseEntryInput(text, caller.locale);
|
|
60
87
|
if (!parsed.ok) return parsed.reason;
|
|
61
88
|
const { category, amountCents, note } = parsed.entry;
|
|
62
89
|
const seq = await this.#withUserLock(caller.ownerKey, async () => {
|
|
@@ -73,16 +100,17 @@ export class LedgerService {
|
|
|
73
100
|
});
|
|
74
101
|
const amount = caller.isPrivate ? formatCents(amountCents) : MASKED_AMOUNT;
|
|
75
102
|
const noteText = note ? ` · ${note}` : '';
|
|
76
|
-
return
|
|
103
|
+
return t(caller.locale, 'added', { seq, category, amount, note: noteText });
|
|
77
104
|
}
|
|
78
105
|
|
|
79
106
|
async deleteEntry(caller: LedgerCaller, seq: number): Promise<string> {
|
|
107
|
+
await this.#rememberPrivate(caller);
|
|
80
108
|
return this.#withUserLock(caller.ownerKey, async () => {
|
|
81
109
|
const entry = await this.#store.findEntry(caller.ownerKey, seq);
|
|
82
|
-
if (!entry) return
|
|
110
|
+
if (!entry) return t(caller.locale, 'notFound', { seq });
|
|
83
111
|
await this.#store.deleteEntry(caller.ownerKey, seq);
|
|
84
112
|
const amount = caller.isPrivate ? formatCents(entry.amountCents) : MASKED_AMOUNT;
|
|
85
|
-
return
|
|
113
|
+
return t(caller.locale, 'deleted', { seq, category: entry.category, amount });
|
|
86
114
|
});
|
|
87
115
|
}
|
|
88
116
|
|
|
@@ -94,36 +122,39 @@ export class LedgerService {
|
|
|
94
122
|
|
|
95
123
|
async periodSummary(caller: LedgerCaller, period: LedgerPeriod): Promise<string> {
|
|
96
124
|
const entries = await this.#entriesIn(caller.ownerKey, period);
|
|
97
|
-
const reply = formatPeriodSummary(summarize(entries, period, this.#now(), this.#config.timezone));
|
|
98
|
-
return this.#deliver(caller, reply,
|
|
125
|
+
const reply = formatPeriodSummary(summarize(entries, period, this.#now(), this.#config.timezone, caller.locale), caller.locale);
|
|
126
|
+
return this.#deliver(caller, reply, t(caller.locale, 'spending', { period: periodLabel(period, caller.locale) }));
|
|
99
127
|
}
|
|
100
128
|
|
|
101
129
|
async categoryStatistics(caller: LedgerCaller, period: LedgerPeriod): Promise<string> {
|
|
102
130
|
const entries = await this.#entriesIn(caller.ownerKey, period);
|
|
103
|
-
const reply = formatCategoryStatistics(summarize(entries, period, this.#now(), this.#config.timezone));
|
|
104
|
-
return this.#deliver(caller, reply, '
|
|
131
|
+
const reply = formatCategoryStatistics(summarize(entries, period, this.#now(), this.#config.timezone, caller.locale), caller.locale);
|
|
132
|
+
return this.#deliver(caller, reply, t(caller.locale, 'statistics'));
|
|
105
133
|
}
|
|
106
134
|
|
|
107
135
|
async listEntries(caller: LedgerCaller, period: LedgerPeriod): Promise<string> {
|
|
108
136
|
const entries = await this.#entriesIn(caller.ownerKey, period);
|
|
109
|
-
const reply = formatEntryList(entries, period, this.#now(), this.#config.timezone);
|
|
110
|
-
return this.#deliver(caller, reply,
|
|
137
|
+
const reply = formatEntryList(entries, period, this.#now(), this.#config.timezone, caller.locale);
|
|
138
|
+
return this.#deliver(caller, reply, t(caller.locale, 'entryList', { period: periodLabel(period, caller.locale) }));
|
|
111
139
|
}
|
|
112
140
|
|
|
113
141
|
/** Private chat: full reply. Group/channel: try a private message, else the masked reply. */
|
|
114
142
|
async #deliver(caller: LedgerCaller, reply: LedgerReply, title: string): Promise<string> {
|
|
115
|
-
if (caller.isPrivate)
|
|
143
|
+
if (caller.isPrivate) {
|
|
144
|
+
await this.#rememberPrivate(caller);
|
|
145
|
+
return reply.full;
|
|
146
|
+
}
|
|
116
147
|
if (reply.full === reply.masked) return reply.full;
|
|
117
148
|
const delivered = await this.#sendPrivately(caller, reply.full);
|
|
118
|
-
if (delivered) return
|
|
119
|
-
return
|
|
149
|
+
if (delivered) return t(caller.locale, 'privateSent', { title });
|
|
150
|
+
return t(caller.locale, 'privateFallback', { text: reply.masked });
|
|
120
151
|
}
|
|
121
152
|
|
|
122
153
|
async #sendPrivately(caller: LedgerCaller, content: string): Promise<boolean> {
|
|
123
154
|
const sendTarget = this.#outbound?.sendTarget;
|
|
124
155
|
if (!sendTarget) return false;
|
|
125
156
|
try {
|
|
126
|
-
await sendTarget.call(this.#outbound, caller
|
|
157
|
+
await sendTarget.call(this.#outbound, await this.#privateTargetOf(caller), content, { locale: resolveLocale(caller.locale) });
|
|
127
158
|
return true;
|
|
128
159
|
} catch (error) {
|
|
129
160
|
logger.debug(`private delivery unavailable: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -131,29 +162,31 @@ export class LedgerService {
|
|
|
131
162
|
}
|
|
132
163
|
}
|
|
133
164
|
|
|
134
|
-
async dailyReport(caller: LedgerCaller, action: DailyReportAction): Promise<string> {
|
|
165
|
+
async dailyReport(caller: LedgerCaller, action: DailyReportAction, commandHead?: string): Promise<string> {
|
|
166
|
+
await this.#rememberPrivate(caller);
|
|
167
|
+
const hint = (action: 'on' | 'off') => commandHead ? t(caller.locale, 'commandHint', { command: `${commandHead} ${resolveLocale(caller.locale) === 'en' ? action : action === 'on' ? '开启' : '关闭'}` }) : '';
|
|
135
168
|
const reportTime = this.#config.reportTime;
|
|
136
169
|
const user = await this.#store.getUser(caller.ownerKey);
|
|
137
170
|
if (action === 'status') {
|
|
138
|
-
if (user?.reportEnabled) return
|
|
139
|
-
return '
|
|
171
|
+
if (user?.reportEnabled) return t(caller.locale, 'reportEnabled', { time: reportTime, timezone: this.#config.timezone });
|
|
172
|
+
return [t(caller.locale, 'reportDisabled'), hint('on')].filter(Boolean).join(' ');
|
|
140
173
|
}
|
|
141
174
|
if (action === 'off') {
|
|
142
175
|
await this.#store.setReport(caller.ownerKey, false, undefined, user?.lastReportDate ?? '');
|
|
143
|
-
return '
|
|
176
|
+
return t(caller.locale, 'reportOff');
|
|
144
177
|
}
|
|
145
|
-
if (!this.#outbound?.sendTarget) return '
|
|
146
|
-
const confirmation =
|
|
178
|
+
if (!this.#outbound?.sendTarget) return t(caller.locale, 'noPrivate');
|
|
179
|
+
const confirmation = [t(caller.locale, 'reportOn', { time: reportTime, timezone: this.#config.timezone }), hint('off')].filter(Boolean).join(' ');
|
|
147
180
|
if (!caller.isPrivate) {
|
|
148
181
|
const reachable = await this.#sendPrivately(caller, confirmation);
|
|
149
|
-
if (!reachable) return '
|
|
182
|
+
if (!reachable) return [t(caller.locale, 'cannotPrivate'), hint('on')].filter(Boolean).join(' ');
|
|
150
183
|
}
|
|
151
184
|
const nowMs = this.#now();
|
|
152
185
|
const today = localDateKey(nowMs, this.#config.timezone);
|
|
153
186
|
const alreadyPastReportTime = localTimeKey(nowMs, this.#config.timezone) >= reportTime;
|
|
154
187
|
const lastReportDate = alreadyPastReportTime ? today : '';
|
|
155
|
-
await this.#store.setReport(caller.ownerKey, true, caller
|
|
156
|
-
return caller.isPrivate ? confirmation :
|
|
188
|
+
await this.#store.setReport(caller.ownerKey, true, await this.#privateTargetOf(caller), lastReportDate, resolveLocale(caller.locale));
|
|
189
|
+
return caller.isPrivate ? confirmation : t(caller.locale, 'reportOnPrivate');
|
|
157
190
|
}
|
|
158
191
|
|
|
159
192
|
/**
|
|
@@ -174,9 +207,9 @@ export class LedgerService {
|
|
|
174
207
|
if (!subscriber.reportTarget || !this.#outbound?.sendTarget) continue;
|
|
175
208
|
const entries = await this.#entriesIn(subscriber.ownerKey, 'today');
|
|
176
209
|
if (entries.length === 0 && !this.#config.reportWhenEmpty) continue;
|
|
177
|
-
const summary = formatPeriodSummary(summarize(entries, 'today', nowMs, timezone));
|
|
210
|
+
const summary = formatPeriodSummary(summarize(entries, 'today', nowMs, timezone, subscriber.reportLocale), subscriber.reportLocale);
|
|
178
211
|
try {
|
|
179
|
-
await this.#outbound.sendTarget(subscriber.reportTarget,
|
|
212
|
+
await this.#outbound.sendTarget(subscriber.reportTarget, t(subscriber.reportLocale, 'report', { text: summary.full }), { locale: resolveLocale(subscriber.reportLocale) });
|
|
180
213
|
sent += 1;
|
|
181
214
|
} catch (error) {
|
|
182
215
|
logger.warn(`daily report delivery failed: ${error instanceof Error ? error.message : String(error)}`);
|