feat: schedule entries, add time modal, and proxy routes

- Add CreateScheduleEntryModal with pill-based type selector and split date/time inputs
- Rewrite AddTimeModal to self-fetch activities with loading/empty states
- Empty state offers 'Create Schedule Entry' shortcut when no open activities
- Add SvelteKit proxy routes for /activities and /time endpoints
- Split datetime-local inputs into separate date+time fields across modals
- Fix CW date format: strip milliseconds from ISO strings (keep Z)
- Add ScheduleEntry to workflow history type whitelist
- Show open schedule entries panel in WorkflowPanel
- Auto-refresh ActivityTab after workflow actions
- Reduce activity dot size and fix connector width overflow
- Hide creation date row for Schedule Entry activities in timeline
This commit is contained in:
2026-04-19 01:26:29 +00:00
parent 3db045289c
commit 38654601c9
21 changed files with 2230 additions and 362 deletions
+4
View File
@@ -34,6 +34,8 @@ import { default as fetchByUserId } from "./opportunities/fetchByUserId";
import { default as workflowDispatch } from "./opportunities/[id]/workflow/dispatch";
import { default as workflowStatus } from "./opportunities/[id]/workflow/status";
import { default as workflowHistory } from "./opportunities/[id]/workflow/history";
import { default as addTime } from "./opportunities/[id]/time";
import { default as fetchActivities } from "./opportunities/[id]/activities";
export {
addProduct,
@@ -72,4 +74,6 @@ export {
workflowDispatch,
workflowStatus,
workflowHistory,
addTime,
fetchActivities,
};
@@ -0,0 +1,51 @@
import { createRoute } from "../../../../modules/api-utils/createRoute";
import { apiResponse } from "../../../../modules/api-utils/apiResponse";
import { ContentfulStatusCode } from "hono/utils/http-status";
import { authMiddleware } from "../../../middleware/authorization";
import { opportunities } from "../../../../managers/opportunities";
import { activityCw } from "../../../../modules/cw-utils/activities/activities";
import { OptimaType } from "../../../../workflows/wf.opportunity";
/* GET /v1/sales/opportunities/opportunity/:identifier/activities */
export default createRoute(
"get",
["/opportunities/opportunity/:identifier/activities"],
async (c) => {
const identifier = c.req.param("identifier");
const opportunity = await opportunities.fetchItem(identifier);
const rawActivities = await activityCw.fetchByOpportunityDirect(
opportunity.cwOpportunityId,
);
// Return only open workflow activities (status != 2)
const openActivities = rawActivities
.filter((a: any) => a.status?.id !== 2)
.map((a: any) => {
const optimaTypeField = a.customFields?.find(
(f: any) => f.id === OptimaType.FIELD_ID || f.caption === "Optima_Type",
);
const parentActivityField = a.customFields?.find(
(f: any) => f.id === 50 || f.caption === "Parent_Activity",
);
return {
cwActivityId: a.id,
name: a.name,
optimaType: optimaTypeField?.value ?? null,
parentActivityId: parentActivityField?.value
? parseInt(parentActivityField.value, 10) || null
: null,
status: a.status,
dateStart: a.dateStart ?? null,
dateEnd: a.dateEnd ?? null,
};
});
const response = apiResponse.successful("Open activities fetched.", {
activities: openActivities,
});
return c.json(response, response.status as ContentfulStatusCode);
},
authMiddleware({ permissions: ["sales.opportunity.view"] }),
);
@@ -0,0 +1,112 @@
import { z } from "zod";
import { createRoute } from "../../../../modules/api-utils/createRoute";
import { apiResponse } from "../../../../modules/api-utils/apiResponse";
import { ContentfulStatusCode } from "hono/utils/http-status";
import { authMiddleware } from "../../../middleware/authorization";
import { opportunities } from "../../../../managers/opportunities";
import { cwMembers } from "../../../../managers/cwMembers";
import { activityCw } from "../../../../modules/cw-utils/activities/activities";
import { submitTimeEntry } from "../../../../services/cw.opportunityService";
import { OptimaType } from "../../../../workflows/wf.opportunity";
import GenericError from "../../../../Errors/GenericError";
const addTimeSchema = z.object({
/** CW activity ID to log time against. */
activityId: z.number().int().positive(),
/** ISO-8601 datetime when work started. */
timeStarted: z.string().datetime(),
/** ISO-8601 datetime when work ended. */
timeEnded: z.string().datetime(),
/** Optional notes for the time entry. */
notes: z.string().optional(),
});
/* POST /v1/sales/opportunities/opportunity/:identifier/time */
export default createRoute(
"post",
["/opportunities/opportunity/:identifier/time"],
async (c) => {
const identifier = c.req.param("identifier");
const body = await c.req.json();
const data = addTimeSchema.parse(body);
const user = c.get("user");
if (!user.cwIdentifier) {
throw new GenericError({
status: 400,
name: "MissingCwIdentifier",
message:
"Your account is not linked to a ConnectWise member. A CW member association is required to log time.",
});
}
// Verify the opportunity exists and belongs to the correct identifier
const opportunity = await opportunities.fetchItem(identifier);
const cwMember = await cwMembers.fetch(user.cwIdentifier);
// Fetch the activity to verify it belongs to this opportunity and is open
const activity = await activityCw.fetch(data.activityId);
if (activity.opportunity?.id !== opportunity.cwOpportunityId) {
throw new GenericError({
status: 400,
name: "ActivityMismatch",
message: "The specified activity does not belong to this opportunity.",
});
}
if (activity.status?.id === 2) {
throw new GenericError({
status: 400,
name: "ActivityClosed",
message: "Cannot log time against a closed activity.",
});
}
// Submit the time entry
const result = await submitTimeEntry({
activityId: data.activityId,
cwMemberId: cwMember.cwMemberId,
timeStart: data.timeStarted,
timeEnd: data.timeEnded,
notes: data.notes ?? "",
});
if (!result.success) {
throw new GenericError({
status: 502,
name: "TimeEntryFailed",
message: result.message,
});
}
// If the activity is a Schedule Entry (Optima_Type = "Schedule Entry"),
// close it now that time has been logged against it.
const optimaTypeField = activity.customFields?.find(
(f: any) => f.id === OptimaType.FIELD_ID || f.caption === "Optima_Type",
);
if (optimaTypeField?.value === OptimaType.ScheduleEntry) {
try {
await activityCw.update(data.activityId, [
{ op: "replace", path: "status", value: { id: 2 } },
]);
} catch (closeErr) {
// Non-fatal — time entry was already submitted successfully
console.error(
`[AddTime] Failed to close Schedule Entry activity ${data.activityId}:`,
closeErr,
);
}
}
const response = apiResponse.successful("Time entry submitted successfully.", {
cwTimeEntryId: result.cwTimeEntryId,
activityId: data.activityId,
activityWasClosed: optimaTypeField?.value === OptimaType.ScheduleEntry,
});
return c.json(response, response.status as ContentfulStatusCode);
},
authMiddleware({ permissions: ["sales.opportunity.update"] }),
);
@@ -5,6 +5,10 @@ import { ContentfulStatusCode } from "hono/utils/http-status";
import { authMiddleware } from "../../../middleware/authorization";
import GenericError from "../../../../Errors/GenericError";
import { z } from "zod";
import {
OpportunityStatus,
StatusIdToKey,
} from "../../../../workflows/wf.opportunity";
const updateSchema = z
.object({
@@ -46,6 +50,21 @@ export default createRoute(
const item = await opportunities.fetchRecord(identifier);
// Read-only guard: only New and Active statuses allow opportunity data mutations.
const editableStatuses = new Set<number>([
OpportunityStatus.New,
OpportunityStatus.Active,
]);
const currentStatusId = item.statusCwId ?? null;
if (currentStatusId !== null && !editableStatuses.has(currentStatusId)) {
const statusKey = StatusIdToKey[currentStatusId] ?? `ID ${currentStatusId}`;
throw new GenericError({
status: 422,
name: "OpportunityReadOnly",
message: `Opportunity data cannot be edited in "${statusKey}" status. Only "New" and "Active" opportunities are editable.`,
});
}
try {
const updated = await item.updateOpportunity(data);
@@ -91,6 +91,19 @@ const dispatchSchema = z.discriminatedUnion("action", [
action: z.literal("reopen"),
payload: noteRequiredPayload,
}),
z.object({
action: z.literal("sendBackForRevision"),
payload: noteRequiredPayload,
}),
z.object({
action: z.literal("createScheduleEntry"),
payload: basePayload.extend({
activityTypeValue: z.enum(["Follow-Up", "Appointment", "Admin"]),
dueDate: z.string().optional(),
startTime: z.string().optional(),
endTime: z.string().optional(),
}),
}),
]);
// ── Route ─────────────────────────────────────────────────────────────────
@@ -22,6 +22,7 @@ const OPTIMA_TYPE_VALUES = new Set<string>([
OptimaType.Revision,
OptimaType.Finalized,
OptimaType.Converted,
OptimaType.ScheduleEntry,
]);
/** QuoteID custom field ID (matches wf.opportunity.ts QUOTE_ID_FIELD_ID). */
@@ -248,6 +248,15 @@ const ACTION_MAP: Record<number, AvailableAction[]> = {
requiresPermission: null,
payloadHints: { needsRevision: "true" },
},
{
action: "sendBackForRevision",
label: "Send Back for Revision",
targetStatuses: [
{ key: "PendingRevision", id: OpportunityStatus.PendingRevision },
],
requiresNote: true,
requiresPermission: null,
},
],
[OpportunityStatus.Active]: [
+162 -6
View File
@@ -123,6 +123,7 @@ export const OptimaType = {
Revision: "Revision",
Finalized: "Finalized",
Converted: "Converted",
ScheduleEntry: "Schedule Entry",
} as const;
/** CW custom field ID for the QuoteID field on activities. */
@@ -131,6 +132,9 @@ const QUOTE_ID_FIELD_ID = 48;
/** CW custom field ID for the Close Date field on activities. */
const CLOSE_DATE_FIELD_ID = 49;
/** CW custom field ID for the Parent Activity field on activities. */
export const PARENT_ACTIVITY_FIELD_ID = 50;
/**
* Optima_Type values whose activities should remain Open until the
* next workflow transition closes them automatically.
@@ -139,6 +143,7 @@ const STAYS_OPEN_TYPES = new Set<OptimaTypeValue>([
OptimaType.OpportunitySetup,
OptimaType.OpportunityReview,
OptimaType.Revision,
OptimaType.ScheduleEntry,
]);
export type OptimaTypeValue =
@@ -151,7 +156,8 @@ export type OptimaTypeValue =
| typeof OptimaType.QuoteGenerated
| typeof OptimaType.Revision
| typeof OptimaType.Finalized
| typeof OptimaType.Converted;
| typeof OptimaType.Converted
| typeof OptimaType.ScheduleEntry;
/** Permission nodes required by gated transitions. */
export const WorkflowPermissions = {
@@ -210,6 +216,7 @@ const ALLOWED_TRANSITIONS: Record<number, Set<number>> = {
OpportunityStatus.PendingWon,
OpportunityStatus.PendingLost,
OpportunityStatus.Active,
OpportunityStatus.PendingRevision, // needs revision
OpportunityStatus.InternalReview, // cold automation only
]),
@@ -219,6 +226,7 @@ const ALLOWED_TRANSITIONS: Record<number, Set<number>> = {
OpportunityStatus.PendingLost,
OpportunityStatus.Active,
OpportunityStatus.InternalReview, // cold automation only
OpportunityStatus.PendingRevision, // send back for revision
]),
[OpportunityStatus.Active]: new Set([
@@ -317,7 +325,7 @@ export interface SendQuotePayload extends BaseActionPayload {
/**
* Quote needs revision.
* Creates a revision activity and transitions to Active.
* Creates a revision activity and transitions to PendingRevision.
*/
needsRevision?: boolean;
}
@@ -342,6 +350,25 @@ export interface ResurrectPayload extends BaseActionPayload {
/** Begin revision from PendingRevision → Active. */
export interface BeginRevisionPayload extends BaseActionPayload {}
/** Send back for revision from ConfirmedQuote → PendingRevision. */
export interface SendBackForRevisionPayload extends BaseActionPayload {
note: string; // required
}
/** Create a Schedule Entry activity (no status change). */
export interface CreateScheduleEntryPayload extends BaseActionPayload {
/** Activity type value: Follow-Up, Appointment, or Admin. */
activityTypeValue: "Follow-Up" | "Appointment" | "Admin";
/** ISO-8601 due date. */
dueDate?: string;
/** ISO-8601 start time. */
startTime?: string;
/** ISO-8601 end time. */
endTime?: string;
/** Optional notes for the schedule entry. */
note?: string;
}
/** Re-send from Active → QuoteSent. */
export interface ResendQuotePayload extends SendQuotePayload {}
@@ -371,7 +398,9 @@ export type WorkflowAction =
| { action: "beginRevision"; payload: BeginRevisionPayload }
| { action: "resendQuote"; payload: ResendQuotePayload }
| { action: "cancel"; payload: CancelPayload }
| { action: "reopen"; payload: ReopenPayload };
| { action: "reopen"; payload: ReopenPayload }
| { action: "sendBackForRevision"; payload: SendBackForRevisionPayload }
| { action: "createScheduleEntry"; payload: CreateScheduleEntryPayload };
// -----------------------------------------------------------------------
// Result
@@ -1083,9 +1112,9 @@ export async function transitionToQuoteSent(
return ok(currentStatus, targetStatus, activities);
}
// ── needsRevision flag → Active ────────────────────────────────────
// ── needsRevision flag → PendingRevision ────────────────────────────
if (payload.needsRevision) {
const targetStatus = OpportunityStatus.Active;
const targetStatus = OpportunityStatus.PendingRevision;
const wasAlsoConfirmed = !!payload.quoteConfirmed;
@@ -1518,6 +1547,117 @@ export async function beginRevision(
return ok(currentStatus, targetStatus, activities);
}
/**
* ConfirmedQuote → PendingRevision
*
* Sends the opportunity back for revision from ConfirmedQuote.
* Requires a mandatory note explaining why.
*/
export async function sendBackForRevision(
opportunity: OpportunityController,
user: WorkflowUser,
payload: SendBackForRevisionPayload,
): Promise<WorkflowResult> {
const currentStatus = opportunity.statusCwId;
if (currentStatus == null) return fail("Opportunity has no current status.");
const noteErr = assertNotePresent(payload.note);
if (noteErr) return fail(noteErr, currentStatus);
const targetStatus = OpportunityStatus.PendingRevision;
const transErr = assertTransitionAllowed(currentStatus, targetStatus);
if (transErr) return fail(transErr, currentStatus);
const activity = await createWorkflowActivity({
name: `[Workflow] Sent back for revision — ${opportunity.name}`,
opportunityCwId: opportunity.cwOpportunityId,
companyCwId: opportunity.companyCwId,
assignToCwMemberId: user.cwMemberId,
notes: payload.note,
optimaType: OptimaType.Revision,
});
await syncOpportunityStatus({
opportunityId: opportunity.cwOpportunityId,
statusCwId: targetStatus,
});
await handleTimeEntry(
activity.cwActivityId,
user.cwMemberId,
payload,
payload.note,
);
return ok(currentStatus, targetStatus, [activity]);
}
/**
* Create a Schedule Entry activity without changing the opportunity status.
*
* Schedule Entry activities stay open until time is logged against them.
*/
export async function createScheduleEntry(
opportunity: OpportunityController,
user: WorkflowUser,
payload: CreateScheduleEntryPayload,
): Promise<WorkflowResult> {
const currentStatus = opportunity.statusCwId;
if (currentStatus == null) return fail("Opportunity has no current status.");
// CW activities require ISO-8601 without milliseconds, e.g. "2026-04-19T20:15:00Z"
const toCwDateTime = (iso: string): string => iso.replace(/\.\d+Z$/, "Z");
const dateStart = payload.startTime
? toCwDateTime(payload.startTime)
: payload.dueDate
? toCwDateTime(payload.dueDate)
: undefined;
const dateEnd = payload.endTime ? toCwDateTime(payload.endTime) : undefined;
const activity = await ActivityController.create({
name: `[Schedule Entry] ${payload.activityTypeValue}${opportunity.name}`,
type: { id: 3 }, // HistoricEntry
opportunity: { id: opportunity.cwOpportunityId },
...(opportunity.companyCwId ? { company: { id: opportunity.companyCwId } } : {}),
assignTo: { id: user.cwMemberId },
notes: payload.note ?? "",
...(dateStart ? { dateStart } : {}),
...(dateEnd ? { dateEnd } : {}),
});
// Set Optima_Type to Schedule Entry (stays open)
await activity.update([
{
op: "replace",
path: "customFields",
value: [
{
id: OptimaType.FIELD_ID,
caption: "Optima_Type",
type: "Text",
entryMethod: "List",
numberOfDecimals: 0,
value: OptimaType.ScheduleEntry,
},
],
},
]);
// Return a no-transition result (status unchanged)
return {
success: true,
previousStatusId: currentStatus,
newStatusId: currentStatus,
previousStatus: StatusIdToKey[currentStatus] ?? null,
newStatus: StatusIdToKey[currentStatus] ?? null,
activitiesCreated: [activity],
coldCheck: null,
error: null,
};
}
/**
* Any cancelable status → Canceled
*
@@ -1732,6 +1872,9 @@ async function closeOpenWorkflowActivities(
// Only close activities whose type is in the stays-open set
if (!STAYS_OPEN_TYPES.has(optimaField.value as OptimaTypeValue)) continue;
// Never auto-close Schedule Entry activities — they close only when time is logged
if (optimaField.value === OptimaType.ScheduleEntry) continue;
const closeDate = new Date().toISOString();
const existingFields = (raw.customFields ?? []).map((f: any) =>
f.id === CLOSE_DATE_FIELD_ID ? { ...f, value: closeDate } : f,
@@ -1797,7 +1940,10 @@ export async function processOpportunityAction(
}
// ── Close any open workflow activities from previous stage ──────────
await closeOpenWorkflowActivities(opportunity.cwOpportunityId);
// Skip for createScheduleEntry — we intentionally preserve open activities.
if (action !== "createScheduleEntry") {
await closeOpenWorkflowActivities(opportunity.cwOpportunityId);
}
// ── Route to transition function ────────────────────────────────────
let result: WorkflowResult;
@@ -1856,6 +2002,16 @@ export async function processOpportunityAction(
result = await reopenCancelledOpportunity(opportunity, user, payload);
break;
case "sendBackForRevision":
result = await sendBackForRevision(opportunity, user, payload);
break;
case "createScheduleEntry":
// Schedule Entry does not close open activities — skip that step.
// We call it directly rather than falling through closeOpenWorkflowActivities.
result = await createScheduleEntry(opportunity, user, payload);
break;
default: {
const _exhaustive: never = action;
return fail(`Unknown workflow action: "${_exhaustive}"`);