UNPKG

km-web-plugin

Version:

ICE Web Plugin Initializer

249 lines (210 loc) 7.67 kB
import type { QuickViewRecord, ExampleRecord } from '@/types/example.ts'; import { v4 as uuidv4 } from 'uuid'; // @ts-ignore - JS module import { cdoNames } from '@/data/pluginSettings'; // @ts-ignore - JS module import { getUsers } from '@/utils/em.api'; import { BaseApiService } from './BaseApiService.ts'; interface LedgerData { items: QuickViewRecord[]; } export class ExampleService extends BaseApiService { private getRecordCdoName(recordId: string): string { return `${cdoNames.LEDGER}.${recordId}`; } private nameExists( name: string, recordId: string, items: QuickViewRecord[], ): boolean { return items.some( (record) => record.name === name && record.id !== recordId, ); } private createQuickViewRecord(record: ExampleRecord): QuickViewRecord { return { id: record.id, name: record.name, lastEditedBy: record.lastEditedBy, lastEditedOn: record.lastEditedOn.toISOString(), }; } async loadLedger(): Promise<QuickViewRecord[]> { try { const response = await this.getCdo(cdoNames.LEDGER); if (response && Array.isArray(response.items)) { this.log('CDO ledger data loaded successfully'); return response.items.map((record: QuickViewRecord) => ({ ...record, })); } else { this.log('No CDO ledger data found, using defaults'); return []; } } catch (error) { this.log(error); return []; } } private async saveLedger(items: QuickViewRecord[]): Promise<void> { try { const ledgerData: LedgerData = { items }; await this.saveCdo(cdoNames.LEDGER, ledgerData); this.log('CDO ledger data saved successfully'); } catch (error) { this.log(error); throw error; } } private async saveRecordCdo(record: ExampleRecord): Promise<void> { try { const cdoName = this.getRecordCdoName(record.id); await this.saveCdo(cdoName, record); this.log(`Record CDO ${cdoName} saved successfully`); } catch (error) { this.log(error); throw error; } } async getRecordDetails(recordId: string): Promise<ExampleRecord | null> { try { const cdoName = this.getRecordCdoName(recordId); const response = await this.getCdo(cdoName); if (response) { this.log(`Record CDO ${cdoName} loaded successfully`); return response as ExampleRecord; } else { this.log(`No record CDO found for ${recordId}`); return null; } } catch (error) { this.log(error); return null; } } private async deleteRecordCdo(recordId: string): Promise<void> { try { const cdoName = this.getRecordCdoName(recordId); await this.saveCdo(cdoName, {}); this.log(`Record ${recordId} deleted successfully`); } catch (error) { this.log(error); throw error; } } async createRecord( recordData: Omit<ExampleRecord, 'id' | 'lastEditedBy' | 'lastEditedOn'>, existingItems: QuickViewRecord[], ): Promise< | { success: true; record: ExampleRecord } | { success: false; error: string } > { const user = await this.getCurrentUser(); const now = new Date(); const newRecord: ExampleRecord = { id: uuidv4(), name: recordData.name, lastEditedBy: user?.id || 'Unknown User', lastEditedOn: now, }; if (this.nameExists(newRecord.name, newRecord.id, existingItems)) { return { success: false, error: 'Duplicate name exists' }; } const quickViewRecord = this.createQuickViewRecord(newRecord); const updatedItems = [quickViewRecord, ...existingItems]; await Promise.all([ this.saveLedger(updatedItems), this.saveRecordCdo(newRecord), ]); return { success: true, record: newRecord }; } async updateRecord( recordData: ExampleRecord, existingItems: QuickViewRecord[], ): Promise< | { success: true; record: ExampleRecord } | { success: false; error: string } > { const user = await this.getCurrentUser(); const now = new Date(); const updatedRecord: ExampleRecord = { ...recordData, lastEditedBy: user?.id || 'Unknown User', lastEditedOn: now, }; if ( this.nameExists(updatedRecord.name, updatedRecord.id, existingItems) ) { return { success: false, error: 'Duplicate name exists' }; } const quickViewRecord = this.createQuickViewRecord(updatedRecord); const ledgerIndex = existingItems.findIndex( (item) => item.id === updatedRecord.id, ); if (ledgerIndex === -1) { return { success: false, error: 'Record not found' }; } const updatedItems = [...existingItems]; updatedItems[ledgerIndex] = quickViewRecord; await Promise.all([ this.saveLedger(updatedItems), this.saveRecordCdo(updatedRecord), ]); return { success: true, record: updatedRecord }; } async deleteRecord( recordId: string, existingItems: QuickViewRecord[], ): Promise<boolean> { try { const updatedItems = existingItems.filter( (item) => item.id !== recordId, ); await Promise.all([ this.saveLedger(updatedItems), this.deleteRecordCdo(recordId), ]); return true; } catch (error) { return false; } } async duplicateRecord( recordId: string, existingItems: QuickViewRecord[], ): Promise< | { success: true; record: ExampleRecord } | { success: false; error: string } > { try { const sourceRecord = await this.getRecordDetails(recordId); if (!sourceRecord) { return { success: false, error: 'Source record not found' }; } const duplicateName = `Copy of - ${sourceRecord.name}`; if (this.nameExists(duplicateName, '', existingItems)) { return { success: false, error: 'Duplicate name exists' }; } const user = await this.getCurrentUser(); const now = new Date(); const duplicatedRecord: ExampleRecord = { ...sourceRecord, id: uuidv4(), name: duplicateName, lastEditedBy: user?.id || 'Unknown User', lastEditedOn: now, }; const quickViewRecord = this.createQuickViewRecord(duplicatedRecord); const updatedItems = [quickViewRecord, ...existingItems]; await Promise.all([ this.saveLedger(updatedItems), this.saveRecordCdo(duplicatedRecord), ]); return { success: true, record: duplicatedRecord }; } catch (error) { return { success: false, error: 'Failed to duplicate record' }; } } } export const exampleService = new ExampleService();