@ritas-inc/sapb1commandapi-client
Version:
A stateless TypeScript client for SAP B1 Service Layer Command API with comprehensive error handling, type safety, and batch operations
32 lines (31 loc) • 1.58 kB
JavaScript
import { CreateWorkOrderItemRequestSchema, } from '../schemas/workorders.schema.js';
export class WorkOrdersService {
httpClient;
constructor(httpClient) {
this.httpClient = httpClient;
}
async create(userId, planId, workOrder, origin) {
const validatedRequest = CreateWorkOrderItemRequestSchema.parse({ ...workOrder, planId, ...(origin ?? { origin: 'manual' }) });
return this.httpClient.post('/api/v1/workorders', validatedRequest, userId);
}
async release(userId, workOrderId) {
return this.httpClient.patch(`/api/v1/workorders/${workOrderId}/release`, undefined, userId);
}
async cancel(userId, workOrderId) {
return this.httpClient.delete(`/api/v1/workorders/${workOrderId}`, userId);
}
batch = {
create: async (userId, workOrders) => {
const validatedItems = workOrders.map(item => CreateWorkOrderItemRequestSchema.parse({ ...item.workOrder, planId: item.planId, ...(item.origin ?? { origin: 'manual' }) }));
return this.httpClient.post('/api/v1/workorders/batch', validatedItems, userId);
},
release: async (userId, workOrderIds) => {
const items = workOrderIds.map(id => ({ absoluteEntry: id }));
return this.httpClient.post('/api/v1/workorders/batch/release', items, userId);
},
cancel: async (userId, workOrderIds) => {
const items = workOrderIds.map(id => ({ absoluteEntry: id }));
return this.httpClient.post('/api/v1/workorders/batch/cancel', items, userId);
}
};
}