Esempio n. 1
0
        //[OutputCache(Duration = 60)]
        public async Task <IEnumerable <SelectListItem> > GetAllCaseSubTypesAsync(string twoLetterCultureLanguage)
        {
            using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
            {
                var entityName = "hr_casesubtypes";
                var orderby    = "hr_sortorder";
                var response   = await client.GetAsync($"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}?$orderby={orderby}");

                if (response.IsSuccessStatusCode)
                {
                    var results = await response.Content.ReadAsStringAsync();

                    if (results != null)
                    {
                        var subTypeList = JsonConvert.DeserializeObject <List <CaseSubType> >(JObject.Parse(results)["value"].ToString(), new IsoDateTimeConverter {
                            DateTimeFormat = "yyyy-MM-dd"
                        });
                        if (twoLetterCultureLanguage == "en")
                        {
                            return(subTypeList.Select(n => new SelectListItem {
                                Value = n.hr_casesubtypeid, Text = n.hr_nameen
                            }));
                        }
                        else
                        {
                            return(subTypeList.Select(n => new SelectListItem {
                                Value = n.hr_casesubtypeid, Text = n.hr_namefr
                            }));
                        }
                    }
                }
            }
            return(null);
        }
Esempio n. 2
0
        public async Task <bool> DeleteHRCaseAsync(string caseId)
        {
            try
            {
                using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
                {
                    var entityName = "hr_hrcases";

                    HttpRequestMessage deleteRequest = new HttpRequestMessage(HttpMethod.Delete, $"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}({caseId})");

                    var response = await client.SendAsync(deleteRequest, HttpCompletionOption.ResponseHeadersRead);

                    if (response.IsSuccessStatusCode)
                    {
                        return(true);
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                //var errorMsg = Message.HttpRequestNotSuccessfull(ex.Message, httpMethod, messageUri, body);
                //this.log.Error(Message.Error($"{nameof(RestApiClient)}.{nameof(this.SendMessageAsync)}", errorMsg));

                throw ex;
            }
            return(false);
        }
Esempio n. 3
0
        public async Task <IEnumerable <SelectListItem> > GetCaseSubTypesAsync(string caseTypeId, string twoLetterCultureLanguage)
        {
            using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
            {
                var entityName = "hr_casetypes";
                var orderby    = "hr_sortorder";
                var query      = $"$top=50&$expand=hr_CaseType_hr_CaseSubType_hr_CaseSubType($select=hr_name,hr_nameen,hr_namefr,hr_casesubtypeid,hr_sortorder)&$filter=hr_casetypeid%20eq%20{caseTypeId}";
                var response   = await client.GetAsync($"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}?{query}&$orderby={orderby}");

                //var response = await client.GetAsync($"https://hrcms-dev-tcd365.crm3.dynamics.com/api/data/v9.1/hr_casetypes?$top=50&$expand=hr_CaseType_hr_CaseSubType_hr_CaseSubType($select=hr_name,hr_casesubtypeid)&$filter=hr_casetypeid%20eq%20{caseTypeId}");
                if (response.IsSuccessStatusCode)
                {
                    var results = await response.Content.ReadAsStringAsync();

                    if (results != null)
                    {
                        List <CaseSubType> caseSubTypeList = JsonConvert.DeserializeObject <List <CaseSubType> >(JObject.Parse(results)["value"][0]["hr_CaseType_hr_CaseSubType_hr_CaseSubType"].ToString()).OrderBy(m => m.hr_sortorder).ToList();
                        if (twoLetterCultureLanguage == "en")
                        {
                            return(caseSubTypeList.Select(n => new SelectListItem {
                                Value = n.hr_casesubtypeid, Text = n.hr_nameen
                            }));
                        }
                        else
                        {
                            return(caseSubTypeList.Select(n => new SelectListItem {
                                Value = n.hr_casesubtypeid, Text = n.hr_namefr
                            }));
                        }
                    }
                }
            }
            return(null);
        }
Esempio n. 4
0
        public async Task <List <QuestionModel> > GetAllUnAnsweredQuestionsAsync(string pri)
        {
            using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
            {
                var entityName = "hr_questionandanswerses";
                var orderby    = $"$orderby=createdon%20desc";
                var select     = $"$select=hr_questionandanswersid,hr_question,hr_askedon&$expand=hr_HRCase($select=hr_hrcaseid,hr_name)";
                var filter     = $"$filter=hr_answer%20eq%20null%20and%20hr_HRCase/hr_pri%20eq%20%27{pri}%27";
                var response   = await client.GetAsync($"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}?{select}&{filter}&{orderby}");

                if (response.IsSuccessStatusCode)
                {
                    var results = await response.Content.ReadAsStringAsync();

                    if (results != null)
                    {
                        var questions = JsonConvert.DeserializeObject <List <Question> >(JObject.Parse(results)["value"].ToString(), new IsoDateTimeConverter {
                            DateTimeFormat = "yyyy-MM-dd"
                        });
                        var questionModels = _mapper.Map <List <QuestionModel> >(questions);
                        return(questionModels);
                    }
                }
            }
            return(null);
        }
Esempio n. 5
0
        public async Task <List <HRCaseModel> > GetAllCasesAsync(string pri, string statuses, string twoLetterCultureLanguage)
        {
            using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
            {
                var entityName   = "hr_hrcases";
                var statusList   = statuses.Split("|");
                var orderby      = $"$orderby=createdon%20desc";
                var statusFilter = "[%27" + string.Join("%27,%27", statusList) + "%27]";
                var select       = $"$select=hr_lastname,hr_name,hr_casestatus,hr_firstname,hr_hrcaseid,createdon,hr_datereceived&$expand=hr_CaseType($select=hr_name,hr_nameen,hr_namefr),hr_CaseSubType($select=hr_name,hr_nameen,hr_namefr)";
                var filter       = $"$filter=hr_pri%20eq%20%27{pri}%27%20and%20Microsoft.Dynamics.CRM.In(PropertyName=%27hr_casestatus%27,PropertyValues={statusFilter})";
                var response     = await client.GetAsync($"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}?{select}&{filter}&{orderby}");

                if (response.IsSuccessStatusCode)
                {
                    var results = await response.Content.ReadAsStringAsync();

                    if (results != null)
                    {
                        var hrCases = JsonConvert.DeserializeObject <List <HRCase> >(JObject.Parse(results)["value"].ToString(), new IsoDateTimeConverter {
                            DateTimeFormat = "yyyy-MM-dd"
                        });
                        if (twoLetterCultureLanguage == "en")
                        {
                            hrCases.Select(c => { if (c.hr_CaseType != null)
                                                  {
                                                      c.hr_CaseType.hr_name = c.hr_CaseType.hr_nameen;
                                                  }
                                                  return(c); }).ToList();
                            hrCases.Select(c => { if (c.hr_CaseSubType != null)
                                                  {
                                                      c.hr_CaseSubType.hr_name = c.hr_CaseSubType.hr_nameen;
                                                  }
                                                  return(c); }).ToList();
                        }
                        else
                        {
                            hrCases.Select(c => { if (c.hr_CaseType != null)
                                                  {
                                                      c.hr_CaseType.hr_name = c.hr_CaseType.hr_namefr;
                                                  }
                                                  return(c); }).ToList();
                            hrCases.Select(c => { if (c.hr_CaseSubType != null)
                                                  {
                                                      c.hr_CaseSubType.hr_name = c.hr_CaseSubType.hr_namefr;
                                                  }
                                                  return(c); }).ToList();
                        }
                        var hrCaseModels = _mapper.Map <List <HRCaseModel> >(hrCases);
                        return(hrCaseModels);
                    }
                }
                else
                {
                    //ModelState.AddModelError(string.Empty, "Unable to authenticate. Please check your user name");
                }
            }
            return(null);
        }
Esempio n. 6
0
        public async Task <HRCaseModel> GetCaseAsync(string caseId, string twoLetterCultureLanguage)
        {
            using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
            {
                var entityName = "hr_hrcases";
                var appUserid  = _appSettings.AppUserId;
                var select     = $"$expand=hr_CaseType($select=hr_name,hr_nameen,hr_namefr),hr_CaseSubType($select=hr_name,hr_nameen,hr_namefr)," +
                                 $"hr_HRCase_hr_HRCase_hr_QuestionandAnswers($select=hr_questionandanswersid,hr_question,hr_answer,hr_read,hr_askedon,hr_answeredon,hr_questionsequencenumber)," +
                                 $"hr_hrcase_Annotations($select=_objectid_value,filename,subject,notetext,createdon,mimetype;$filter=isdocument%20eq%20true%20and%20_createdby_value%20eq%20%27{appUserid}%27)";
                var response = await client.GetAsync($"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}({caseId})?{select}");

                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();

                    if (result != null)
                    {
                        var hrCase = JsonConvert.DeserializeObject <HRCase>(result, new IsoDateTimeConverter {
                            DateTimeFormat = "yyyy-MM-dd"
                        });
                        if (twoLetterCultureLanguage == "en")
                        {
                            if (hrCase.hr_CaseType != null)
                            {
                                hrCase.hr_CaseType.hr_name = hrCase.hr_CaseType.hr_nameen;
                            }
                            if (hrCase.hr_CaseSubType != null)
                            {
                                hrCase.hr_CaseSubType.hr_name = hrCase.hr_CaseSubType.hr_nameen;
                            }
                        }
                        else
                        {
                            if (hrCase.hr_CaseType != null)
                            {
                                hrCase.hr_CaseType.hr_name = hrCase.hr_CaseType.hr_namefr;
                            }
                            if (hrCase.hr_CaseSubType != null)
                            {
                                hrCase.hr_CaseSubType.hr_name = hrCase.hr_CaseSubType.hr_namefr;
                            }
                        }
                        var hrCaseModel = _mapper.Map <HRCaseModel>(hrCase);

                        hrCaseModel.UnAnsweredQuestions = hrCaseModel.Questions.Where(q => string.IsNullOrEmpty(q.DateAnswered)).OrderBy(q => q.DateAsked).ToList();
                        hrCaseModel.AnsweredQuestions   = hrCaseModel.Questions.Where(q => !string.IsNullOrEmpty(q.DateAnswered)).OrderBy(q => q.DateAsked).ToList();

                        hrCaseModel.Attachments.Sort((x, y) => string.Compare(y.DateCreated, x.DateCreated));
                        return(hrCaseModel);
                    }
                }
            }
            return(null);
        }
Esempio n. 7
0
        public async Task <string> CreateHRCaseAsync(HRCase hrCase)
        {
            try
            {
                using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
                {
                    var     entityName = "hr_hrcases";
                    dynamic jCase      = new JObject();
                    jCase.hr_lastname    = hrCase.hr_lastname;
                    jCase.hr_firstname   = hrCase.hr_firstname;
                    jCase.hr_pri         = hrCase.hr_pri;
                    jCase.hr_email       = hrCase.hr_email;
                    jCase.hr_casestatus  = hrCase.hr_casestatus;
                    jCase.hr_description = hrCase.hr_description;
                    //jCase.hr_datereceived = DateTime.Now;
                    if (hrCase.hr_CaseType.hr_casetypeid != null)
                    {
                        jCase["*****@*****.**"] = $"/hr_casetypes({hrCase.hr_CaseType.hr_casetypeid})";
                    }
                    if (hrCase.hr_CaseSubType.hr_casesubtypeid != null)
                    {
                        jCase["*****@*****.**"] = $"/hr_casesubtypes({hrCase.hr_CaseSubType.hr_casesubtypeid})";
                    }

                    var caseContent = new StringContent(jCase.ToString(), Encoding.UTF8, "application/json");

                    HttpRequestMessage createrequest1 = new HttpRequestMessage(HttpMethod.Post, $"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}");
                    createrequest1.Content = caseContent;

                    var response = await client.SendAsync(createrequest1, HttpCompletionOption.ResponseHeadersRead);

                    if (response.IsSuccessStatusCode)
                    {
                        var result = await response.Content.ReadAsStringAsync();

                        if (result != null)
                        {
                            var entityId = response.Headers.GetValues("OData-EntityId").FirstOrDefault();
                            entityId = entityId.Substring(entityId.IndexOf("(") + 1, 36);
                            return(entityId);
                        }
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                //var errorMsg = Message.HttpRequestNotSuccessfull(ex.Message, httpMethod, messageUri, body);
                //this.log.Error(Message.Error($"{nameof(RestApiClient)}.{nameof(this.SendMessageAsync)}", errorMsg));

                //throw ex;
            }
            return(null);
        }
Esempio n. 8
0
        public async Task <string> UploadAttatchmentAsync(Annotation note)
        {
            try
            {
                using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
                {
                    //client.DefaultRequestHeaders.Add("Prefer", "return=representation");
                    var     entityName = "annotations";
                    dynamic jNote      = new JObject();
                    jNote.subject      = note.subject;
                    jNote.notetext     = note.notetext;
                    jNote.filename     = note.filename;
                    jNote.isdocument   = true;
                    jNote.documentbody = note.documentbody;
                    jNote["*****@*****.**"] = $"/hr_hrcases({note._objectid_value})";

                    var caseContent = new StringContent(jNote.ToString(), Encoding.UTF8, "application/json");

                    HttpRequestMessage uploadRequest = new HttpRequestMessage(HttpMethod.Post, $"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}");
                    uploadRequest.Content = caseContent;

                    var response = await client.SendAsync(uploadRequest, HttpCompletionOption.ResponseHeadersRead);

                    if (response.IsSuccessStatusCode)
                    {
                        var result = await response.Content.ReadAsStringAsync();

                        if (result != null)
                        {
                            var entityId = response.Headers.GetValues("OData-EntityId").FirstOrDefault();
                            entityId = entityId.Substring(entityId.IndexOf("(") + 1, 36);
                            return(entityId);
                        }
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                //var errorMsg = Message.HttpRequestNotSuccessfull(ex.Message, httpMethod, messageUri, body);
                //this.log.Error(Message.Error($"{nameof(RestApiClient)}.{nameof(this.SendMessageAsync)}", errorMsg));

                throw ex;
            }
            return(null);
        }
Esempio n. 9
0
        //public async Task<Question> GetQuestionAsync(string questionId)
        //{
        //    using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
        //    {
        //        var entityName = "hr_questionandanswerses";
        //        var response = await client.GetAsync($"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}({questionId})");

        //        if (response.IsSuccessStatusCode)
        //        {
        //            var result = await response.Content.ReadAsStringAsync();
        //            if (result != null)
        //            {
        //                var question = JsonConvert.DeserializeObject<Question>(result, new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" });
        //                return question;
        //            }
        //        }
        //    }
        //    return null;
        //}


        public async Task <string> UpdateAnswerAsync(Question ques)
        {
            try
            {
                using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
                {
                    //client.DefaultRequestHeaders.Add("Prefer", "return=representation");
                    var     entityName = "hr_questionandanswerses";
                    dynamic jQuestion  = new JObject();
                    jQuestion.hr_answer     = ques.hr_answer;
                    jQuestion.hr_answeredon = DateTime.UtcNow.ToString();

                    var caseContent = new StringContent(jQuestion.ToString(), Encoding.UTF8, "application/json");

                    HttpRequestMessage updateRequest = new HttpRequestMessage(HttpMethod.Patch, $"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}({ques.hr_questionandanswersid})");
                    updateRequest.Content = caseContent;

                    var response = await client.SendAsync(updateRequest, HttpCompletionOption.ResponseHeadersRead);

                    if (response.IsSuccessStatusCode)
                    {
                        var result = await response.Content.ReadAsStringAsync();

                        if (result != null)
                        {
                            var entityId = response.Headers.GetValues("OData-EntityId").FirstOrDefault();
                            entityId = entityId.Substring(entityId.IndexOf("(") + 1, 36);
                            return(entityId);
                        }
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                //var errorMsg = Message.HttpRequestNotSuccessfull(ex.Message, httpMethod, messageUri, body);
                //this.log.Error(Message.Error($"{nameof(RestApiClient)}.{nameof(this.SendMessageAsync)}", errorMsg));

                throw ex;
            }
            return(null);
        }
Esempio n. 10
0
        public async Task <IEnumerable <SelectListItem> > GetAllCaseStatusesAsync(string twoLetterCultureLanguage)
        {
            using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
            {
                var optionSetName = "GlobalOptionSetDefinitions";
                var optionSetId   = "Name='hr_casestatus'";
                var response      = await client.GetAsync($"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{optionSetName}({optionSetId})");

                if (response.IsSuccessStatusCode)
                {
                    var results = await response.Content.ReadAsStringAsync();

                    if (results != null)
                    {
                        var subTypeList  = new List <CaseStatus>();
                        var caseStatuses = JObject.Parse(results)["Options"];
                        foreach (var sObject in caseStatuses)
                        {
                            if (twoLetterCultureLanguage == "en")
                            {
                                subTypeList.Add(new CaseStatus {
                                    Value = sObject["Value"].ToString(), Label = sObject["Label"]["LocalizedLabels"][0]["Label"].ToString()
                                });
                            }
                            else
                            {
                                subTypeList.Add(new CaseStatus {
                                    Value = sObject["Value"].ToString(), Label = sObject["Label"]["LocalizedLabels"][1]["Label"].ToString()
                                });
                            }
                        }
                        //var subTypeList = JsonConvert.DeserializeObject<List<CaseStatus>>(JObject.Parse(results)["Options"].ToString());
                        return(subTypeList.Select(n => new SelectListItem {
                            Value = n.Value, Text = n.Label
                        }).OrderBy(m => m.Text));
                    }
                }
            }
            return(null);
        }
Esempio n. 11
0
        public async Task <AnnotationModel> GetAnnotationAsync(string annotationId)
        {
            using (var client = DynamicsApiHelper.GetHttpClient(_appSettings))
            {
                var entityName = "annotations";
                var response   = await client.GetAsync($"{_appSettings.ResourceUrl}/api/data/v{_appSettings.ApiVersion}/{entityName}({annotationId})");

                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();

                    if (result != null)
                    {
                        var note = JsonConvert.DeserializeObject <Annotation>(result, new IsoDateTimeConverter {
                            DateTimeFormat = "yyyy-MM-dd"
                        });
                        var attachment = _mapper.Map <AnnotationModel>(note);
                        return(attachment);
                    }
                }
            }
            return(null);
        }