added the work pre punchout prefill

This commit is contained in:
suryacp23 2026-07-29 12:42:26 +05:30
parent 8ad60001fe
commit 0ab9be85a6
2 changed files with 88 additions and 10 deletions

View File

@ -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'
}
};

View File

@ -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<string, unknown> = {};
@ -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<string, unknown>) => {
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<string, unknown>[]).map(row => {
const newRow: Record<string, unknown> = { ...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);
}
}
@ -102,6 +141,44 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
});
}
if (currentActivityId === DAILY_REPORTS.activities.PUNCH_OUT.uid) {
const user = client.currentUser();
const userEmail = user?.email;
if (userEmail) {
try {
const headers: Record<string, string> = {
'templateid': '192',
'x-pipeline-version': 'draft',
'orgid': '57',
'groupid': '25'
};
const summaryRes = await client.request<any>(
'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);
if (res.activity_name) {