feat: add assets service
This commit is contained in:
parent
c6266d3fb7
commit
09d14a137b
73
app/services/assets.ts
Normal file
73
app/services/assets.ts
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
import { pb } from '~/services/pocketbase';
|
||||||
|
import { ASSET_FILTER, ASSETS_COLLECTION, ASSETS_PAGE_SIZE, type ModelFieldName } from '~/data/assets/constants';
|
||||||
|
import { generateRecordId, getArUrl } from '~/lib/assets/qr';
|
||||||
|
import type { AssetFormValues } from '~/schemas/assets/asset-form';
|
||||||
|
import type { ArAsset, AssetCounts, AssetPage } from '~/types/asset';
|
||||||
|
|
||||||
|
const assets = () => pb.collection<ArAsset>(ASSETS_COLLECTION);
|
||||||
|
|
||||||
|
// `filter` is "all", "unassigned" or a project ID
|
||||||
|
function toFilter(filter: string) {
|
||||||
|
if (filter === ASSET_FILTER.all) return '';
|
||||||
|
if (filter === ASSET_FILTER.unassigned) return 'project = ""';
|
||||||
|
return pb.filter('project = {:project}', { project: filter });
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function listAssets(page: number, filter: string): Promise<AssetPage> {
|
||||||
|
const { items, totalPages, totalItems } = await assets().getList(page, ASSETS_PAGE_SIZE, {
|
||||||
|
sort: '-created',
|
||||||
|
filter: toFilter(filter),
|
||||||
|
expand: 'project',
|
||||||
|
});
|
||||||
|
return { items, page, totalPages: Math.max(totalPages, 1), totalItems };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function countAssets(): Promise<AssetCounts> {
|
||||||
|
const count = async (filter: string) => (await assets().getList(1, 1, { filter: toFilter(filter), fields: 'id' })).totalItems;
|
||||||
|
const [total, unassigned] = await Promise.all([count(ASSET_FILTER.all), count(ASSET_FILTER.unassigned)]);
|
||||||
|
return { total, unassigned };
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getAsset = (id: string) => assets().getOne(id, { expand: 'project' });
|
||||||
|
|
||||||
|
export const deleteAsset = (id: string) => assets().delete(id);
|
||||||
|
|
||||||
|
async function assetExists(id: string) {
|
||||||
|
const found = await assets().getList(1, 1, { filter: pb.filter('id = {:id}', { id }), fields: 'id' });
|
||||||
|
return found.totalItems > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Writes `<key>_file` / `<key>_url`, clearing whichever one the chosen mode does not use
|
||||||
|
function appendModelSource(payload: FormData, key: ModelFieldName, source: AssetFormValues['glb']) {
|
||||||
|
if (source.mode === 'url') {
|
||||||
|
payload.append(`${key}_url`, source.url);
|
||||||
|
payload.append(`${key}_file`, '');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
payload.append(`${key}_url`, '');
|
||||||
|
if (source.file) payload.append(`${key}_file`, source.file);
|
||||||
|
else if (!source.existing) payload.append(`${key}_file`, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
function basePayload(values: AssetFormValues) {
|
||||||
|
const payload = new FormData();
|
||||||
|
payload.append('name', values.name);
|
||||||
|
payload.append('project', values.project);
|
||||||
|
payload.append('branded_qr', String(values.brandedQr));
|
||||||
|
appendModelSource(payload, 'glb', values.glb);
|
||||||
|
appendModelSource(payload, 'usdz', values.usdz);
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createAsset(values: AssetFormValues) {
|
||||||
|
if (values.id && (await assetExists(values.id))) {
|
||||||
|
throw new Error(`An asset with ID "${values.id}" already exists`);
|
||||||
|
}
|
||||||
|
const id = values.id || generateRecordId();
|
||||||
|
const payload = basePayload(values);
|
||||||
|
payload.append('id', id);
|
||||||
|
payload.append('qr_url', getArUrl(id));
|
||||||
|
return assets().create(payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const updateAsset = (id: string, values: AssetFormValues) => assets().update(id, basePayload(values));
|
||||||
Loading…
x
Reference in New Issue
Block a user