mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 14:04:04 +00:00
feat(miniapp): add member rent weight controls
This commit is contained in:
@@ -39,6 +39,7 @@ import {
|
|||||||
createMiniAppPendingMembersHandler,
|
createMiniAppPendingMembersHandler,
|
||||||
createMiniAppPromoteMemberHandler,
|
createMiniAppPromoteMemberHandler,
|
||||||
createMiniAppSettingsHandler,
|
createMiniAppSettingsHandler,
|
||||||
|
createMiniAppUpdateMemberRentWeightHandler,
|
||||||
createMiniAppUpdateSettingsHandler,
|
createMiniAppUpdateSettingsHandler,
|
||||||
createMiniAppUpsertUtilityCategoryHandler
|
createMiniAppUpsertUtilityCategoryHandler
|
||||||
} from './miniapp-admin'
|
} from './miniapp-admin'
|
||||||
@@ -358,6 +359,15 @@ const server = createBotWebhookServer({
|
|||||||
logger: getLogger('miniapp-admin')
|
logger: getLogger('miniapp-admin')
|
||||||
})
|
})
|
||||||
: undefined,
|
: undefined,
|
||||||
|
miniAppUpdateMemberRentWeight: householdOnboardingService
|
||||||
|
? createMiniAppUpdateMemberRentWeightHandler({
|
||||||
|
allowedOrigins: runtime.miniAppAllowedOrigins,
|
||||||
|
botToken: runtime.telegramBotToken,
|
||||||
|
onboardingService: householdOnboardingService,
|
||||||
|
miniAppAdminService: miniAppAdminService!,
|
||||||
|
logger: getLogger('miniapp-admin')
|
||||||
|
})
|
||||||
|
: undefined,
|
||||||
miniAppBillingCycle: householdOnboardingService
|
miniAppBillingCycle: householdOnboardingService
|
||||||
? createMiniAppBillingCycleHandler({
|
? createMiniAppBillingCycleHandler({
|
||||||
allowedOrigins: runtime.miniAppAllowedOrigins,
|
allowedOrigins: runtime.miniAppAllowedOrigins,
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ function onboardingRepository(): HouseholdConfigurationRepository {
|
|||||||
displayName: input.displayName,
|
displayName: input.displayName,
|
||||||
preferredLocale: input.preferredLocale ?? null,
|
preferredLocale: input.preferredLocale ?? null,
|
||||||
householdDefaultLocale: household.defaultLocale,
|
householdDefaultLocale: household.defaultLocale,
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: input.isAdmin === true
|
isAdmin: input.isAdmin === true
|
||||||
}),
|
}),
|
||||||
getHouseholdMember: async () => null,
|
getHouseholdMember: async () => null,
|
||||||
@@ -94,6 +95,7 @@ function onboardingRepository(): HouseholdConfigurationRepository {
|
|||||||
displayName: 'Mia',
|
displayName: 'Mia',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: household.defaultLocale,
|
householdDefaultLocale: household.defaultLocale,
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: false
|
isAdmin: false
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
@@ -110,6 +112,7 @@ function onboardingRepository(): HouseholdConfigurationRepository {
|
|||||||
displayName: 'Mia',
|
displayName: 'Mia',
|
||||||
preferredLocale: locale,
|
preferredLocale: locale,
|
||||||
householdDefaultLocale: household.defaultLocale,
|
householdDefaultLocale: household.defaultLocale,
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: false
|
isAdmin: false
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
@@ -151,6 +154,7 @@ function onboardingRepository(): HouseholdConfigurationRepository {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: household.defaultLocale,
|
householdDefaultLocale: household.defaultLocale,
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: false
|
isAdmin: false
|
||||||
}
|
}
|
||||||
].find((entry) => entry.id === memberId)
|
].find((entry) => entry.id === memberId)
|
||||||
@@ -161,7 +165,20 @@ function onboardingRepository(): HouseholdConfigurationRepository {
|
|||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
: null
|
: null
|
||||||
|
},
|
||||||
|
updateHouseholdMemberRentShareWeight: async (_householdId, memberId, rentShareWeight) =>
|
||||||
|
memberId === 'member-123456'
|
||||||
|
? {
|
||||||
|
id: memberId,
|
||||||
|
householdId: household.householdId,
|
||||||
|
telegramUserId: '123456',
|
||||||
|
displayName: 'Stan',
|
||||||
|
preferredLocale: null,
|
||||||
|
householdDefaultLocale: household.defaultLocale,
|
||||||
|
rentShareWeight,
|
||||||
|
isAdmin: false
|
||||||
}
|
}
|
||||||
|
: null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,6 +194,7 @@ describe('createMiniAppPendingMembersHandler', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -239,6 +257,7 @@ describe('createMiniAppApproveMemberHandler', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -282,6 +301,7 @@ describe('createMiniAppApproveMemberHandler', () => {
|
|||||||
displayName: 'Mia',
|
displayName: 'Mia',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: false
|
isAdmin: false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -300,6 +320,7 @@ describe('createMiniAppSettingsHandler', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -311,6 +332,7 @@ describe('createMiniAppSettingsHandler', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -365,6 +387,7 @@ describe('createMiniAppSettingsHandler', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -384,6 +407,7 @@ describe('createMiniAppUpdateSettingsHandler', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -452,6 +476,7 @@ describe('createMiniAppPromoteMemberHandler', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -495,6 +520,7 @@ describe('createMiniAppPromoteMemberHandler', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -178,6 +178,37 @@ async function readPromoteMemberPayload(request: Request): Promise<{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function readRentWeightPayload(request: Request): Promise<{
|
||||||
|
initData: string
|
||||||
|
memberId: string
|
||||||
|
rentShareWeight: number
|
||||||
|
}> {
|
||||||
|
const clonedRequest = request.clone()
|
||||||
|
const payload = await readMiniAppRequestPayload(request)
|
||||||
|
if (!payload.initData) {
|
||||||
|
throw new Error('Missing initData')
|
||||||
|
}
|
||||||
|
|
||||||
|
const text = await clonedRequest.text()
|
||||||
|
let parsed: { memberId?: string; rentShareWeight?: number }
|
||||||
|
try {
|
||||||
|
parsed = JSON.parse(text)
|
||||||
|
} catch {
|
||||||
|
throw new Error('Invalid JSON body')
|
||||||
|
}
|
||||||
|
|
||||||
|
const memberId = parsed.memberId?.trim()
|
||||||
|
if (!memberId || typeof parsed.rentShareWeight !== 'number') {
|
||||||
|
throw new Error('Missing member rent weight fields')
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
initData: payload.initData,
|
||||||
|
memberId,
|
||||||
|
rentShareWeight: parsed.rentShareWeight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function serializeBillingSettings(settings: HouseholdBillingSettingsRecord) {
|
function serializeBillingSettings(settings: HouseholdBillingSettingsRecord) {
|
||||||
return {
|
return {
|
||||||
householdId: settings.householdId,
|
householdId: settings.householdId,
|
||||||
@@ -629,6 +660,97 @@ export function createMiniAppPromoteMemberHandler(options: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createMiniAppUpdateMemberRentWeightHandler(options: {
|
||||||
|
allowedOrigins: readonly string[]
|
||||||
|
botToken: string
|
||||||
|
onboardingService: HouseholdOnboardingService
|
||||||
|
miniAppAdminService: MiniAppAdminService
|
||||||
|
logger?: Logger
|
||||||
|
}): {
|
||||||
|
handler: (request: Request) => Promise<Response>
|
||||||
|
} {
|
||||||
|
const sessionService = createMiniAppSessionService({
|
||||||
|
botToken: options.botToken,
|
||||||
|
onboardingService: options.onboardingService
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
handler: async (request) => {
|
||||||
|
const origin = allowedMiniAppOrigin(request, options.allowedOrigins)
|
||||||
|
|
||||||
|
if (request.method === 'OPTIONS') {
|
||||||
|
return miniAppJsonResponse({ ok: true }, 204, origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.method !== 'POST') {
|
||||||
|
return miniAppJsonResponse({ ok: false, error: 'Method Not Allowed' }, 405, origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const payload = await readRentWeightPayload(request)
|
||||||
|
const session = await sessionService.authenticate({
|
||||||
|
initData: payload.initData
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!session) {
|
||||||
|
return miniAppJsonResponse(
|
||||||
|
{ ok: false, error: 'Invalid Telegram init data' },
|
||||||
|
401,
|
||||||
|
origin
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!session.authorized || !session.member) {
|
||||||
|
return miniAppJsonResponse(
|
||||||
|
{ ok: false, error: 'Access limited to active household members' },
|
||||||
|
403,
|
||||||
|
origin
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await options.miniAppAdminService.updateMemberRentShareWeight({
|
||||||
|
householdId: session.member.householdId,
|
||||||
|
actorIsAdmin: session.member.isAdmin,
|
||||||
|
memberId: payload.memberId,
|
||||||
|
rentShareWeight: payload.rentShareWeight
|
||||||
|
})
|
||||||
|
|
||||||
|
if (result.status === 'rejected') {
|
||||||
|
return miniAppJsonResponse(
|
||||||
|
{
|
||||||
|
ok: false,
|
||||||
|
error:
|
||||||
|
result.reason === 'invalid_weight'
|
||||||
|
? 'Invalid rent share weight'
|
||||||
|
: result.reason === 'member_not_found'
|
||||||
|
? 'Member not found'
|
||||||
|
: 'Admin access required'
|
||||||
|
},
|
||||||
|
result.reason === 'invalid_weight'
|
||||||
|
? 400
|
||||||
|
: result.reason === 'member_not_found'
|
||||||
|
? 404
|
||||||
|
: 403,
|
||||||
|
origin
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return miniAppJsonResponse(
|
||||||
|
{
|
||||||
|
ok: true,
|
||||||
|
authorized: true,
|
||||||
|
member: result.member
|
||||||
|
},
|
||||||
|
200,
|
||||||
|
origin
|
||||||
|
)
|
||||||
|
} catch (error) {
|
||||||
|
return miniAppErrorResponse(error, origin, options.logger)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createMiniAppApproveMemberHandler(options: {
|
export function createMiniAppApproveMemberHandler(options: {
|
||||||
allowedOrigins: readonly string[]
|
allowedOrigins: readonly string[]
|
||||||
botToken: string
|
botToken: string
|
||||||
|
|||||||
@@ -73,6 +73,15 @@ describe('createBotWebhookServer', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
miniAppUpdateMemberRentWeight: {
|
||||||
|
handler: async () =>
|
||||||
|
new Response(JSON.stringify({ ok: true, authorized: true, member: {} }), {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json; charset=utf-8'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
miniAppBillingCycle: {
|
miniAppBillingCycle: {
|
||||||
handler: async () =>
|
handler: async () =>
|
||||||
new Response(JSON.stringify({ ok: true, authorized: true, cycleState: {} }), {
|
new Response(JSON.stringify({ ok: true, authorized: true, cycleState: {} }), {
|
||||||
@@ -304,6 +313,22 @@ describe('createBotWebhookServer', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('accepts mini app rent weight update request', async () => {
|
||||||
|
const response = await server.fetch(
|
||||||
|
new Request('http://localhost/api/miniapp/admin/members/rent-weight', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ initData: 'payload' })
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(response.status).toBe(200)
|
||||||
|
expect(await response.json()).toEqual({
|
||||||
|
ok: true,
|
||||||
|
authorized: true,
|
||||||
|
member: {}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
test('accepts mini app billing cycle request', async () => {
|
test('accepts mini app billing cycle request', async () => {
|
||||||
const response = await server.fetch(
|
const response = await server.fetch(
|
||||||
new Request('http://localhost/api/miniapp/admin/billing-cycle', {
|
new Request('http://localhost/api/miniapp/admin/billing-cycle', {
|
||||||
|
|||||||
@@ -56,6 +56,12 @@ export interface BotWebhookServerOptions {
|
|||||||
handler: (request: Request) => Promise<Response>
|
handler: (request: Request) => Promise<Response>
|
||||||
}
|
}
|
||||||
| undefined
|
| undefined
|
||||||
|
miniAppUpdateMemberRentWeight?:
|
||||||
|
| {
|
||||||
|
path?: string
|
||||||
|
handler: (request: Request) => Promise<Response>
|
||||||
|
}
|
||||||
|
| undefined
|
||||||
miniAppBillingCycle?:
|
miniAppBillingCycle?:
|
||||||
| {
|
| {
|
||||||
path?: string
|
path?: string
|
||||||
@@ -136,6 +142,8 @@ export function createBotWebhookServer(options: BotWebhookServerOptions): {
|
|||||||
options.miniAppUpsertUtilityCategory?.path ?? '/api/miniapp/admin/utility-categories/upsert'
|
options.miniAppUpsertUtilityCategory?.path ?? '/api/miniapp/admin/utility-categories/upsert'
|
||||||
const miniAppPromoteMemberPath =
|
const miniAppPromoteMemberPath =
|
||||||
options.miniAppPromoteMember?.path ?? '/api/miniapp/admin/members/promote'
|
options.miniAppPromoteMember?.path ?? '/api/miniapp/admin/members/promote'
|
||||||
|
const miniAppUpdateMemberRentWeightPath =
|
||||||
|
options.miniAppUpdateMemberRentWeight?.path ?? '/api/miniapp/admin/members/rent-weight'
|
||||||
const miniAppBillingCyclePath =
|
const miniAppBillingCyclePath =
|
||||||
options.miniAppBillingCycle?.path ?? '/api/miniapp/admin/billing-cycle'
|
options.miniAppBillingCycle?.path ?? '/api/miniapp/admin/billing-cycle'
|
||||||
const miniAppOpenCyclePath =
|
const miniAppOpenCyclePath =
|
||||||
@@ -198,6 +206,13 @@ export function createBotWebhookServer(options: BotWebhookServerOptions): {
|
|||||||
return await options.miniAppPromoteMember.handler(request)
|
return await options.miniAppPromoteMember.handler(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
options.miniAppUpdateMemberRentWeight &&
|
||||||
|
url.pathname === miniAppUpdateMemberRentWeightPath
|
||||||
|
) {
|
||||||
|
return await options.miniAppUpdateMemberRentWeight.handler(request)
|
||||||
|
}
|
||||||
|
|
||||||
if (options.miniAppBillingCycle && url.pathname === miniAppBillingCyclePath) {
|
if (options.miniAppBillingCycle && url.pathname === miniAppBillingCyclePath) {
|
||||||
return await options.miniAppBillingCycle.handler(request)
|
return await options.miniAppBillingCycle.handler(request)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
joinMiniAppHousehold,
|
joinMiniAppHousehold,
|
||||||
openMiniAppBillingCycle,
|
openMiniAppBillingCycle,
|
||||||
promoteMiniAppMember,
|
promoteMiniAppMember,
|
||||||
|
updateMiniAppMemberRentWeight,
|
||||||
type MiniAppAdminCycleState,
|
type MiniAppAdminCycleState,
|
||||||
type MiniAppAdminSettingsPayload,
|
type MiniAppAdminSettingsPayload,
|
||||||
updateMiniAppLocalePreference,
|
updateMiniAppLocalePreference,
|
||||||
@@ -143,6 +144,8 @@ function App() {
|
|||||||
const [joining, setJoining] = createSignal(false)
|
const [joining, setJoining] = createSignal(false)
|
||||||
const [approvingTelegramUserId, setApprovingTelegramUserId] = createSignal<string | null>(null)
|
const [approvingTelegramUserId, setApprovingTelegramUserId] = createSignal<string | null>(null)
|
||||||
const [promotingMemberId, setPromotingMemberId] = createSignal<string | null>(null)
|
const [promotingMemberId, setPromotingMemberId] = createSignal<string | null>(null)
|
||||||
|
const [savingRentWeightMemberId, setSavingRentWeightMemberId] = createSignal<string | null>(null)
|
||||||
|
const [rentWeightDrafts, setRentWeightDrafts] = createSignal<Record<string, string>>({})
|
||||||
const [savingMemberLocale, setSavingMemberLocale] = createSignal(false)
|
const [savingMemberLocale, setSavingMemberLocale] = createSignal(false)
|
||||||
const [savingHouseholdLocale, setSavingHouseholdLocale] = createSignal(false)
|
const [savingHouseholdLocale, setSavingHouseholdLocale] = createSignal(false)
|
||||||
const [savingBillingSettings, setSavingBillingSettings] = createSignal(false)
|
const [savingBillingSettings, setSavingBillingSettings] = createSignal(false)
|
||||||
@@ -212,6 +215,11 @@ function App() {
|
|||||||
try {
|
try {
|
||||||
const payload = await fetchMiniAppAdminSettings(initData)
|
const payload = await fetchMiniAppAdminSettings(initData)
|
||||||
setAdminSettings(payload)
|
setAdminSettings(payload)
|
||||||
|
setRentWeightDrafts(
|
||||||
|
Object.fromEntries(
|
||||||
|
payload.members.map((member) => [member.id, String(member.rentShareWeight)])
|
||||||
|
)
|
||||||
|
)
|
||||||
setCycleForm((current) => ({
|
setCycleForm((current) => ({
|
||||||
...current,
|
...current,
|
||||||
utilityCategorySlug:
|
utilityCategorySlug:
|
||||||
@@ -721,11 +729,50 @@ function App() {
|
|||||||
}
|
}
|
||||||
: current
|
: current
|
||||||
)
|
)
|
||||||
|
setRentWeightDrafts((current) => ({
|
||||||
|
...current,
|
||||||
|
[member.id]: String(member.rentShareWeight)
|
||||||
|
}))
|
||||||
} finally {
|
} finally {
|
||||||
setPromotingMemberId(null)
|
setPromotingMemberId(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleSaveRentWeight(memberId: string) {
|
||||||
|
const initData = webApp?.initData?.trim()
|
||||||
|
const currentReady = readySession()
|
||||||
|
const nextWeight = Number(rentWeightDrafts()[memberId] ?? '')
|
||||||
|
if (
|
||||||
|
!initData ||
|
||||||
|
currentReady?.mode !== 'live' ||
|
||||||
|
!currentReady.member.isAdmin ||
|
||||||
|
!Number.isInteger(nextWeight) ||
|
||||||
|
nextWeight <= 0
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setSavingRentWeightMemberId(memberId)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const member = await updateMiniAppMemberRentWeight(initData, memberId, nextWeight)
|
||||||
|
setAdminSettings((current) =>
|
||||||
|
current
|
||||||
|
? {
|
||||||
|
...current,
|
||||||
|
members: current.members.map((item) => (item.id === member.id ? member : item))
|
||||||
|
}
|
||||||
|
: current
|
||||||
|
)
|
||||||
|
setRentWeightDrafts((current) => ({
|
||||||
|
...current,
|
||||||
|
[member.id]: String(member.rentShareWeight)
|
||||||
|
}))
|
||||||
|
} finally {
|
||||||
|
setSavingRentWeightMemberId(null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const renderPanel = () => {
|
const renderPanel = () => {
|
||||||
switch (activeNav()) {
|
switch (activeNav()) {
|
||||||
case 'balances':
|
case 'balances':
|
||||||
@@ -1223,6 +1270,32 @@ function App() {
|
|||||||
<strong>{member.displayName}</strong>
|
<strong>{member.displayName}</strong>
|
||||||
<span>{member.isAdmin ? copy().adminTag : copy().residentTag}</span>
|
<span>{member.isAdmin ? copy().adminTag : copy().residentTag}</span>
|
||||||
</header>
|
</header>
|
||||||
|
<label class="settings-field settings-field--wide">
|
||||||
|
<span>{copy().rentWeightLabel}</span>
|
||||||
|
<input
|
||||||
|
inputmode="numeric"
|
||||||
|
value={rentWeightDrafts()[member.id] ?? String(member.rentShareWeight)}
|
||||||
|
onInput={(event) =>
|
||||||
|
setRentWeightDrafts((current) => ({
|
||||||
|
...current,
|
||||||
|
[member.id]: event.currentTarget.value
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<button
|
||||||
|
class="ghost-button"
|
||||||
|
type="button"
|
||||||
|
disabled={
|
||||||
|
savingRentWeightMemberId() === member.id ||
|
||||||
|
Number(rentWeightDrafts()[member.id] ?? member.rentShareWeight) <= 0
|
||||||
|
}
|
||||||
|
onClick={() => void handleSaveRentWeight(member.id)}
|
||||||
|
>
|
||||||
|
{savingRentWeightMemberId() === member.id
|
||||||
|
? copy().savingRentWeight
|
||||||
|
: copy().saveRentWeightAction}
|
||||||
|
</button>
|
||||||
{!member.isAdmin ? (
|
{!member.isAdmin ? (
|
||||||
<button
|
<button
|
||||||
class="ghost-button"
|
class="ghost-button"
|
||||||
|
|||||||
@@ -88,6 +88,9 @@ export const dictionary = {
|
|||||||
savingCategory: 'Saving…',
|
savingCategory: 'Saving…',
|
||||||
adminsTitle: 'Admins',
|
adminsTitle: 'Admins',
|
||||||
adminsBody: 'Promote trusted household members so they can manage billing and approvals.',
|
adminsBody: 'Promote trusted household members so they can manage billing and approvals.',
|
||||||
|
rentWeightLabel: 'Rent weight',
|
||||||
|
saveRentWeightAction: 'Save rent weight',
|
||||||
|
savingRentWeight: 'Saving weight…',
|
||||||
promoteAdminAction: 'Promote to admin',
|
promoteAdminAction: 'Promote to admin',
|
||||||
promotingAdmin: 'Promoting…',
|
promotingAdmin: 'Promoting…',
|
||||||
residentHouseTitle: 'Household access',
|
residentHouseTitle: 'Household access',
|
||||||
@@ -192,6 +195,9 @@ export const dictionary = {
|
|||||||
adminsTitle: 'Админы',
|
adminsTitle: 'Админы',
|
||||||
adminsBody:
|
adminsBody:
|
||||||
'Повышай доверенных участников, чтобы они могли управлять биллингом и подтверждениями.',
|
'Повышай доверенных участников, чтобы они могли управлять биллингом и подтверждениями.',
|
||||||
|
rentWeightLabel: 'Вес аренды',
|
||||||
|
saveRentWeightAction: 'Сохранить вес аренды',
|
||||||
|
savingRentWeight: 'Сохраняем вес…',
|
||||||
promoteAdminAction: 'Сделать админом',
|
promoteAdminAction: 'Сделать админом',
|
||||||
promotingAdmin: 'Повышаем…',
|
promotingAdmin: 'Повышаем…',
|
||||||
residentHouseTitle: 'Доступ к household',
|
residentHouseTitle: 'Доступ к household',
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ export interface MiniAppPendingMember {
|
|||||||
export interface MiniAppMember {
|
export interface MiniAppMember {
|
||||||
id: string
|
id: string
|
||||||
displayName: string
|
displayName: string
|
||||||
|
rentShareWeight: number
|
||||||
isAdmin: boolean
|
isAdmin: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,6 +453,37 @@ export async function promoteMiniAppMember(
|
|||||||
return payload.member
|
return payload.member
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function updateMiniAppMemberRentWeight(
|
||||||
|
initData: string,
|
||||||
|
memberId: string,
|
||||||
|
rentShareWeight: number
|
||||||
|
): Promise<MiniAppMember> {
|
||||||
|
const response = await fetch(`${apiBaseUrl()}/api/miniapp/admin/members/rent-weight`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
initData,
|
||||||
|
memberId,
|
||||||
|
rentShareWeight
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const payload = (await response.json()) as {
|
||||||
|
ok: boolean
|
||||||
|
authorized?: boolean
|
||||||
|
member?: MiniAppMember
|
||||||
|
error?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok || !payload.member) {
|
||||||
|
throw new Error(payload.error ?? 'Failed to update member rent weight')
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload.member
|
||||||
|
}
|
||||||
|
|
||||||
export async function fetchMiniAppBillingCycle(initData: string): Promise<MiniAppAdminCycleState> {
|
export async function fetchMiniAppBillingCycle(initData: string): Promise<MiniAppAdminCycleState> {
|
||||||
const response = await fetch(`${apiBaseUrl()}/api/miniapp/admin/billing-cycle`, {
|
const response = await fetch(`${apiBaseUrl()}/api/miniapp/admin/billing-cycle`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ function repository(): HouseholdConfigurationRepository {
|
|||||||
displayName: input.displayName,
|
displayName: input.displayName,
|
||||||
preferredLocale: input.preferredLocale ?? null,
|
preferredLocale: input.preferredLocale ?? null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: input.isAdmin === true
|
isAdmin: input.isAdmin === true
|
||||||
}),
|
}),
|
||||||
getHouseholdMember: async () => null,
|
getHouseholdMember: async () => null,
|
||||||
@@ -80,6 +81,7 @@ function repository(): HouseholdConfigurationRepository {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: false
|
isAdmin: false
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
@@ -100,6 +102,7 @@ function repository(): HouseholdConfigurationRepository {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: locale,
|
preferredLocale: locale,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: false
|
isAdmin: false
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
@@ -141,8 +144,22 @@ function repository(): HouseholdConfigurationRepository {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
|
: null,
|
||||||
|
updateHouseholdMemberRentShareWeight: async (_householdId, memberId, rentShareWeight) =>
|
||||||
|
memberId === 'member-123456'
|
||||||
|
? {
|
||||||
|
id: memberId,
|
||||||
|
householdId: 'household-1',
|
||||||
|
telegramUserId: '123456',
|
||||||
|
displayName: 'Stan',
|
||||||
|
preferredLocale: null,
|
||||||
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight,
|
||||||
|
isAdmin: false
|
||||||
|
}
|
||||||
: null
|
: null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -283,6 +300,7 @@ describe('createMiniAppAdminService', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: false
|
isAdmin: false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -306,6 +324,7 @@ describe('createMiniAppAdminService', () => {
|
|||||||
displayName: 'Stan',
|
displayName: 'Stan',
|
||||||
preferredLocale: null,
|
preferredLocale: null,
|
||||||
householdDefaultLocale: 'ru',
|
householdDefaultLocale: 'ru',
|
||||||
|
rentShareWeight: 1,
|
||||||
isAdmin: true
|
isAdmin: true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -108,6 +108,21 @@ export interface MiniAppAdminService {
|
|||||||
reason: 'not_admin' | 'member_not_found'
|
reason: 'not_admin' | 'member_not_found'
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
|
updateMemberRentShareWeight(input: {
|
||||||
|
householdId: string
|
||||||
|
actorIsAdmin: boolean
|
||||||
|
memberId: string
|
||||||
|
rentShareWeight: number
|
||||||
|
}): Promise<
|
||||||
|
| {
|
||||||
|
status: 'ok'
|
||||||
|
member: HouseholdMemberRecord
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
status: 'rejected'
|
||||||
|
reason: 'not_admin' | 'invalid_weight' | 'member_not_found'
|
||||||
|
}
|
||||||
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createMiniAppAdminService(
|
export function createMiniAppAdminService(
|
||||||
@@ -288,6 +303,39 @@ export function createMiniAppAdminService(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: 'ok',
|
||||||
|
member
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async updateMemberRentShareWeight(input) {
|
||||||
|
if (!input.actorIsAdmin) {
|
||||||
|
return {
|
||||||
|
status: 'rejected',
|
||||||
|
reason: 'not_admin'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Number.isInteger(input.rentShareWeight) || input.rentShareWeight <= 0) {
|
||||||
|
return {
|
||||||
|
status: 'rejected',
|
||||||
|
reason: 'invalid_weight'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const member = await repository.updateHouseholdMemberRentShareWeight(
|
||||||
|
input.householdId,
|
||||||
|
input.memberId,
|
||||||
|
input.rentShareWeight
|
||||||
|
)
|
||||||
|
if (!member) {
|
||||||
|
return {
|
||||||
|
status: 'rejected',
|
||||||
|
reason: 'member_not_found'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: 'ok',
|
status: 'ok',
|
||||||
member
|
member
|
||||||
|
|||||||
Reference in New Issue
Block a user