Skip to content

Commit

Permalink
[backend] use patchAttribute for ref relation Replace
Browse files Browse the repository at this point in the history
  • Loading branch information
marieflorescontact committed Mar 20, 2024
1 parent 70fb123 commit 5e4c4d3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
21 changes: 7 additions & 14 deletions opencti-platform/opencti-graphql/src/manager/taskManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import { RELATION_GRANTED_TO, RELATION_OBJECT } from '../schema/stixRefRelations
import { ACTION_TYPE_DELETE, ACTION_TYPE_SHARE, ACTION_TYPE_UNSHARE, TASK_TYPE_LIST, TASK_TYPE_QUERY, TASK_TYPE_RULE } from '../domain/backgroundTask-common';
import { controlUserConfidenceAgainstElement } from '../utils/confidence-level';
import { validateUpdatableAttribute } from '../schema/schema-validator';
import { schemaRelationsRefDefinition } from '../schema/schema-relationsRef';

// Task manager responsible to execute long manual tasks
// Each API will start is task manager.
Expand Down Expand Up @@ -242,32 +243,24 @@ const executeRemove = async (context, user, actionContext, element) => {
export const executeReplace = async (context, user, actionContext, element) => {
const { field, type: contextType, values } = actionContext;
if (contextType === ACTION_TYPE_RELATION) {
const toCreate = [];
// 01 - Delete all relations of the element
// 01 - Get all relations of the element
const rels = await listAllRelations(context, user, field, { fromId: element.id });
// Patch
if (rels.length > 0) {
// Items to Delete
for (let index = 0; index < rels.length; index += 1) {
if (!values.includes(rels[index].toId)) {
const rel = rels[index];
await deleteElementById(context, user, rel.id, rel.entity_type);
}
}
// 02 - Create new ones
for (let indexCreate = 0; indexCreate < values.length; indexCreate += 1) {
const target = values[indexCreate];
if (!rels.some(({ toId }) => toId === target)) {
toCreate.push(target);
const inputName = schemaRelationsRefDefinition.convertDatabaseNameToInputName(element.entity_type, field);
const patch = generatePatch(inputName, values, element.entity_type);
await patchAttribute(context, user, element.id, element.entity_type, patch);
}
}
} else if (rels.length === 0) {
// 02 - Create relations if no relation to replace
for (let indexCreate = 0; indexCreate < values.length; indexCreate += 1) {
const target = values[indexCreate];
toCreate.push(target);
await createRelation(context, user, { fromId: element.id, toId: target, relationship_type: field });
}
}
await Promise.all(toCreate.map(async (tc) => await createRelation(context, user, { fromId: element.id, toId: tc, relationship_type: field })));
}
if (contextType === ACTION_TYPE_ATTRIBUTE) {
const patch = generatePatch(field, values, element.entity_type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('TaskManager executeReplace tests ', () => {
expect(markingEntity.standard_id).toEqual(MARKING_TLP_CLEAR);
}
});
it('REPLACE report no marking with marking', async () => {
it('REPLACE report without marking with marking', async () => {
const reportInput = {
name: 'test report no marking with marking',
objectMarking: [],
Expand Down

0 comments on commit 5e4c4d3

Please sign in to comment.