diff --git a/src/api/config.ts b/src/api/config.ts index c39be4e..8a40343 100644 --- a/src/api/config.ts +++ b/src/api/config.ts @@ -217,6 +217,7 @@ export const SALES_REPORT_SO_NAMES = [ export const PIPELINE = { endpoints: { nearestStores: '/api/papi2/nearest-stores', - dailySalesReport: '/api/papi2/daily-sales-report' + dailySalesReport: '/api/papi2/daily-sales-report', + productiveCallSummary: '/api/papi2/productive-call-summary' } }; diff --git a/src/components/forms/DynamicForm.tsx b/src/components/forms/DynamicForm.tsx index 5dd0cad..ba844b0 100644 --- a/src/components/forms/DynamicForm.tsx +++ b/src/components/forms/DynamicForm.tsx @@ -17,7 +17,7 @@ import { WfLookupField, RadioField, } from './fields'; -import { ORDER_BOOKING, STORE } from '../../api/config'; +import { ORDER_BOOKING, STORE, DAILY_REPORTS, PIPELINE } from '../../api/config'; export interface DynamicFormProps { client: ZinoClient; @@ -52,7 +52,7 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: let mounted = true; setLoading(true); client.formSchema(currentActivityId, currentInstanceId) - .then(res => { + .then(async (res) => { if (mounted) { setSchema(res); const defaultValues: Record = {}; @@ -78,15 +78,54 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: res.fields.filter(f => f.data_type === 'image' || f.data_type === 'file').map(f => f.id) ); + const mapPrefillData = (sourceData: Record) => { + res.fields.forEach(f => { + if (imageFieldIds.has(f.id)) return; + + const getBaseId = (id: string) => id.replace(/_\d+$/, ''); + + if (sourceData[f.id] !== undefined) { + defaultValues[f.id] = sourceData[f.id]; + } else if (f.mapped_workflow_field && sourceData[f.mapped_workflow_field] !== undefined) { + defaultValues[f.id] = sourceData[f.mapped_workflow_field]; + } else if (sourceData[getBaseId(f.id)] !== undefined) { + defaultValues[f.id] = sourceData[getBaseId(f.id)]; + } else { + const matchingDataKey = Object.keys(sourceData).find(k => getBaseId(k) === f.id || getBaseId(k) === getBaseId(f.id) || k === f.mapped_workflow_field); + if (matchingDataKey) { + defaultValues[f.id] = sourceData[matchingDataKey]; + } + } + + if (f.data_type === 'grid' && Array.isArray(defaultValues[f.id])) { + defaultValues[f.id] = (defaultValues[f.id] as Record[]).map(row => { + const newRow: Record = { ...row }; + f.columns?.forEach(col => { + const colBaseId = getBaseId(col.id); + if (row[col.id] !== undefined) { + newRow[col.id] = row[col.id]; + } else if (col.mapped_workflow_field && row[col.mapped_workflow_field] !== undefined) { + newRow[col.id] = row[col.mapped_workflow_field]; + } else if (row[colBaseId] !== undefined) { + newRow[col.id] = row[colBaseId]; + } else { + const matchingRowKey = Object.keys(row).find(k => getBaseId(k) === col.id || getBaseId(k) === colBaseId || k === col.mapped_workflow_field); + if (matchingRowKey) { + newRow[col.id] = row[matchingRowKey]; + } + } + }); + return newRow; + }); + } + }); + }; + if (!ignorePrefill) { - if (res.prefill_data) { - Object.entries(res.prefill_data).forEach(([k, v]) => { - if (!imageFieldIds.has(k)) defaultValues[k] = v; - }); + if (res.prefill_data && Object.keys(res.prefill_data).length > 0) { + mapPrefillData(res.prefill_data); } else if (res.data) { - Object.entries(res.data).forEach(([k, v]) => { - if (!imageFieldIds.has(k)) defaultValues[k] = v; - }); + mapPrefillData(res.data); } } @@ -101,6 +140,44 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: if (!imageFieldIds.has(k)) defaultValues[k] = v; }); } + + if (currentActivityId === DAILY_REPORTS.activities.PUNCH_OUT.uid) { + const user = client.currentUser(); + const userEmail = user?.email; + if (userEmail) { + try { + const headers: Record = { + 'templateid': '192', + 'x-pipeline-version': 'draft', + 'orgid': '57', + 'groupid': '25' + }; + + const summaryRes = await client.request( + 'POST', + PIPELINE.endpoints.productiveCallSummary, + { email: userEmail }, + headers + ); + let summaryData = summaryRes.data || summaryRes; + if (Array.isArray(summaryData)) { + summaryData = summaryData[0] || {}; + } + + // Map the API response keys to match the form field keys + if (summaryData.productive_call !== undefined) { + summaryData.total_productive_calls = summaryData.productive_call; + } + if (summaryData.non_productive_call !== undefined) { + summaryData.total_non_productive_calls = summaryData.non_productive_call; + } + + mapPrefillData(summaryData); + } catch (e) { + console.warn('Failed to load productive call summary', e); + } + } + } setValues(defaultValues); setLoading(false);