diff --git a/migrations/sql/V2023.10.06.10.36__add_now_submissions_document_delete_ind.sql b/migrations/sql/V2023.10.06.10.36__add_now_submissions_document_delete_ind.sql new file mode 100644 index 0000000000..dbb5c3d299 --- /dev/null +++ b/migrations/sql/V2023.10.06.10.36__add_now_submissions_document_delete_ind.sql @@ -0,0 +1,4 @@ +ALTER TABLE + now_submissions.document +ADD + COLUMN IF NOT EXISTS deleted_ind boolean DEFAULT false NOT NULL; \ No newline at end of file diff --git a/services/core-api/app/api/now_applications/resources/now_application_document_resource.py b/services/core-api/app/api/now_applications/resources/now_application_document_resource.py index fa47ebd0ab..316f29d675 100644 --- a/services/core-api/app/api/now_applications/resources/now_application_document_resource.py +++ b/services/core-api/app/api/now_applications/resources/now_application_document_resource.py @@ -16,6 +16,7 @@ from app.api.now_applications.models.now_application_document_identity_xref import NOWApplicationDocumentIdentityXref from app.api.utils.custom_reqparser import CustomReqparser from app.api.mines.response_models import MINE_DOCUMENT_MODEL +from app.api.now_submissions.models.document import Document class NOWApplicationDocumentUploadResource(Resource, UserMixin): @@ -83,8 +84,8 @@ class NOWApplicationDocumentResource(Resource, UserMixin): @requires_role_edit_permit def delete(self, application_guid, mine_document_guid): mine_document = MineDocument.find_by_mine_document_guid(mine_document_guid) - now_app_doc_xref_exists = mine_document.now_application_document_xref is not None - now_app_doc_id_xref_exists = mine_document.now_application_document_identity_xref is not None + now_app_doc_xref_exists = mine_document and mine_document.now_application_document_xref is not None + now_app_doc_id_xref_exists = mine_document and mine_document.now_application_document_identity_xref is not None if not mine_document or (now_app_doc_xref_exists and application_guid != str( @@ -106,6 +107,13 @@ def delete(self, application_guid, mine_document_guid): if now_app_doc_xref_exists: mine_document.now_application_document_xref.delete(True) elif now_app_doc_id_xref_exists: + now_submission_document = Document.find_by_messageid_and_filename( + mine_document.now_application_document_identity_xref.messageid, + mine_document.now_application_document_identity_xref.filename + ) + + if now_submission_document is not None: + now_submission_document.delete(True) mine_document.now_application_document_identity_xref.delete(True) return None, 204 diff --git a/services/core-api/app/api/now_submissions/models/document.py b/services/core-api/app/api/now_submissions/models/document.py index 10173e5364..9b0553431f 100644 --- a/services/core-api/app/api/now_submissions/models/document.py +++ b/services/core-api/app/api/now_submissions/models/document.py @@ -1,12 +1,12 @@ from sqlalchemy.schema import FetchedValue -from app.api.utils.models_mixins import Base +from app.api.utils.models_mixins import Base, SoftDeleteMixin from app.extensions import db from sqlalchemy.dialects.postgresql import UUID from app.api.constants import * -class Document(Base): +class Document(SoftDeleteMixin, Base): __tablename__ = "document" __table_args__ = {"schema": "now_submissions"} _edit_groups = [NOW_APPLICATION_EDIT_GROUP] @@ -23,4 +23,11 @@ def __repr__(self): @classmethod def find_by_id(cls, id): - return cls.query.filter_by(id=id).one_or_none() \ No newline at end of file + return cls.query.filter_by(id=id).one_or_none() + + @classmethod + def find_by_messageid_and_filename(cls, messageid, filename): + return cls.query.filter_by(messageid=messageid, filename=filename).first() + + def delete(self, commit=True): + super(Document, self).delete(commit) \ No newline at end of file