feat: add time entry manager, controller, and API routes
This commit is contained in:
@@ -1616,6 +1616,32 @@ export async function createScheduleEntry(
|
||||
: undefined;
|
||||
const dateEnd = payload.endTime ? toCwDateTime(payload.endTime) : undefined;
|
||||
|
||||
// Find the currently open workflow activity (OpportunitySetup, OpportunityReview,
|
||||
// or Revision) to use as the parent for this schedule entry.
|
||||
let parentActivityCwId: number | null = null;
|
||||
try {
|
||||
const existingActivities = await activityCw.fetchByOpportunityDirect(
|
||||
opportunity.cwOpportunityId,
|
||||
);
|
||||
for (const raw of existingActivities) {
|
||||
if (raw.status?.id === 2) continue; // already closed
|
||||
const optimaField = raw.customFields?.find(
|
||||
(f: any) => f.id === OptimaType.FIELD_ID,
|
||||
);
|
||||
if (!optimaField?.value) continue;
|
||||
if (optimaField.value === OptimaType.ScheduleEntry) continue; // skip other schedule entries
|
||||
if (STAYS_OPEN_TYPES.has(optimaField.value as OptimaTypeValue)) {
|
||||
parentActivityCwId = raw.id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
// Non-fatal — schedule entry will be created without a parent
|
||||
console.warn(
|
||||
`[Workflow:ScheduleEntry] Could not resolve parent activity: ${err}`,
|
||||
);
|
||||
}
|
||||
|
||||
const activity = await ActivityController.create({
|
||||
name: `[Schedule Entry] ${payload.activityTypeValue} — ${opportunity.name}`,
|
||||
type: { id: 3 }, // HistoricEntry
|
||||
@@ -1627,21 +1653,35 @@ export async function createScheduleEntry(
|
||||
...(dateEnd ? { dateEnd } : {}),
|
||||
});
|
||||
|
||||
// Set Optima_Type to Schedule Entry (stays open)
|
||||
// Build custom fields: always Optima_Type, plus Parent_Activity when resolved
|
||||
const customFields: any[] = [
|
||||
{
|
||||
id: OptimaType.FIELD_ID,
|
||||
caption: "Optima_Type",
|
||||
type: "Text",
|
||||
entryMethod: "List",
|
||||
numberOfDecimals: 0,
|
||||
value: OptimaType.ScheduleEntry,
|
||||
},
|
||||
];
|
||||
|
||||
if (parentActivityCwId != null) {
|
||||
customFields.push({
|
||||
id: PARENT_ACTIVITY_FIELD_ID,
|
||||
caption: "Parent_Activity",
|
||||
type: "Text",
|
||||
entryMethod: "EntryField",
|
||||
numberOfDecimals: 0,
|
||||
value: String(parentActivityCwId),
|
||||
});
|
||||
}
|
||||
|
||||
// Set Optima_Type (+ Parent_Activity) on the new schedule entry
|
||||
await activity.update([
|
||||
{
|
||||
op: "replace",
|
||||
path: "customFields",
|
||||
value: [
|
||||
{
|
||||
id: OptimaType.FIELD_ID,
|
||||
caption: "Optima_Type",
|
||||
type: "Text",
|
||||
entryMethod: "List",
|
||||
numberOfDecimals: 0,
|
||||
value: OptimaType.ScheduleEntry,
|
||||
},
|
||||
],
|
||||
value: customFields,
|
||||
},
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user