// // Summary: // Finds a list of tasks and sends them to the associated user by Email. // // Parameters: // userId: // A UserId to find his tasks and send him a mail. // // Returns: // True if everything was sent fine. // public bool SendTasksToUser(string userId) { try { long longUserId; if (!(long.TryParse(userId, out longUserId))) { throw new Exception("Token not valid"); } UserService userService = new UserService(configuration, _context); User user = userService.GetUserById(longUserId); List <Models.Task> userTasks = GetUserTasks(userId); if (userTasks.Count == 0) { throw new Exception("No tasks to send"); } MailService mailService = new MailService(configuration); string messageBodyPrefix = configuration.email.messageBodyPreprefix + " " + user.FirstName + configuration.email.messageYourTasksBodyPrefix; string messageBodySufix = configuration.email.messageSuffix + configuration.email.fromName; string messageBody = mailService.BuildMessageBodyFromTasks(userTasks, messageBodyPrefix, messageBodySufix); mailService.SendEmail(user.UserName, user.Email, configuration.email.subjectYourTasks, messageBody); return(true); } catch (Exception err) { throw err; } }
// // Summary: // Finds a list of tasks (their Ids are given) and if they are not already associated, // associate them to a new user and then sends them to the new user. // // Parameters: // shareData: // A shareData object containing the list of tasks to share and the new userId to share with. // // userId: // A Id of the already associated user. // // Returns: // True if everything was associated and sent fine. // public bool ShareTasksWithUser(TasksShareData shareData, string userId) { try { if (shareData.TasksIdsToShare.Count == 0) { throw new Exception("No tasks to share"); } long longUserId; if (!(long.TryParse(userId, out longUserId))) { throw new Exception("Token not valid"); } UserService userService = new UserService(configuration, _context); User sharingUser = userService.GetUserById(longUserId); List <Models.Task> sharingUserTasks = GetUserTasks(userId); User userToShare = userService.GetUserById(shareData.UserIdToShare); List <long> userToShareExistingTasksIds = GetUserTasks(shareData.UserIdToShare.ToString()).Select(tsk => tsk.TaskId).ToList();; List <long> newTasksIdsToShare = new List <long>(); foreach (long TasksIdToShare in shareData.TasksIdsToShare) { if (!userToShareExistingTasksIds.Contains(TasksIdToShare)) { newTasksIdsToShare.Add(TasksIdToShare); } } List <Models.Task> tasksToShare = new List <Models.Task>(); foreach (Models.Task sharingUserTask in sharingUserTasks) { if (newTasksIdsToShare.Contains(sharingUserTask.TaskId)) { tasksToShare.Add(sharingUserTask); } } if (tasksToShare.Count == 0) { throw new Exception("No tasks to share"); } UserTaskService userTaskService = new UserTaskService(configuration, _context); userTaskService.AssociateTasksToUser(tasksToShare, userToShare); MailService mailService = new MailService(configuration); string messageBodyPrefix = configuration.email.messageBodyPreprefix + " " + userToShare.FirstName + ",\n" + sharingUser.UserName + configuration.email.messageSharedTasksBodyPrefix; string messageBodySufix = configuration.email.messageSuffix + configuration.email.fromName; string messageBody = mailService.BuildMessageBodyFromTasks(tasksToShare, messageBodyPrefix, messageBodySufix); mailService.SendEmail(userToShare.UserName, userToShare.Email, sharingUser.UserName + " " + configuration.email.subjectSharedTasks, messageBody); return(true); } catch (Exception err) { throw err; } }