dc11ef90fd
PocketBase JS SDK auto-cancels pending requests when a new request targets the same collection. This causes errors when loadLedgers and getLedgerSummary run in parallel via Promise.all. Added $autoCancel:false to all API calls in ledgers.ts and assets.ts, matching project convention. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
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, { $autoCancel: false }) 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',
|
|
$autoCancel: false
|
|
})
|
|
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, { $autoCancel: false }) as Promise<Asset>
|
|
}
|
|
|
|
return pb.collection('assets').update(assetId, data, { $autoCancel: false }) as Promise<Asset>
|
|
}
|
|
|
|
export async function transferAsset(assetId: string, userId: string): Promise<Asset> {
|
|
return pb.collection('assets').update(assetId, {
|
|
currentHolder: userId
|
|
}, { $autoCancel: false }) as Promise<Asset>
|
|
}
|
|
|
|
export async function deleteAsset(assetId: string): Promise<void> {
|
|
await pb.collection('assets').delete(assetId, { $autoCancel: false })
|
|
}
|
|
|
|
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)
|
|
}
|