Skip to content

Commit

Permalink
Update ProductTransitions table on state transition
Browse files Browse the repository at this point in the history
  • Loading branch information
FyreByrd committed Sep 18, 2024
1 parent c9cf707 commit c9cbd78
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 39 deletions.
9 changes: 7 additions & 2 deletions source/SIL.AppBuilder.Portal/common/public/workflow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {
AnyEventObject,
StateMachineDefinition,
TransitionDefinition
TransitionDefinition,
StateNode as XStateNode
} from 'xstate';

export type WorkflowContext = {
Expand All @@ -27,6 +28,10 @@ export type StateNode = {
final: boolean;
};

export function stateName(s: XStateNode<any, any>, machineId: string) {
return s.id.replace(machineId + '.', '');
}

export function targetStringFromEvent(
e: TransitionDefinition<any, AnyEventObject>[],
machineId: string
Expand Down Expand Up @@ -69,4 +74,4 @@ export function transform(machine: StateMachineDefinition<any, AnyEventObject>):
};
});
return a;
}
}
118 changes: 115 additions & 3 deletions source/SIL.AppBuilder.Portal/common/workflow/db.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import DatabaseWrites from '../databaseProxy/index.js';
import { prisma } from '../index.js';
import { WorkflowContext } from '../public/workflow.js';
import { AnyStateMachine } from 'xstate';
import { RoleId } from '../public/prisma.js';
import { WorkflowContext, targetStringFromEvent, stateName } from '../public/workflow.js';
import { AnyStateMachine, StateNode } from 'xstate';
import { RoleId, ProductTransitionType } from '../public/prisma.js';

export type Snapshot = {
value: string;
Expand Down Expand Up @@ -119,3 +119,115 @@ export async function updateUserTasks(
}))
});
}

function transitionFromState(state: StateNode<any, any>, machineId: string, productId: string) {
console.log(state);
return {
ProductId: productId,
TransitionType: ProductTransitionType.Activity,
InitialState: stateName(state, machineId),
DestinationState: targetStringFromEvent(Object.values(state.on)[0], machineId),
Command:
Object.keys(state.on)[0].split(':')[1] !== 'Auto'
? Object.keys(state.on)[0].split(':')[0]
: null
};
}

async function populateTransitions(machine: AnyStateMachine, productId: string) {
return DatabaseWrites.productTransitions.createManyAndReturn({
data: [
{
ProductId: productId,
DateTransition: new Date(),
TransitionType: ProductTransitionType.StartWorkflow
}
].concat(
Object.entries(machine.states).reduce(
(p, [k, v], i) =>
p.concat(
i === 1 || (i > 1 && p[p.length - 1]?.DestinationState === k && v.type !== 'final')
? [transitionFromState(v, machine.id, productId)]
: []
),
[]
)
)
});
}

/**
* Get all product transitions for a product.
* If there are none, create new ones based on main sequence (i.e. no Author steps)
* If sequence matching params exists, but no timestamp, update
* Otherwise, create.
*/
export async function updateProductTransitions(
machine: AnyStateMachine,
productId: string,
userId: number | null,
initialState: string,
destinationState: string,
command?: string,
comment?: string
) {
const transitions = await prisma.productTransitions.count({
where: {
ProductId: productId
}
});
if (transitions <= 0) {
await populateTransitions(machine, productId);
}
const transition = await prisma.productTransitions.findFirst({
where: {
ProductId: productId,
InitialState: initialState,
DestinationState: destinationState,
DateTransition: null
},
select: {
Id: true
}
});

const user = userId
? await prisma.users.findUnique({
where: {
Id: userId
},
select: {
Name: true,
WorkflowUserId: true
}
})
: null;

if (transition) {
DatabaseWrites.productTransitions.update({
where: {
Id: transition.Id
},
data: {
WorkflowUserId: user?.WorkflowUserId ?? null,
AllowedUserNames: user?.Name ?? null,
Command: command ?? null,
DateTransition: new Date(),
Comment: comment ?? null
}
});
} else {
await DatabaseWrites.productTransitions.create({
data: {
ProductId: productId,
WorkflowUserId: user?.WorkflowUserId ?? null,
AllowedUserNames: user?.Name ?? null,
InitialState: initialState,
DestinationState: destinationState,
Command: command ?? null,
DateTransition: new Date(),
Comment: comment ?? null
}
});
}
}
Loading

0 comments on commit c9cbd78

Please sign in to comment.