Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Hide one-time completion self-report assessments after complete (M2-8164, M2-8026) #547

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const useTextVariablesReplacer = ({
}: Props) => {
const replaceTextVariables = (text: string) => {
if (items && answers) {
const nickname = respondentMeta?.nickname;
const nickname = respondentMeta?.nickname ?? '';

const replacer = new MarkdownVariableReplacer(items, answers, completedEntityTime, nickname);
return replacer.process(text);
Expand Down
7 changes: 6 additions & 1 deletion src/shared/api/types/applet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export type GetPublicAppletActivityByIdPayload = {
activityId: string;
};

export type RespondentMetaDTO = { nickname: string };
export type RespondentMetaDTO = {
subjectId: string | null;
nickname: string | null;
tag: string | null;
};

// API Responses - Success
export type AppletListSuccessResponse = BaseSuccessListResponse<AppletListDTO>;
Expand Down Expand Up @@ -66,6 +70,7 @@ export type AppletBaseDTO = {
activityFlows: Array<ActivityFlowDTO>;
encryption: EncryptionDTO | null;
integrations?: Integration[];
respondentMeta?: RespondentMetaDTO;
};

export type ActivityBaseDTO = {
Expand Down
39 changes: 18 additions & 21 deletions src/widgets/ActivityGroups/model/hooks/useEntitiesSync.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import { useCallback, useEffect } from 'react';
import { useCallback, useContext, useEffect } from 'react';

import { AppletDetailsContext } from '../../lib';

import { ActivityPipelineType } from '~/abstract/lib';
import { appletModel } from '~/entities/applet';
import { CompletedEntitiesDTO, CompletedEntityDTO } from '~/shared/api';

type FilterCompletedEntitiesProps = {
completedEntities: CompletedEntitiesDTO | undefined;
completedEntities?: CompletedEntitiesDTO;
};

export const useEntitiesSync = (props: FilterCompletedEntitiesProps) => {
export const useEntitiesSync = ({ completedEntities }: FilterCompletedEntitiesProps) => {
const { applet } = useContext(AppletDetailsContext);
const { saveGroupProgress, getGroupProgress } = appletModel.hooks.useGroupProgressStateManager();

const syncEntity = useCallback(
(entity: CompletedEntityDTO) => {
const hoursMinutes = entity.localEndTime.split(':');
const endAtDate = new Date(entity.localEndDate).setHours(
Number(hoursMinutes[0]),
Number(hoursMinutes[1]),
);
const endAtDate = new Date(`${entity.localEndDate}T${entity.localEndTime}`);
const endAtTimestamp = endAtDate.getTime();

const entityId = entity.id;
const eventId = entity.scheduledEventId;
const targetSubjectId = entity.targetSubjectId;
// Normalize targetSubjectId to null for self-reports
const targetSubjectId =
entity.targetSubjectId === applet.respondentMeta?.subjectId ? null : entity.targetSubjectId;

const groupProgress = getGroupProgress({
entityId,
Expand All @@ -37,7 +39,7 @@ export const useEntitiesSync = (props: FilterCompletedEntitiesProps) => {
progressPayload: {
type: ActivityPipelineType.Regular,
startAt: null,
endAt: new Date(endAtDate).getTime(),
endAt: endAtTimestamp,
context: {
summaryData: {},
},
Expand All @@ -46,7 +48,7 @@ export const useEntitiesSync = (props: FilterCompletedEntitiesProps) => {
}

if (groupProgress.endAt) {
const isServerEndAtBigger = endAtDate > new Date(groupProgress.endAt).getTime();
const isServerEndAtBigger = endAtTimestamp > new Date(groupProgress.endAt).getTime();

if (!isServerEndAtBigger) {
return;
Expand All @@ -58,24 +60,19 @@ export const useEntitiesSync = (props: FilterCompletedEntitiesProps) => {
targetSubjectId,
progressPayload: {
...groupProgress,
endAt: new Date(endAtDate).getTime(),
endAt: endAtTimestamp,
},
});
}
},
[getGroupProgress, saveGroupProgress],
[applet.respondentMeta?.subjectId, getGroupProgress, saveGroupProgress],
);

useEffect(() => {
if (!props.completedEntities) {
if (!completedEntities) {
return;
}

const completedEntities = [
...props.completedEntities.activities,
...props.completedEntities.activityFlows,
];

completedEntities.forEach(syncEntity);
}, [props.completedEntities, syncEntity]);
[...completedEntities.activities, ...completedEntities.activityFlows].forEach(syncEntity);
}, [completedEntities, syncEntity]);
};
Loading