Skip to content

Commit

Permalink
fix: ensure suggestion status is copied during import to avoid duplic…
Browse files Browse the repository at this point in the history
…ate suggestions
  • Loading branch information
Tethik committed Nov 1, 2023
1 parent fa0d30e commit 380e616
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
19 changes: 9 additions & 10 deletions core/src/data/suggestions/SuggestionDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ function convertToSuggestionThreat(row: any) {
return threat;
}

const log = log4js.getLogger("SuggestionDataService");

export class SuggestionDataService extends EventEmitter {
constructor(private pool: Pool, private dal: DataAccessLayer) {
super();
this.log = log4js.getLogger("SuggestionDataService");
}

log: any;

/**
* Copy suggestions from one model to anothger
* @param fromModelId
Expand Down Expand Up @@ -103,7 +102,7 @@ export class SuggestionDataService extends EventEmitter {
* @param suggestions
*/
async bulkInsert(modelId: string, suggestions: EngineSuggestedResult) {
this.log.debug(
log.debug(
`Got suggestions from engine: ${JSON.stringify(suggestions, null, 2)}`
);

Expand Down Expand Up @@ -152,7 +151,7 @@ export class SuggestionDataService extends EventEmitter {
client.query(threatQuery, [
threat.id.val,
modelId,
SuggestionStatus.New,
threat.status || SuggestionStatus.New,
threat.componentId,
threat.title,
threat.description,
Expand All @@ -168,7 +167,7 @@ export class SuggestionDataService extends EventEmitter {
client.query(controlQuery, [
control.id.val,
modelId,
SuggestionStatus.New,
control.status || SuggestionStatus.New,
control.componentId,
control.title,
control.description,
Expand All @@ -181,15 +180,15 @@ export class SuggestionDataService extends EventEmitter {
const queries = bulkThreats.concat(bulkControls);
await Promise.all(queries);
await client.query("COMMIT");
this.log.debug(
log.debug(
`inserted ${bulkThreats.length} suggested threats, ${bulkControls.length} suggested controls.`
);
this.emit("updated-for", {
modelId,
});
} catch (e) {
await client.query("ROLLBACK");
this.log.error("Failed to insert suggestions", e);
log.error("Failed to insert suggestions", e);
} finally {
client.release();
}
Expand Down Expand Up @@ -324,7 +323,7 @@ export class SuggestionDataService extends EventEmitter {
const suggestion = await this.getById(modelId, suggestionId);

if (suggestion === null) {
this.log.debug(`suggestion ${suggestionId.val} not found`);
log.debug(`suggestion ${suggestionId.val} not found`);
return false;
}

Expand All @@ -335,7 +334,7 @@ export class SuggestionDataService extends EventEmitter {
SuggestionStatus.Accepted
))
) {
this.log.debug(
log.debug(
`was not able to set suggestion ${suggestionId.val} to accepted`
);
return false;
Expand Down
3 changes: 3 additions & 0 deletions core/src/suggestions/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Model from "../data/models/Model.js";
import { SuggestionStatus } from "../data/suggestions/Suggestion.js";
import { InvalidInputError } from "../util/errors.js";

export interface Suggestion {
Expand Down Expand Up @@ -83,11 +84,13 @@ export interface SuggestionSource {
export interface EngineSuggestedControl extends SourceSuggestedControl {
id: SuggestionID;
source: string;
status?: SuggestionStatus;
}

export interface EngineSuggestedThreat extends SourceSuggestedThreat {
id: SuggestionID;
source: string;
status?: SuggestionStatus;
}

export interface EngineSuggestedResult {
Expand Down

0 comments on commit 380e616

Please sign in to comment.