Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow Duplicates Working Branch #4541

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
UEISerializer,
)

from dissemination.models import General

UserModel = get_user_model()

AUDITEE_INFO_PREVIOUS_STEP_DATA_WE_NEED = [
Expand Down Expand Up @@ -242,6 +244,29 @@ def post(self, request):
data["auditee_uei"] = data["auditee_uei"].upper()
serializer = UEISerializer(data=data)

# Before checking the UEI, we want to see if this is a duplicate submission
auditee_uei=data["auditee_uei"].upper()
audit_year=data["audit_year"]

# verify that there is an audit year.
if not audit_year:
return Response(
{
"valid": False,
"errors": ["invalid-year"]
}
)

duplicates = General.objects.filter(audit_year=audit_year, auditee_uei=auditee_uei).values("report_id")
if duplicates:
return Response(
{
"valid": False,
"response": {"duplicates": duplicates},
"errors": ["duplicate-submission"]
}
)

if serializer.is_valid():
return Response(
{
Expand Down
23 changes: 23 additions & 0 deletions backend/audit/models/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datetime import timedelta
from itertools import chain
import datetime
import json
import logging

from dissemination.models import General
from django.db import models
from django.db.transaction import TransactionManagementError
from django.conf import settings
Expand Down Expand Up @@ -239,6 +241,27 @@ def disseminate(self):

return None

def is_duplicate(self):
"""
Validates whether the SAC contains identical information to disseminated records.
"""
general_information = self.general_information

audit_year = general_information.get("auditee_fiscal_period_end")
uei = general_information.get("auditee_uei")

if audit_year and uei:

# extract date from year.
audit_year = datetime.datetime.strptime(audit_year, "%Y-%m-%d").year

# check disseminated.
return General.objects.filter(
audit_year=audit_year, auditee_uei=uei
).exists()

return False

def assign_cog_over(self):
"""
Function that the FAC app uses when a submission is completed and cog_over needs to be assigned.
Expand Down
40 changes: 25 additions & 15 deletions backend/report_submission/templates/report_submission/step-2.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,32 @@ <h2>
<strong>Entity UEI <a>Unique Entity Identifier</a></strong><abbr title="required" class="usa-hint usa-hint--required">*</abbr>
</p>
<div class="usa-hint margin-y-05" id="auditee_uei_hint">12-character alphanumeric ID</div>
<div class="usa-search" role="search">
<label class="usa-sr-only" for="auditee_uei">Search</label>
<div class="usa-search">
<input class="usa-input"
id="auditee_uei"
type="search"
name="auditee_uei"
required
data-validate-not-null=""
data-validate-length="== 12"
aria-autocomplete="inline"/>
<button class="usa-button"
id="auditee_uei-btn"
type="submit"
aria-controls="uei-search-result"
data-open-modal>
<span class="usa-search__submit-text">
<svg class="usa-icon usa-icon--size-3 margin-right-1" aria-hidden="true" focusable="false" role="img">
{% uswds_sprite "verified" %}
</svg>
<p class="margin-0 text-no-wrap">Validate UEI</p>
</span>
</button>
</div>
<ul class="usa-error-message" id="auditee_uei-error-message" role="alert">
<li id="auditee_uei-not-null" hidden>Can&#39;t be null</li>
<li id="auditee_uei-length" hidden>UEI is twelve characters long</li>
</ul>
<span class="usa-error-message margin-top-1">{{ form.auditee_uei.errors|striptags }}</span>
{% if duplicate_report_ids %}
<span class="usa-error-message margin-top-1">
Here they are!
<ul>
{% for x in duplicate_report_ids %}
<li>
<a class="usa-link" href="{% url 'dissemination:Summary' report_id=x.report_id %}">{{ x.report_id }}</a>
</li>
{% endfor %}
</ul>
</span>
{% endif %}
</div>
<div class="usa-form-group">
<label for="auditee_name" hidden>Auditee name</label>
Expand Down Expand Up @@ -133,6 +131,18 @@ <h2>
</div>
</div>
</fieldset>
<button class="usa-button"
id="auditee_uei-btn"
type="submit"
aria-controls="uei-search-result"
data-open-modal>
<span class="usa-search__submit-text">
<svg class="usa-icon usa-icon--size-3 margin-right-1" aria-hidden="true" focusable="false" role="img">
{% uswds_sprite "verified" %}
</svg>
<p class="margin-0 text-no-wrap">Validate UEI</p>
</span>
</button>
<ul class="usa-button-group">
<li class="usa-button-group__item">
<button class="usa-button" id="continue">Continue</button>
Expand Down
20 changes: 20 additions & 0 deletions backend/report_submission/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from audit.fixtures.excel import FORM_SECTIONS
from config.settings import STATIC_SITE_URL, STATE_ABBREVS, DOLLAR_THRESHOLDS

from dissemination.models import General

from report_submission.forms import AuditeeInfoForm, GeneralInformationForm

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -95,6 +97,24 @@ def post(self, request):
].strftime("%Y-%m-%d"),
}

audit_year = form.cleaned_data["auditee_fiscal_period_start"].year
uei = form.cleaned_data["auditee_uei"].upper()

# check disseminated for duplicates
duplicates = General.objects.filter(audit_year=audit_year, auditee_uei=uei).values("report_id")

if duplicates:
form.add_error("auditee_uei", f"A record has already been submitted for UEI {uei} in Audit Year {audit_year}.")
return render(
request,
"report_submission/step-2.html",
{
"form": form,
"step": 2,
"duplicate_report_ids": duplicates,
},
)

info_check = api.views.auditee_info_check(request.user, formatted_post)
if info_check.get("errors"):
return redirect(reverse("report_submission:auditeeinfo"))
Expand Down
76 changes: 64 additions & 12 deletions backend/static/js/check-ueid.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
if (valid) {
handleValidUei(response);
} else {
handleInvalidUei(errors);
handleInvalidUei(errors, response);
}
}

Expand All @@ -17,9 +17,13 @@
populateModal('success', response.auditee_name);
}

function handleInvalidUei(errors) {
function handleInvalidUei(errors, response) {
if (Object.keys(errors).includes('auditee_uei')) {
populateModal('not-found');
} else if (errors.includes('duplicate-submission')) {
populateModal('duplicate-submission', undefined, response);
} else if (errors.includes('invalid-year')) {
populateModal('invalid-year');
}
}

Expand All @@ -43,6 +47,7 @@
const auditeeUei = document.getElementById('auditee_uei').value;
const auditeeName = document.getElementById('auditee_name');
const ueiInfoEl = document.createElement('div');
const noUeiWarning = document.getElementById('no-uei-warning');

ueiInfoEl.innerHTML = `
<dl data-testid="uei-info">
Expand All @@ -55,14 +60,14 @@

auditeeName.removeAttribute('disabled');
auditeeName.parentNode.setAttribute('hidden', 'hidden');
document.getElementById('no-uei-warning').replaceWith(ueiInfoEl);
if (noUeiWarning) {
noUeiWarning.replaceWith(ueiInfoEl);
}
}

function setupFormWithValidUei() {
hideUeiStuff();
showValidUeiInfo();
isUEIValidated = true;
setFormDisabled(false);
}

function resetModal() {
Expand All @@ -85,9 +90,11 @@
document.querySelector('.uei-search-result').classList.add('loading');
}

// 'connection-error' | 'not-found' | 'success'
function populateModal(formStatus, auditeeName) {
// 'connection-error' | 'not-found' | 'success' | 'duplicate-submission'
function populateModal(formStatus, auditeeName = '', response = {}) {
const auditeeUei = document.getElementById('auditee_uei').value;
const auditee_fiscal_period_start = document.getElementById('auditee_fiscal_period_start').value;
const audit_year = (new Date(auditee_fiscal_period_start)).getFullYear()
const modalContainerEl = document.querySelector(
'#uei-search-result .usa-modal__main'
);
Expand Down Expand Up @@ -150,11 +157,47 @@
},
},
},
'duplicate-submission': {
heading: 'Duplicate submission',
description: `
<dl>
<dt>UEI</dt>
<dd>${auditeeUei}</dd>
<dt>Audit Year</dt>
<dd>${audit_year}</dd>
</dl>
`,
buttons: {
primary: {
text: `Go back`,
},
},
},
'invalid-year': {
heading: 'Invalid year',
description: `
<p>We can't proceed without a valid year. Please enter in a valid year for this audit.</p>
`,
buttons: {
primary: {
text: `Go back`,
},
},
}
};

const contentForStatus = modalContent[formStatus];
modalHeadingEl.textContent = contentForStatus.heading;
modalDescriptionEl.innerHTML = contentForStatus.description;

if (formStatus === "duplicate-submission"){
modalDescriptionEl.innerHTML += "<p>These submissions have the same UEI and audit year:</p><ul>"
response.duplicates.forEach((element) =>
modalDescriptionEl.innerHTML += `<li>${element.report_id}</li>`
);
modalDescriptionEl.innerHTML += "</ul>"
}

modalButtonPrimaryEl.textContent = contentForStatus.buttons.primary.text;

if (contentForStatus.buttons.secondary) {
Expand All @@ -163,7 +206,9 @@
}

if (formStatus == 'success') {
isUEIValidated = true;
modalButtonPrimaryEl.onclick = setupFormWithValidUei;
setFormDisabled(false);
}

document.querySelector('.uei-search-result').classList.remove('loading');
Expand All @@ -173,10 +218,12 @@
resetModal();

const auditee_uei = document.getElementById('auditee_uei').value;
const auditee_fiscal_period_start = document.getElementById('auditee_fiscal_period_start').value;
const audit_year = (new Date(auditee_fiscal_period_start)).getFullYear()

queryAPI(
'/api/sac/ueivalidation',
{ auditee_uei },
{ auditee_uei, audit_year },
{
method: 'POST',
},
Expand Down Expand Up @@ -205,12 +252,11 @@
} else {
fyFormGroup.classList.remove('usa-form-group--error');
}

setFormDisabled(!allResponsesValid());
}

function setFormDisabled(shouldDisable) {
const continueBtn = document.getElementById('continue');

// If we want to disable the button, do it.
if (shouldDisable) {
continueBtn.disabled = true;
Expand All @@ -231,8 +277,10 @@
}

function performValidations(field) {
const errors = checkValidity(field);
setFormDisabled(errors.length > 0);
checkValidity(field);
if (errors.length > 0) {

Check failure on line 281 in backend/static/js/check-ueid.js

View workflow job for this annotation

GitHub Actions / linting / frontend-linting

'errors' is not defined
setFormDisabled(true);
}
}

function attachEventHandlers() {
Expand Down Expand Up @@ -272,6 +320,10 @@
dateInputsNeedingValidation.forEach((q) => {
q.addEventListener('blur', (e) => {
performValidations(e.target);
setFormDisabled(true);
});
q.addEventListener('input', (e) => {

Check failure on line 325 in backend/static/js/check-ueid.js

View workflow job for this annotation

GitHub Actions / linting / frontend-linting

'e' is defined but never used
setFormDisabled(true);
});
});
}
Expand Down
Loading