I am faced with a situation where I need to create a user through a function, but before proceeding with the creation process, I have to verify whether another user with the same userName is already present in the session:
public createUser(form: FormGroup, sessionCode?: string): void {
if (sessionCode && this.checkForDuplicateUserInSession(form, sessionCode)) return;
this.apollo.mutate<CreateUser>({
mutation: CREATE_USER,
variables: {
name: form.controls['user'].get('userName')?.value,
},
}).subscribe(
...
);
}
private checkForDuplicateUserInSession(form: FormGroup, sessionCode?: string): void {
this.apollo.query<GetSession>({
query: GET_SESSION,
variables: {
code: sessionCode
}
}).subscribe({
next: ({data}) => {
return data.getSession.players.some((player) => player.name === form.controls['user'].get('userName')?.value);
}
});
}
The functionality of checkForDuplicateUserInSession()
is accurate in determining the presence of any duplicate users, however, integrating it within the createUser()
function seems challenging.
Should I establish an observable like userIsDuplicate$
and feed the outcome of some()
into that before subscribing? Is there perhaps an alternative approach that I am overlooking?