From 7808907861ea60656d53b367f7e334a1e779bd3a Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 4 Jul 2025 15:07:01 +0200 Subject: [PATCH] Implement pocketbase API --- src/lib/pbtypes.ts | 13 +++ src/lib/pocketbase.ts | 6 + src/services/facilityService.ts | 42 ++++--- src/services/jobService.ts | 190 ++++++++++++++++---------------- 4 files changed, 139 insertions(+), 112 deletions(-) create mode 100644 src/lib/pocketbase.ts diff --git a/src/lib/pbtypes.ts b/src/lib/pbtypes.ts index 1446aaf..0cdd6ea 100644 --- a/src/lib/pbtypes.ts +++ b/src/lib/pbtypes.ts @@ -12,6 +12,7 @@ export enum Collections { Otps = "_otps", Superusers = "_superusers", IndBillitem = "ind_billItem", + IndFacility = "ind_facility", IndJob = "ind_job", IndTransaction = "ind_transaction", Regionview = "regionview", @@ -105,6 +106,14 @@ export type IndBillitemRecord = { updated?: IsoDateString } +export type IndFacilityRecord = { + created?: IsoDateString + id: string + location: string + name: string + updated?: IsoDateString +} + export enum IndJobStatusOptions { "Planned" = "Planned", "Acquisition" = "Acquisition", @@ -206,6 +215,7 @@ export type MfasResponse = Required & BaseSystemF export type OtpsResponse = Required & BaseSystemFields export type SuperusersResponse = Required & AuthSystemFields export type IndBillitemResponse = Required & BaseSystemFields +export type IndFacilityResponse = Required & BaseSystemFields export type IndJobResponse = Required & BaseSystemFields export type IndTransactionResponse = Required & BaseSystemFields export type RegionviewResponse = Required & BaseSystemFields @@ -223,6 +233,7 @@ export type CollectionRecords = { _otps: OtpsRecord _superusers: SuperusersRecord ind_billItem: IndBillitemRecord + ind_facility: IndFacilityRecord ind_job: IndJobRecord ind_transaction: IndTransactionRecord regionview: RegionviewRecord @@ -239,6 +250,7 @@ export type CollectionResponses = { _otps: OtpsResponse _superusers: SuperusersResponse ind_billItem: IndBillitemResponse + ind_facility: IndFacilityResponse ind_job: IndJobResponse ind_transaction: IndTransactionResponse regionview: RegionviewResponse @@ -258,6 +270,7 @@ export type TypedPocketBase = PocketBase & { collection(idOrName: '_otps'): RecordService collection(idOrName: '_superusers'): RecordService collection(idOrName: 'ind_billItem'): RecordService + collection(idOrName: 'ind_facility'): RecordService collection(idOrName: 'ind_job'): RecordService collection(idOrName: 'ind_transaction'): RecordService collection(idOrName: 'regionview'): RecordService diff --git a/src/lib/pocketbase.ts b/src/lib/pocketbase.ts new file mode 100644 index 0000000..bca13a0 --- /dev/null +++ b/src/lib/pocketbase.ts @@ -0,0 +1,6 @@ +import PocketBase from 'pocketbase'; +import { TypedPocketBase } from './pbtypes'; + +const pb = new PocketBase('https://evebase.site.quack-lab.dev') as TypedPocketBase; + +export default pb; diff --git a/src/services/facilityService.ts b/src/services/facilityService.ts index 9c2284d..7bd7527 100644 --- a/src/services/facilityService.ts +++ b/src/services/facilityService.ts @@ -1,18 +1,30 @@ +import type { IndFacilityRecord, IndFacilityResponse } from '../lib/pbtypes'; +import pb from '../lib/pocketbase'; -import { Facility } from './jobService'; +export type { IndFacilityRecord as Facility } from '../lib/pbtypes'; -// Stub functions - to be implemented with PocketBase -export const facilityService = { - async getFacilities(): Promise { - // TODO: Implement with PocketBase - console.log('Fetching facilities'); - // Mock data for development - return [ - { id: '1', name: 'Jita IV - Moon 4 - Caldari Navy Assembly Plant', location: 'Jita' }, - { id: '2', name: 'Amarr VIII (Oris) - Emperor Family Academy', location: 'Amarr' }, - { id: '3', name: 'Dodixie IX - Moon 20 - Federation Navy Assembly Plant', location: 'Dodixie' }, - { id: '4', name: 'Rens VI - Moon 8 - Brutor Tribe Treasury', location: 'Rens' }, - { id: '5', name: 'Hek VIII - Moon 12 - Boundless Creation Factory', location: 'Hek' } - ]; +export async function getFacilities(): Promise { + const result = await pb.collection('ind_facility').getFullList(); + return result as IndFacilityResponse[]; +} + +export async function getFacility(id: string): Promise { + try { + return await pb.collection('ind_facility').getOne(id) as IndFacilityResponse; + } catch (e) { + if (e.status === 404) return null; + throw e; } -}; +} + +export async function createFacility(facility: Omit): Promise { + return await pb.collection('ind_facility').create(facility) as IndFacilityResponse; +} + +export async function updateFacility(id: string, updates: Partial): Promise { + return await pb.collection('ind_facility').update(id, updates) as IndFacilityResponse; +} + +export async function deleteFacility(id: string): Promise { + await pb.collection('ind_facility').delete(id); +} diff --git a/src/services/jobService.ts b/src/services/jobService.ts index 1609119..816987c 100644 --- a/src/services/jobService.ts +++ b/src/services/jobService.ts @@ -1,102 +1,98 @@ -export interface Job { - id: string; - outputItem: { - id: string; - name: string; - quantity: number; - }; - dates: { - creation: Date; - start: Date | null; - end: Date | null; - saleStart: Date | null; - saleEnd: Date | null; - }; - status: 'Planned' | 'Transporting Materials' | 'Running' | 'Done' | 'Selling' | 'Closed'; - facilityId: string; - expenditures: Transaction[]; - income: Transaction[]; - billOfMaterials: BillOfMaterialsItem[]; - consumedMaterials: ConsumedMaterialsItem[]; +import type { IndJobRecord, IndJobResponse, IndTransactionRecord, IndTransactionResponse, IndBillitemRecord, IndBillitemResponse } from '../lib/pbtypes'; +import pb from '../lib/pocketbase'; + +export type { IndJobRecord as Job } from '../lib/pbtypes'; +export type { IndTransactionRecord as Transaction } from '../lib/pbtypes'; +export type { IndBillitemRecord as BillItem } from '../lib/pbtypes'; + +export async function createJob(job: Omit): Promise { + return await pb.collection('ind_job').create(job) as IndJobResponse; } -export interface BillOfMaterialsItem { - name: string; - quantity: number; +export async function getJobs(): Promise { + const result = await pb.collection('ind_job').getFullList(); + return result as IndJobResponse[]; } -export interface ConsumedMaterialsItem { - name: string; - required: number; -} - -export interface Transaction { - id: string; - date: Date; - quantity: number; - itemName: string; - unitPrice: number; - totalAmount: number; - buyer?: string; - location?: string; - corporation?: string; - wallet?: string; -} - -export interface Facility { - id: string; - name: string; - location: string; -} - -// Stub functions - to be implemented with PocketBase -export const jobService = { - async createJob(job: Omit): Promise { - // TODO: Implement with PocketBase - console.log('Creating job:', job); - return { - ...job, - id: Date.now().toString(), - expenditures: [], - income: [] - }; - }, - - async getJobs(): Promise { - // TODO: Implement with PocketBase - console.log('Fetching jobs'); - return []; - }, - - async getJob(id: string): Promise { - // TODO: Implement with PocketBase - console.log('Fetching job:', id); - return null; - }, - - async updateJob(id: string, updates: Partial): Promise { - // TODO: Implement with PocketBase - console.log('Updating job:', id, updates); - throw new Error('Not implemented'); - }, - - async deleteJob(id: string): Promise { - // TODO: Implement with PocketBase - console.log('Deleting job:', id); - }, - - async addTransaction(jobId: string, transaction: Omit, type: 'expenditure' | 'income'): Promise { - // TODO: Implement with PocketBase - console.log('Adding transaction:', jobId, transaction, type); - }, - - async updateTransaction(jobId: string, transactionId: string, updates: Partial): Promise { - // TODO: Implement with PocketBase - console.log('Updating transaction:', jobId, transactionId, updates); - }, - - async deleteTransaction(jobId: string, transactionId: string): Promise { - // TODO: Implement with PocketBase - console.log('Deleting transaction:', jobId, transactionId); +export async function getJob(id: string): Promise { + try { + return await pb.collection('ind_job').getOne(id) as IndJobResponse; + } catch (e) { + if (e.status === 404) return null; + throw e; } -}; +} + +export async function updateJob(id: string, updates: Partial): Promise { + return await pb.collection('ind_job').update(id, updates) as IndJobResponse; +} + +export async function deleteJob(id: string): Promise { + await pb.collection('ind_job').delete(id); +} + +export async function addTransaction( + jobId: string, + transaction: Omit, + type: 'expenditure' | 'income' +): Promise { + // Create the transaction + const createdTransaction = await pb.collection('ind_transaction').create({ + ...transaction, + job: jobId + }) as IndTransactionResponse; + + // Update the job to include the new transaction + const job = await getJob(jobId); + if (!job) throw new Error('Job not found'); + + const field = type === 'expenditure' ? 'expenditures' : 'income'; + const currentIds = job[field] || []; + + await updateJob(jobId, { + [field]: [...currentIds, createdTransaction.id] + }); +} + +export async function updateTransaction( + jobId: string, + transactionId: string, + updates: Partial +): Promise { + await pb.collection('ind_transaction').update(transactionId, updates); +} + +export async function deleteTransaction(jobId: string, transactionId: string): Promise { + // Delete the transaction + await pb.collection('ind_transaction').delete(transactionId); + + // Update the job to remove the transaction reference + const job = await getJob(jobId); + if (!job) return; + + // Remove from both expenditures and income arrays + const expenditures = (job.expenditures || []).filter(id => id !== transactionId); + const income = (job.income || []).filter(id => id !== transactionId); + + await updateJob(jobId, { + expenditures, + income + }); +} + +export async function addBillItem( + jobId: string, + billItem: Omit +): Promise { + // Create the bill item + const createdItem = await pb.collection('ind_billItem').create(billItem) as IndBillitemResponse; + + // Update the job to include the new bill item + const job = await getJob(jobId); + if (!job) throw new Error('Job not found'); + + const currentBillItems = job.billOfMaterials || []; + await updateJob(jobId, { + billOfMaterials: [...currentBillItems, createdItem.id] + }); +}