Skip to content

Commit

Permalink
Merge pull request #3994 from cisagov/bug/CSET-2805
Browse files Browse the repository at this point in the history
Fixed custom module assessment import: Bug/cset 2805
  • Loading branch information
jekuipers authored Jul 23, 2024
2 parents 83ef244 + 390ecdf commit 3fa091b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ private Stream ArchiveStream(int assessmentId, string password, string passwordH
var set = _context.SETS
.Include(s => s.Set_Category)
.Where(s => s.Set_Name == standard.Set_Name).FirstOrDefault();
if (set == null || !set.Is_Custom)
if (set == null)
{
continue;
}
Expand Down Expand Up @@ -472,7 +472,9 @@ join nqs in _context.NEW_QUESTION_SETS on nq.Question_Id equals nqs.Question_Id
return t;
})).ToList();

model.CustomStandards.Add(setname);
model.CustomStandards.Add(extStandard.shortName);





Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Xml;
using Ionic.Zip;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;

namespace CSETWebCore.Business.AssessmentIO.Import
{
Expand Down Expand Up @@ -118,16 +119,22 @@ public async Task ProcessCSETAssessmentImport(byte[] zipFileFromDatabase, int? c

foreach (var standard in model.CustomStandards)
{
var sets = context.SETS.Where(s => s.Set_Name.Contains(standard)).ToList();

var sets = context.SETS.Where(s => s.Short_Name.Contains(standard)).ToList();
if (sets.Count == 0)
{
throw new Exception("Custom module not found");
}
SETS set = null;
//StreamReader setReader = new StreamReader(zip.GetEntry(standard + ".json").Open());
ms.Position = 0;
StreamReader setReader = new StreamReader(ms);
var setJson = setReader.ReadToEnd();
var setModel = JsonConvert.DeserializeObject<ExternalStandard>(setJson);
var originalSetName = setModel.shortName;
var originalSetName = standard;
foreach (var testSet in sets)
{
setModel.shortName = testSet.Short_Name;
originalSetName = testSet.Short_Name;
var testSetJson = JsonConvert.SerializeObject(testSet.ToExternalStandard(_context), Newtonsoft.Json.Formatting.Indented);
if (testSetJson == setJson)
{
Expand All @@ -136,18 +143,18 @@ public async Task ProcessCSETAssessmentImport(byte[] zipFileFromDatabase, int? c
}
else
{
setModel.shortName = originalSetName;
setModel.name = originalSetName;
}
}

if (set == null)
{
int incr = 1;
while (sets.Any(s => s.Short_Name == setModel.shortName))
{
setModel.shortName = originalSetName + " " + incr;
incr++;
}
//int incr = 1;
//while (sets.Any(s => s.Short_Name == originalSetName))
//{
// setModel.name = originalSetName + " " + incr;
// incr++;
//}
var setResult = await setModel.ToSet(_context);
if (setResult.IsSuccess)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static async Task<ConverterResult<SETS>> ToSet(this ExternalStandard exte
var result = new ConverterResult<SETS>(logger);
SETS_CATEGORY category;
int? categoryOrder = 0;
var setname = Regex.Replace(externalStandard.shortName, @"\W", "_");
var setname = Regex.Replace(externalStandard.name, @"\W", "_");

try
{
var documentImporter = new DocumentImporter(_context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ namespace CSETWebCore.Model.AssessmentIO

public class ExternalStandard
{

public List<string> CustomStandards { get; set; }

[Required]
[DataMember]
public string name { get; set; }
Expand All @@ -36,5 +39,8 @@ public class ExternalStandard

[DataMember]
public string summary { get; set; }



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public async Task<IActionResult> ImportAssessment([FromHeader] string pwd)
returnMessage = (hint == null) ? "Invalid Password" : "Invalid Password - " + hint.FileName;
return StatusCode(406, returnMessage);
}
else if (e.Message == "Custom module not found")
{
returnMessage = "Custom module not found";
return StatusCode(404, returnMessage);
}
else
{
return BadRequest(e);
Expand Down
7 changes: 7 additions & 0 deletions CSETWebNg/src/app/services/import-assessment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ export class ImportAssessmentService {
// pass the percentage into the progress-stream
progress.next(percentDone);
} else if (event instanceof HttpResponseBase) {
if (event.status == 404) {
let errObj = {
message: "File import failed. Custom module not found",
};
progress.error(errObj);
}
if (event.status == 423) {
let errObj = {
message: "File requires a password",
Expand All @@ -116,6 +122,7 @@ export class ImportAssessmentService {
};
progress.error(errObj);
}

// Close the progress-stream if we get an answer form the API
// The upload is complete
else progress.complete();
Expand Down

0 comments on commit 3fa091b

Please sign in to comment.