@@ -22,8 +22,22 @@ import { GroupFormFields } from './types';
22
22
import { GroupMember } from '@smile-cdr/fhirts/dist/FHIR-R4/classes/groupMember' ;
23
23
import { v4 } from 'uuid' ;
24
24
import { Dayjs } from 'dayjs' ;
25
- import type { TFunction } from '@opensrp/i18n' ;
25
+ import { TFunction } from '@opensrp/i18n' ;
26
26
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' ;
27
41
28
42
/**
29
43
* Check if date in past
@@ -83,10 +97,15 @@ export function getValuesetSelectOptions<TData extends IValueSet>(data: TData) {
83
97
*/
84
98
export const projectOptions = ( data : IBundle ) => {
85
99
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
+ } ) ;
90
109
return options ;
91
110
} ;
92
111
@@ -99,14 +118,16 @@ export const projectOptions = (data: IBundle) => {
99
118
* @returns returns group member
100
119
*/
101
120
const getMember = ( productId : string , startDate : Date , endDate : Date ) : GroupMember [ ] => {
121
+ const startDateToString = new Date ( startDate ) . toISOString ( ) ;
122
+ const endDateToString = new Date ( endDate ) . toISOString ( ) ;
102
123
return [
103
124
{
104
125
entity : {
105
126
reference : `Group/${ productId } ` ,
106
127
} ,
107
128
period : {
108
- start : startDate ,
109
- end : endDate ,
129
+ start : startDateToString as any ,
130
+ end : endDateToString as any ,
110
131
} ,
111
132
inactive : false ,
112
133
} ,
@@ -118,16 +139,21 @@ const getMember = (productId: string, startDate: Date, endDate: Date): GroupMemb
118
139
*
119
140
* @param unicefSection - selected unicef section
120
141
* @param donor - selected donor
142
+ * @param quantity - product quantity
121
143
* @returns returns characteristivs
122
144
*/
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 [ ] = [
125
151
{
126
152
code : {
127
153
coding : [
128
154
{
129
- system : 'http://smartregister.org/' ,
130
- code : '98734231' ,
155
+ system : codeSystem ,
156
+ code : unicefCharacteristicCode ,
131
157
display : 'Unicef Section' ,
132
158
} ,
133
159
] ,
@@ -137,12 +163,14 @@ const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSet
137
163
text : unicefSection . display ,
138
164
} ,
139
165
} ,
140
- {
166
+ ] ;
167
+ if ( donor ) {
168
+ characteristics . push ( {
141
169
code : {
142
170
coding : [
143
171
{
144
- system : 'http://smartregister.org/' ,
145
- code : '45647484' ,
172
+ system : codeSystem ,
173
+ code : donorCharacteristicCode ,
146
174
display : 'Donor' ,
147
175
} ,
148
176
] ,
@@ -151,7 +179,63 @@ const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSet
151
179
coding : [ donor ] ,
152
180
text : donor . display ,
153
181
} ,
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 ,
154
237
} ,
238
+ { use : IdentifierUseCodes . USUAL , value : 'a065c211-cf3e-4b5b-972f-fdac0e45fef7' } ,
155
239
] ;
156
240
} ;
157
241
@@ -162,13 +246,24 @@ const generateCharacteristics = (unicefSection: ValueSetConcept, donor: ValueSet
162
246
* @returns returns group resource payload
163
247
*/
164
248
export const getLocationInventoryPayload = ( values : GroupFormFields ) : IGroup => {
165
- const donor = JSON . parse ( values . donor ) ;
249
+ const donor = values . donor ? JSON . parse ( values . donor ) : values . donor ;
166
250
const unicefSection = JSON . parse ( values . unicefSection ) ;
251
+ const product = JSON . parse ( values . product ) ;
167
252
const payload : IGroup = {
168
253
resourceType : groupResourceType ,
169
254
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 ) ,
171
257
characteristic : generateCharacteristics ( unicefSection , donor ) ,
258
+ code : {
259
+ coding : [
260
+ {
261
+ system : codeSystem ,
262
+ code : supplyInventoryCode ,
263
+ display : 'Supply Inventory' ,
264
+ } ,
265
+ ] ,
266
+ } ,
172
267
} ;
173
268
if ( values . active ) payload . active = values . active ;
174
269
if ( values . actual ) payload . actual = values . actual ;
@@ -199,13 +294,25 @@ export async function getOrCreateList(baseUrl: string, listId: string) {
199
294
const serve = new FHIRServiceClass < IList > ( baseUrl , listResourceType ) ;
200
295
return serve . read ( listId ) . catch ( ( err ) => {
201
296
if ( err . statusCode === 404 ) {
202
- const listResource = createSupplyManagementList ( listId ) ;
203
- return serve . update ( listResource ) ;
297
+ return createList ( baseUrl , listId ) ;
204
298
}
205
299
throw err ;
206
300
} ) ;
207
301
}
208
302
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
+
209
316
/**
210
317
* @param baseUrl - the api base url
211
318
* @param listId - list resource id to add the group to
@@ -237,7 +344,7 @@ export const updateListReferencesFactory =
237
344
*
238
345
* @param id - externally defined id that will be the id of the new list resource
239
346
*/
240
- export function createSupplyManagementList ( id : string ) : IList {
347
+ export function createSupplyManagementList ( id : string , entries ?: ListEntry [ ] ) : IList {
241
348
return {
242
349
resourceType : listResourceType ,
243
350
id : id ,
@@ -260,7 +367,7 @@ export function createSupplyManagementList(id: string): IList {
260
367
] ,
261
368
text : 'Supply Chain Commodity' ,
262
369
} ,
263
- entry : [ ] ,
370
+ entry : entries || [ ] ,
264
371
} ;
265
372
}
266
373
0 commit comments