Skip to content

Commit

Permalink
(fix:M2-7597) The extractActivityIdsToSubmit has incorrect business l…
Browse files Browse the repository at this point in the history
…ogic (#524)

* Remove activityProgress after autoCompletion

* Fix: clearTimer when useEffect unmount phase

* fix: M2-7353

* Fix the activity ids to submit calculcation logic

---------

Co-authored-by: Viktor Riabkov <[email protected]>
  • Loading branch information
moiskillnadne and Viktor Riabkov committed Aug 14, 2024
1 parent e4877b1 commit fb8f110
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { extractActivityIdsToSubmitByParams } from './extractActivityidsToSubmitByParams';

import { appletModel } from '~/entities/applet';

describe('extractActivityIdsToSubmitByParams', () => {
const mockActivityProgress: appletModel.ActivityProgress = {
items: [],
step: 0,
userEvents: [],
isSummaryScreenOpen: false,
scoreSettings: undefined,
itemTimer: {},
};

it('should return an array with the current activity id if isFlow is false', () => {
const params = {
isFlow: false,
currentActivityId: 'activity1',
interruptedActivityId: 'activity1',
flowActivityIds: null,
interruptedActivityProgress: mockActivityProgress,
};

const result = extractActivityIdsToSubmitByParams(params);
Expand All @@ -16,8 +28,9 @@ describe('extractActivityIdsToSubmitByParams', () => {
it('should throw an error if flowActivityIds is not defined', () => {
const params = {
isFlow: true,
currentActivityId: 'activity1',
interruptedActivityId: 'activity1',
flowActivityIds: null,
interruptedActivityProgress: mockActivityProgress,
};

expect(() => extractActivityIdsToSubmitByParams(params)).toThrowError(
Expand All @@ -28,8 +41,9 @@ describe('extractActivityIdsToSubmitByParams', () => {
it('should return an array with the current activity id and the last activity id if isInterruptedActivityLast is false', () => {
const params = {
isFlow: true,
currentActivityId: 'activity1',
interruptedActivityId: 'activity1',
flowActivityIds: ['activity1', 'activity2', 'activity3'],
interruptedActivityProgress: mockActivityProgress,
};

const result = extractActivityIdsToSubmitByParams(params);
Expand All @@ -40,8 +54,9 @@ describe('extractActivityIdsToSubmitByParams', () => {
it('should return an array with only the current activity id if isInterruptedActivityLast is true', () => {
const params = {
isFlow: true,
currentActivityId: 'activity3',
interruptedActivityId: 'activity3',
flowActivityIds: ['activity1', 'activity2', 'activity3'],
interruptedActivityProgress: mockActivityProgress,
};

const result = extractActivityIdsToSubmitByParams(params);
Expand All @@ -52,12 +67,52 @@ describe('extractActivityIdsToSubmitByParams', () => {
it('should return an array with the current activity id and the last activity id if isInterruptedActivityLast is false', () => {
const params = {
isFlow: true,
currentActivityId: 'activity2',
interruptedActivityId: 'activity2',
flowActivityIds: ['activity1', 'activity2', 'activity3'],
interruptedActivityProgress: mockActivityProgress,
};

const result = extractActivityIdsToSubmitByParams(params);

expect(result).toEqual(['activity2', 'activity3']);
});

it('should return an array with the last activity id if is interrupted activity id has not progress state', () => {
const params = {
isFlow: true,
interruptedActivityId: 'activity2',
flowActivityIds: ['activity1', 'activity2', 'activity3'],
interruptedActivityProgress: null,
};

const result = extractActivityIdsToSubmitByParams(params);

expect(result).toEqual(['activity3']);
});

it('should return an array with the current activity id and the last activity id if isInterruptedActivityLast is false', () => {
const params = {
isFlow: true,
interruptedActivityId: 'activity2',
flowActivityIds: ['activity1', 'activity2', 'activity3'],
interruptedActivityProgress: mockActivityProgress,
};

const result = extractActivityIdsToSubmitByParams(params);

expect(result).toEqual(['activity2', 'activity3']);
});

it('should return an array with the last activity id if the interrupted activity progress is empty', () => {
const params = {
isFlow: true,
interruptedActivityId: 'activity2',
flowActivityIds: ['activity1', 'activity2', 'activity3'],
interruptedActivityProgress: null,
};

const result = extractActivityIdsToSubmitByParams(params);

expect(result).toEqual(['activity3']);
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { appletModel } from '~/entities/applet';

type ActivityIdsExtractorParams = {
isFlow: boolean;
currentActivityId: string;
interruptedActivityId: string;

interruptedActivityProgress: appletModel.ActivityProgress | null;
flowActivityIds: string[] | null;
};

export const extractActivityIdsToSubmitByParams = (
params: ActivityIdsExtractorParams,
): string[] => {
if (!params.isFlow) {
return [params.currentActivityId];
return [params.interruptedActivityId];
}

if (!params.flowActivityIds) {
Expand All @@ -17,13 +20,15 @@ export const extractActivityIdsToSubmitByParams = (
);
}

const interruptedActivityId = params.currentActivityId;

const lastActivityId = params.flowActivityIds[params.flowActivityIds.length - 1];

const isInterruptedActivityLast = interruptedActivityId === lastActivityId;
const isInterruptedActivityLast = params.interruptedActivityId === lastActivityId;

const activitiesToSubmit = [];

const activitiesToSubmit = [interruptedActivityId];
if (params.interruptedActivityProgress) {
activitiesToSubmit.push(params.interruptedActivityId);
}

if (!isInterruptedActivityLast) {
activitiesToSubmit.push(lastActivityId);
Expand Down
13 changes: 11 additions & 2 deletions src/widgets/Survey/model/hooks/useEntityTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,18 @@ export const useEntityTimer = ({ onFinish }: Props) => {

const timerSettings = context.event.timers.timer;

const entityStartedAt = groupProgress?.startAt ?? null;
if (!groupProgress || !timerSettings || isSummaryScreenOpen) {
return;
}

const entityStartedAt = groupProgress.startAt;
const entityEndedAt = groupProgress.endAt;

if (!entityStartedAt) {
return;
}

if (!groupProgress || !entityStartedAt || !timerSettings || isSummaryScreenOpen) {
if (entityEndedAt) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion src/widgets/Survey/ui/ScreenManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export const ScreenManager = ({ openTimesUpModal }: Props) => {
const onTimerFinish = useCallback(() => {
const activitiesToSubmit: string[] = AutoCompletionModel.extractActivityIdsToSubmitByParams({
isFlow: !!context.flow,
currentActivityId: context.activityId,
interruptedActivityId: context.activityId,
flowActivityIds: context.flow?.activityIds ?? null,
interruptedActivityProgress: activityProgress ?? null,
});

saveAutoCompletion({
Expand All @@ -45,6 +46,7 @@ export const ScreenManager = ({ openTimesUpModal }: Props) => {

return openTimesUpModal();
}, [
activityProgress,
context.activityId,
context.entityId,
context.eventId,
Expand Down

0 comments on commit fb8f110

Please sign in to comment.