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

🐛 Business services: Fix create/edit when owner is included #1418

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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
2 changes: 1 addition & 1 deletion client/src/app/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
id: number;
name: string;
description?: string;
owner?: Stakeholder;
owner?: Ref;
}

export interface Stakeholder {
Expand Down Expand Up @@ -205,7 +205,7 @@
identity?: Ref;
createTime?: string;
createUser?: string;
id: any;

Check warning on line 208 in client/src/app/api/models.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.x)

Unexpected any. Specify a different type
enabled: boolean;
}

Expand Down Expand Up @@ -362,7 +362,7 @@

export interface TaskgroupTask {
name: string;
data: any;

Check warning on line 365 in client/src/app/api/models.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.x)

Unexpected any. Specify a different type
application: Ref;
}

Expand Down
32 changes: 16 additions & 16 deletions client/src/app/api/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@

type Direction = "asc" | "desc";

const buildQuery = (params: any) => {

Check warning on line 120 in client/src/app/api/rest.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.x)

'buildQuery' is assigned a value but never used

Check warning on line 120 in client/src/app/api/rest.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.x)

Unexpected any. Specify a different type
const query: string[] = [];

Object.keys(params).forEach((key) => {
const value = (params as any)[key];

Check warning on line 124 in client/src/app/api/rest.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.x)

Unexpected any. Specify a different type

if (value !== undefined && value !== null) {
let queryParamValues: string[] = [];
Expand Down Expand Up @@ -618,27 +618,27 @@
): Promise<StakeholderGroup> =>
axios.put(`${STAKEHOLDER_GROUPS}/${obj.id}`, obj);

// ---------------------------------------
// Business services
//
export const getBusinessServices = () =>
axios
.get<BusinessService[]>(BUSINESS_SERVICES)
.then((response) => response.data);

export const getBusinessServices = (): Promise<BusinessService[]> =>
axios.get(BUSINESS_SERVICES).then((response) => response.data);

export const deleteBusinessService = (
id: number | string
): Promise<BusinessService> => axios.delete(`${BUSINESS_SERVICES}/${id}`);
export const getBusinessServiceById = (id: number | string) =>
axios

Check warning on line 630 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L630

Added line #L630 was not covered by tests
.get<BusinessService>(`${BUSINESS_SERVICES}/${id}`)
.then((response) => response.data);

Check warning on line 632 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L632

Added line #L632 was not covered by tests

export const createBusinessService = (
obj: New<BusinessService>
): Promise<BusinessService> => axios.post(BUSINESS_SERVICES, obj);
export const createBusinessService = (obj: New<BusinessService>) =>
axios.post<BusinessService>(BUSINESS_SERVICES, obj);

Check warning on line 635 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L635

Added line #L635 was not covered by tests

export const updateBusinessService = (
obj: BusinessService
): Promise<BusinessService> => axios.put(`${BUSINESS_SERVICES}/${obj.id}`, obj);
export const updateBusinessService = (obj: BusinessService) =>
axios.put<void>(`${BUSINESS_SERVICES}/${obj.id}`, obj);

Check warning on line 638 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L638

Added line #L638 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the void callout here. Not sure why we were pretending to get something useful back before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be revisiting all of the rest functions soon in a similar way.


export const getBusinessServiceById = (
id: number | string
): Promise<BusinessService> =>
axios.get(`${BUSINESS_SERVICES}/${id}`).then((response) => response.data);
export const deleteBusinessService = (id: number | string) =>
axios.delete<void>(`${BUSINESS_SERVICES}/${id}`);

Check warning on line 641 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L641

Added line #L641 was not covered by tests

// Job functions

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { useTranslation } from "react-i18next";
import { AxiosError, AxiosResponse } from "axios";
import { AxiosError } from "axios";
import { object, string } from "yup";

import {
Expand All @@ -27,6 +27,7 @@ import {
} from "@app/components/HookFormPFFields";
import { OptionWithValue, SimpleSelect } from "@app/components/SimpleSelect";
import { NotificationsContext } from "@app/components/NotificationsContext";
import { matchItemsToRef } from "@app/utils/model-utils";

export interface FormValues {
name: string;
Expand All @@ -44,10 +45,16 @@ export const BusinessServiceForm: React.FC<BusinessServiceFormProps> = ({
onClose,
}) => {
const { t } = useTranslation();
const { pushNotification } = React.useContext(NotificationsContext);

const { businessServices } = useFetchBusinessServices();
const { stakeholders } = useFetchStakeholders();
const {
businessServices,
stakeholders,
stakeholderToRef,
createBusinessService,
updateBusinessService,
} = useBusinessServiceFormData({
onActionSuccess: onClose,
});

const stakeholdersOptions = stakeholders.map((stakeholder) => {
return {
Expand Down Expand Up @@ -92,73 +99,18 @@ export const BusinessServiceForm: React.FC<BusinessServiceFormProps> = ({
mode: "all",
});

const onCreateBusinessServiceSuccess = (
response: AxiosResponse<BusinessService>
) => {
pushNotification({
title: t("toastr.success.createWhat", {
type: t("terms.businessService"),
what: response.data.name,
}),
variant: "success",
});
onClose();
};

const onUpdateBusinessServiceSuccess = () => {
pushNotification({
title: t("toastr.success.save", {
type: t("terms.businessService"),
}),
variant: "success",
});
onClose();
};

const onCreateBusinessServiceError = (error: AxiosError) => {
pushNotification({
title: t("toastr.fail.create", {
type: t("terms.businessService").toLowerCase(),
}),
variant: "danger",
});
};

const { mutate: createBusinessService } = useCreateBusinessServiceMutation(
onCreateBusinessServiceSuccess,
onCreateBusinessServiceError
);

const onUpdateBusinessServiceError = (error: AxiosError) => {
pushNotification({
title: t("toastr.fail.save", {
type: t("terms.businessService").toLowerCase(),
}),
variant: "danger",
});
};

const { mutate: updateBusinessService } = useUpdateBusinessServiceMutation(
onUpdateBusinessServiceSuccess,
onUpdateBusinessServiceError
);

const onSubmit = (formValues: FormValues) => {
const matchingStakeholderRef = stakeholders.find(
(stakeholder) => stakeholder.name === formValues.owner
);
const payload: New<BusinessService> = {
name: formValues.name.trim(),
description: formValues.description.trim(),
owner: matchingStakeholderRef,
owner: stakeholderToRef(formValues.owner),
};

if (businessService) {
updateBusinessService({ id: businessService.id, ...payload });
} else {
createBusinessService(payload);
}
onClose();
};

return (
Expand Down Expand Up @@ -223,3 +175,91 @@ export const BusinessServiceForm: React.FC<BusinessServiceFormProps> = ({
</Form>
);
};

const useBusinessServiceFormData = ({
onActionSuccess = () => {},
onActionFail = () => {},
}: {
onActionSuccess?: () => void;
onActionFail?: () => void;
}) => {
const { t } = useTranslation();
const { pushNotification } = React.useContext(NotificationsContext);

// Fetch data
const { businessServices } = useFetchBusinessServices();
const { stakeholders } = useFetchStakeholders();

// Helpers
const stakeholderToRef = (name: string | undefined | null) =>
matchItemsToRef(stakeholders, (i) => i.name, name);

// Mutation notification handlers
const onCreateBusinessServiceSuccess = (data: BusinessService) => {
pushNotification({
title: t("toastr.success.createWhat", {
type: t("terms.businessService"),
what: data.name,
}),
variant: "success",
});
onActionSuccess();
};

const onCreateBusinessServiceError = (
_error: AxiosError,
_payload: New<BusinessService>
) => {
pushNotification({
title: t("toastr.fail.create", {
type: t("terms.businessService").toLowerCase(),
}),
variant: "danger",
});
onActionFail();
};

const onUpdateBusinessServiceSuccess = (payload: BusinessService) => {
pushNotification({
title: t("toastr.success.saveWhat", {
type: t("terms.businessService"),
what: payload.name,
}),
variant: "success",
});
onActionSuccess();
};

const onUpdateBusinessServiceError = (
_error: AxiosError,
_payload: New<BusinessService>
) => {
pushNotification({
title: t("toastr.fail.save", {
type: t("terms.businessService").toLowerCase(),
}),
variant: "danger",
});
onActionFail();
};

// Mutations
const { mutate: createBusinessService } = useCreateBusinessServiceMutation(
onCreateBusinessServiceSuccess,
onCreateBusinessServiceError
);

const { mutate: updateBusinessService } = useUpdateBusinessServiceMutation(
onUpdateBusinessServiceSuccess,
onUpdateBusinessServiceError
);

// Send back source data and action that are needed by the ApplicationForm
return {
businessServices,
stakeholders,
stakeholderToRef,
createBusinessService,
updateBusinessService,
};
};
32 changes: 17 additions & 15 deletions client/src/app/queries/businessservices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
getBusinessServices,
updateBusinessService,
} from "@app/api/rest";
import { BusinessService, New } from "@app/api/models";

export const BusinessServicesQueryKey = "businessservices";
export const BusinessServiceQueryKey = "businessservice";
Expand Down Expand Up @@ -40,49 +41,50 @@
};

export const useCreateBusinessServiceMutation = (
onSuccess: (res: any) => void,
onError: (err: AxiosError) => void
onSuccess: (res: BusinessService) => void,
onError: (err: AxiosError, payload: New<BusinessService>) => void
) => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: createBusinessService,
onSuccess: (res) => {
onSuccess(res);
onSuccess: ({ data }, _payload) => {
onSuccess(data);

Check warning on line 52 in client/src/app/queries/businessservices.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/businessservices.ts#L51-L52

Added lines #L51 - L52 were not covered by tests
queryClient.invalidateQueries([BusinessServicesQueryKey]);
},
onError,
});
};

export const useUpdateBusinessServiceMutation = (
onSuccess: () => void,
onError: (err: AxiosError) => void
onSuccess: (payload: BusinessService) => void,
onError: (err: AxiosError, payload: BusinessService) => void
) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: updateBusinessService,
onSuccess: () => {
onSuccess();
onSuccess: (_res, payload) => {
onSuccess(payload);

Check warning on line 67 in client/src/app/queries/businessservices.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/businessservices.ts#L66-L67

Added lines #L66 - L67 were not covered by tests
queryClient.invalidateQueries([BusinessServicesQueryKey]);
},
onError: onError,
});
};

export const useDeleteBusinessServiceMutation = (
onSuccess: (res: any) => void,
onError: (err: AxiosError) => void
onSuccess: (id: number | string) => void,
onError: (err: AxiosError, id: number | string) => void
) => {
const queryClient = useQueryClient();

const { isLoading, mutate, error } = useMutation(deleteBusinessService, {
onSuccess: (res) => {
onSuccess(res);
const { isLoading, mutate, error } = useMutation({

Check warning on line 80 in client/src/app/queries/businessservices.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/businessservices.ts#L80

Added line #L80 was not covered by tests
mutationFn: deleteBusinessService,
onSuccess: (_res, id) => {
onSuccess(id);

Check warning on line 83 in client/src/app/queries/businessservices.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/businessservices.ts#L82-L83

Added lines #L82 - L83 were not covered by tests
queryClient.invalidateQueries([BusinessServicesQueryKey]);
},
onError: (err: AxiosError) => {
onError(err);
onError: (err: AxiosError, id) => {
onError(err, id);

Check warning on line 87 in client/src/app/queries/businessservices.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/businessservices.ts#L86-L87

Added lines #L86 - L87 were not covered by tests
queryClient.invalidateQueries([BusinessServicesQueryKey]);
},
});
Expand Down
Loading