|
| 1 | +import { createUnionType, Field, ObjectType } from '@nestjs/graphql'; |
| 2 | +import { cacheable } from '@seedcompany/common'; |
| 3 | +import * as uuid from 'uuid'; |
| 4 | +import { DataObject, ID, IdField } from '~/common'; |
| 5 | +import { Workflow } from '../define-workflow'; |
| 6 | +import { DynamicState } from '../transitions/dynamic-state'; |
| 7 | +import { TransitionType } from './workflow-transition.dto'; |
| 8 | + |
| 9 | +@ObjectType('WorkflowState') |
| 10 | +export class SerializedWorkflowState extends DataObject { |
| 11 | + @Field() |
| 12 | + readonly value: string; |
| 13 | + |
| 14 | + @Field() |
| 15 | + readonly label: string; |
| 16 | +} |
| 17 | + |
| 18 | +@ObjectType('WorkflowTransitionStaticTo') |
| 19 | +export class SerializedWorkflowTransitionStaticTo extends DataObject { |
| 20 | + @Field() |
| 21 | + readonly state: SerializedWorkflowState; |
| 22 | +} |
| 23 | + |
| 24 | +@ObjectType('WorkflowTransitionDynamicTo') |
| 25 | +export class SerializedWorkflowTransitionDynamicTo extends DataObject { |
| 26 | + @IdField() |
| 27 | + readonly id: string; |
| 28 | + |
| 29 | + @Field() |
| 30 | + readonly label: string; |
| 31 | + |
| 32 | + @Field(() => [SerializedWorkflowState]) |
| 33 | + readonly relatedStates: readonly SerializedWorkflowState[]; |
| 34 | +} |
| 35 | + |
| 36 | +export const SerializedWorkflowTransitionTo = createUnionType({ |
| 37 | + name: 'WorkflowTransitionTo', |
| 38 | + types: () => [ |
| 39 | + SerializedWorkflowTransitionStaticTo, |
| 40 | + SerializedWorkflowTransitionDynamicTo, |
| 41 | + ], |
| 42 | + resolveType: ( |
| 43 | + value: |
| 44 | + | SerializedWorkflowTransitionStaticTo |
| 45 | + | SerializedWorkflowTransitionDynamicTo, |
| 46 | + ) => |
| 47 | + 'state' in value |
| 48 | + ? SerializedWorkflowTransitionStaticTo |
| 49 | + : SerializedWorkflowTransitionDynamicTo, |
| 50 | +}); |
| 51 | + |
| 52 | +@ObjectType('WorkflowCondition') |
| 53 | +export class SerializedWorkflowCondition extends DataObject { |
| 54 | + @Field() |
| 55 | + readonly label: string; |
| 56 | +} |
| 57 | + |
| 58 | +@ObjectType('WorkflowNotifier') |
| 59 | +export class SerializedWorkflowNotifier extends DataObject { |
| 60 | + @Field() |
| 61 | + readonly label: string; |
| 62 | +} |
| 63 | + |
| 64 | +@ObjectType('WorkflowTransition') |
| 65 | +export class SerializedWorkflowTransition extends DataObject { |
| 66 | + @IdField() |
| 67 | + readonly key: ID; |
| 68 | + |
| 69 | + @Field() |
| 70 | + readonly devName: string; |
| 71 | + |
| 72 | + @Field() |
| 73 | + readonly label: string; |
| 74 | + |
| 75 | + @Field(() => TransitionType) |
| 76 | + readonly type: TransitionType; |
| 77 | + |
| 78 | + @Field(() => [SerializedWorkflowState]) |
| 79 | + readonly from: readonly SerializedWorkflowState[]; |
| 80 | + |
| 81 | + @Field(() => SerializedWorkflowTransitionTo) |
| 82 | + readonly to: typeof SerializedWorkflowTransitionTo; |
| 83 | + |
| 84 | + @Field(() => [SerializedWorkflowCondition]) |
| 85 | + readonly conditions: readonly SerializedWorkflowCondition[]; |
| 86 | + |
| 87 | + @Field(() => [SerializedWorkflowNotifier]) |
| 88 | + readonly notifiers: readonly SerializedWorkflowNotifier[]; |
| 89 | +} |
| 90 | + |
| 91 | +@ObjectType('Workflow') |
| 92 | +export class SerializedWorkflow extends DataObject { |
| 93 | + @IdField() |
| 94 | + readonly id: ID; |
| 95 | + |
| 96 | + @Field(() => [SerializedWorkflowState]) |
| 97 | + readonly states: readonly SerializedWorkflowState[]; |
| 98 | + |
| 99 | + @Field(() => [SerializedWorkflowTransition]) |
| 100 | + readonly transitions: readonly SerializedWorkflowTransition[]; |
| 101 | + |
| 102 | + static from<W extends Workflow>(workflow: W): SerializedWorkflow { |
| 103 | + const serializeState = (state: Workflow['state']) => { |
| 104 | + const { value, label } = workflow.states.entry(state); |
| 105 | + return { value, label }; |
| 106 | + }; |
| 107 | + const dynamicToId = cacheable( |
| 108 | + new Map<DynamicState<W['state'], W['context']>, string>(), |
| 109 | + () => uuid.v1(), |
| 110 | + ); |
| 111 | + return { |
| 112 | + id: workflow.id, |
| 113 | + states: [...workflow.states].map(serializeState), |
| 114 | + transitions: workflow.transitions.map((transition) => ({ |
| 115 | + key: transition.key, |
| 116 | + devName: transition.name, |
| 117 | + label: transition.label, |
| 118 | + type: transition.type, |
| 119 | + from: transition.from ? [...transition.from].map(serializeState) : [], |
| 120 | + to: |
| 121 | + typeof transition.to === 'string' |
| 122 | + ? { |
| 123 | + state: serializeState(transition.to), |
| 124 | + } |
| 125 | + : { |
| 126 | + id: dynamicToId(transition.to), |
| 127 | + label: transition.to.description, |
| 128 | + relatedStates: |
| 129 | + transition.to.relatedStates?.map(serializeState) ?? [], |
| 130 | + }, |
| 131 | + conditions: (transition.conditions ?? []).map((condition) => ({ |
| 132 | + label: condition.description, |
| 133 | + })), |
| 134 | + notifiers: (transition.notifiers ?? []).map((notifier) => ({ |
| 135 | + label: notifier.description, |
| 136 | + })), |
| 137 | + })), |
| 138 | + }; |
| 139 | + } |
| 140 | +} |
0 commit comments