Skip to content

Commit

Permalink
M2-6235: Added migrations for in progress activities and flows entity…
Browse files Browse the repository at this point in the history
…Name, refactor, code review fixes
  • Loading branch information
vmkhitaryanscn committed May 21, 2024
1 parent d428ce8 commit c810088
Show file tree
Hide file tree
Showing 13 changed files with 540 additions and 18 deletions.
10 changes: 4 additions & 6 deletions src/abstract/lib/types/entityProgress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ export const enum ActivityPipelineType {
Flow,
}

export type EntityName = {
entityName: string;
};

export type FlowProgress = {
type: ActivityPipelineType.Flow;
pipelineActivityOrder: number;
Expand All @@ -18,11 +14,13 @@ export type FlowProgress = {
currentActivityImage: string | null;
currentActivityStartAt: number | null;
executionGroupKey: string;
} & EntityName;
entityName: string;
};

export type ActivityProgress = {
type: ActivityPipelineType.Regular;
} & EntityName;
entityName: string;
};

export type EntityProgress = FlowProgress | ActivityProgress;

Expand Down
2 changes: 2 additions & 0 deletions src/app/model/migrations/MigrationFactory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MigrationToVersion0002 } from '@app/app/model/migrations/migrations/to0002/MigrationToVersion0002';
import { __queryClient__ } from '@app/app/ui/AppProvider/ReactQueryProvider';

import { MigrationToVersion0001 } from './migrations/to0001/MigrationToVersion0001';
Expand All @@ -11,6 +12,7 @@ export class MigrationFactory {
public createMigrations(): Migrations {
return {
1: new MigrationToVersion0001(__queryClient__),
2: new MigrationToVersion0002(__queryClient__),
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ImageUrl } from '@app/shared/lib';

export type ActivityRecordDto = {
id: string;
name: string;
description: string;
image: ImageUrl | null;
order: number;
};

export type ActivityFlowRecordDto = {
id: string;
name: string;
description: string;
order: number;
activityIds: Array<string>;
};

export type AppletDetailsDto = {
id: string;
displayName: string;
activities: ActivityRecordDto[];
activityFlows: ActivityFlowRecordDto[];
};

export type AppletDetailsResponse = {
result: AppletDetailsDto;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { ActivityPipelineType } from '@app/abstract/lib';

export type FlowProgressFrom = {
type: ActivityPipelineType.Flow;
pipelineActivityOrder: number;
totalActivitiesInPipeline: number;
currentActivityId: string;
currentActivityName: string;
currentActivityDescription: string;
currentActivityImage: string | null;
currentActivityStartAt: number | null;
executionGroupKey: string;
};

type ActivityProgressFrom = {
type: ActivityPipelineType.Regular;
};

type EntityProgressFrom = FlowProgressFrom | ActivityProgressFrom;

export type StoreProgressPayloadFrom = EntityProgressFrom & {
startAt: number;
endAt: number | null;
};

type StoreEventsProgressFrom = Record<string, StoreProgressPayloadFrom>;

type StoreEntitiesProgressFrom = Record<string, StoreEventsProgressFrom>;

type StoreProgressFrom = Record<string, StoreEntitiesProgressFrom>;

export type FlowProgressTo = {
type: ActivityPipelineType.Flow;
pipelineActivityOrder: number;
totalActivitiesInPipeline: number;
currentActivityId: string;
currentActivityName: string;
currentActivityDescription: string;
currentActivityImage: string | null;
currentActivityStartAt: number | null;
executionGroupKey: string;
entityName: string;
};

type ActivityProgressTo = {
type: ActivityPipelineType.Regular;
entityName: string;
};

type EntityProgressTo = FlowProgressTo | ActivityProgressTo;

export type StoreProgressPayloadTo = EntityProgressTo & {
startAt: number;
endAt: number | null;
};

type StoreEventsProgressTo = Record<string, StoreProgressPayloadTo>;

type StoreEntitiesProgressTo = Record<string, StoreEventsProgressTo>;

export type StoreProgressTo = Record<string, StoreEntitiesProgressTo>;

export type RootStateFrom = {
applets: {
inProgress: StoreProgressFrom;
completedEntities: any;

Check warning on line 66 in src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts#L66

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
completions: any;

Check warning on line 67 in src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts#L67

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
};
streaming: any;

Check warning on line 69 in src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts#L69

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
identity: any;

Check warning on line 70 in src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts#L70

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
};

export type RootStateTo = {
applets: {
inProgress: StoreProgressTo;
completedEntities: any;

Check warning on line 76 in src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts#L76

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
completions: any;

Check warning on line 77 in src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts#L77

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
};
streaming: any;

Check warning on line 79 in src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts#L79

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
identity: any;

Check warning on line 80 in src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationReduxTypes0002.ts#L80

Unexpected any. Specify a different type (@typescript-eslint/no-explicit-any)
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type FlowPipelineItemFrom = {
type: 'Stepper' | 'Intermediate' | 'Summary' | 'Finish';
payload: {
appletId: string;
activityId: string;
eventId: string;
flowId?: string;
order: number;
activityName: string;
activityDescription?: string;
activityImage?: string | null;
};
};

export type FlowStateFrom = {
step: number;
pipeline: FlowPipelineItemFrom[];
isCompletedDueToTimer: boolean;
context: Record<string, unknown>;
flowName: string;
};
212 changes: 212 additions & 0 deletions src/app/model/migrations/migrations/to0002/MigrationToVersion0002.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import { QueryClient } from '@tanstack/react-query';

import { Logger } from '@app/shared/lib';

import {
RootStateFrom,
RootStateTo,
StoreProgressPayloadTo,
} from './MigrationReduxTypes0002.ts';
import { FlowStateFrom } from './MigrationStorageTypes0002.ts';
import {
getUpdatedReduxState,
selectNotCompletedActivities,
selectNotCompletedFlows,
} from './MigrationUtils0002';
import { QueryDataUtils } from './MigrationUtils0002.ts';
import {
NotCompletedEntitiesFrom,
NotCompletedEntitiesTo,
} from './MigrationUtils0002.ts';
import {
IMigration,
MigrationInput,
MigrationOutput,
Storages,
} from '../../types';
import { getStorageRecord } from '../../utils.ts';

export class MigrationToVersion0002 implements IMigration {
private queryDataUtils: QueryDataUtils;

constructor(queryClient: QueryClient) {
this.queryDataUtils = new QueryDataUtils(queryClient);
}

private getFlowState = (key: string): FlowStateFrom | null => {
return getStorageRecord(Storages.FlowProgress, key);
};

private getFlowRecordKey = (
appletId: string,
flowId: string | null,
eventId: string,
) => {
const flowKey = flowId ?? 'default_one_step_flow';
return `${flowKey}-${appletId}-${eventId}`;
};

private getUpdatedFlowProgress(
progressFlowFrom: NotCompletedEntitiesFrom,
entityName: string,
): NotCompletedEntitiesTo {
const storeProgressPayloadTo: StoreProgressPayloadTo = {
...progressFlowFrom.payload,
entityName: entityName,
};

return {
appletId: progressFlowFrom.appletId,
eventId: progressFlowFrom.eventId,
entityId: progressFlowFrom.entityId,
type: progressFlowFrom.type,
payload: storeProgressPayloadTo,
};
}

private getUpdatedActivityProgress(
progressFlowFrom: NotCompletedEntitiesFrom,
entityName: string,
): NotCompletedEntitiesTo {
const storeProgressPayloadTo: StoreProgressPayloadTo = {
...progressFlowFrom.payload,
entityName: entityName,
};

return {
appletId: progressFlowFrom.appletId,
eventId: progressFlowFrom.eventId,
entityId: progressFlowFrom.entityId,
type: progressFlowFrom.type,
payload: storeProgressPayloadTo,
};
}
private getUpdatedProgressForFlows(
reduxState: RootStateFrom,
): NotCompletedEntitiesTo[] {
const progressFlowsFrom = selectNotCompletedFlows(reduxState);
const progressFlowsTo: NotCompletedEntitiesTo[] = [];

for (const progressFlowFrom of progressFlowsFrom) {
const { appletId, entityId, eventId } = progressFlowFrom;

const logAppletName = '',
logFlowName = '';
const logFlowStateFrom = '';
const logCurrentActivityDto = '';
const logActivityFlowDto = '';
const logProgressFlowFrom = JSON.stringify(progressFlowFrom, null, 2);

try {
const key = this.getFlowRecordKey(appletId, entityId, eventId);

const flowStateFrom = this.getFlowState(key)!;

Check warning on line 103 in src/app/model/migrations/migrations/to0002/MigrationToVersion0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationToVersion0002.ts#L103

Forbidden non-null assertion (@typescript-eslint/no-non-null-assertion)

if (!flowStateFrom) {
Logger.warn(
`[MigrationToVersion0002.getUpdatedProgressForFlows]: Migration cannot be executed as flowState doesn't exist appletName=${logAppletName}, appletId=${appletId}, entityId=${entityId}, eventId=${eventId}`,
);
continue;
}
const flowName = flowStateFrom.flowName;

const progressFlowTo = this.getUpdatedFlowProgress(
progressFlowFrom,
flowName,
);

progressFlowsTo.push(progressFlowTo);
} catch (error) {
Logger.warn(
`[MigrationToVersion0002.getUpdatedProgressForFlows]: Error occurred, appletName=${logAppletName}, flowName=${logFlowName}, progressFlowFrom=${logProgressFlowFrom}, flowStateFrom=${logFlowStateFrom}, currentActivityDto=${logCurrentActivityDto}, activityFlowDto=${logActivityFlowDto} \nerror: \n${error}`,

Check warning on line 121 in src/app/model/migrations/migrations/to0002/MigrationToVersion0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationToVersion0002.ts#L121

Invalid type "unknown" of template literal expression (@typescript-eslint/restrict-template-expressions)
);
}
}

return progressFlowsTo;
}

private getUpdatedProgressForActivities(
reduxState: RootStateFrom,
): NotCompletedEntitiesTo[] {
const progressActivitiesFrom = selectNotCompletedActivities(reduxState);
const progressActivitiesTo = [];

for (const progressActivityFrom of progressActivitiesFrom) {
const { appletId, entityId } = progressActivityFrom;
let logAppletName = '',
logActivityName = '';
const logActivityStateFrom = '';
const logCurrentActivityDto = '';
const logActivityActivityDto = '';
const logProgressActivityFrom = JSON.stringify(
progressActivityFrom,
null,
2,
);

try {
const appletDto = this.queryDataUtils.getAppletDto(appletId);

if (!appletDto) {
Logger.warn(
"[MigrationToVersion0002.getUpdatedProgressForActivities]: Migration cannot be executed as applet doesn't exist: " +
appletId,
);
continue;
}
logAppletName = appletDto.displayName;

const activityDto = appletDto.activities.find(f => f.id === entityId);

if (!activityDto) {
Logger.warn(
"[MigrationToVersion0002.getUpdatedProgressForActivities]: activityFlow doesn't exist: " +
entityId,
);
continue;
}

logActivityName = activityDto.name;

const progressActivityTo = this.getUpdatedActivityProgress(
progressActivityFrom,
activityDto.name,
);

progressActivitiesTo.push(progressActivityTo);
} catch (error) {
Logger.warn(
`[MigrationToVersion0002.getUpdatedProgressForActivities]: Error occurred, appletName=${logAppletName}, flowName=${logActivityName}, progressActivityFrom=${logProgressActivityFrom}, flowStateFrom=${logActivityStateFrom}, currentActivityDto=${logCurrentActivityDto}, activityActivityDto=${logActivityActivityDto} \nerror: \n${error}`,

Check warning on line 180 in src/app/model/migrations/migrations/to0002/MigrationToVersion0002.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/app/model/migrations/migrations/to0002/MigrationToVersion0002.ts#L180

Invalid type "unknown" of template literal expression (@typescript-eslint/restrict-template-expressions)
);
}
}

return progressActivitiesTo;
}

private getUpdatedProgress(
reduxState: RootStateFrom,
): NotCompletedEntitiesTo[] {
const activitiesProgressTo =
this.getUpdatedProgressForActivities(reduxState);
const flowsProgressTo = this.getUpdatedProgressForFlows(reduxState);

return [...activitiesProgressTo, ...flowsProgressTo];
}

migrate(input: MigrationInput): MigrationOutput {
const result: MigrationOutput = {
reduxState: { ...input.reduxState } as RootStateTo,
};

const reduxRootStateFrom: RootStateFrom = input.reduxState as RootStateFrom;

const progressTo = this.getUpdatedProgress(reduxRootStateFrom);
result.reduxState = getUpdatedReduxState(reduxRootStateFrom, progressTo);

return result;
}
}

export default MigrationToVersion0002;
Loading

0 comments on commit c810088

Please sign in to comment.