mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 22:34:03 +00:00
feat(miniapp): add pending member admin approval
This commit is contained in:
@@ -7,6 +7,7 @@ export {
|
||||
export { createFinanceCommandService, type FinanceCommandService } from './finance-command-service'
|
||||
export { createHouseholdSetupService, type HouseholdSetupService } from './household-setup-service'
|
||||
export { createHouseholdAdminService, type HouseholdAdminService } from './household-admin-service'
|
||||
export { createMiniAppAdminService, type MiniAppAdminService } from './miniapp-admin-service'
|
||||
export {
|
||||
createHouseholdOnboardingService,
|
||||
type HouseholdMiniAppAccess,
|
||||
|
||||
138
packages/application/src/miniapp-admin-service.test.ts
Normal file
138
packages/application/src/miniapp-admin-service.test.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
|
||||
import type { HouseholdConfigurationRepository } from '@household/ports'
|
||||
|
||||
import { createMiniAppAdminService } from './miniapp-admin-service'
|
||||
|
||||
function repository(): HouseholdConfigurationRepository {
|
||||
return {
|
||||
registerTelegramHouseholdChat: async () => ({
|
||||
status: 'existing',
|
||||
household: {
|
||||
householdId: 'household-1',
|
||||
householdName: 'Kojori House',
|
||||
telegramChatId: '-100123',
|
||||
telegramChatType: 'supergroup',
|
||||
title: 'Kojori House'
|
||||
}
|
||||
}),
|
||||
getTelegramHouseholdChat: async () => null,
|
||||
getHouseholdChatByHouseholdId: async () => null,
|
||||
bindHouseholdTopic: async (input) => ({
|
||||
householdId: input.householdId,
|
||||
role: input.role,
|
||||
telegramThreadId: input.telegramThreadId,
|
||||
topicName: input.topicName?.trim() || null
|
||||
}),
|
||||
getHouseholdTopicBinding: async () => null,
|
||||
findHouseholdTopicByTelegramContext: async () => null,
|
||||
listHouseholdTopicBindings: async () => [],
|
||||
upsertHouseholdJoinToken: async () => ({
|
||||
householdId: 'household-1',
|
||||
householdName: 'Kojori House',
|
||||
token: 'join-token',
|
||||
createdByTelegramUserId: null
|
||||
}),
|
||||
getHouseholdJoinToken: async () => null,
|
||||
getHouseholdByJoinToken: async () => null,
|
||||
upsertPendingHouseholdMember: async (input) => ({
|
||||
householdId: input.householdId,
|
||||
householdName: 'Kojori House',
|
||||
telegramUserId: input.telegramUserId,
|
||||
displayName: input.displayName,
|
||||
username: input.username?.trim() || null,
|
||||
languageCode: input.languageCode?.trim() || null
|
||||
}),
|
||||
getPendingHouseholdMember: async () => null,
|
||||
findPendingHouseholdMemberByTelegramUserId: async () => null,
|
||||
ensureHouseholdMember: async (input) => ({
|
||||
id: `member-${input.telegramUserId}`,
|
||||
householdId: input.householdId,
|
||||
telegramUserId: input.telegramUserId,
|
||||
displayName: input.displayName,
|
||||
isAdmin: input.isAdmin === true
|
||||
}),
|
||||
getHouseholdMember: async () => null,
|
||||
listHouseholdMembersByTelegramUserId: async () => [],
|
||||
listPendingHouseholdMembers: async () => [
|
||||
{
|
||||
householdId: 'household-1',
|
||||
householdName: 'Kojori House',
|
||||
telegramUserId: '123456',
|
||||
displayName: 'Stan',
|
||||
username: 'stan',
|
||||
languageCode: 'ru'
|
||||
}
|
||||
],
|
||||
approvePendingHouseholdMember: async (input) =>
|
||||
input.telegramUserId === '123456'
|
||||
? {
|
||||
id: 'member-123456',
|
||||
householdId: input.householdId,
|
||||
telegramUserId: input.telegramUserId,
|
||||
displayName: 'Stan',
|
||||
isAdmin: false
|
||||
}
|
||||
: null
|
||||
}
|
||||
}
|
||||
|
||||
describe('createMiniAppAdminService', () => {
|
||||
test('lists pending members for admins', async () => {
|
||||
const service = createMiniAppAdminService(repository())
|
||||
|
||||
const result = await service.listPendingMembers({
|
||||
householdId: 'household-1',
|
||||
actorIsAdmin: true
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
status: 'ok',
|
||||
members: [
|
||||
{
|
||||
householdId: 'household-1',
|
||||
householdName: 'Kojori House',
|
||||
telegramUserId: '123456',
|
||||
displayName: 'Stan',
|
||||
username: 'stan',
|
||||
languageCode: 'ru'
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
|
||||
test('rejects pending member listing for non-admins', async () => {
|
||||
const service = createMiniAppAdminService(repository())
|
||||
|
||||
const result = await service.listPendingMembers({
|
||||
householdId: 'household-1',
|
||||
actorIsAdmin: false
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
status: 'rejected',
|
||||
reason: 'not_admin'
|
||||
})
|
||||
})
|
||||
|
||||
test('approves a pending member for admins', async () => {
|
||||
const service = createMiniAppAdminService(repository())
|
||||
|
||||
const result = await service.approvePendingMember({
|
||||
householdId: 'household-1',
|
||||
actorIsAdmin: true,
|
||||
pendingTelegramUserId: '123456'
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
status: 'approved',
|
||||
member: {
|
||||
id: 'member-123456',
|
||||
householdId: 'household-1',
|
||||
telegramUserId: '123456',
|
||||
displayName: 'Stan',
|
||||
isAdmin: false
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
78
packages/application/src/miniapp-admin-service.ts
Normal file
78
packages/application/src/miniapp-admin-service.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import type {
|
||||
HouseholdConfigurationRepository,
|
||||
HouseholdMemberRecord,
|
||||
HouseholdPendingMemberRecord
|
||||
} from '@household/ports'
|
||||
|
||||
export interface MiniAppAdminService {
|
||||
listPendingMembers(input: { householdId: string; actorIsAdmin: boolean }): Promise<
|
||||
| {
|
||||
status: 'ok'
|
||||
members: readonly HouseholdPendingMemberRecord[]
|
||||
}
|
||||
| {
|
||||
status: 'rejected'
|
||||
reason: 'not_admin'
|
||||
}
|
||||
>
|
||||
approvePendingMember(input: {
|
||||
householdId: string
|
||||
actorIsAdmin: boolean
|
||||
pendingTelegramUserId: string
|
||||
}): Promise<
|
||||
| {
|
||||
status: 'approved'
|
||||
member: HouseholdMemberRecord
|
||||
}
|
||||
| {
|
||||
status: 'rejected'
|
||||
reason: 'not_admin' | 'pending_not_found'
|
||||
}
|
||||
>
|
||||
}
|
||||
|
||||
export function createMiniAppAdminService(
|
||||
repository: HouseholdConfigurationRepository
|
||||
): MiniAppAdminService {
|
||||
return {
|
||||
async listPendingMembers(input) {
|
||||
if (!input.actorIsAdmin) {
|
||||
return {
|
||||
status: 'rejected',
|
||||
reason: 'not_admin'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
status: 'ok',
|
||||
members: await repository.listPendingHouseholdMembers(input.householdId)
|
||||
}
|
||||
},
|
||||
|
||||
async approvePendingMember(input) {
|
||||
if (!input.actorIsAdmin) {
|
||||
return {
|
||||
status: 'rejected',
|
||||
reason: 'not_admin'
|
||||
}
|
||||
}
|
||||
|
||||
const member = await repository.approvePendingHouseholdMember({
|
||||
householdId: input.householdId,
|
||||
telegramUserId: input.pendingTelegramUserId
|
||||
})
|
||||
|
||||
if (!member) {
|
||||
return {
|
||||
status: 'rejected',
|
||||
reason: 'pending_not_found'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
status: 'approved',
|
||||
member
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user