Skip to content

Commit

Permalink
fix: primary locations for transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
willruggiano committed Feb 12, 2025
1 parent 4e19895 commit 6ff5616
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 31 deletions.
72 changes: 72 additions & 0 deletions schema/application/__snapshots__/runtime.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ exports[`runtime demo entrypoint query 1`] = `
},
],
},
"parent": {
"name": {
"value": "Mixing Line",
},
},
"state": {
"__typename": "Open",
},
Expand Down Expand Up @@ -231,6 +236,11 @@ exports[`runtime demo entrypoint query 1`] = `
},
],
},
"parent": {
"name": {
"value": "Fill Line",
},
},
"state": {
"__typename": "Open",
},
Expand Down Expand Up @@ -351,6 +361,11 @@ exports[`runtime demo entrypoint query 1`] = `
},
],
},
"parent": {
"name": {
"value": "Assembly Line",
},
},
"state": {
"__typename": "Open",
},
Expand Down Expand Up @@ -471,6 +486,11 @@ exports[`runtime demo entrypoint query 1`] = `
},
],
},
"parent": {
"name": {
"value": "Cartoning Line",
},
},
"state": {
"__typename": "Open",
},
Expand Down Expand Up @@ -591,6 +611,11 @@ exports[`runtime demo entrypoint query 1`] = `
},
],
},
"parent": {
"name": {
"value": "Packaging Line",
},
},
"state": {
"__typename": "Open",
},
Expand Down Expand Up @@ -632,6 +657,13 @@ exports[`runtime demo start run 1`] = `
"value": "Run",
},
},
"parent": {
"__typename": "Location",
"name": {
"__typename": "Name",
"value": "Mixing Line",
},
},
"state": {
"__typename": "Open",
},
Expand Down Expand Up @@ -731,6 +763,11 @@ exports[`runtime demo start run 1`] = `
},
],
},
"parent": {
"name": {
"value": "Mixing Line",
},
},
"state": {
"__typename": "InProgress",
"inProgressBy": {
Expand Down Expand Up @@ -950,6 +987,11 @@ exports[`runtime demo stale: start run (should fail) 1`] = `
},
],
},
"parent": {
"name": {
"value": "Mixing Line",
},
},
"state": {
"__typename": "InProgress",
"inProgressBy": {
Expand Down Expand Up @@ -1138,6 +1180,11 @@ exports[`runtime demo production -> idle time 1`] = `
},
],
},
"parent": {
"name": {
"value": "Mixing Line",
},
},
"state": {
"__typename": "InProgress",
"inProgressBy": {
Expand Down Expand Up @@ -1232,6 +1279,11 @@ exports[`runtime demo stale: production -> idle time (should fail) 1`] = `
},
],
},
"parent": {
"name": {
"value": "Mixing Line",
},
},
"state": {
"__typename": "InProgress",
"inProgressBy": {
Expand Down Expand Up @@ -1347,6 +1399,11 @@ exports[`runtime demo end idle time 1`] = `
},
],
},
"parent": {
"name": {
"value": "Mixing Line",
},
},
"state": {
"__typename": "InProgress",
"inProgressBy": {
Expand Down Expand Up @@ -1566,6 +1623,11 @@ exports[`runtime demo end idle time 2`] = `
},
],
},
"parent": {
"name": {
"value": "Mixing Line",
},
},
"state": {
"__typename": "InProgress",
"inProgressBy": {
Expand Down Expand Up @@ -1754,6 +1816,11 @@ exports[`runtime demo production -> downtime 1`] = `
},
],
},
"parent": {
"name": {
"value": "Mixing Line",
},
},
"state": {
"__typename": "InProgress",
"inProgressBy": {
Expand Down Expand Up @@ -1869,6 +1936,11 @@ exports[`runtime demo end downtime 1`] = `
},
],
},
"parent": {
"name": {
"value": "Mixing Line",
},
},
"state": {
"__typename": "InProgress",
"inProgressBy": {
Expand Down
25 changes: 21 additions & 4 deletions schema/application/runtime.test.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ query TestRuntimeEntrypoint(
}
}
hash
parent {
... on Location {
name {
value
}
}
}
state {
__typename
... on Closed {
Expand Down Expand Up @@ -130,6 +137,13 @@ mutation TestRuntimeTransitionMutation(
value
}
}
parent {
... on Location {
name {
value
}
}
}
state {
__typename
}
Expand Down Expand Up @@ -172,10 +186,6 @@ fragment Trackables_fragment on Trackable {
edges {
node {
... on Task {
# chainAgg(overType: ["Runtime", "Idle Time", "Downtime"]) {
# group
# value
# }
displayName {
name {
value
Expand Down Expand Up @@ -229,6 +239,13 @@ fragment TaskFSM_fragment on TaskStateMachine {
}
}
}
parent {
... on Location {
name {
value
}
}
}
state {
__typename
... on InProgress {
Expand Down
21 changes: 15 additions & 6 deletions schema/system/component/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ export async function advanceTask(
if (hash !== opts.hash) {
console.warn("WARNING: Hash mismatch precludes advancement");
console.debug(`| task: ${t.id}`);
console.debug(`| ours: ${hash}`);
console.debug(`| ours: ${hash}`);
console.debug(`| theirs: ${opts.hash}`);
return {
task: t,
Expand Down Expand Up @@ -701,11 +701,20 @@ export async function advanceTask(
select * from when_open
union all
select * from when_in_progress
limit 1
`;

if (!result) {
assert(false, "seemingly impossible no-op scenario");
console.warn(
// FIXME: this is possible under concurrency. We are doing OCC here after
// all! What we need to do better here is differentiate between an illegal
// action (e.g. advancing a closed task) and losing the race. If the action
// is legal then the only alternative is that we lost the race, and thus we
// can return a proper diagnostic indicative of this outcome.
//
// TODO: think about how we can remove the assumption inherent in our logic
// here. This would mean allowing for arbitrary task states via something
// like wtnt rather than assuming the canonical open -> in-prog -> closed.
console.error(
`Discarding candidate change, presumably because the Task (${t}) is not in a state suitable to advancement.`,
);
return {
Expand Down Expand Up @@ -786,6 +795,9 @@ export async function advanceTask(
};
}

/**
* @param mergeAction `replace` overwrites, `keep` does not
*/
export function applyAssignments$fragment(
ctx: Context,
t: Task,
Expand All @@ -807,15 +819,12 @@ export function applyAssignments$fragment(
with cte as (
select i.workinstancecustomerid as _parent, field.workresultinstanceid as _id
from public.workinstance as i
inner join public.systag as i_state
on i.workinstancestatusid = i_state.systagid
inner join public.workresultinstance as field
on i.workinstanceid = field.workresultinstanceworkinstanceid
inner join public.workresult as field_t
on field.workresultinstanceworkresultid = field_t.workresultid
where
i.id = ${t._id}
and i_state.systagtype in ('In Progress', 'Complete')
and field_t.workresulttypeid = 848
and field_t.workresultentitytypeid = 850
and field_t.workresultisprimary = true
Expand Down
79 changes: 64 additions & 15 deletions schema/system/component/task_fsm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { type Sql, type TxSql, sql } from "@/datasources/postgres";
import { copyFromWorkTemplate } from "@/schema/application/resolvers/Mutation/copyFrom";
import { type Diagnostic, DiagnosticKind } from "@/schema/result";
import type { Mutation } from "@/schema/root";
import type { Context } from "@/schema/types";
Expand All @@ -10,7 +9,13 @@ import { match } from "ts-pattern";
import { decodeGlobalId } from "..";
import type { StateMachine } from "../fsm";
import type { Edge } from "../pagination";
import { type AdvanceTaskOptions, Task, advanceTask } from "./task";
import {
type AdvanceTaskOptions,
Task,
advanceTask,
applyAssignments$fragment,
applyEdits$fragment,
} from "./task";

export function fsm$fragment(t: Task): Fragment {
assert(t._type === "workinstance");
Expand Down Expand Up @@ -253,20 +258,64 @@ export async function advanceFsm(
} satisfies AdvanceTaskStateMachineResult;
})
.with("worktemplate", async () => {
const _ = await copyFromWorkTemplate(
sql,
choice._id,
const result = await sql<[{ instance: ID }]>`
with options as (
select
w.id as previous_id,
auth.current_identity(w.workinstancecustomerid, ${ctx.auth.userId}) as modified_by,
(
select l.id
from legacy0.primary_location_for_instance(w.id) as l
) as location_id
from public.workinstance as w
where w.id = ${decodeGlobalId(fsm.active).id}
)
select encode(('workinstance:' || t.instance)::bytea, 'base64') as instance
from
options,
engine0.instantiate(
template_id := ${choice._id},
location_id := options.location_id,
target_state := 'In Progress',
target_type := 'Task',
modified_by := options.modified_by,
chain_root_id := ${root._id},
chain_prev_id := options.previous_id
) as t
group by t.instance;
`;
console.debug(`advance: engine.instantiate.count: ${result.length}`);

const ins = result.at(0)?.instance;
if (ins) {
const t = new Task({ id: ins }, ctx);

// In Progress must have a start date.
await sql`
update public.workinstance
set workinstancestartdate = now()
where id = ${t._id}
`;

// Auto-assign.
{
chain: "continue",
previous: decodeGlobalId(fsm.active).id,
// some extra options
autoAssign: true,
carryOverAssignments: true,
fieldOverrides: opts.task.overrides,
withStatus: "inProgress",
},
ctx,
);
const ma = "replace";
const result = await sql`${applyAssignments$fragment(ctx, t, ma)}`;
console.debug(
`advance: applied ${result.count} assignments (mergeAction: ${ma})`,
);
}

if (opts.task.overrides) {
const f = applyEdits$fragment(ctx, t, opts.task.overrides);
if (f) {
const result = await sql`${f}`;
console.debug(`advance: applied ${result.count} field-level edits`);
}
}
}

return {
root,
instantiations: [],
Expand Down
2 changes: 2 additions & 0 deletions sql/add-wtc-unique-constraints.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
begin
;

drop index temp_wtc_idx;

create unique index temp_wtc_with_result_idx on public.worktemplateconstraint (
worktemplateconstraintcustomerid,
worktemplateconstrainttemplateid,
Expand Down
Loading

0 comments on commit 6ff5616

Please sign in to comment.