Skip to content

Commit 4608f6a

Browse files
committed
Update location inventory payload
1 parent 3cedbaa commit 4608f6a

File tree

3 files changed

+134
-26
lines changed

3 files changed

+134
-26
lines changed

packages/fhir-group-management/src/components/LocationInventory/form.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Form, Button, Input, DatePicker, Space, Switch, InputNumber } from 'antd';
2+
import { Form, Button, Input, DatePicker, Space, Switch } from 'antd';
33
import { SelectProps } from 'antd/lib/select';
44
import { AsyncSelectProps, formItemLayout, tailLayout } from '@opensrp/react-utils';
55
import { useTranslation } from '../../mls';
@@ -78,6 +78,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
7878
const { mutate, isLoading } = useMutation(
7979
(values: GroupFormFields) => {
8080
const payload = getLocationInventoryPayload(values);
81+
console.log('payload: ', payload);
8182
return postLocationInventory(fhirBaseURL, values);
8283
},
8384
{
@@ -197,7 +198,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
197198
name={serialNumber}
198199
label={t('Serial number')}
199200
>
200-
<InputNumber placeholder={t('Serial number')} style={{ width: '100%' }} />
201+
<Input placeholder={t('Serial number')} />
201202
</FormItem>
202203

203204
<AsyncSelect {...donorSelectProps} />
@@ -208,7 +209,7 @@ const AddLocationInventoryForm = (props: LocationInventoryFormProps) => {
208209
name={PONumber}
209210
label={t('PO number')}
210211
>
211-
<InputNumber placeholder={t('PO number')} style={{ width: '100%' }} />
212+
<Input placeholder={t('PO number')} />
212213
</FormItem>
213214

214215
{/* start hidden fields */}

packages/fhir-group-management/src/components/LocationInventory/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export interface CommonGroupFormFields {
2929
[deliveryDate]: Date;
3030
[accountabilityEndDate]: Date;
3131
[expiryDate]: Date;
32-
[serialNumber]: number;
33-
[PONumber]: number;
34-
[donor]: string;
32+
[serialNumber]: string;
33+
[PONumber]: string;
34+
[donor]?: string;
3535
[unicefSection]: string;
3636
[product]: string;
3737
}

packages/fhir-group-management/src/components/LocationInventory/utils.tsx

+127-20
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,22 @@ import { GroupFormFields } from './types';
2222
import { GroupMember } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/groupMember';
2323
import { v4 } from 'uuid';
2424
import { Dayjs } from 'dayjs';
25-
import type { TFunction } from '@opensrp/i18n';
25+
import { TFunction } from '@opensrp/i18n';
2626
import { Rule } from 'rc-field-form/lib/interface';
27+
import { attractiveCharacteristicCode } from '../../helpers/utils';
28+
import { GroupCharacteristic } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/groupCharacteristic';
29+
import { Identifier } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/identifier';
30+
import { ListEntry } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/listEntry';
31+
32+
const codeSystem = 'http://smartregister.org/codes';
33+
const unicefCharacteristicCode = '98734231';
34+
const donorCharacteristicCode = '45981276';
35+
const quantityCharacteristicCode = '33467722';
36+
const supplyInventoryCode = '78991122';
37+
const poNumberDisplay = 'PO Number';
38+
const poNumberCode = 'PONUM';
39+
const serialNumberDisplay = 'Serial Number';
40+
const serialNumberCode = 'SERNUM';
2741

2842
/**
2943
* Check if date in past
@@ -83,10 +97,15 @@ export function getValuesetSelectOptions<TData extends IValueSet>(data: TData) {
8397
*/
8498
export const projectOptions = (data: IBundle) => {
8599
const productsList = getResourcesFromBundle<IGroup>(data);
86-
const options: DefaultOptionType[] = productsList.map((prod: IGroup) => ({
87-
value: prod.id,
88-
label: prod.name,
89-
}));
100+
const options: DefaultOptionType[] = productsList.map((prod: IGroup) => {
101+
const attractive = prod.characteristic?.some(
102+
(char) => char.code.coding?.[0]?.code === attractiveCharacteristicCode
103+
);
104+
return {
105+
value: JSON.stringify({ id: prod.id, attractive }),
106+
label: prod.name,
107+
};
108+
});
90109
return options;
91110
};
92111

@@ -99,14 +118,16 @@ export const projectOptions = (data: IBundle) => {
99118
* @returns returns group member
100119
*/
101120
const getMember = (productId: string, startDate: Date, endDate: Date): GroupMember[] => {
121+
const startDateToString = new Date(startDate).toISOString();
122+
const endDateToString = new Date(endDate).toISOString();
102123
return [
103124
{
104125
entity: {
105126
reference: `Group/${productId}`,
106127
},
107128
period: {
108-
start: startDate,
109-
end: endDate,
129+
start: startDateToString as any,
130+
end: endDateToString as any,
110131
},
111132
inactive: false,
112133
},
@@ -118,16 +139,21 @@ const getMember = (productId: string, startDate: Date, endDate: Date): GroupMemb
118139
*
119140
* @param unicefSection - selected unicef section
120141
* @param donor - selected donor
142+
* @param quantity - product quantity
121143
* @returns returns characteristivs
122144
*/
123-
const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSetConcept) => {
124-
return [
145+
const generateCharacteristics = (
146+
unicefSection: ValueSetConcept,
147+
donor?: ValueSetConcept,
148+
quantity?: number
149+
): GroupCharacteristic[] => {
150+
const characteristics: GroupCharacteristic[] = [
125151
{
126152
code: {
127153
coding: [
128154
{
129-
system: 'http://smartregister.org/',
130-
code: '98734231',
155+
system: codeSystem,
156+
code: unicefCharacteristicCode,
131157
display: 'Unicef Section',
132158
},
133159
],
@@ -137,12 +163,14 @@ const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSet
137163
text: unicefSection.display,
138164
},
139165
},
140-
{
166+
];
167+
if (donor) {
168+
characteristics.push({
141169
code: {
142170
coding: [
143171
{
144-
system: 'http://smartregister.org/',
145-
code: '45647484',
172+
system: codeSystem,
173+
code: donorCharacteristicCode,
146174
display: 'Donor',
147175
},
148176
],
@@ -151,7 +179,63 @@ const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSet
151179
coding: [donor],
152180
text: donor.display,
153181
},
182+
});
183+
}
184+
if (quantity) {
185+
characteristics.push({
186+
code: {
187+
coding: [
188+
{
189+
system: codeSystem,
190+
code: quantityCharacteristicCode,
191+
display: 'Quantity ',
192+
},
193+
],
194+
},
195+
valueQuantity: { value: quantity },
196+
});
197+
}
198+
return characteristics;
199+
};
200+
201+
/**
202+
* get identifier data for group resource
203+
*
204+
* @param poId - Po number
205+
* @param serialId - serial number
206+
* @returns returns group identifier
207+
*/
208+
const generateIdentifier = (poId: string, serialId: string): Identifier[] => {
209+
return [
210+
{
211+
use: IdentifierUseCodes.SECONDARY,
212+
type: {
213+
coding: [
214+
{
215+
system: codeSystem,
216+
code: poNumberCode,
217+
display: poNumberDisplay,
218+
},
219+
],
220+
text: poNumberDisplay,
221+
},
222+
value: poId,
223+
},
224+
{
225+
use: IdentifierUseCodes.OFFICIAL,
226+
type: {
227+
coding: [
228+
{
229+
system: codeSystem,
230+
code: serialNumberCode,
231+
display: serialNumberDisplay,
232+
},
233+
],
234+
text: serialNumberDisplay,
235+
},
236+
value: serialId,
154237
},
238+
{ use: IdentifierUseCodes.USUAL, value: 'a065c211-cf3e-4b5b-972f-fdac0e45fef7' },
155239
];
156240
};
157241

@@ -162,13 +246,24 @@ const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSet
162246
* @returns returns group resource payload
163247
*/
164248
export const getLocationInventoryPayload = (values: GroupFormFields): IGroup => {
165-
const donor = JSON.parse(values.donor);
249+
const donor = values.donor ? JSON.parse(values.donor) : values.donor;
166250
const unicefSection = JSON.parse(values.unicefSection);
251+
const product = JSON.parse(values.product);
167252
const payload: IGroup = {
168253
resourceType: groupResourceType,
169254
id: values.id || v4(),
170-
member: getMember(values.product, values.deliveryDate, values.accountabilityEndDate),
255+
identifier: generateIdentifier(values.poNumber, values.serialNumber),
256+
member: getMember(product.id, values.deliveryDate, values.accountabilityEndDate),
171257
characteristic: generateCharacteristics(unicefSection, donor),
258+
code: {
259+
coding: [
260+
{
261+
system: codeSystem,
262+
code: supplyInventoryCode,
263+
display: 'Supply Inventory',
264+
},
265+
],
266+
},
172267
};
173268
if (values.active) payload.active = values.active;
174269
if (values.actual) payload.actual = values.actual;
@@ -199,13 +294,25 @@ export async function getOrCreateList(baseUrl: string, listId: string) {
199294
const serve = new FHIRServiceClass<IList>(baseUrl, listResourceType);
200295
return serve.read(listId).catch((err) => {
201296
if (err.statusCode === 404) {
202-
const listResource = createSupplyManagementList(listId);
203-
return serve.update(listResource);
297+
return createList(baseUrl, listId);
204298
}
205299
throw err;
206300
});
207301
}
208302

303+
304+
/**
305+
* Gets list resource for given id, create it if it does not exist
306+
*
307+
* @param baseUrl - api base url
308+
* @param listId - list id
309+
*/
310+
export async function createList(baseUrl: string, listId: string) {
311+
const serve = new FHIRServiceClass<IList>(baseUrl, listResourceType);
312+
const listResource = createSupplyManagementList(listId);
313+
return serve.update(listResource);
314+
}
315+
209316
/**
210317
* @param baseUrl - the api base url
211318
* @param listId - list resource id to add the group to
@@ -237,7 +344,7 @@ export const updateListReferencesFactory =
237344
*
238345
* @param id - externally defined id that will be the id of the new list resource
239346
*/
240-
export function createSupplyManagementList(id: string): IList {
347+
export function createSupplyManagementList(id: string, entries?:ListEntry[]): IList {
241348
return {
242349
resourceType: listResourceType,
243350
id: id,
@@ -260,7 +367,7 @@ export function createSupplyManagementList(id: string): IList {
260367
],
261368
text: 'Supply Chain Commodity',
262369
},
263-
entry: [],
370+
entry: entries || [],
264371
};
265372
}
266373

0 commit comments

Comments
 (0)