Skip to content

Commit e9dbbb3

Browse files
authored
Fixed with monthly report template version issue (#292)
* Fixed with monthly report template version issue * remove savedqueryid from HRQuestionResponseXML
1 parent 85be78c commit e9dbbb3

File tree

2 files changed

+73
-27
lines changed

2 files changed

+73
-27
lines changed

OFM.Infrastructure.WebAPI/Models/DataverseModels.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,10 @@ public class ProviderStaff
459459

460460
public class QuestionResponse : ofm_question_response
461461
{
462-
[property: JsonPropertyName("_ofm_question_value")]
463-
public string ofm_questionid { get; set; }
464-
[property: JsonPropertyName("_ofm_header_value")]
465-
public string ofm_headerid { get; set; }
462+
[property: JsonPropertyName("question.ofm_question_id")]
463+
public string ofm_question_qid { get; set; }
464+
[property: JsonPropertyName("header.ofm_question_id")]
465+
public string ofm_header_qid { get; set; }
466466
}
467467

468468

OFM.Infrastructure.WebAPI/Services/Processes/FundingReports/P615CreateMonthlyReportProvider.cs

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class P615CreateMonthlyReportProvider(IOptionsSnapshot<D365AuthSettings>
2626
private string _facId;
2727
private DateTime _previousMonth;
2828
private string _hrQuestionIdsXML;
29+
private Guid _reportTemplateId;
2930

3031
public Int16 ProcessId => Setup.Process.FundingReports.CreateMonthlyReportId;
3132
public string ProcessName => Setup.Process.FundingReports.CreateMonthlyReportName;
@@ -61,38 +62,75 @@ public string RequestUri
6162
}
6263
}
6364

65+
public string HRQuestionTemplateUri
66+
{
67+
get
68+
{
69+
// Note: Get the active funding record
70+
//for reference only
71+
var fetchXml = $"""
72+
<fetch distinct="true">
73+
<entity name="ofm_question">
74+
<attribute name="ofm_question_id" />
75+
<attribute name="ofm_header" />
76+
<attribute name="ofm_questionid" />
77+
<filter>
78+
<condition attribute="ofm_question_id" operator="in">
79+
{_hrQuestionIdsXML}
80+
</condition>
81+
</filter>
82+
<link-entity name="ofm_section" from="ofm_sectionid" to="ofm_section">
83+
<link-entity name="ofm_survey" from="ofm_surveyid" to="ofm_survey">
84+
<filter>
85+
<condition attribute="ofm_surveyid" operator="eq" value="{_reportTemplateId}" />
86+
</filter>
87+
</link-entity>
88+
</link-entity>
89+
</entity>
90+
</fetch>
91+
""";
92+
93+
var hrQuestionTemplateUri = $"""
94+
ofm_questions?fetchXml={WebUtility.UrlEncode(fetchXml)}
95+
""".CleanCRLF();
96+
return hrQuestionTemplateUri;
97+
}
98+
}
99+
64100
public string HRQuestionResponseUri
65101
{
66102
get
67103
{
68104
// Note: Get the active funding record
69105
//for reference only
70106
var fetchXml = $"""
71-
<fetch version="1.0" mapping="logical" distinct="true" no-lock="true">
107+
<fetch version="1.0" mapping="logical" no-lock="true" distinct="true">
72108
<entity name="ofm_question_response">
73109
<attribute name="ofm_response_text" />
74-
<attribute name="ofm_row_id" />
75-
<attribute name="ofm_header" />
76-
<attribute name="ofm_question" />
77-
<link-entity name="ofm_survey_response" from="ofm_survey_responseid" to="ofm_survey_response">
78-
<attribute name="ofm_response_id" />
79-
<attribute name="ofm_facility" />
80-
<attribute name="ofm_start_date" />
81-
<attribute name="ofm_submitted_on" />
82-
<filter>
110+
<link-entity name="ofm_survey_response" link-type="inner" from="ofm_survey_responseid" to="ofm_survey_response">
111+
<filter type="and">
83112
<condition attribute="ofm_facility" operator="eq" value="{_facId}" />
84113
<condition attribute="ofm_start_date" operator="on" value="{_previousMonth}" />
85-
<condition attribute="ofm_submitted_on" operator="not-null" value="" />
114+
<condition attribute="ofm_submitted_on" operator="not-null" />
86115
</filter>
87116
</link-entity>
88-
<link-entity name="ofm_question" from="ofm_questionid" to="ofm_question">
89-
<attribute name="ofm_questionid" />
117+
<link-entity name="ofm_question" from="ofm_questionid" to="ofm_question" link-type="outer" alias="question">
118+
<attribute name="ofm_question_id" />
90119
<filter>
91120
<condition attribute="ofm_question_id" operator="in">
92-
{_hrQuestionIdsXML}
121+
{_hrQuestionIdsXML}
93122
</condition>
94123
</filter>
124+
<link-entity name="ofm_section" from="ofm_sectionid" to="ofm_section">
125+
<filter>
126+
<condition attribute="ofm_section_title" operator="begins-with" value="Human" />
127+
</filter>
128+
</link-entity>
95129
</link-entity>
130+
<link-entity name="ofm_question" from="ofm_questionid" to="ofm_header" link-type="outer" alias="header" visible="false">
131+
<attribute name="ofm_question_id" />
132+
</link-entity>
133+
<attribute name="ofm_row_id" />
96134
</entity>
97135
</fetch>
98136
""";
@@ -104,7 +142,6 @@ public string HRQuestionResponseUri
104142
}
105143
}
106144

107-
108145
public async Task<ProcessData> GetDataAsync()
109146
{
110147
_logger.LogDebug(CustomLogEvent.Process, "Calling GetData of {nameof}", nameof(P615CreateMonthlyReportProvider));
@@ -153,7 +190,7 @@ public async Task<ProcessData> GetReportDataAsync(string uri)
153190
if (!response.IsSuccessStatusCode)
154191
{
155192
var responseBody = await response.Content.ReadAsStringAsync();
156-
_logger.LogError(CustomLogEvent.Process, "Failed to query fiscal year with the server error {responseBody}", responseBody.CleanLog());
193+
_logger.LogError(CustomLogEvent.Process, "Failed to query result with the server error {responseBody}", responseBody.CleanLog());
157194

158195
return await Task.FromResult(new ProcessData(string.Empty));
159196
}
@@ -165,7 +202,7 @@ public async Task<ProcessData> GetReportDataAsync(string uri)
165202
{
166203
if (currentValue?.AsArray().Count == 0)
167204
{
168-
_logger.LogInformation(CustomLogEvent.Process, "No fiscal year found with query {requestUri}", uri.CleanLog());
205+
_logger.LogInformation(CustomLogEvent.Process, "No result found with query {requestUri}", uri.CleanLog());
169206
}
170207
d365Result = currentValue!;
171208
}
@@ -296,7 +333,7 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
296333
_logger.LogInformation(CustomLogEvent.Process, "Cannot find report template.");
297334
return ProcessResult.Completed(ProcessId).SimpleProcessResult;
298335
}
299-
var reportTempateId = reportTemplate.Id;
336+
_reportTemplateId = reportTemplate.Id;
300337

301338

302339
//Convert the report Month
@@ -323,7 +360,7 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
323360
var reportData = new JsonObject
324361
{
325362
{"ofm_facility@odata.bind", $"/accounts({facility})"},
326-
{"ofm_survey@odata.bind", $"/ofm_surveies({reportTempateId})" },
363+
{"ofm_survey@odata.bind", $"/ofm_surveies({_reportTemplateId})" },
327364
{"ofm_fiscal_year@odata.bind", $"/ofm_fiscal_years({fiscalYear})" },
328365
{"ofm_report_month", reportMonth},
329366
{"ofm_duedate", duedateInUTC },
@@ -355,6 +392,11 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
355392
var hrQuestionIds = _processParams.FundingReport.HRQuestions.Split(",");
356393
_hrQuestionIdsXML = "<value>" + String.Join("</value>\r\n <value>", hrQuestionIds) + "</value>";
357394

395+
//Get question template for current report template
396+
397+
var hrQuestionTemplateData = await GetReportDataAsync(HRQuestionTemplateUri);
398+
var serializedHRQuestionTemplateData = System.Text.Json.JsonSerializer.Deserialize<List<Question>>(hrQuestionTemplateData.Data, Setup.s_writeOptionsForLogs);
399+
358400
List<HttpRequestMessage> questionResponseRequests = [];
359401

360402
foreach (var report in createdReports)
@@ -367,12 +409,16 @@ public async Task<JsonObject> RunProcessAsync(ID365AppUserService appUserService
367409

368410
foreach(var questionResponse in serializedPreviousHRQuestionResponseData)
369411
{
370-
var newQuestionResponse = new JsonObject
412+
413+
var questionTemplateId = $"/ofm_questions({serializedHRQuestionTemplateData?.Where(q => q.ofm_question_id == questionResponse.ofm_question_qid).FirstOrDefault().ofm_questionid})";
414+
var headerTemplateId = String.IsNullOrEmpty(questionResponse.ofm_header_qid) ? null:$"/ofm_questions({serializedHRQuestionTemplateData?.Where(q => q.ofm_question_id == questionResponse.ofm_header_qid).FirstOrDefault().ofm_questionid})";
415+
416+
var newQuestionResponse = new JsonObject
371417
{
372418
{"ofm_survey_response@odata.bind", $"/ofm_survey_responses({uid})"},
373-
{"ofm_question@odata.bind", $"/ofm_questions({questionResponse.ofm_questionid})" },
374-
{"ofm_header@odata.bind", String.IsNullOrEmpty(questionResponse.ofm_headerid)? null:$"/ofm_questions({questionResponse.ofm_headerid})" },
375-
{"ofm_row_id", questionResponse.ofm_row_id},
419+
{"ofm_question@odata.bind", questionTemplateId },
420+
{"ofm_header@odata.bind", headerTemplateId },
421+
{"ofm_row_id", questionResponse.ofm_row_id },
376422
{"ofm_response_text", questionResponse.ofm_response_text}
377423
};
378424

0 commit comments

Comments
 (0)