Skip to content

Commit

Permalink
Merged in r2-2608-prevent-multiple-cases (pull request #6504)
Browse files Browse the repository at this point in the history
R2-2608 - Users can create multiple cases for a family details entry

Approved-by: Pavel Nabutovsky
Approved-by: Joshua Toliver
  • Loading branch information
dhernandez-quoin authored and jtoliver-quoin committed Sep 20, 2023
2 parents 86572b1 + bb48db6 commit 80a5dc5
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 54 deletions.
2 changes: 0 additions & 2 deletions app/controllers/api/v2/children_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ def family
)
@current_record.save! if @current_record.has_changes_to_save?
@record.save!

select_updated_fields
select_fields_for_show
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v2/families_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create_case
@current_record = Family.find(create_case_params[:family_id])
@record = @current_record.new_child_from_family_member(current_user, create_case_params['family_member_id'])
@record.save!
select_updated_fields
select_fields_for_show
end

def create_case_params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export const SUBFORM_FIELDS = "SubformFields";
export const SUBFORM_FIELD_ARRAY = "SubformFieldArray";
export const SUBFORM_DIALOG = "SubformDialog";
export const SUBFORM_FIELD_SUBFORM = "SubformFieldSubform";
export const SUBFORM_CREATE_CASE_DIALOG = "SubformCreateCaseDialog";
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import isEmpty from "lodash/isEmpty";
import { useDispatch } from "react-redux";

import { fieldValidations } from "../../validations";
import { SUBFORM_DIALOG } from "../constants";
import { SUBFORM_CREATE_CASE_DIALOG, SUBFORM_DIALOG } from "../constants";
import ServicesSubform from "../services-subform";
import SubformMenu from "../subform-menu";
import { getSubformValues, serviceHasReferFields, updateSubformEntries, addSubformEntries } from "../../utils";
import ActionDialog from "../../../../action-dialog";
import ActionDialog, { useDialog } from "../../../../action-dialog";
import SubformDrawer from "../subform-drawer";
import { compactValues, constructInitialValues } from "../../../utils";
import SubformErrors from "../subform-errors";
Expand Down Expand Up @@ -61,9 +61,9 @@ const Component = ({
const params = useParams();
const dispatch = useDispatch();
const [initialValues, setInitialValues] = useState({});
const { dialogOpen, setDialog } = useDialog(SUBFORM_CREATE_CASE_DIALOG);
const [openConfirmationModal, setOpenConfirmationModal] = useState(false);
const [openCreateCaseConfirmationModal, setOpenCreateCaseConfirmationModal] = useState(false);
const caseFromFamilyMemberLoading = useMemoizedSelector(state => getCaseFormFamilyMemberLoading(state));
const caseFromFamilyMemberLoading = useMemoizedSelector(state => getCaseFormFamilyMemberLoading(state, recordType));
const childFormikRef = useRef();
const isValidIndex = index === 0 || index > 0;
const asDrawer = isViolation || isViolationAssociation || isFamilyMember || isFamilyDetail;
Expand Down Expand Up @@ -186,15 +186,17 @@ const Component = ({
};

const createCaseConfirmationProps = {
open: openCreateCaseConfirmationModal,
open: dialogOpen,
maxSize: "xs",
confirmButtonLabel: isFamilyMember ? i18n.t("family.family_member.create") : i18n.t("case.create"),
omitCloseAfterSuccess: true,
dialogTitle: title,
dialogText: isFamilyMember
? i18n.t("family.messages.confirm_create_case")
: i18n.t("case.messages.confirm_create_case"),
disableBackdropClick: true,
cancelHandler: () => setOpenCreateCaseConfirmationModal(false),
cancelHandler: () => {
setDialog({ dialog: SUBFORM_CREATE_CASE_DIALOG, open: false });
},
successHandler: () => {
if (isFamilyMember) {
dispatch(createCaseFromFamilyMember({ familyId: params.id, familyMemberId: subformValues.unique_id }));
Expand Down Expand Up @@ -272,7 +274,7 @@ const Component = ({
pending={caseFromFamilyMemberLoading}
handleCreateLabel={familyCreateLabel}
handleCreate={() => {
setOpenCreateCaseConfirmationModal(true);
setDialog({ dialog: SUBFORM_CREATE_CASE_DIALOG, open: true });
}}
/>
) : null
Expand Down Expand Up @@ -304,7 +306,7 @@ const Component = ({
}}
</Formik>
</ComponentToRender>
{isFamilySubform && openCreateCaseConfirmationModal && <ActionDialog {...createCaseConfirmationProps} />}
{isFamilySubform && <ActionDialog {...createCaseConfirmationProps} />}
<ActionDialog {...modalConfirmationProps} />
</>
);
Expand Down
30 changes: 3 additions & 27 deletions app/javascript/components/records/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,8 @@ export default namespace =>
}
case `${namespace}/${CREATE_CASE_FROM_FAMILY_MEMBER_SUCCESS}`: {
const recordIndex = state.get("data").findIndex(record => record.get("id") === payload.data.id);
const subformIndex = state
.getIn(["data", recordIndex, "family_members"])
.findIndex(member => member.get("unique_id") === payload.data.record.family_member_id);

return state.updateIn(["data", recordIndex, "family_members", subformIndex], existing =>
mergeRecord(
existing,
fromJS({ case_id: payload.data.record.id, case_id_display: payload.data.record.case_id_display })
)
);
return state.setIn(["data", recordIndex, "family_members"], fromJS(payload.data.family_members));
}
case `${namespace}/${CREATE_CASE_FROM_FAMILY_MEMBER_FAILURE}`: {
return state.setIn(["case_from_family", "errors"], true);
Expand All @@ -383,24 +375,8 @@ export default namespace =>
case `${namespace}/${CREATE_CASE_FROM_FAMILY_DETAIL_SUCCESS}`: {
const recordIndex = state.get("data").findIndex(record => record.get("id") === payload.data.id);

const result = payload.data.record.family_details_section.reduce((acc, familyDetail) => {
const subformIndex = acc
.getIn(["data", recordIndex, "family_details_section"])
.findIndex(member => member.get("unique_id") === familyDetail.unique_id);

if (subformIndex > -1) {
return acc.updateIn(["data", recordIndex, "family_details_section", subformIndex], existing =>
mergeRecord(existing, familyDetail)
);
}

return acc.setIn(
["data", recordIndex, "family_details_section"],
acc.getIn(["data", recordIndex, "family_details_section"]).push(fromJS(familyDetail))
);
}, state);

return result
return state
.setIn(["data", recordIndex, "family_details_section"], fromJS(payload.data.family_details_section))
.setIn(["data", recordIndex, "family_number"], payload.data.family_number)
.setIn(["data", recordIndex, "family_member_id"], payload.data.family_member_id)
.setIn(["data", recordIndex, "family_id"], payload.data.family_id);
Expand Down
18 changes: 12 additions & 6 deletions app/javascript/components/records/reducer.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,13 @@ describe("<RecordList /> - Reducers", () => {
});
const action = {
type: "TestRecordType/CREATE_CASE_FROM_FAMILY_MEMBER_SUCCESS",
payload: { data: { id: "f001", record: { id: "c001", case_id_display: "001", family_member_id: "m002" } } }
payload: {
data: {
id: "f001",
family_members: [{ unique_id: "m002", case_id: "c001", case_id_display: "001" }],
record: { id: "c001", case_id_display: "001", family_member_id: "m002" }
}
}
};

const newState = nsReducer(initialState, action);
Expand Down Expand Up @@ -787,14 +793,14 @@ describe("<RecordList /> - Reducers", () => {
family_id: "f001",
family_number: "fn001",
family_member_id: "fd001",
family_details_section: [
{ unique_id: "fd001", relation_sex: "female", case_id: "c002", case_id_display: "002" },
{ unique_id: "fd002", relation_sex: "male", case_id: "c001", case_id_display: "001" }
],
record: {
id: "c002",
case_id_display: "002",
family_member_id: "fd002",
family_details_section: [
{ unique_id: "fd001", relation_sex: "female", case_id: "c002", case_id_display: "002" },
{ unique_id: "fd002", relation_sex: "male", case_id: "c001", case_id_display: "001" }
]
family_member_id: "fd002"
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions app/views/api/v2/children/family.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ json.data do
json.family_id @current_record.family_id if @selected_field_names.include?('family_id')
json.family_number @current_record.family_number if @selected_field_names.include?('family_number')
json.family_member_id @current_record.family_member_id if @selected_field_names.include?('family_member_id')

if @selected_field_names.include?('family_details_section')
json.family_details_section @current_record.family_members_details
end

json.record do
json.partial! 'api/v2/records/record',
record: @record,
selected_field_names: @updated_field_names
json.id @record.id
json.case_id_display @record.case_id_display
json.family_member_id @record.family_member_id if @selected_field_names.include?('family_member_id')
end
end
1 change: 1 addition & 0 deletions app/views/api/v2/families/create_case.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

json.data do
json.id @current_record.id
json.family_members @current_record.family_members if @selected_field_names.include?('family_members')
json.record do
json.id @record.id
json.case_id_display @record.case_id_display
Expand Down
24 changes: 18 additions & 6 deletions spec/requests/api/v2/children_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1121,8 +1121,14 @@
expect(json['data']).to have_key('family_id')
expect(json['data']).to have_key('family_number')
expect(json['data']).to have_key('family_member_id')
expect(json['data']['record']['sex']).to eq('male')
expect(json['data']['record']['age']).to eq(5)
expect(json['data']['family_details_section'][0]['unique_id']).to eq(@member_unique_id3)
expect(json['data']['family_details_section'][0]['relation_sex']).to eq('male')
expect(json['data']['family_details_section'][0]['relation_age']).to eq(5)
expect(json['data']['family_details_section'][0]['case_id']).not_to be_nil
expect(json['data']['family_details_section'][0]['case_id_display']).not_to be_nil
expect(json['data']['record']['id']).not_to be_nil
expect(json['data']['record']['case_id_display']).not_to be_nil
expect(json['data']['record']['family_member_id']).to eq(@member_unique_id3)
end

it 'creates a new child linked to a family when there is a family record' do
Expand All @@ -1137,9 +1143,16 @@
expect(json['data']['family_id']).to eq(@family2.id)
expect(json['data']['family_number']).to eq(@family2.family_number)
expect(json['data']['family_member_id']).to eq(@member_unique_id2)
expect(json['data']['family_details_section'].size).to eq(2)
expect(json['data']['family_details_section'][1]['unique_id']).to eq(@member_unique_id5)
expect(json['data']['family_details_section'][1]['relation_name']).to eq('Test8 - Member3')
expect(json['data']['family_details_section'][1]['relation_sex']).to eq('male')
expect(json['data']['family_details_section'][1]['relation_age']).to eq(4)
expect(json['data']['family_details_section'][1]['case_id']).not_to be_nil
expect(json['data']['family_details_section'][1]['case_id_display']).not_to be_nil
expect(json['data']['record']['id']).not_to be_nil
expect(json['data']['record']['sex']).to eq('male')
expect(json['data']['record']['age']).to eq(4)
expect(json['data']['record']['case_id_display']).not_to be_nil
expect(json['data']['record']['family_member_id']).to eq(@member_unique_id5)
end

it 'creates a new child and returns the family data if a user has the view_family_record permission' do
Expand All @@ -1162,8 +1175,7 @@
expect(json['data']['family_number']).to eq(@family2.family_number)
expect(json['data']['family_member_id']).to eq(@member_unique_id2)
expect(json['data']['record']['id']).not_to be_nil
expect(json['data']['record']['sex']).to eq('male')
expect(json['data']['record']['age']).to eq(10)
expect(json['data']['record']['case_id_display']).not_to be_nil
expect(json['data']['record']['family_member_id']).to eq(@member_unique_id4)
end
end
Expand Down

0 comments on commit 80a5dc5

Please sign in to comment.