vedranjukic
12/23/2017 - 7:00 PM

Email service interface

Email service interface

//
//  Email service interface implemented
//
async function updateUser ({userRepository, emailService, updateParams}) {
  const { userId, userName, userEmail } = updateParams

  //  validate params
  if (!userId || !userName || !userEmail) {
    throw new InvalidParamsException('Missing required params')
  }
  
  if (!isValidUserName(userName)) {
    throw new InvalidParamsException('Invalid username')
  }
  if (!isValidEmail(userEmail)) {
    throw new InvalidParamsException('Invalid email')
  }

  const user = await userRepository.getById(userId)
  
  //  no need to validate does user exist
  //  because userRepository will throw UserNotFoundException
  //  if there is no user to return

  const updatedUser = {
    ...user,
    userName,
    userEmail
  }
  
  await userRepository.save(updatedUser)
  
  emailService.send({
    to: userEmail,
    subject: 'Updated details',
    body: 'Your details have been updated'
  })
}