public async Task <JsonResult> AddAgreement([Required] AddAgreementViewModel model)
 {
     if (!ModelState.IsValid)
     {
         return(JsonModelStateErrors());
     }
     return(await JsonAsync(_agreementService.AddAgreementAsync(model)));
 }
        /// <summary>
        /// Add agreement
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual async Task <ResultModel <Guid> > AddAgreementAsync(AddAgreementViewModel model)
        {
            if (model == null)
            {
                return(new InvalidParametersResultModel <Guid>());
            }

            var contactRequest = await _contactService.GetContactByIdAsync(model.ContactId);

            if (contactRequest.IsSuccess)
            {
                if (contactRequest.Result.OrganizationId != model.OrganizationId)
                {
                    return new ResultModel <Guid> {
                               IsSuccess = false, Errors = new List <IErrorModel> {
                                   new ErrorModel {
                                       Message = "The organization does not have this contact "
                                   }
                               }
                    }
                }
                ;
            }

            var agreement = _mapper.Map <Agreement>(model);

            var existAgreement = _context.Agreements.Where(x =>
                                                           string.Equals(x.Name, agreement.Name, StringComparison.CurrentCultureIgnoreCase));

            if (existAgreement.Any())
            {
                var version = await existAgreement.CountAsync();

                agreement.Name += " (" + version + ")";
            }


            _context.Agreements.Add(agreement);
            var result = await _context.PushAsync();

            if (result.IsSuccess)
            {
                await _notify.SendNotificationAsync(new List <Guid> {
                    agreement.UserId
                }, new Notification
                {
                    Content            = $"Add agreement {agreement?.Name}",
                    Subject            = "Info",
                    NotificationTypeId = NotificationType.Info
                });
            }

            return(result.Map <Guid>(agreement.Id));
        }