async function submitUser()

in components/register-new-user.tsx [111:194]


async function submitUser(props: RegNewUserProps, dispatch: Dispatch<RegUserAction>) {

    try {
        if (!validateFields(props)) {
            dispatch({ type: 'alertMessage', payload: 'Please fill in all fields; *and* please ensure that you\'ve supplied a selfie image.' });
            return;
        }

        dispatch({ type: 'alertMessage', payload: '' });
        dispatch({type: 'busy', payload: 'true'});

        // create ddb entry first
        var filename = "regimages/" + props.userid + ".jpg";
        var userInfo = {
            companyid: 'Amazon',
            userid: props.userid,
            firstname: props.firstname,
            lastname: props.lastname,
            dob: props.dob,
            registrationstatus: 'error-initialentry',
            faceimage: filename,
        }

        const createUserResponse = await callGraphQL<CreateUserInfoMutation>(
            {
                query: createUserInfo,
                authMode: GRAPHQL_AUTH_MODE.AMAZON_COGNITO_USER_POOLS,
                variables: {
                    input: userInfo
                }
            }
        );

        // then store image in s3 bucket
        let imageData = await fetch(props.screenshot);
        let blob = await imageData.blob();
        const storageResponse = await Storage.put(filename,
            blob, {
            contentType: 'image/jpeg',
            progressCallback(progress: any) {
                console.log(`Uploaded: ${progress.loaded}/${progress.total}`);
            }
        }) as StoragePutResponse;

        if (storageResponse && storageResponse.key) {

            // call api to run through idv new user registration flow.
            // see lambda function idvworkflowfn for more details
            const variables = {userInfoAsJson: JSON.stringify(userInfo)};
            const registerUserResponse = await callGraphQLSimpleQuery<RegisternewuserMutation>(
                {
                    query: registernewuser,
                    authMode: GRAPHQL_AUTH_MODE.AMAZON_COGNITO_USER_POOLS,
                    variables: variables,
                }
            );

            console.log(registerUserResponse);

            if (!registerUserResponse.data?.registernewuser?.Success) {
                // cleanup
                console.log("Cleaning up incomplete user registration...")
                const variables = {userInfoAsJson: JSON.stringify(userInfo)};
                const deleteUserResponse = await callGraphQLSimpleQuery<DeleteuserMutation>(
                    {
                        query: deleteuser,
                        authMode: GRAPHQL_AUTH_MODE.AMAZON_COGNITO_USER_POOLS,
                        variables: variables,
                    }
                );
                console.log("Done cleaning up incomplete user registration...")

                dispatch({ type: 'alertMessage', payload: registerUserResponse.data?.registernewuser?.Message as string });
                dispatch({ type: 'busy', payload: 'false' });
            } else {
                dispatch({ type: 'success', payload: '' });
            }
        }
    } catch (errors) {
        dispatch({ type: 'alertMessage', payload: 'Possible duplicate key. ' + JSON.stringify(errors) });
        dispatch({type: 'busy', payload: 'false'});
        console.log(errors);
    }
}