import { Context } from "aws-lambda";
import { CognitoIdentityServiceProvider } from "aws-sdk";
import { initCommonMiddlewares } from "../middlewares";

interface IEvent {
  username: string;
  confirmationCode: string;
}

type IResult = boolean;

const { POOL_CLIENT_ID } = process.env;

const userProvider = new CognitoIdentityServiceProvider();

async function awsHandler(event: IEvent, context: Context): Promise<IResult> {
  const { username, confirmationCode } = event;

  await userProvider
    .confirmSignUp({
      ClientId: POOL_CLIENT_ID,
      Username: username,
      ConfirmationCode: confirmationCode,
    })
    .promise();

  return true;
}

export const handler = initCommonMiddlewares(awsHandler);
