Example #1
0
        public async Task <IActionResult> UpdateRequestedItem(Guid requestedItemId, UpdateRequestedItemDto updateItemDto)
        {
            var itemToUpdate = await _beachBuddyRepository.GetRequestedItem(requestedItemId);

            if (itemToUpdate == null)
            {
                return(NotFound());
            }

            if (updateItemDto.Name == null)
            {
                updateItemDto.Name = itemToUpdate.Name;
            }

            if (updateItemDto.Count == 0)
            {
                updateItemDto.Count = itemToUpdate.Count;
            }

            // Only set the completed time if it was previously not set
            if (updateItemDto.IsRequestCompleted && !itemToUpdate.IsRequestCompleted)
            {
                itemToUpdate.CompletedDateTime = DateTimeOffset.UtcNow;
            }

            _mapper.Map(updateItemDto, itemToUpdate);
            _beachBuddyRepository.UpdateRequestedItem(itemToUpdate);
            await _beachBuddyRepository.Save();

            var itemToReturn = _mapper.Map <RequestedItemDto>(itemToUpdate);

            // Notify other devices an item has been changed
            await _notificationService.sendNotification(itemToUpdate, NotificationType.RequestedItemCompleted, null, null, true);

            return(Ok(itemToReturn));
        }
Example #2
0
        public async Task MessageReceived(string fromNumber, string toNumber, string text, List <RemoteFile> files)
        {
            _logger.LogInformation($"New Incoming SMS from {fromNumber}: {text}");

            var users = await _beachBuddyRepository.GetUsers(new UserResourceParameters()
            {
                PhoneNumber = fromNumber
            });

            var userWhoSentMessage = users.FirstOrDefault();

            if (userWhoSentMessage == null)
            {
                await _twilioService.SendSms(fromNumber, "Sorry, I don't know who you are.");

                return;
            }

            var words = text.Split(" ");
            var firstWordOfMessage = words[0].ToLower();

            var firstChar = firstWordOfMessage.ToCharArray()[0];

            if (firstChar == '+' || firstChar == '-')
            {
                await ProcessScoreUpdate(text, userWhoSentMessage, fromNumber, toNumber);

                return;
            }

            string secondWordOfMessage = null;

            if (words.Length >= 2)
            {
                secondWordOfMessage = text.Split(" ")[1].ToLower();
            }

            switch (firstWordOfMessage.ToLower())
            {
            case "done":
                _backgroundTaskQueue.QueueSunscreenReminderForUser(userWhoSentMessage.Id);
                await _twilioService.SendSms(fromNumber, "Great! I'll let you know once it's dry and then again when it's time to reapply. ⏱");

                return;

            case "refresh":
                await _notificationService.sendNotification(null, NotificationType.DashboardPulledToRefresh, null, null, true);

                return;

            case "remove":
                await RemoveItems(fromNumber, toNumber, text, firstWordOfMessage);

                // Send data notification so app will update
                await _notificationService.sendNotification(null, NotificationType.RequestedItemRemoved, null, null, true);

                return;

            case "list":
                await ShowAllItems(fromNumber);

                return;

            case "h":
                await ShowHelp(fromNumber);

                return;

            case "nukefromorbit":
                await RemoveItems(fromNumber, toNumber, text, firstWordOfMessage, true);

                // Send data notification so app will update
                await _notificationService.sendNotification(null, NotificationType.RequestedItemRemoved, null, null, true);

                return;

            case "bal":
                await GetBalance(fromNumber);

                return;

            case "add":
                if (secondWordOfMessage != null && secondWordOfMessage == "game")
                {
                    var gameName = text.Substring("add game".Length).Trim();
                    await AddGame(gameName, fromNumber);

                    return;
                }

                break;

            case "leaderboard":
                await ShowLeaderBoard(fromNumber);

                return;
            }

            var requestedItemToSave = new RequestedItem();

            // Check if there is a quantity
            if (int.TryParse(firstWordOfMessage, out var quantity))
            {
                text = text.Substring(firstWordOfMessage.Length).Trim();
            }
            else
            {
                quantity = 1;
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                await _twilioService.SendSms(fromNumber,
                                             $"Sorry, I didn't detect anything to add to the list.");

                return;
            }

            requestedItemToSave.Id                = Guid.NewGuid();
            requestedItemToSave.Count             = quantity;
            requestedItemToSave.CreatedDateTime   = DateTimeOffset.UtcNow;
            requestedItemToSave.RequestedByUserId = userWhoSentMessage.Id;
            requestedItemToSave.RequestedByUser   = userWhoSentMessage;
            requestedItemToSave.Name              = text;

            try
            {
                await _beachBuddyRepository.AddRequestedItem(requestedItemToSave);

                await _beachBuddyRepository.Save();
            }
            catch (Exception e)
            {
                _logger.LogError($"Something did not work when trying to save a requested item via Twilio: {e.Message}",
                                 e);
                await _twilioService.SendSms(fromNumber,
                                             $"Sorry, I couldn't add {text} to the list. Try again.)");

                return;
            }

            await _notificationService.sendNotification(
                await _beachBuddyRepository.GetRequestedItem(requestedItemToSave.Id),
                NotificationType.RequestedItemAdded,
                $"\"{requestedItemToSave.Name}\" added to list", $"{userWhoSentMessage.FirstName} " +
                $"added {requestedItemToSave.Count} {requestedItemToSave.Name} to the Beach List.",
                false);

            await _twilioService.SendSms(fromNumber, $"\"{text}\" was added to the list!");
        }