All files / server/gql/users/mutations ResendConfirmationEmail.ts

0% Statements 0/25
0% Branches 0/2
100% Functions 0/0
0% Lines 0/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                                                           
import { UserType } from 'aws-sdk/clients/cognitoidentityserviceprovider';
import { GraphQLNonNull, GraphQLString } from 'graphql';
import { Pool, UserPoolId } from '../../../index';
import findEmailInAttributes from '../lib/findEmailInAttributes';
import { getGroups, setGroups } from '../lib/groups';
import GraphQLUser from '../outputs/User';
import { generateCreateUserParams } from './helpers';
 
export default {
  type: GraphQLUser,
  args: {
    username: {
      type: new GraphQLNonNull(GraphQLString),
    },
  },
  resolve: async (
    _: any,
    { username, role }: any,
    { userDetails }: any,
  ): Promise<UserType | undefined> => {
    const params = {
      UserPoolId,
      Username: username,
    };
 
    try {
      // fetch existing data
      const userData = await Pool.adminGetUser(params).promise();
      const groups = await getGroups(username);
 
      // @ts-ignore
      const email = findEmailInAttributes(userData.UserAttributes);
 
      // remove the user
      if (userData) {
        await Pool.adminDeleteUser(params).promise();
      }
 
      const createdUser = await Pool.adminCreateUser(
        // @ts-ignore
        generateCreateUserParams(username, email, UserPoolId),
      ).promise();
 
      if (groups && groups.length > 0) {
        await setGroups(username, groups);
      }
 
      return createdUser.User;
    } catch (error) {
      throw new Error(error);
    }
  },
};