async function submitUser()

in components/register-new-user-with-idcard.tsx [195:284]


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

    try {
        if (!validateFields(props)) {
            dispatch({ type: 'alertMessage', payload: 'Please fill in all fields; *and* please ensure that you\'ve supplied both a selfie image and uploaded an id card.' });
            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) {
            console.log("Unable to upload image");
            dispatch({ type: 'busy', payload: 'false' });
            return;
        }

        // call api to run through idv new user registration flow
        // see lambda function idvworkflowfn for more details
        const variables = {
            userInfoAsJson: JSON.stringify(userInfo),
            faceImageDataBase64: getImageFromUploadComponent(props.screenshot),
            idImageDataBase64: getImageFromUploadComponent(props.idCard[0]["data_url"])
        };

        const registerUserResponse = await callGraphQLSimpleQuery<RegisternewuserwithidcardMutation>(
            {
                query: registernewuserwithidcard,
                authMode: GRAPHQL_AUTH_MODE.AMAZON_COGNITO_USER_POOLS,
                variables: variables,
            }
        );

        if (!registerUserResponse.data?.registernewuserwithidcard?.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?.registernewuserwithidcard?.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);
    }
}