I have a function that creates a user, but before that user is created I want to check if another user with the same userName already exists in that 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 checkForDuplicateUserInSession()
does correctly check if another user already exists or not, but I'm not sure how I can use that in the createUser()
function.
Do I have to make a make a userIsDuplicate$
observable to which I push the some()
result and then subscribe to that? Or is there a different way I'm not seeing?