def post(self, request, application_id):
        documents_string = request.POST.get("documents")
        request_files = request.FILES.getlist("files")

        # Validations.
        validations_errors = self._get_validation_errors(
            request_files, documents_string)
        if validations_errors:
            return validations_errors

        application = get_application_for_user(application_id, request.user.id)
        if application.prepared_pdf_id is None:
            return JsonMessageResponse("PO PDF is not generated.", status=400)

        # Data conversion.
        incoming_documents = json.loads(documents_string)
        po_pdf_content, po_json = get_protection_order_content(application)
        outgoing_documents = self._process_incoming_files_and_documents(
            po_pdf_content, po_json, incoming_documents, request_files)
        del request_files

        data = self.efiling_parsing.convert_data_for_efiling(
            request, application, outgoing_documents)

        # EFiling upload document.
        transaction_id = str(uuid.uuid4())
        efiling_submission = EFilingSubmissionModel(
            transaction_id=transaction_id,
            application_id=application.id,
        )
        efiling_submission.save()
        outgoing_files = convert_document_to_multi_part(outgoing_documents)
        del outgoing_documents
        upload_result = self.efiling_submission.upload_documents(
            request.user.universal_id, transaction_id, outgoing_files)

        if upload_result is None or "submissionId" not in upload_result:
            message = (upload_result["message"]
                       if upload_result and "message" in upload_result else
                       "Document Upload Failed.")
            return JsonMessageResponse(message, status=500)

        # EFiling package submission.
        submission_id = upload_result["submissionId"]
        redirect_url, message = self.efiling_submission.generate_efiling_url(
            request.user.universal_id, transaction_id, submission_id, data)

        if redirect_url is not None:
            efiling_submission.submission_id = submission_id
            efiling_submission.last_updated = timezone.now()
            efiling_submission.save()

            application.last_efiling_submission_id = efiling_submission.id
            application.save()
            return JsonResponse({
                "redirectUrl": redirect_url,
                "message": message
            })

        return JsonMessageResponse(message, status=500)
    def post(self, request, change_of_address_id):

        if 'document_type' in request.data:
            document_type = request.data['document_type']
        else:
            document_type = "CNAC"  # type Form18 for Efiling

        uid = request.user.id

        change_of_address = self.get_change_of_address_for_user(
            change_of_address_id, uid)
        if not change_of_address:
            return HttpResponseNotFound("no record found")

        if change_of_address.package_number or change_of_address.package_url:
            return JsonMessageResponse(
                "This application has already been submitted.", status=500)

        outgoing_documents = self._get_pdf_content(change_of_address,
                                                   document_type)
        data_for_efiling = self.efiling_parsing.convert_data_for_efiling(
            request, change_of_address, outgoing_documents, document_type)

        # EFiling upload document.
        transaction_id = str(uuid.uuid4())
        change_of_address.transaction_id = transaction_id
        change_of_address.save()

        outgoing_files = convert_document_to_multi_part(outgoing_documents)
        del outgoing_documents
        upload_result = self.efiling_submission.upload_documents(
            request.user.universal_id, transaction_id, outgoing_files)

        if upload_result is None or "submissionId" not in upload_result:
            message = (upload_result["message"]
                       if upload_result and "message" in upload_result else
                       "Document Upload Failed.")
            return JsonMessageResponse(message, status=500)

        # EFiling package submission.
        submission_id = upload_result["submissionId"]
        redirect_url, message = self.efiling_submission.generate_efiling_url(
            request.user.universal_id, transaction_id, submission_id,
            data_for_efiling)

        if redirect_url is not None:
            change_of_address.submission_id = submission_id
            change_of_address.last_filed = timezone.now()
            change_of_address.save()

            return JsonResponse({
                "redirectUrl": redirect_url,
                "message": message
            })

        return JsonMessageResponse(message, status=500)
            return HttpResponseNotFound("no record found")

        if case.package_number or case.package_url:
            return JsonMessageResponse(
                "This application has already been submitted.", status=500)

        outgoing_documents = self._get_pdf_content(case, document_type)
        data_for_efiling = self.efiling_parsing.convert_data_for_efiling(
            request, case, outgoing_documents, document_type)

        # EFiling upload document.
        transaction_id = str(uuid.uuid4())
        case.transaction_id = transaction_id
        case.save()

        outgoing_files = convert_document_to_multi_part(outgoing_documents)
        del outgoing_documents
        upload_result = self.efiling_submission.upload_documents(
            request.user.universal_id, transaction_id, outgoing_files)

        if upload_result is None or "submissionId" not in upload_result:
            message = (upload_result["message"]
                       if upload_result and "message" in upload_result else
                       "Document Upload Failed.")
            return JsonMessageResponse(message, status=500)

        # EFiling package submission.
        submission_id = upload_result["submissionId"]
        redirect_url, message = self.efiling_submission.generate_efiling_url(
            request.user.universal_id, transaction_id, submission_id,
            data_for_efiling)
    def post(self, request, notice_id):
        
        document_type = "NHA" # type Form5 for Efiling
        uid = request.user.id

        documents_string = request.POST.get("documents")
        attachment_files = request.FILES.getlist("files")

        notice = self.get_notice_for_user(notice_id, uid)        
        if not notice:
            return HttpResponseNotFound("no record found")

        if notice.package_number or notice.package_url: 
            return JsonMessageResponse("This application has already been submitted.", status=500)


        # Validations.
        validations_errors = _get_validation_errors(
            attachment_files, documents_string
        )
        if validations_errors:
            return validations_errors

        # Unique names.
        attachment_files = _unique_file_names(attachment_files)

        # Data conversion.
        incoming_documents = json.loads(documents_string)

        outgoing_documents = self._get_pdf_content(notice, document_type)               
        
        outgoing_documents = _process_incoming_files_and_documents(
            incoming_documents, attachment_files, outgoing_documents
        )
        del attachment_files 



        data_for_efiling = self.efiling_parsing.convert_data_for_efiling(
            request, notice, outgoing_documents, document_type
        )
        
        # EFiling upload document.
        transaction_id = str(uuid.uuid4())
        notice.transaction_id = transaction_id
        notice.save()

        outgoing_files = convert_document_to_multi_part(outgoing_documents)
        del outgoing_documents
        upload_result = self.efiling_submission.upload_documents(
            request.user.universal_id, transaction_id, outgoing_files
        )
       
        if upload_result is None or "submissionId" not in upload_result:
            message = (
                upload_result["message"]
                if upload_result and "message" in upload_result
                else "Document Upload Failed."
            )
            return JsonMessageResponse(message, status=500)

    
        # EFiling package submission.
        submission_id = upload_result["submissionId"]
        redirect_url, message = self.efiling_submission.generate_efiling_url(
            request.user.universal_id, transaction_id, submission_id, data_for_efiling
        )

        if redirect_url is not None:
            notice.submission_id = submission_id 
            notice.last_filed = timezone.now()            
            notice.save()

            return JsonResponse({"redirectUrl": redirect_url, "message": message})

        return JsonMessageResponse(message, status=500)