コード例 #1
0
        public async Task <JsonResult> ResponseToCollaborationRequest([FromHeader] int accountId, [FromRoute] int collaborationId, [FromRoute] int isAccepted)
        {
            var collaborationRequest = await _collaborationService.GetCollaborationRequest(collaborationId, accountId);

            if (collaborationRequest == null)
            {
                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Failed, Message = "An issue happened while getting data."
                }));
            }

            if (isAccepted == 1)
            {
                collaborationRequest.IsAccepted = true;
                collaborationRequest.AcceptedOn = DateTime.UtcNow;
            }
            else
            {
                collaborationRequest.IsAccepted = false;
                collaborationRequest.RejectedOn = DateTime.UtcNow;
            }

            await _collaborationService.StartTransaction();

            var updateResult = await _collaborationService.UpdateCollaboration(collaborationRequest);

            if (!updateResult.HasValue || !updateResult.Value)
            {
                await _collaborationService.RevertTransaction();

                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Failed, Message = "An issue happened while updating data."
                }));
            }

            var requestedCollaborator = await _userService.GetUserById(collaborationRequest.CollaboratorId);

            if (requestedCollaborator == null)
            {
                await _collaborationService.RevertTransaction();

                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Partial, Message = "An issue happened while sending notification."
                }));
            }

            var isNotified = await SendNotification(
                requestedCollaborator,
                collaborationRequest.UserId,
                new UserNotification {
                Message = $"Your collaboration request with { TokenNotifierName } has been { (isAccepted == 1 ? "accepted. You guys can start sharing tasks now." : "rejected.") }.",
                Title   = "Collaboration Request Received"
            },
                _userService
                );

            if (!isNotified)
            {
                await _collaborationService.RevertTransaction();

                return(new JsonResult(new JsonResponse {
                    Result = SharedEnums.RequestResults.Failed, Message = "An issue happened while sending notification."
                }));
            }

            await _collaborationService.CommitTransaction();

            return(new JsonResult(new JsonResponse {
                Result = SharedEnums.RequestResults.Success
            }));
        }