mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 14:04:04 +00:00
feat(miniapp): add household general settings
This commit is contained in:
@@ -469,6 +469,7 @@ describe('createMiniAppSettingsHandler', () => {
|
||||
expect(await response.json()).toEqual({
|
||||
ok: true,
|
||||
authorized: true,
|
||||
householdName: 'Kojori House',
|
||||
settings: {
|
||||
householdId: 'household-1',
|
||||
settlementCurrency: 'GEL',
|
||||
@@ -570,6 +571,7 @@ describe('createMiniAppUpdateSettingsHandler', () => {
|
||||
expect(await response.json()).toEqual({
|
||||
ok: true,
|
||||
authorized: true,
|
||||
householdName: 'Kojori House',
|
||||
settings: {
|
||||
householdId: 'household-1',
|
||||
settlementCurrency: 'GEL',
|
||||
|
||||
@@ -49,6 +49,7 @@ async function readApprovalPayload(request: Request): Promise<{
|
||||
|
||||
async function readSettingsUpdatePayload(request: Request): Promise<{
|
||||
initData: string
|
||||
householdName?: string
|
||||
settlementCurrency?: string
|
||||
paymentBalanceAdjustmentPolicy?: string
|
||||
rentAmountMajor?: string
|
||||
@@ -69,6 +70,7 @@ async function readSettingsUpdatePayload(request: Request): Promise<{
|
||||
|
||||
const text = await clonedRequest.text()
|
||||
let parsed: {
|
||||
householdName?: string
|
||||
settlementCurrency?: string
|
||||
paymentBalanceAdjustmentPolicy?: string
|
||||
rentAmountMajor?: string
|
||||
@@ -99,6 +101,11 @@ async function readSettingsUpdatePayload(request: Request): Promise<{
|
||||
|
||||
return {
|
||||
initData: payload.initData,
|
||||
...(typeof parsed.householdName === 'string'
|
||||
? {
|
||||
householdName: parsed.householdName
|
||||
}
|
||||
: {}),
|
||||
...(typeof parsed.rentAmountMajor === 'string'
|
||||
? {
|
||||
rentAmountMajor: parsed.rentAmountMajor
|
||||
@@ -545,6 +552,7 @@ export function createMiniAppSettingsHandler(options: {
|
||||
{
|
||||
ok: true,
|
||||
authorized: true,
|
||||
householdName: result.householdName,
|
||||
settings: serializeBillingSettings(result.settings),
|
||||
assistantConfig: serializeAssistantConfig(result.assistantConfig),
|
||||
topics: result.topics,
|
||||
@@ -620,6 +628,11 @@ export function createMiniAppUpdateSettingsHandler(options: {
|
||||
const result = await options.miniAppAdminService.updateSettings({
|
||||
householdId: session.member.householdId,
|
||||
actorIsAdmin: session.member.isAdmin,
|
||||
...(payload.householdName !== undefined
|
||||
? {
|
||||
householdName: payload.householdName
|
||||
}
|
||||
: {}),
|
||||
...(payload.settlementCurrency
|
||||
? {
|
||||
settlementCurrency: payload.settlementCurrency
|
||||
@@ -675,6 +688,7 @@ export function createMiniAppUpdateSettingsHandler(options: {
|
||||
{
|
||||
ok: true,
|
||||
authorized: true,
|
||||
householdName: result.householdName,
|
||||
settings: serializeBillingSettings(result.settings),
|
||||
assistantConfig: serializeAssistantConfig(result.assistantConfig)
|
||||
},
|
||||
|
||||
@@ -100,6 +100,7 @@ export interface MiniAppSessionResult {
|
||||
member?: {
|
||||
id: string
|
||||
householdId: string
|
||||
householdName: string
|
||||
displayName: string
|
||||
status: 'active' | 'away' | 'left'
|
||||
isAdmin: boolean
|
||||
|
||||
@@ -90,6 +90,7 @@ type SessionState =
|
||||
mode: 'live' | 'demo'
|
||||
member: {
|
||||
id: string
|
||||
householdName: string
|
||||
displayName: string
|
||||
status: 'active' | 'away' | 'left'
|
||||
isAdmin: boolean
|
||||
@@ -348,6 +349,7 @@ function App() {
|
||||
const [testingRolePreview, setTestingRolePreview] = createSignal<TestingRolePreview | null>(null)
|
||||
const [addingPayment, setAddingPayment] = createSignal(false)
|
||||
const [billingForm, setBillingForm] = createSignal({
|
||||
householdName: '',
|
||||
settlementCurrency: 'GEL' as 'USD' | 'GEL',
|
||||
paymentBalanceAdjustmentPolicy: 'utilities' as 'utilities' | 'rent' | 'separate',
|
||||
rentAmountMajor: '',
|
||||
@@ -762,6 +764,7 @@ function App() {
|
||||
''
|
||||
}))
|
||||
setBillingForm({
|
||||
householdName: payload.householdName,
|
||||
settlementCurrency: payload.settings.settlementCurrency,
|
||||
paymentBalanceAdjustmentPolicy: payload.settings.paymentBalanceAdjustmentPolicy,
|
||||
rentAmountMajor: payload.settings.rentAmountMinor
|
||||
@@ -883,6 +886,7 @@ function App() {
|
||||
)
|
||||
)
|
||||
setBillingForm({
|
||||
householdName: demoAdminSettings.householdName,
|
||||
settlementCurrency: demoAdminSettings.settings.settlementCurrency,
|
||||
paymentBalanceAdjustmentPolicy: demoAdminSettings.settings.paymentBalanceAdjustmentPolicy,
|
||||
rentAmountMajor: demoAdminSettings.settings.rentAmountMinor
|
||||
@@ -1200,7 +1204,7 @@ function App() {
|
||||
setSavingBillingSettings(true)
|
||||
|
||||
try {
|
||||
const { settings, assistantConfig } = await updateMiniAppBillingSettings(
|
||||
const { householdName, settings, assistantConfig } = await updateMiniAppBillingSettings(
|
||||
initData,
|
||||
billingForm()
|
||||
)
|
||||
@@ -1208,11 +1212,27 @@ function App() {
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
householdName,
|
||||
settings,
|
||||
assistantConfig
|
||||
}
|
||||
: current
|
||||
)
|
||||
setBillingForm((current) => ({
|
||||
...current,
|
||||
householdName
|
||||
}))
|
||||
setSession((current) =>
|
||||
current.status === 'ready'
|
||||
? {
|
||||
...current,
|
||||
member: {
|
||||
...current.member,
|
||||
householdName
|
||||
}
|
||||
}
|
||||
: current
|
||||
)
|
||||
setCycleForm((current) => ({
|
||||
...current,
|
||||
rentCurrency: settings.rentCurrency,
|
||||
@@ -1992,6 +2012,8 @@ function App() {
|
||||
locale={locale()}
|
||||
readyIsAdmin={effectiveIsAdmin()}
|
||||
householdDefaultLocale={readySession()?.member.householdDefaultLocale ?? 'en'}
|
||||
householdName={readySession()?.member.householdName ?? billingForm().householdName}
|
||||
profileDisplayName={readySession()?.member.displayName ?? displayNameDraft()}
|
||||
dashboard={dashboard()}
|
||||
adminSettings={adminSettings()}
|
||||
cycleState={cycleState()}
|
||||
@@ -2030,6 +2052,7 @@ function App() {
|
||||
resolvedMemberAbsencePolicy(memberId, status)
|
||||
}
|
||||
onChangeHouseholdLocale={handleHouseholdLocaleChange}
|
||||
onOpenProfileEditor={() => setProfileEditorOpen(true)}
|
||||
onOpenCycleModal={() => setCycleRentOpen(true)}
|
||||
onCloseCycleModal={() => setCycleRentOpen(false)}
|
||||
onSaveCycleRent={handleSaveCycleRent}
|
||||
@@ -2061,6 +2084,12 @@ function App() {
|
||||
settlementCurrency: value
|
||||
}))
|
||||
}
|
||||
onBillingHouseholdNameChange={(value) =>
|
||||
setBillingForm((current) => ({
|
||||
...current,
|
||||
householdName: value
|
||||
}))
|
||||
}
|
||||
onBillingAdjustmentPolicyChange={(value) =>
|
||||
setBillingForm((current) => ({
|
||||
...current,
|
||||
@@ -2290,7 +2319,11 @@ function App() {
|
||||
|
||||
<TopBar
|
||||
subtitle={copy().appSubtitle}
|
||||
title={copy().appTitle}
|
||||
title={
|
||||
readySession()?.member.householdName ??
|
||||
onboardingSession()?.householdName ??
|
||||
copy().appTitle
|
||||
}
|
||||
languageLabel={copy().language}
|
||||
locale={locale()}
|
||||
saving={savingMemberLocale()}
|
||||
@@ -2391,15 +2424,6 @@ function App() {
|
||||
)}
|
||||
</Show>
|
||||
</div>
|
||||
<Show when={readySession()?.mode === 'live'}>
|
||||
<Button
|
||||
variant="secondary"
|
||||
class="app-context-row__action"
|
||||
onClick={() => setProfileEditorOpen(true)}
|
||||
>
|
||||
{copy().manageProfileAction}
|
||||
</Button>
|
||||
</Show>
|
||||
</section>
|
||||
|
||||
<section class="content-stack">{panel()}</section>
|
||||
|
||||
@@ -9,6 +9,7 @@ import type {
|
||||
export const demoMember: NonNullable<MiniAppSession['member']> = {
|
||||
id: 'demo-member',
|
||||
householdId: 'demo-household',
|
||||
householdName: 'Kojori House',
|
||||
displayName: 'Stas',
|
||||
status: 'active',
|
||||
isAdmin: true,
|
||||
@@ -191,6 +192,7 @@ export const demoPendingMembers: readonly MiniAppPendingMember[] = [
|
||||
]
|
||||
|
||||
export const demoAdminSettings: MiniAppAdminSettingsPayload = {
|
||||
householdName: 'Kojori House',
|
||||
settings: {
|
||||
householdId: 'demo-household',
|
||||
settlementCurrency: 'GEL',
|
||||
|
||||
@@ -30,6 +30,10 @@ export const dictionary = {
|
||||
reload: 'Retry',
|
||||
language: 'Language',
|
||||
householdLanguage: 'Household language',
|
||||
generalSettingsBody:
|
||||
'Household identity, default language, and personal profile controls live here.',
|
||||
householdNameLabel: 'Household name',
|
||||
householdNameHint: 'This appears in the mini app, join flow, and bot responses.',
|
||||
savingLanguage: 'Saving…',
|
||||
onLabel: 'On',
|
||||
offLabel: 'Off',
|
||||
@@ -37,6 +41,7 @@ export const dictionary = {
|
||||
balances: 'Balances',
|
||||
ledger: 'Ledger',
|
||||
house: 'House',
|
||||
houseSectionGeneral: 'General',
|
||||
houseSectionBilling: 'Billing',
|
||||
houseSectionUtilities: 'Utilities',
|
||||
houseSectionMembers: 'Members',
|
||||
@@ -292,6 +297,9 @@ export const dictionary = {
|
||||
reload: 'Повторить',
|
||||
language: 'Язык',
|
||||
householdLanguage: 'Язык дома',
|
||||
generalSettingsBody: 'Здесь живут имя дома, язык по умолчанию и доступ к личному профилю.',
|
||||
householdNameLabel: 'Название дома',
|
||||
householdNameHint: 'Показывается в mini app, при вступлении и в ответах бота.',
|
||||
savingLanguage: 'Сохраняем…',
|
||||
onLabel: 'Вкл',
|
||||
offLabel: 'Выкл',
|
||||
@@ -299,6 +307,7 @@ export const dictionary = {
|
||||
balances: 'Баланс',
|
||||
ledger: 'Леджер',
|
||||
house: 'Дом',
|
||||
houseSectionGeneral: 'Общее',
|
||||
houseSectionBilling: 'Биллинг',
|
||||
houseSectionUtilities: 'Коммуналка',
|
||||
houseSectionMembers: 'Участники',
|
||||
|
||||
@@ -5,6 +5,7 @@ export interface MiniAppSession {
|
||||
member?: {
|
||||
id: string
|
||||
householdId: string
|
||||
householdName: string
|
||||
displayName: string
|
||||
status: 'active' | 'away' | 'left'
|
||||
isAdmin: boolean
|
||||
@@ -138,6 +139,7 @@ export interface MiniAppDashboard {
|
||||
}
|
||||
|
||||
export interface MiniAppAdminSettingsPayload {
|
||||
householdName: string
|
||||
settings: MiniAppBillingSettings
|
||||
assistantConfig: MiniAppAssistantConfig
|
||||
topics: readonly MiniAppTopicBinding[]
|
||||
@@ -386,6 +388,7 @@ export async function fetchMiniAppAdminSettings(
|
||||
const payload = (await response.json()) as {
|
||||
ok: boolean
|
||||
authorized?: boolean
|
||||
householdName?: string
|
||||
settings?: MiniAppBillingSettings
|
||||
assistantConfig?: MiniAppAssistantConfig
|
||||
topics?: MiniAppTopicBinding[]
|
||||
@@ -398,6 +401,7 @@ export async function fetchMiniAppAdminSettings(
|
||||
if (
|
||||
!response.ok ||
|
||||
!payload.authorized ||
|
||||
!payload.householdName ||
|
||||
!payload.settings ||
|
||||
!payload.assistantConfig ||
|
||||
!payload.topics ||
|
||||
@@ -409,6 +413,7 @@ export async function fetchMiniAppAdminSettings(
|
||||
}
|
||||
|
||||
return {
|
||||
householdName: payload.householdName,
|
||||
settings: payload.settings,
|
||||
assistantConfig: payload.assistantConfig,
|
||||
topics: payload.topics,
|
||||
@@ -423,6 +428,7 @@ export async function updateMiniAppBillingSettings(
|
||||
input: {
|
||||
settlementCurrency?: 'USD' | 'GEL'
|
||||
paymentBalanceAdjustmentPolicy?: 'utilities' | 'rent' | 'separate'
|
||||
householdName?: string
|
||||
rentAmountMajor?: string
|
||||
rentCurrency: 'USD' | 'GEL'
|
||||
rentDueDay: number
|
||||
@@ -434,6 +440,7 @@ export async function updateMiniAppBillingSettings(
|
||||
assistantTone?: string
|
||||
}
|
||||
): Promise<{
|
||||
householdName: string
|
||||
settings: MiniAppBillingSettings
|
||||
assistantConfig: MiniAppAssistantConfig
|
||||
}> {
|
||||
@@ -451,16 +458,24 @@ export async function updateMiniAppBillingSettings(
|
||||
const payload = (await response.json()) as {
|
||||
ok: boolean
|
||||
authorized?: boolean
|
||||
householdName?: string
|
||||
settings?: MiniAppBillingSettings
|
||||
assistantConfig?: MiniAppAssistantConfig
|
||||
error?: string
|
||||
}
|
||||
|
||||
if (!response.ok || !payload.authorized || !payload.settings || !payload.assistantConfig) {
|
||||
if (
|
||||
!response.ok ||
|
||||
!payload.authorized ||
|
||||
!payload.householdName ||
|
||||
!payload.settings ||
|
||||
!payload.assistantConfig
|
||||
) {
|
||||
throw new Error(payload.error ?? 'Failed to update billing settings')
|
||||
}
|
||||
|
||||
return {
|
||||
householdName: payload.householdName,
|
||||
settings: payload.settings,
|
||||
assistantConfig: payload.assistantConfig
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ type UtilityBillDraft = {
|
||||
}
|
||||
|
||||
type BillingForm = {
|
||||
householdName: string
|
||||
settlementCurrency: 'USD' | 'GEL'
|
||||
paymentBalanceAdjustmentPolicy: 'utilities' | 'rent' | 'separate'
|
||||
rentAmountMajor: string
|
||||
@@ -55,6 +56,8 @@ type Props = {
|
||||
locale: 'en' | 'ru'
|
||||
readyIsAdmin: boolean
|
||||
householdDefaultLocale: 'en' | 'ru'
|
||||
householdName: string
|
||||
profileDisplayName: string
|
||||
dashboard: MiniAppDashboard | null
|
||||
adminSettings: MiniAppAdminSettingsPayload | null
|
||||
cycleState: MiniAppAdminCycleState | null
|
||||
@@ -101,6 +104,7 @@ type Props = {
|
||||
effectiveFromPeriod: string | null
|
||||
}
|
||||
onChangeHouseholdLocale: (locale: 'en' | 'ru') => Promise<void>
|
||||
onOpenProfileEditor: () => void
|
||||
onOpenCycleModal: () => void
|
||||
onCloseCycleModal: () => void
|
||||
onSaveCycleRent: () => Promise<void>
|
||||
@@ -111,6 +115,7 @@ type Props = {
|
||||
onOpenBillingSettingsModal: () => void
|
||||
onCloseBillingSettingsModal: () => void
|
||||
onSaveBillingSettings: () => Promise<void>
|
||||
onBillingHouseholdNameChange: (value: string) => void
|
||||
onBillingSettlementCurrencyChange: (value: 'USD' | 'GEL') => void
|
||||
onBillingAdjustmentPolicyChange: (value: 'utilities' | 'rent' | 'separate') => void
|
||||
onBillingRentAmountChange: (value: string) => void
|
||||
@@ -213,15 +218,83 @@ export function HouseScreen(props: Props) {
|
||||
<strong>{props.copy.residentHouseTitle ?? ''}</strong>
|
||||
</header>
|
||||
<p>{props.copy.residentHouseBody ?? ''}</p>
|
||||
<div class="panel-toolbar">
|
||||
<Button variant="secondary" onClick={props.onOpenProfileEditor}>
|
||||
<PencilIcon />
|
||||
{props.copy.manageProfileAction ?? ''}
|
||||
</Button>
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<div class="admin-layout">
|
||||
<HouseSection
|
||||
title={props.copy.houseSectionGeneral ?? ''}
|
||||
body={props.copy.generalSettingsBody}
|
||||
defaultOpen
|
||||
>
|
||||
<section class="admin-section">
|
||||
<div class="admin-grid">
|
||||
<article class="balance-item">
|
||||
<header>
|
||||
<strong>{props.copy.householdNameLabel ?? ''}</strong>
|
||||
<span>{props.householdName}</span>
|
||||
</header>
|
||||
<p>{props.copy.householdNameHint ?? ''}</p>
|
||||
<div class="panel-toolbar">
|
||||
<Button variant="secondary" onClick={props.onOpenBillingSettingsModal}>
|
||||
<SettingsIcon />
|
||||
{props.copy.manageSettingsAction ?? ''}
|
||||
</Button>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
<article class="balance-item">
|
||||
<header>
|
||||
<strong>{props.copy.householdLanguage ?? ''}</strong>
|
||||
<span>{props.householdDefaultLocale.toUpperCase()}</span>
|
||||
</header>
|
||||
<div class="locale-switch__buttons locale-switch__buttons--inline">
|
||||
<button
|
||||
classList={{ 'is-active': props.householdDefaultLocale === 'en' }}
|
||||
type="button"
|
||||
disabled={props.savingHouseholdLocale}
|
||||
onClick={() => void props.onChangeHouseholdLocale('en')}
|
||||
>
|
||||
EN
|
||||
</button>
|
||||
<button
|
||||
classList={{ 'is-active': props.householdDefaultLocale === 'ru' }}
|
||||
type="button"
|
||||
disabled={props.savingHouseholdLocale}
|
||||
onClick={() => void props.onChangeHouseholdLocale('ru')}
|
||||
>
|
||||
RU
|
||||
</button>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
<article class="balance-item">
|
||||
<header>
|
||||
<strong>{props.copy.manageProfileAction ?? ''}</strong>
|
||||
<span>{props.profileDisplayName}</span>
|
||||
</header>
|
||||
<p>{props.copy.profileEditorBody ?? ''}</p>
|
||||
<div class="panel-toolbar">
|
||||
<Button variant="secondary" onClick={props.onOpenProfileEditor}>
|
||||
<PencilIcon />
|
||||
{props.copy.manageProfileAction ?? ''}
|
||||
</Button>
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
</section>
|
||||
</HouseSection>
|
||||
|
||||
<HouseSection
|
||||
title={props.copy.houseSectionBilling ?? ''}
|
||||
body={props.copy.billingSettingsEditorBody}
|
||||
defaultOpen
|
||||
>
|
||||
<section class="admin-section">
|
||||
<div class="admin-grid">
|
||||
@@ -290,31 +363,6 @@ export function HouseScreen(props: Props) {
|
||||
</Button>
|
||||
</div>
|
||||
</article>
|
||||
|
||||
<article class="balance-item">
|
||||
<header>
|
||||
<strong>{props.copy.householdLanguage ?? ''}</strong>
|
||||
<span>{props.householdDefaultLocale.toUpperCase()}</span>
|
||||
</header>
|
||||
<div class="locale-switch__buttons locale-switch__buttons--inline">
|
||||
<button
|
||||
classList={{ 'is-active': props.householdDefaultLocale === 'en' }}
|
||||
type="button"
|
||||
disabled={props.savingHouseholdLocale}
|
||||
onClick={() => void props.onChangeHouseholdLocale('en')}
|
||||
>
|
||||
EN
|
||||
</button>
|
||||
<button
|
||||
classList={{ 'is-active': props.householdDefaultLocale === 'ru' }}
|
||||
type="button"
|
||||
disabled={props.savingHouseholdLocale}
|
||||
onClick={() => void props.onChangeHouseholdLocale('ru')}
|
||||
>
|
||||
RU
|
||||
</button>
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
<Modal
|
||||
open={props.cycleRentOpen}
|
||||
@@ -414,6 +462,18 @@ export function HouseScreen(props: Props) {
|
||||
}
|
||||
>
|
||||
<div class="editor-grid">
|
||||
<Field
|
||||
label={props.copy.householdNameLabel ?? ''}
|
||||
hint={props.copy.householdNameHint ?? ''}
|
||||
wide
|
||||
>
|
||||
<input
|
||||
value={props.billingForm.householdName}
|
||||
onInput={(event) =>
|
||||
props.onBillingHouseholdNameChange(event.currentTarget.value)
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label={props.copy.settlementCurrency ?? ''}>
|
||||
<select
|
||||
value={props.billingForm.settlementCurrency}
|
||||
|
||||
@@ -1320,6 +1320,35 @@ export function createDbHouseholdConfigurationRepository(databaseUrl: string): {
|
||||
}
|
||||
},
|
||||
|
||||
async updateHouseholdName(householdId, householdName) {
|
||||
const updatedHouseholds = await db
|
||||
.update(schema.households)
|
||||
.set({
|
||||
name: householdName
|
||||
})
|
||||
.where(eq(schema.households.id, householdId))
|
||||
.returning({
|
||||
id: schema.households.id,
|
||||
name: schema.households.name,
|
||||
defaultLocale: schema.households.defaultLocale
|
||||
})
|
||||
|
||||
const household = updatedHouseholds[0]
|
||||
if (!household) {
|
||||
throw new Error('Failed to update household name')
|
||||
}
|
||||
|
||||
const chat = await this.getHouseholdChatByHouseholdId(householdId)
|
||||
if (!chat) {
|
||||
throw new Error('Failed to resolve household chat after name update')
|
||||
}
|
||||
|
||||
return {
|
||||
...chat,
|
||||
householdName: household.name
|
||||
}
|
||||
},
|
||||
|
||||
async updateMemberPreferredLocale(householdId, telegramUserId, locale) {
|
||||
const rows = await db
|
||||
.update(schema.members)
|
||||
|
||||
@@ -338,6 +338,7 @@ describe('createHouseholdOnboardingService', () => {
|
||||
member: {
|
||||
id: 'member-42',
|
||||
householdId: 'household-1',
|
||||
householdName: 'Kojori House',
|
||||
displayName: 'Stan',
|
||||
status: 'active',
|
||||
preferredLocale: null,
|
||||
|
||||
@@ -16,6 +16,7 @@ export type HouseholdMiniAppAccess =
|
||||
member: {
|
||||
id: string
|
||||
householdId: string
|
||||
householdName: string
|
||||
displayName: string
|
||||
status: HouseholdMemberRecord['status']
|
||||
isAdmin: boolean
|
||||
@@ -68,6 +69,7 @@ export interface HouseholdOnboardingService {
|
||||
member: {
|
||||
id: string
|
||||
householdId: string
|
||||
householdName: string
|
||||
displayName: string
|
||||
status: HouseholdMemberRecord['status']
|
||||
isAdmin: boolean
|
||||
@@ -161,9 +163,19 @@ export function createHouseholdOnboardingService(options: {
|
||||
) ?? null)
|
||||
|
||||
if (matchingActiveMember) {
|
||||
const household = await options.repository.getHouseholdChatByHouseholdId(
|
||||
matchingActiveMember.householdId
|
||||
)
|
||||
if (!household) {
|
||||
throw new Error('Failed to resolve household for active mini app member')
|
||||
}
|
||||
|
||||
return {
|
||||
status: 'active',
|
||||
member: toMember(matchingActiveMember)
|
||||
member: {
|
||||
...toMember(matchingActiveMember),
|
||||
householdName: household.householdName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,9 +244,19 @@ export function createHouseholdOnboardingService(options: {
|
||||
).find((member) => member.householdId === household.householdId)
|
||||
|
||||
if (activeMember) {
|
||||
const householdRecord = await options.repository.getHouseholdChatByHouseholdId(
|
||||
activeMember.householdId
|
||||
)
|
||||
if (!householdRecord) {
|
||||
throw new Error('Failed to resolve household after mini app join')
|
||||
}
|
||||
|
||||
return {
|
||||
status: 'active',
|
||||
member: toMember(activeMember)
|
||||
member: {
|
||||
...toMember(activeMember),
|
||||
householdName: householdRecord.householdName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,14 @@ function repository(): HouseholdConfigurationRepository {
|
||||
}
|
||||
}),
|
||||
getTelegramHouseholdChat: async () => null,
|
||||
getHouseholdChatByHouseholdId: async () => null,
|
||||
getHouseholdChatByHouseholdId: async () => ({
|
||||
householdId: 'household-1',
|
||||
householdName: 'Kojori House',
|
||||
telegramChatId: '-100123',
|
||||
telegramChatType: 'supergroup',
|
||||
title: 'Kojori House',
|
||||
defaultLocale: 'ru'
|
||||
}),
|
||||
bindHouseholdTopic: async (input) => ({
|
||||
householdId: input.householdId,
|
||||
role: input.role,
|
||||
@@ -268,6 +275,7 @@ describe('createMiniAppAdminService', () => {
|
||||
|
||||
expect(result).toEqual({
|
||||
status: 'ok',
|
||||
householdName: 'Kojori House',
|
||||
settings: {
|
||||
householdId: 'household-1',
|
||||
settlementCurrency: 'GEL',
|
||||
@@ -327,6 +335,7 @@ describe('createMiniAppAdminService', () => {
|
||||
|
||||
expect(result).toEqual({
|
||||
status: 'ok',
|
||||
householdName: 'Kojori House',
|
||||
settings: {
|
||||
householdId: 'household-1',
|
||||
settlementCurrency: 'GEL',
|
||||
|
||||
@@ -29,6 +29,7 @@ export interface MiniAppAdminService {
|
||||
getSettings(input: { householdId: string; actorIsAdmin: boolean }): Promise<
|
||||
| {
|
||||
status: 'ok'
|
||||
householdName: string
|
||||
settings: HouseholdBillingSettingsRecord
|
||||
assistantConfig: HouseholdAssistantConfigRecord
|
||||
categories: readonly HouseholdUtilityCategoryRecord[]
|
||||
@@ -44,6 +45,7 @@ export interface MiniAppAdminService {
|
||||
updateSettings(input: {
|
||||
householdId: string
|
||||
actorIsAdmin: boolean
|
||||
householdName?: string
|
||||
settlementCurrency?: string
|
||||
paymentBalanceAdjustmentPolicy?: string
|
||||
rentAmountMajor?: string
|
||||
@@ -58,6 +60,7 @@ export interface MiniAppAdminService {
|
||||
}): Promise<
|
||||
| {
|
||||
status: 'ok'
|
||||
householdName: string
|
||||
settings: HouseholdBillingSettingsRecord
|
||||
assistantConfig: HouseholdAssistantConfigRecord
|
||||
}
|
||||
@@ -215,6 +218,19 @@ function normalizeDisplayName(raw: string): string | null {
|
||||
return trimmed.replace(/\s+/g, ' ')
|
||||
}
|
||||
|
||||
function normalizeHouseholdName(raw: string | undefined): string | null | undefined {
|
||||
if (raw === undefined) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const trimmed = raw.trim()
|
||||
if (trimmed.length < 2 || trimmed.length > 120) {
|
||||
return null
|
||||
}
|
||||
|
||||
return trimmed.replace(/\s+/g, ' ')
|
||||
}
|
||||
|
||||
function defaultAssistantConfig(householdId: string): HouseholdAssistantConfigRecord {
|
||||
return {
|
||||
householdId,
|
||||
@@ -255,6 +271,11 @@ export function createMiniAppAdminService(
|
||||
}
|
||||
}
|
||||
|
||||
const household = await repository.getHouseholdChatByHouseholdId(input.householdId)
|
||||
if (!household) {
|
||||
throw new Error('Failed to resolve household chat for mini app settings')
|
||||
}
|
||||
|
||||
const [settings, assistantConfig, categories, members, memberAbsencePolicies, topics] =
|
||||
await Promise.all([
|
||||
repository.getHouseholdBillingSettings(input.householdId),
|
||||
@@ -269,6 +290,7 @@ export function createMiniAppAdminService(
|
||||
|
||||
return {
|
||||
status: 'ok',
|
||||
householdName: household.householdName,
|
||||
settings,
|
||||
assistantConfig,
|
||||
categories,
|
||||
@@ -303,8 +325,11 @@ export function createMiniAppAdminService(
|
||||
|
||||
const assistantContext = normalizeAssistantText(input.assistantContext, 1200)
|
||||
const assistantTone = normalizeAssistantText(input.assistantTone, 160)
|
||||
const householdName = normalizeHouseholdName(input.householdName)
|
||||
const nextHouseholdName = householdName ?? undefined
|
||||
|
||||
if (
|
||||
(input.householdName !== undefined && householdName === null) ||
|
||||
(input.assistantContext !== undefined &&
|
||||
assistantContext === null &&
|
||||
input.assistantContext.trim().length > 0) ||
|
||||
@@ -349,7 +374,7 @@ export function createMiniAppAdminService(
|
||||
const shouldUpdateAssistantConfig =
|
||||
assistantContext !== undefined || assistantTone !== undefined
|
||||
|
||||
const [settings, nextAssistantConfig] = await Promise.all([
|
||||
const [settings, nextAssistantConfig, household] = await Promise.all([
|
||||
repository.updateHouseholdBillingSettings({
|
||||
householdId: input.householdId,
|
||||
...(settlementCurrency
|
||||
@@ -398,11 +423,19 @@ export function createMiniAppAdminService(
|
||||
householdId: input.householdId,
|
||||
assistantContext: assistantContext ?? null,
|
||||
assistantTone: assistantTone ?? null
|
||||
})
|
||||
}),
|
||||
nextHouseholdName !== undefined && repository.updateHouseholdName
|
||||
? repository.updateHouseholdName(input.householdId, nextHouseholdName)
|
||||
: repository.getHouseholdChatByHouseholdId(input.householdId)
|
||||
])
|
||||
|
||||
if (!household) {
|
||||
throw new Error('Failed to resolve household chat after settings update')
|
||||
}
|
||||
|
||||
return {
|
||||
status: 'ok',
|
||||
householdName: household.householdName,
|
||||
settings,
|
||||
assistantConfig: nextAssistantConfig
|
||||
}
|
||||
|
||||
@@ -213,6 +213,10 @@ export interface HouseholdConfigurationRepository {
|
||||
householdId: string,
|
||||
locale: SupportedLocale
|
||||
): Promise<HouseholdTelegramChatRecord>
|
||||
updateHouseholdName?(
|
||||
householdId: string,
|
||||
householdName: string
|
||||
): Promise<HouseholdTelegramChatRecord>
|
||||
updateMemberPreferredLocale(
|
||||
householdId: string,
|
||||
telegramUserId: string,
|
||||
|
||||
Reference in New Issue
Block a user