Skip to content

Commit

Permalink
move reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic committed May 6, 2020
1 parent 98cff9b commit e63e934
Showing 1 changed file with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@ interface CommentsAndIndex {
commentId: string;
commentIndex: number;
}
// if the comment happens after the lastUpdateToCaseIndex, it should be included in commentsToUpdate
const buildCommentsToUpdate = (
commentsAndIndex: CommentsAndIndex[],
lastUpdateToCaseIndex: number
) =>
commentsAndIndex.reduce<string[]>(
(bacc, currentComment) =>
currentComment.commentIndex > lastUpdateToCaseIndex
? bacc.indexOf(currentComment.commentId) > -1
? [...bacc.filter(e => e !== currentComment.commentId), currentComment.commentId]
: [...bacc, currentComment.commentId]
: bacc,
[]
);

export const getPushedInfo = (
caseUserActions: CaseUserActions[],
Expand Down Expand Up @@ -102,7 +88,7 @@ export const getPushedInfo = (
[]
);

const caseServices = caseUserActions.reduce<CaseServices>((acc, cua, i) => {
let caseServices = caseUserActions.reduce<CaseServices>((acc, cua, i) => {
if (cua.action !== 'push-to-service') {
return acc;
}
Expand All @@ -120,7 +106,7 @@ export const getPushedInfo = (
...acc[externalService.connectorId],
...externalService,
lastPushIndex: i,
commentsToUpdate: buildCommentsToUpdate(commentsAndIndex, i),
commentsToUpdate: [],
},
}
: {
Expand All @@ -129,12 +115,31 @@ export const getPushedInfo = (
firstPushIndex: i,
lastPushIndex: i,
hasDataToPush: hasDataToPushForConnector(externalService.connectorId),
commentsToUpdate: buildCommentsToUpdate(commentsAndIndex, i),
commentsToUpdate: [],
},
}),
};
}, {});

caseServices = Object.keys(caseServices).reduce<CaseServices>((acc, key) => {
return {
...acc,
[key]: {
...caseServices[key],
// if the comment happens after the lastUpdateToCaseIndex, it should be included in commentsToUpdate
commentsToUpdate: commentsAndIndex.reduce<string[]>(
(bacc, currentComment) =>
currentComment.commentIndex > caseServices[key].lastPushIndex
? bacc.indexOf(currentComment.commentId) > -1
? [...bacc.filter(e => e !== currentComment.commentId), currentComment.commentId]
: [...bacc, currentComment.commentId]
: bacc,
[]
),
},
};
}, {});

const hasDataToPush =
caseServices[caseConnectorId] != null ? caseServices[caseConnectorId].hasDataToPush : true;
return {
Expand Down

0 comments on commit e63e934

Please sign in to comment.