Skip to content

Commit

Permalink
[backend] improve delete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyCloarec committed Oct 29, 2024
1 parent 8b93ce1 commit bd70dbc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion opencti-platform/opencti-graphql/src/database/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3517,7 +3517,7 @@ export const elDeleteInstances = async (instances) => {
for (let i = 0; i < groupsOfInstances.length; i += 1) {
const instancesBulk = groupsOfInstances[i];
const bodyDelete = instancesBulk.flatMap((doc) => {
return [{ delete: { _index: doc._index, _id: doc.internal_id, retry_on_conflict: ES_RETRY_ON_CONFLICT } }];
return [{ delete: { _index: doc._index, _id: doc._id ?? doc.internal_id, retry_on_conflict: ES_RETRY_ON_CONFLICT } }];
});
await elBulk({ refresh: true, timeout: BULK_TIMEOUT, body: bodyDelete });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const findAllUsersWithDraftContext = async (context: AuthContext, user: AuthUser
return listAllEntities(context, user, [ENTITY_TYPE_USER], listArgs);
};

// When deleting a draft, we need to move all users that are still in the draft context back to the live context
const deleteDraftContextFromUsers = async (context: AuthContext, user: AuthUser, draftId: string) => {
const usersWithDraftContext = await findAllUsersWithDraftContext(context, user, draftId);
if (usersWithDraftContext.length > 0) {
Expand All @@ -77,8 +78,7 @@ export const deleteDraftWorkspace = async (context: AuthContext, user: AuthUser,
if (!draftWorkspace) {
throw FunctionalError(`Draft workspace ${id} cannot be found`, id);
}

await elDeleteDraftElements(context, user, id);
await elDeleteDraftElements(context, user, id); // delete all draft elements from draft index
await deleteDraftContextFromUsers(context, user, id);
await deleteElementById(context, user, id, ENTITY_TYPE_DRAFT_WORKSPACE);

Expand All @@ -93,23 +93,27 @@ export const validateDraftWorkspace = async (context: AuthContext, user: AuthUse
const contextInDraft = { ...context, draft_context: draft_id };
const contextOutOfDraft = { ...context, draft_context: '' };
const includeDeleteOption = { includeDeletedElements: true };
// We start by listing all elements currently in this draft context
const draftEntities = await elList(contextInDraft, user, READ_INDEX_DRAFT_OBJECTS, includeDeleteOption);

const draftEntitiesMinusRefRel = draftEntities.filter((e) => !isStixRefRelationship(e.entity_type));

// We add all created elements as stix objects to the bundle
const createEntities = draftEntitiesMinusRefRel.filter((e) => e.draft_change?.draft_operation === DRAFT_OPERATION_CREATE);
const createEntitiesIds = createEntities.map((e) => e.internal_id);
const createStixEntities = await stixLoadByIds(contextInDraft, user, createEntitiesIds);

// We add all deleted elements as stix objects to the bundle, but we mark them as a delete operation
const deletedEntities = draftEntitiesMinusRefRel.filter((e) => e.draft_change?.draft_operation === DRAFT_OPERATION_DELETE);
const deleteEntitiesIds = deletedEntities.map((e) => e.internal_id);
const deleteStixEntities = await stixLoadByIds(contextInDraft, user, deleteEntitiesIds, includeDeleteOption);
const deleteStixEntitiesModified = deleteStixEntities.map((d: any) => ({ ...d, opencti_operation: 'delete' }));

// We add all deleted refs in the bundle, marking them as a delete operation
const draftStixDeleteRefs = draftEntities.filter((e) => isStixRefRelationship(e.entity_type) && e.draft_change?.draft_operation === DRAFT_OPERATION_DELETE);
const deletedRefsBundle = draftStixDeleteRefs.map((ref) => ({ id: ref.internal_id, type: ABSTRACT_STIX_REF_RELATIONSHIP, opencti_operation: 'delete' }));

// TODO: for now, updated entities are fully sent in bundle. But once update metadata are OK, only send real updates in bundle
// TODO: for now, updated entities are fully sent in bundle. But once update metadata are OK, we will want to take the live element and only apply updates done in the draft
const updateEntities = draftEntitiesMinusRefRel.filter((e) => e.draft_change?.draft_operation === DRAFT_OPERATION_UPDATE);
const updateEntitiesIds = updateEntities.map((e) => e.internal_id);
const updateStixEntities = await stixLoadByIds(contextInDraft, user, updateEntitiesIds);
Expand Down

0 comments on commit bd70dbc

Please sign in to comment.