feat: phase 3 - ledger and asset management
Add group expense tracking (ledger) and public asset inventory (asset) features. Ledger supports income/expense recording with monthly summary. Asset tracks group equipment with free-form holder transfer. Both are independent pages accessible from GroupView navigation. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import { pb } from './pocketbase'
|
||||
import type { Asset } from '@/types'
|
||||
|
||||
export interface CreateAssetData {
|
||||
group: string
|
||||
name: string
|
||||
type: string
|
||||
description?: string
|
||||
image?: File
|
||||
}
|
||||
|
||||
export interface UpdateAssetData {
|
||||
name?: string
|
||||
type?: string
|
||||
description?: string
|
||||
}
|
||||
|
||||
export async function createAsset(data: CreateAssetData): Promise<Asset> {
|
||||
const user = pb.authStore.model
|
||||
const formData = new FormData()
|
||||
formData.append('group', data.group)
|
||||
formData.append('creator', user?.id || '')
|
||||
formData.append('name', data.name)
|
||||
formData.append('type', data.type)
|
||||
if (data.description) formData.append('description', data.description)
|
||||
if (data.image) formData.append('image', data.image)
|
||||
|
||||
return pb.collection('assets').create(formData) as Promise<Asset>
|
||||
}
|
||||
|
||||
export async function listAssets(groupId: string): Promise<Asset[]> {
|
||||
const result = await pb.collection('assets').getFullList({
|
||||
filter: `group="${groupId}"`,
|
||||
sort: 'created',
|
||||
expand: 'creator,currentHolder'
|
||||
})
|
||||
return result as unknown as Asset[]
|
||||
}
|
||||
|
||||
export async function updateAsset(
|
||||
assetId: string,
|
||||
data: UpdateAssetData,
|
||||
image?: File
|
||||
): Promise<Asset> {
|
||||
if (image) {
|
||||
const formData = new FormData()
|
||||
if (data.name) formData.append('name', data.name)
|
||||
if (data.type) formData.append('type', data.type)
|
||||
if (data.description !== undefined) formData.append('description', data.description)
|
||||
formData.append('image', image)
|
||||
|
||||
return pb.collection('assets').update(assetId, formData) as Promise<Asset>
|
||||
}
|
||||
|
||||
return pb.collection('assets').update(assetId, data) as Promise<Asset>
|
||||
}
|
||||
|
||||
export async function transferAsset(assetId: string, userId: string): Promise<Asset> {
|
||||
return pb.collection('assets').update(assetId, {
|
||||
currentHolder: userId
|
||||
}) as Promise<Asset>
|
||||
}
|
||||
|
||||
export async function deleteAsset(assetId: string): Promise<void> {
|
||||
await pb.collection('assets').delete(assetId)
|
||||
}
|
||||
|
||||
export function subscribeAssets(
|
||||
groupId: string,
|
||||
callback: (data: any) => void
|
||||
): Promise<() => Promise<void>> {
|
||||
return pb.collection('assets').subscribe('*', (data) => {
|
||||
if (data.record?.group === groupId) {
|
||||
callback(data)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function getAssetImageUrl(assetId: string, filename: string, thumb?: string): string {
|
||||
const record = { id: assetId, image: filename }
|
||||
return pb.files.getUrl(record, filename, thumb ? { thumb } : undefined)
|
||||
}
|
||||
Reference in New Issue
Block a user