Skip to content

Commit b0ca2e0

Browse files
authored
Merge pull request #210 from nitheesh-aot/comp-307
COMP-307: Date selection on Creating Inspection
2 parents e2fd969 + 079f390 commit b0ca2e0

File tree

13 files changed

+111
-67
lines changed

13 files changed

+111
-67
lines changed

compliance-api/src/compliance_api/schemas/inspection.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
"""Inspection Schema Schema."""
15-
from marshmallow import EXCLUDE, ValidationError, fields, post_dump, post_load, validates_schema
15+
from marshmallow import EXCLUDE, ValidationError, fields, post_dump, post_load, pre_load, validates_schema
1616
from marshmallow_enum import EnumField
1717

1818
from compliance_api.models.inspection import (
@@ -105,7 +105,7 @@ class InspectionUpdateSchema(BaseSchema):
105105
end_date = fields.DateTime(
106106
format=INPUT_DATE_TIME_FORMAT,
107107
metadata={"description": "The inspection end date in ISO 8601 format."},
108-
required=True,
108+
allow_none=True,
109109
error_messages={
110110
"invalid": f"Not a valid datetime. Expected format: {INPUT_DATE_TIME_FORMAT}."
111111
},
@@ -155,6 +155,15 @@ class InspectionUpdateSchema(BaseSchema):
155155
required=False,
156156
)
157157

158+
@pre_load
159+
def end_date_populate(
160+
self, data, **kwargs
161+
): # pylint: disable=no-self-use, unused-argument
162+
"""Populate the end_date if it is not provided."""
163+
if data.get("end_date") is None:
164+
data["end_date"] = data["start_date"]
165+
return data
166+
158167
@validates_schema
159168
def validate_attendance_other(
160169
self, data, **kwargs

compliance-web/cypress/components/_components/_App/_CaseFiles/CaseFileForm.cy.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ const mockInitiations: Initiation[] = [
1919
];
2020

2121
const mockStaffUsers = [
22-
{ id: 1, name: "John Doe" },
23-
{ id: 2, name: "Jane Smith" },
24-
{ id: 3, name: "Alice Johnson" },
22+
{ id: 1, name: "John Doe", is_active: true },
23+
{ id: 2, name: "Jane Smith", is_active: true },
24+
{ id: 3, name: "Alice Johnson", is_active: true },
2525
];
2626

2727
const setup = (args: { isSuperUser: boolean }) => {

compliance-web/cypress/components/_components/_App/_CaseFiles/CaseFileGeneralInformation.cy.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ describe("CaseFileGeneralInformation", () => {
1313
project: { id: 1, name: "Test Project" },
1414
date_created: "2023-04-15T12:00:00Z",
1515
initiation: { id: "1", name: "Test Initiation" },
16-
primary_officer: { id: 1, name: "John Doe" },
16+
primary_officer: { id: 1, name: "John Doe", is_active: true },
1717
officers: [
18-
{ id: 2, name: "Jane Smith" },
19-
{ id: 3, name: "Bob Johnson" },
18+
{ id: 2, name: "Jane Smith", is_active: true },
19+
{ id: 3, name: "Bob Johnson", is_active: true },
2020
],
2121
project_id: 0,
2222
primary_officer_id: 0,

compliance-web/cypress/components/_components/_App/_Complaints/ComplaintFormLeft.cy.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { LocalizationProvider } from "@mui/x-date-pickers";
77
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs"; // Adjust to your date adapter
88

99
const mockStaffUsers = [
10-
{ id: 1, name: "John Doe" },
11-
{ id: 2, name: "Jane Smith" },
10+
{ id: 1, name: "John Doe", is_active: true },
11+
{ id: 2, name: "Jane Smith", is_active: true },
1212
];
1313

1414
describe("ComplaintFormLeft Component", () => {

compliance-web/cypress/components/_components/_App/_Inspections/InspectionDrawer.cy.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { AttendanceEnum } from "@/components/App/Inspections/InspectionFormUtils
88
import { CaseFile } from "@/models/CaseFile";
99
import { AuthProvider } from "react-oidc-context";
1010
import { OidcConfig } from "@/utils/config";
11+
import { LocalizationProvider } from "@mui/x-date-pickers";
12+
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
1113

1214
describe("InspectionDrawer Component", () => {
1315
let mockOnSubmit: sinon.SinonStub;
@@ -18,10 +20,10 @@ describe("InspectionDrawer Component", () => {
1820
project: { id: 1, name: "Test Project" },
1921
date_created: "2023-04-15T12:00:00Z",
2022
initiation: { id: "1", name: "Test Initiation" },
21-
primary_officer: { id: 1, name: "John Doe", auth_user_guid: "123" },
23+
primary_officer: { id: 1, name: "John Doe", auth_user_guid: "123", is_active: true },
2224
officers: [
23-
{ id: 2, name: "Jane Smith", auth_user_guid: "124" },
24-
{ id: 3, name: "Bob Johnson", auth_user_guid: "125" },
25+
{ id: 2, name: "Jane Smith", auth_user_guid: "124", is_active: true },
26+
{ id: 3, name: "Bob Johnson", auth_user_guid: "125", is_active: true },
2527
],
2628
project_id: 0,
2729
primary_officer_id: 0,
@@ -45,7 +47,7 @@ describe("InspectionDrawer Component", () => {
4547
type: "Type",
4648
sub_type: "Sub Type",
4749
location_description: "Location Description",
48-
primary_officer: { id: 1, name: "Officer Name" },
50+
primary_officer: { id: 1, name: "Officer Name", is_active: true },
4951
ir_status: { id: "1", name: "Status" },
5052
project_status: { id: "1", name: "Project Status" },
5153
types: [
@@ -79,7 +81,7 @@ describe("InspectionDrawer Component", () => {
7981
initiation: { id: "1", name: "Initiation" },
8082
is_active: true,
8183
project: { id: 1, name: "Project" },
82-
primary_officer: { id: 1, name: "Officer Name" },
84+
primary_officer: { id: 1, name: "Officer Name", is_active: true },
8385
},
8486
};
8587

@@ -97,7 +99,9 @@ describe("InspectionDrawer Component", () => {
9799
return (
98100
<QueryClientProvider client={queryClient}>
99101
<AuthProvider {...OidcConfig}>
100-
{children}
102+
<LocalizationProvider dateAdapter={AdapterDayjs}>
103+
{children}
104+
</LocalizationProvider>
101105
</AuthProvider>
102106
</QueryClientProvider>
103107
);

compliance-web/cypress/components/_components/_App/_Inspections/InspectionFormLeft.cy.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const mockInitiations = [
2323
];
2424

2525
const mockStaffUsers = [
26-
{ id: 1, name: "John Doe" },
27-
{ id: 2, name: "Jane Smith" },
26+
{ id: 1, name: "John Doe", is_active: true },
27+
{ id: 2, name: "Jane Smith", is_active: true },
2828
];
2929

3030
const mockIRTypes = [
@@ -85,7 +85,8 @@ describe("InspectionFormLeft Component", () => {
8585
cy.contains("UTM (optional)").should("exist");
8686
cy.contains("Primary").should("exist");
8787
cy.contains("Type").should("exist");
88-
cy.contains("Dates").should("exist");
88+
cy.contains("Start Date").should("exist");
89+
cy.contains("End Date").should("exist");
8990
cy.contains("Initiation").should("exist");
9091
});
9192

@@ -126,11 +127,17 @@ describe("InspectionFormLeft Component", () => {
126127
});
127128

128129
it("allows selecting a date range", () => {
129-
cy.get('input[name="dateRange"]').click();
130-
cy.get(".MuiPickersDay-root").contains("10").click(); // Select the start date
131-
cy.get(".MuiPickersDay-root").contains("20").click(); // Select the end date
132-
cy.get('input[name="dateRange"]').should("contain.value", "10");
133-
cy.get('input[name="dateRange"]').should("contain.value", "20");
130+
131+
cy.get('.cy-start-date').should("exist");
132+
cy.get('.cy-end-date').should("exist");
133+
134+
cy.get('.cy-start-date button[aria-label="Choose date"]').click();
135+
cy.get(".MuiPickersDay-root", { timeout: 10000 }).contains("10").click(); // Select the start date
136+
cy.get('input[name="startDate"]').should("contain.value", "10");
137+
138+
cy.get('.cy-end-date button[aria-label="Choose date"]').click();
139+
cy.get(".MuiPickersDay-root", { timeout: 10000 }).contains("20").click(); // Select the end date
140+
cy.get('input[name="endDate"]').should("contain.value", "20");
134141
});
135142

136143
it("allows selecting initiation", () => {

compliance-web/cypress/components/_components/_App/_Inspections/InspectionFormRight.cy.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const mockAgenciesList = [
2222
];
2323

2424
const mockStaffUsers = [
25-
{ id: 1, name: "John Doe" },
26-
{ id: 2, name: "Jane Smith" },
25+
{ id: 1, name: "John Doe", is_active: true },
26+
{ id: 2, name: "Jane Smith", is_active: true },
2727
];
2828

2929
const mockFirstNationsList = [

compliance-web/cypress/components/_components/_App/_Inspections/InspectionFormUtils.cy.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ describe("InspectionFormUtils", () => {
6666
primaryOfficer: { id: 1, full_name: "John Doe" },
6767
officers: [{ id: 2, full_name: "Jane Smith" }],
6868
irTypes: [{ id: 1, name: "IR Type Alpha" }],
69-
dateRange: {
70-
startDate: dayjs("2023-01-01"),
71-
endDate: dayjs("2023-01-02"),
72-
},
69+
startDate: dayjs("2023-01-01"),
70+
endDate: dayjs("2023-01-02"),
7371
initiation: { id: 1, name: "Initiation Alpha" },
7472
irStatus: { id: 1, name: "Status 1" },
7573
projectStatus: { id: 1, name: "Status 1" },

compliance-web/src/components/App/Inspections/InspectionDrawer.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ type InspectionDrawerProps = {
4040

4141
const initFormData: InspectionFormData = {
4242
project: undefined,
43-
dateRange: undefined,
4443
primaryOfficer: undefined,
4544
irTypes: [],
45+
startDate: undefined,
46+
endDate: undefined,
4647
initiation: undefined,
4748
irStatus: undefined,
4849
projectStatus: undefined,
@@ -81,10 +82,8 @@ const InspectionDrawer: React.FC<InspectionDrawerProps> = ({
8182
irStatus: inspection.ir_status,
8283
projectStatus: inspection.project_status,
8384
irTypes: inspection.types,
84-
dateRange: {
85-
startDate: dayjs(inspection.start_date),
86-
endDate: dayjs(inspection.end_date),
87-
},
85+
startDate: dayjs(inspection.start_date),
86+
endDate: dayjs(inspection.end_date),
8887
inAttendance: inspection.inspectionAttendances?.map(
8988
(item) => item.attendance_option
9089
),

compliance-web/src/components/App/Inspections/InspectionFormLeft.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { BCDesignTokens } from "epic.theme";
66
import { Initiation } from "@/models/Initiation";
77
import ControlledTextField from "@/components/Shared/Controlled/ControlledTextField";
88
import { IRType } from "@/models/IRType";
9-
import ControlledDateRangePicker from "@/components/Shared/Controlled/ControlledDateRangePicker";
109
import { IRStatus } from "@/models/IRStatus";
1110
import { ProjectStatus } from "@/models/ProjectStatus";
11+
import ControlledDateField from "@/components/Shared/Controlled/ControlledDateField";
12+
import { useFormContext } from "react-hook-form";
1213

1314
type InspectionFormLeftProps = {
1415
initiationList: Initiation[];
@@ -25,6 +26,10 @@ const InspectionFormLeft: FC<InspectionFormLeftProps> = ({
2526
irStatusList,
2627
projectStatusList,
2728
}) => {
29+
const { watch } = useFormContext();
30+
const startDate = watch("startDate");
31+
const endDate = watch("endDate");
32+
2833
return (
2934
<>
3035
<Box
@@ -91,7 +96,20 @@ const InspectionFormLeft: FC<InspectionFormLeftProps> = ({
9196
multiple
9297
fullWidth
9398
/>
94-
<ControlledDateRangePicker name="dateRange" label="Dates" fullWidth />
99+
<Stack direction={"row"} gap={2} sx={{ width: "100%" }}>
100+
<ControlledDateField
101+
className="cy-start-date"
102+
name="startDate"
103+
label="Start Date"
104+
maxDate={endDate}
105+
/>
106+
<ControlledDateField
107+
className="cy-end-date"
108+
name="endDate"
109+
label="End Date (optional)"
110+
minDate={startDate}
111+
/>
112+
</Stack>
95113
</Stack>
96114
<Stack direction={"row"} gap={2}>
97115
<ControlledAutoComplete

0 commit comments

Comments
 (0)