Skip to content

Commit

Permalink
Make it possible to update group avatar path #3257
Browse files Browse the repository at this point in the history
  • Loading branch information
LinaKind committed May 20, 2024
1 parent 6b978ce commit b6daef6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/read-models/groups/handle-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const handleEvent = (readmodel: ReadModel, event: DomainEvent): ReadModel
if (event.largeLogoPath !== undefined) {
readmodel[event.groupId].largeLogoPath = O.some(event.largeLogoPath);
}
if (event.avatarPath !== undefined) {
readmodel[event.groupId].avatarPath = event.avatarPath;
}
}
return readmodel;
};
9 changes: 8 additions & 1 deletion src/write-side/resources/group/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type GroupState = {
name: string,
shortDescription: string,
largeLogoPath: string | undefined,
avatarPath: string,
};

type ReplayedGroupState = E.Either<'no-such-group' | 'bad-data', GroupState>;
Expand All @@ -26,6 +27,7 @@ const buildGroup = (state: ReplayedGroupState, event: DomainEvent): ReplayedGrou
name: event.name,
shortDescription: event.shortDescription,
largeLogoPath: event.largeLogoPath,
avatarPath: event.avatarPath,
});
}
if (isEventOfType('GroupDetailsUpdated')(event)) {
Expand All @@ -37,6 +39,7 @@ const buildGroup = (state: ReplayedGroupState, event: DomainEvent): ReplayedGrou
name: event.name ?? groupState.name,
shortDescription: event.shortDescription ?? groupState.shortDescription,
largeLogoPath: event.largeLogoPath ?? groupState.largeLogoPath,
avatarPath: event.avatarPath ?? groupState.avatarPath,
}),
),
);
Expand Down Expand Up @@ -91,12 +94,17 @@ const calculateAttributesToUpdate = (
&& command.shortDescription !== groupState.shortDescription)
? command.shortDescription
: undefined,
avatarPath: (command.avatarPath !== undefined
&& command.avatarPath !== groupState.avatarPath)
? command.avatarPath
: undefined,
});

const hasAnyValues = (attributes: Record<string, string | undefined>): boolean => (
(attributes.name !== undefined)
|| (attributes.shortDescription !== undefined)
|| (attributes.largeLogoPath !== undefined)
|| (attributes.avatarPath !== undefined)
);

export const update: ResourceAction<UpdateGroupDetailsCommand> = (command) => (events) => pipe(
Expand All @@ -110,7 +118,6 @@ export const update: ResourceAction<UpdateGroupDetailsCommand> = (command) => (e
E.map((attributesToUpdate) => (hasAnyValues(attributesToUpdate) ? [constructEvent('GroupDetailsUpdated')({
groupId: command.groupId,
homepage: undefined,
avatarPath: undefined,
descriptionPath: undefined,
slug: undefined,
...attributesToUpdate,
Expand Down

0 comments on commit b6daef6

Please sign in to comment.