public async Task <IActionResult> SendPM([FromBody] IracingPm pm) { var success = await _iracingService.SendPrivateMessage(pm.UserId, pm.Message); if (!success) { return(StatusCode(StatusCodes.Status500InternalServerError, "An error occured sending a PM. Please contact Support for assistance.")); } return(Ok()); }
public async Task <IActionResult> SendIracingVerification([FromBody] IracingIdWrapper wrapper) { if (String.IsNullOrEmpty(wrapper.IracingId)) { return(BadRequest("Iracing Id must be populated")); } wrapper.IracingId = wrapper.IracingId.Trim(); bool canConvert = int.TryParse(wrapper.IracingId, out _); if (!canConvert) { return(BadRequest("Iracing Id must be a number")); } var user = _context.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); if (user == null) { return(Unauthorized()); } if (!String.IsNullOrEmpty(user.IracingId)) { return(BadRequest("User has already been verified on iRacing")); } var existingUser = _context.Users.FirstOrDefault(u => u.IracingId == wrapper.IracingId); if (existingUser != null) { return(BadRequest($"User with iracing {wrapper.IracingId} has already been created")); } var invite = _context.UserInvites.FirstOrDefault(u => u.UserId == user.Id && u.Status == InviteStatus.SENT); if (invite == null) { var duplicateInvite = _context.UserInvites.FirstOrDefault(u => u.IracingId == wrapper.IracingId && u.Status == InviteStatus.SENT); if (duplicateInvite != null) { return(BadRequest("Someone else has claimed this iRacing account already. Please contact Support to resolve this")); } invite = new UserInvite { UserId = user.Id, Status = InviteStatus.SENT, LastUpdated = DateTime.UtcNow, IracingId = wrapper.IracingId }; _context.UserInvites.Add(invite); } else { DateTime now = DateTime.UtcNow; if (invite.LastUpdated > now.AddMinutes(-15) && invite.LastUpdated <= now) { return(BadRequest("You must wait 15 minutes after a verification message to request an additional one")); } if (invite.IracingId != wrapper.IracingId) { return(BadRequest("You have an existing invite with a different iRacing Id. Please contact Support to resolve this")); } invite.LastUpdated = DateTime.UtcNow; } try { var success = await _iracingService.SendPrivateMessage(wrapper.IracingId, $"Click on the link to validate your RaceSpot Liveries Account! {_baseUrl}/#key={invite.Id}"); if (!success) { return(StatusCode(StatusCodes.Status500InternalServerError, "An error occured sending a PM. Please contact Support for assistance.")); } _context.SaveChanges(); return(Ok()); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, "There was an internal error that prevented a PM from being sent. Please contact Support for assistance.")); } }