Skip to content

Commit

Permalink
Update location inventory payload
Browse files Browse the repository at this point in the history
  • Loading branch information
ciremusyoka committed Mar 19, 2024
1 parent 3cedbaa commit 4608f6a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Form, Button, Input, DatePicker, Space, Switch, InputNumber } from 'antd';
import { Form, Button, Input, DatePicker, Space, Switch } from 'antd';
import { SelectProps } from 'antd/lib/select';
import { AsyncSelectProps, formItemLayout, tailLayout } from '@opensrp/react-utils';
import { useTranslation } from '../../mls';
Expand Down Expand Up @@ -78,6 +78,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
const { mutate, isLoading } = useMutation(
(values: GroupFormFields) => {
const payload = getLocationInventoryPayload(values);
console.log('payload: ', payload);

Check warning on line 81 in packages/fhir-group-management/src/components/LocationInventory/form.tsx

View workflow job for this annotation

GitHub Actions / test (16.17.0, ubuntu-latest)

Unexpected console statement
return postLocationInventory(fhirBaseURL, values);
},
{
Expand Down Expand Up @@ -197,7 +198,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
name={serialNumber}
label={t('Serial number')}
>
<InputNumber placeholder={t('Serial number')} style={{ width: '100%' }} />
<Input placeholder={t('Serial number')} />
</FormItem>

<AsyncSelect {...donorSelectProps} />
Expand All @@ -208,7 +209,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
name={PONumber}
label={t('PO number')}
>
<InputNumber placeholder={t('PO number')} style={{ width: '100%' }} />
<Input placeholder={t('PO number')} />
</FormItem>

{/* start hidden fields */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export interface CommonGroupFormFields {
[deliveryDate]: Date;
[accountabilityEndDate]: Date;
[expiryDate]: Date;
[serialNumber]: number;
[PONumber]: number;
[donor]: string;
[serialNumber]: string;
[PONumber]: string;
[donor]?: string;
[unicefSection]: string;
[product]: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,22 @@ import { GroupFormFields } from './types';
import { GroupMember } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/groupMember';
import { v4 } from 'uuid';
import { Dayjs } from 'dayjs';
import type { TFunction } from '@opensrp/i18n';
import { TFunction } from '@opensrp/i18n';
import { Rule } from 'rc-field-form/lib/interface';
import { attractiveCharacteristicCode } from '../../helpers/utils';
import { GroupCharacteristic } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/groupCharacteristic';
import { Identifier } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/identifier';
import { ListEntry } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/listEntry';

const codeSystem = 'http://smartregister.org/codes';
const unicefCharacteristicCode = '98734231';
const donorCharacteristicCode = '45981276';
const quantityCharacteristicCode = '33467722';
const supplyInventoryCode = '78991122';
const poNumberDisplay = 'PO Number';
const poNumberCode = 'PONUM';
const serialNumberDisplay = 'Serial Number';
const serialNumberCode = 'SERNUM';

/**
* Check if date in past
Expand Down Expand Up @@ -83,10 +97,15 @@ export function getValuesetSelectOptions<TData extends IValueSet>(data: TData) {
*/
export const projectOptions = (data: IBundle) => {
const productsList = getResourcesFromBundle<IGroup>(data);
const options: DefaultOptionType[] = productsList.map((prod: IGroup) => ({
value: prod.id,
label: prod.name,
}));
const options: DefaultOptionType[] = productsList.map((prod: IGroup) => {
const attractive = prod.characteristic?.some(
(char) => char.code.coding?.[0]?.code === attractiveCharacteristicCode
);
return {
value: JSON.stringify({ id: prod.id, attractive }),
label: prod.name,
};
});
return options;
};

Expand All @@ -99,14 +118,16 @@ export const projectOptions = (data: IBundle) => {
* @returns returns group member
*/
const getMember = (productId: string, startDate: Date, endDate: Date): GroupMember[] => {
const startDateToString = new Date(startDate).toISOString();
const endDateToString = new Date(endDate).toISOString();
return [
{
entity: {
reference: `Group/${productId}`,
},
period: {
start: startDate,
end: endDate,
start: startDateToString as any,

Check warning on line 129 in packages/fhir-group-management/src/components/LocationInventory/utils.tsx

View workflow job for this annotation

GitHub Actions / test (16.17.0, ubuntu-latest)

Unexpected any. Specify a different type
end: endDateToString as any,

Check warning on line 130 in packages/fhir-group-management/src/components/LocationInventory/utils.tsx

View workflow job for this annotation

GitHub Actions / test (16.17.0, ubuntu-latest)

Unexpected any. Specify a different type
},
inactive: false,
},
Expand All @@ -118,16 +139,21 @@ const getMember = (productId: string, startDate: Date, endDate: Date): GroupMemb
*
* @param unicefSection - selected unicef section
* @param donor - selected donor
* @param quantity - product quantity
* @returns returns characteristivs
*/
const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSetConcept) => {
return [
const generateCharacteristics = (
unicefSection: ValueSetConcept,
donor?: ValueSetConcept,
quantity?: number
): GroupCharacteristic[] => {
const characteristics: GroupCharacteristic[] = [
{
code: {
coding: [
{
system: 'http://smartregister.org/',
code: '98734231',
system: codeSystem,
code: unicefCharacteristicCode,
display: 'Unicef Section',
},
],
Expand All @@ -137,12 +163,14 @@ const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSet
text: unicefSection.display,
},
},
{
];
if (donor) {
characteristics.push({
code: {
coding: [
{
system: 'http://smartregister.org/',
code: '45647484',
system: codeSystem,
code: donorCharacteristicCode,
display: 'Donor',
},
],
Expand All @@ -151,7 +179,63 @@ const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSet
coding: [donor],
text: donor.display,
},
});
}
if (quantity) {
characteristics.push({
code: {
coding: [
{
system: codeSystem,
code: quantityCharacteristicCode,
display: 'Quantity ',
},
],
},
valueQuantity: { value: quantity },
});
}
return characteristics;
};

/**
* get identifier data for group resource
*
* @param poId - Po number
* @param serialId - serial number
* @returns returns group identifier
*/
const generateIdentifier = (poId: string, serialId: string): Identifier[] => {
return [
{
use: IdentifierUseCodes.SECONDARY,
type: {
coding: [
{
system: codeSystem,
code: poNumberCode,
display: poNumberDisplay,
},
],
text: poNumberDisplay,
},
value: poId,
},
{
use: IdentifierUseCodes.OFFICIAL,
type: {
coding: [
{
system: codeSystem,
code: serialNumberCode,
display: serialNumberDisplay,
},
],
text: serialNumberDisplay,
},
value: serialId,
},
{ use: IdentifierUseCodes.USUAL, value: 'a065c211-cf3e-4b5b-972f-fdac0e45fef7' },
];
};

Expand All @@ -162,13 +246,24 @@ const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSet
* @returns returns group resource payload
*/
export const getLocationInventoryPayload = (values: GroupFormFields): IGroup => {
const donor = JSON.parse(values.donor);
const donor = values.donor ? JSON.parse(values.donor) : values.donor;
const unicefSection = JSON.parse(values.unicefSection);
const product = JSON.parse(values.product);
const payload: IGroup = {
resourceType: groupResourceType,
id: values.id || v4(),
member: getMember(values.product, values.deliveryDate, values.accountabilityEndDate),
identifier: generateIdentifier(values.poNumber, values.serialNumber),
member: getMember(product.id, values.deliveryDate, values.accountabilityEndDate),
characteristic: generateCharacteristics(unicefSection, donor),
code: {
coding: [
{
system: codeSystem,
code: supplyInventoryCode,
display: 'Supply Inventory',
},
],
},
};
if (values.active) payload.active = values.active;
if (values.actual) payload.actual = values.actual;
Expand Down Expand Up @@ -199,13 +294,25 @@ export async function getOrCreateList(baseUrl: string, listId: string) {
const serve = new FHIRServiceClass<IList>(baseUrl, listResourceType);
return serve.read(listId).catch((err) => {
if (err.statusCode === 404) {
const listResource = createSupplyManagementList(listId);
return serve.update(listResource);
return createList(baseUrl, listId);
}
throw err;
});
}


Check failure on line 303 in packages/fhir-group-management/src/components/LocationInventory/utils.tsx

View workflow job for this annotation

GitHub Actions / test (16.17.0, ubuntu-latest)

Delete `⏎`
/**
* Gets list resource for given id, create it if it does not exist
*
* @param baseUrl - api base url
* @param listId - list id
*/
export async function createList(baseUrl: string, listId: string) {
const serve = new FHIRServiceClass<IList>(baseUrl, listResourceType);
const listResource = createSupplyManagementList(listId);
return serve.update(listResource);
}

/**
* @param baseUrl - the api base url
* @param listId - list resource id to add the group to
Expand Down Expand Up @@ -237,7 +344,7 @@ export const updateListReferencesFactory =
*
* @param id - externally defined id that will be the id of the new list resource
*/
export function createSupplyManagementList(id: string): IList {
export function createSupplyManagementList(id: string, entries?:ListEntry[]): IList {

Check failure on line 347 in packages/fhir-group-management/src/components/LocationInventory/utils.tsx

View workflow job for this annotation

GitHub Actions / test (16.17.0, ubuntu-latest)

Insert `·`
return {
resourceType: listResourceType,
id: id,
Expand All @@ -260,7 +367,7 @@ export function createSupplyManagementList(id: string): IList {
],
text: 'Supply Chain Commodity',
},
entry: [],
entry: entries || [],
};
}

Expand Down

0 comments on commit 4608f6a

Please sign in to comment.