I have the below aggregate to initialize a user. The user has a code. The code is predefined for the first user created DCAA" + cmd.getAcronym() + "001
. Now, for the other users, I always need to retrieve the last created code to construct a new one.
So, my question is: how can I get the userCode
of the last created user in the CommandHandler
?
@Getter
@Setter
@NoArgsConstructor
public class User {
@AggregateIdentifier
private String userId;
private String userCode;
@CommandHandler
@CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING)
private void handle(InitializeUser cmd) {
final String code = "DCAA"+ cmd.getAcronym() +"001";
final UserInitialized event = UserInitialized.builder()
.userId(cmd.getUserId())
.userCode(code)
.build();
AggregateLifecycle.apply(event);
}
@EventSourcingHandler
private void on(UserInitialized evt) {
this.userId = evt.getUserId();
this.userCode = getUserCode();
}
}