Skip to content

Commit

Permalink
[OSDEV-1084] Claims. Update logic for number of workers. (#231)
Browse files Browse the repository at this point in the history
[OSDEV-1084](https://opensupplyhub.atlassian.net/browse/OSDEV-1084)
Claims. Update logic for number of workers.

The implementation of this issue makes it possible to add a range for
the number of workers during the claiming process, either after pressing
the “I want to claim this production location” link or on the Claimed
Facility Details page.

To change the type of the `facility_workers_count` field in the
`FacilityClaim` table from `IntegerField` to `CharField`, there were
added migrations with the next sequence:
* 0146_add_facility_workers_count_new_field_to_facilityclaim - adds the
facility_workers_count_new field to the FacilityClaim model.
* 0147_copy_facility_workers_count_to_facility_workers_count_new -
copies the data from the facility_workers_count field to the
facility_workers_count_new field.
* 0148_remove_facility_workers_count_field_from_facilityclaim - removes
the facility_workers_count field from the FacilityClaim model.
* 0149_rename_facility_workers_count_new_to_facility_workers_count -
renames the facility_workers_count_new field to facility_workers_count.
  • Loading branch information
mazursasha1990 authored Jun 12, 2024
1 parent 6ec6ec9 commit d12b03f
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 57 deletions.
10 changes: 8 additions & 2 deletions doc/release/RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html

### Database changes
#### Migrations:
* *Describe migrations here.*
* 0146_add_facility_workers_count_new_field_to_facilityclaim - adds the facility_workers_count_new field to the FacilityClaim model.
* 0147_copy_facility_workers_count_to_facility_workers_count_new - copies the data from the facility_workers_count field to the facility_workers_count_new field.
* 0148_remove_facility_workers_count_field_from_facilityclaim - removes the facility_workers_count field from the FacilityClaim model.
* 0149_rename_facility_workers_count_new_to_facility_workers_count - renames the facility_workers_count_new field to facility_workers_count.

#### Scheme changes
* *Describe scheme changes here.*
* [OSDEV-1084](https://opensupplyhub.atlassian.net/browse/OSDEV-1084) - To enable adding a range for the number of workers during the claiming process, the type of the `facility_workers_count` field in the `FacilityClaim` table was changed from `IntegerField` to `CharField`.

### Code/API changes
* *Describe code/API changes here.*
Expand Down Expand Up @@ -43,9 +46,12 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
* [OSDEV-939](https://opensupplyhub.atlassian.net/browse/OSDEV-939) - The following changes have been made:
* Created new steps `Supporting Documentation` & `Additional Data` for `Facility Claim Request` page.
* Added popup for successfully submitted claim.
* [OSDEV-1084](https://opensupplyhub.atlassian.net/browse/OSDEV-1084) - Enable adding a range for the number of workers during the claiming process, either after pressing the “I want to claim this production location” link or on the Claimed Facility Details page.

### Release instructions:
* Update code.
* Apply DB migrations up to the latest one.
* Run the index_facilities_new management command.


## Release 1.13.0
Expand Down
12 changes: 12 additions & 0 deletions src/django/api/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,15 @@ def parse_download_date(date: str):
.strptime(date, parse_dateformat)
.strftime("%Y-%m-%d")
)


def validate_workers_count(workers_count):
single_number_pattern = r'^\d+$'
range_pattern = r'^\d+-\d+$'

if re.match(single_number_pattern, workers_count):
return True
elif re.match(range_pattern, workers_count):
return True
else:
return False
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.db import migrations, models


class Migration(migrations.Migration):
'''
This migration adds the facility_workers_count_new field
to the FacilityClaim model.
'''

dependencies = [
('api', '0145_new_functions_for_clean_facilitylistitems_command'),
]

operations = [
migrations.AddField(
model_name='facilityclaim',
name='facility_workers_count_new',
field=models.CharField(
blank=True,
help_text='The editable facility workers count for this claim',
max_length=200,
null=True,
verbose_name='facility workers count',
),
),
migrations.AddField(
model_name='historicalfacilityclaim',
name='facility_workers_count_new',
field=models.CharField(
blank=True,
help_text='The editable facility workers count for this claim',
max_length=200,
null=True,
verbose_name='facility workers count',
),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.db import migrations


def copy_integer_to_char(apps, schema_editor):
FacilityClaim = apps.get_model('api', 'FacilityClaim')
for instance in FacilityClaim.objects.all():
if instance.facility_workers_count:
instance.facility_workers_count_new = str(
instance.facility_workers_count
)
instance.save()


class Migration(migrations.Migration):
'''
This migration copies the data from the facility_workers_count field
to the facility_workers_count_new field.
'''

dependencies = [
('api', '0146_add_facility_workers_count_new_field_to_facilityclaim'),
]

operations = [
migrations.RunPython(copy_integer_to_char),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.db import migrations


class Migration(migrations.Migration):
'''
This migration removes the facility_workers_count field
from the FacilityClaim model.
'''

dependencies = [
(
'api',
'0147_copy_facility_workers_count_to_facility_workers_count_new',
),
]

operations = [
migrations.RemoveField(
model_name='facilityclaim',
name='facility_workers_count',
),
migrations.RemoveField(
model_name='historicalfacilityclaim',
name='facility_workers_count',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.db import migrations


class Migration(migrations.Migration):
'''
This migration renames the facility_workers_count_new field
to facility_workers_count.
'''

dependencies = [
('api', '0148_remove_facility_workers_count_field_from_facilityclaim'),
]

operations = [
migrations.RenameField(
model_name='facilityclaim',
old_name='facility_workers_count_new',
new_name='facility_workers_count',
),
migrations.RenameField(
model_name='historicalfacilityclaim',
old_name='facility_workers_count_new',
new_name='facility_workers_count',
),
]
7 changes: 4 additions & 3 deletions src/django/api/models/facility/facility_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,12 @@ class FacilityClaim(models.Model):
blank=True,
verbose_name='average lead time',
help_text='The editable facilty avg lead time for this claim.')
facility_workers_count = models.IntegerField(
facility_workers_count = models.CharField(
max_length=200,
null=True,
blank=True,
help_text='The editable facility workers count for this claim.',
verbose_name='facility workers count')
verbose_name='facility workers count',
help_text='The editable facility workers count for this claim')
facility_female_workers_percentage = models.IntegerField(
null=True,
blank=True,
Expand Down
13 changes: 9 additions & 4 deletions src/django/api/views/facility/facilities_view_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from contricleaner.lib.exceptions.handler_not_set_error \
import HandlerNotSetError

from api.helpers.helpers import validate_workers_count
from oar.settings import (
MAX_ATTACHMENT_SIZE_IN_BYTES,
MAX_ATTACHMENT_AMOUNT,
Expand Down Expand Up @@ -896,10 +897,14 @@ def claim(self, request, pk=None):
setattr(facility_claim, 'sector', sectors)

try:
workers_count = int(number_of_workers)
except ValueError:
workers_count = None
except TypeError:
workers_count = number_of_workers

if len(workers_count) == 0:
workers_count = None
elif not validate_workers_count(workers_count):
workers_count = None

except (ValueError, TypeError):
workers_count = None

facility_claim.facility_workers_count = workers_count
Expand Down
13 changes: 9 additions & 4 deletions src/django/api/views/facility/facility_claim_view_set.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from api.models.transactions.index_facilities_new import index_facilities_new

from api.helpers.helpers import validate_workers_count
from rest_framework.decorators import action
from rest_framework.exceptions import (
NotFound,
Expand Down Expand Up @@ -341,10 +342,14 @@ def get_claimed_details(self, request, pk=None):
claim.parent_company_name = parent_company_name

try:
workers_count = int(request.data.get('facility_workers_count'))
except ValueError:
workers_count = None
except TypeError:
workers_count = request.data.get('facility_workers_count')

if len(workers_count) == 0:
workers_count = None
elif not validate_workers_count(workers_count):
workers_count = None

except (ValueError, TypeError):
workers_count = None

claim.facility_workers_count = workers_count
Expand Down
7 changes: 1 addition & 6 deletions src/react/src/actions/claimedFacilityDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,14 @@ export function submitClaimedFacilityDetailsUpdate(claimID) {
'initial_facility_address',
]),
{
facility_workers_count:
isInteger(data.facility_workers_count) ||
isInt(data.facility_workers_count)
? data.facility_workers_count
: null,
facility_workers_count: data.facility_workers_count,
facility_female_workers_percentage:
isInteger(data.facility_female_workers_percentage) ||
isInt(data.facility_female_workers_percentage)
? data.facility_female_workers_percentage
: null,
},
);

return apiRequest
.put(makeGetOrUpdateApprovedFacilityClaimURL(claimID), updateData)
.then(({ data: responseData }) =>
Expand Down
14 changes: 3 additions & 11 deletions src/react/src/components/ClaimFacilityAdditionalData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import map from 'lodash/map';
import filter from 'lodash/filter';
import includes from 'lodash/includes';
import isNull from 'lodash/isNull';
import isEmpty from 'lodash/isEmpty';
import every from 'lodash/every';
import Select from 'react-select';
import Creatable from 'react-select/creatable';

Expand All @@ -31,14 +29,11 @@ import { fetchSectorOptions } from '../actions/filterOptions';

import { sectorOptionsPropType } from '../util/propTypes';

import { getValueFromEvent } from '../util/util';
import { getValueFromEvent, validateNumberOfWorkers } from '../util/util';

import { claimAFacilitySupportDocsFormStyles } from '../util/styles';

import {
claimAFacilityAdditionalDataFormFields,
NUMERIC_DASH_REGEX,
} from '../util/constants';
import { claimAFacilityAdditionalDataFormFields } from '../util/constants';

import COLOURS from '../util/COLOURS';

Expand Down Expand Up @@ -289,10 +284,7 @@ function ClaimFacilityAdditionalData({
</Typography>
<TextField
id={numberOfWorkersForm.id}
error={every([
!isEmpty(numberOfWorkers),
!NUMERIC_DASH_REGEX.test(numberOfWorkers),
])}
error={validateNumberOfWorkers(numberOfWorkers)}
variant="outlined"
style={claimAFacilitySupportDocsFormStyles.textFieldStyles}
value={numberOfWorkers}
Expand Down
14 changes: 6 additions & 8 deletions src/react/src/components/ClaimedFacilitiesDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
isValidFacilityURL,
makeClaimGeocoderURL,
logErrorToRollbar,
validateNumberOfWorkers,
} from '../util/util';

import {
Expand Down Expand Up @@ -528,13 +529,9 @@ function ClaimedFacilitiesDetails({
value={data.facility_workers_count}
onChange={updateFacilityWorkersCount}
disabled={updating}
hasValidationErrorFn={() => {
if (isEmpty(data.facility_workers_count)) {
return false;
}

return !isInt(data.facility_workers_count, { min: 0 });
}}
hasValidationErrorFn={() =>
validateNumberOfWorkers(data.facility_workers_count)
}
/>
<InputSection
label="Percentage of female workers"
Expand Down Expand Up @@ -706,7 +703,8 @@ function ClaimedFacilitiesDetails({
(!isEmpty(data.point_of_contact_email) &&
!isEmail(data.point_of_contact_email)) ||
(!isEmpty(data.facility_website) &&
!isValidFacilityURL(data.facility_website))
!isValidFacilityURL(data.facility_website)) ||
validateNumberOfWorkers(data.facility_workers_count)
}
>
Save
Expand Down
26 changes: 15 additions & 11 deletions src/react/src/components/ClaimedFacilitiesDetailsSidebar.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { v4 as uuidv4 } from 'uuid';
import Typography from '@material-ui/core/Typography';

import { facilityDetailsPropType } from '../util/propTypes';
Expand Down Expand Up @@ -50,17 +51,20 @@ export default function ClaimedFacilitiesDetailsSidebar({ facilityDetails }) {
style={claimedFacilitiesDetailsSidebarStyles.bodyTextStyles}
>
{facilityDetails.properties.contributors.map(
({ id, name }) => (
<li key={id}>
<Link
to={makeProfileRouteLink(id)}
href={makeProfileRouteLink(id)}
key={id}
>
{name}
</Link>
</li>
),
({ id, name }) => {
const uniqueKey = uuidv4();
return (
<li key={uniqueKey}>
<Link
to={makeProfileRouteLink(id)}
href={makeProfileRouteLink(id)}
key={uniqueKey}
>
{name}
</Link>
</li>
);
},
)}
</ul>
</div>
Expand Down
2 changes: 0 additions & 2 deletions src/react/src/util/constants.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export const REJECT_ACTION = 'reject';

export const InfoLink = 'https://info.opensupplyhub.org';

export const NUMERIC_DASH_REGEX = /^[0-9-]+$/;

export const InfoPaths = {
storiesResources: 'stories-resources',
privacyPolicy: 'privacy-policy',
Expand Down
Loading

0 comments on commit d12b03f

Please sign in to comment.