Skip to content

Commit

Permalink
fix: fix mutations after version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
DasProffi committed Sep 23, 2024
1 parent e0957a8 commit 60d21ad
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 181 deletions.
73 changes: 44 additions & 29 deletions api-services/mutations/properties/property_mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,30 @@ export const usePropertyListQuery = (subjectType?: SubjectType) => {
}
const result = await APIServices.property.getPropertiesBySubjectType(req, getAuthenticatedGrpcMetadata())
return result.getPropertiesList().filter(value => value.getFieldType() !== FieldType.FIELD_TYPE_UNSPECIFIED).map(property => {
const fieldType = GRPCConverter.fieldTypeMapperFromGRPC(property.getFieldType())
const selectData = property.getSelectData()
if (!selectData) {
const mustHaveSelectData = fieldType === 'singleSelect' || fieldType === 'multiSelect'
if (!selectData && mustHaveSelectData) {
throw Error('usePropertyListQuery could not find selectData')
}
return {
id: property.getId(),
name: property.getName(),
description: property.getDescription(),
subjectType: GRPCConverter.subjectTypeMapperFromGRPC(property.getSubjectType()),
fieldType: GRPCConverter.fieldTypeMapperFromGRPC(property.getFieldType()),
fieldType,
isArchived: property.getIsArchived(),
setId: property.getSetId(),
alwaysIncludeForViewSource: undefined,
selectData: {
isAllowingFreetext: selectData.getAllowFreetext(),
options: selectData.getOptionsList().map(option => ({
selectData: mustHaveSelectData ? {
isAllowingFreetext: selectData!.getAllowFreetext(),
options: selectData!.getOptionsList().map(option => ({
id: option.getId(),
name: option.getName(),
description: option.getDescription(),
isCustom: option.getIsCustom()
}))
}
} : undefined
}
})
},
Expand Down Expand Up @@ -105,23 +107,31 @@ export const usePropertyCreateMutation = (callback: (property: Property) => void
if (property.setId) {
req.setSetId(property.setId)
}
const selectDataVal = new CreatePropertyRequest.SelectData()
selectDataVal.setAllowFreetext(property.selectData.isAllowingFreetext)
selectDataVal.setOptionsList(property.selectData.options.map(option => {
const optionVal = new CreatePropertyRequest.SelectData.SelectOption()
optionVal.setName(option.name)
if (option.description) {
optionVal.setDescription(option.description)
if (property.fieldType === 'singleSelect' || property.fieldType === 'multiSelect') {
if (!property.selectData) {
throw Error('Select FieldType, but select data not set')
}
return optionVal
}))
req.setSelectData(selectDataVal)
const selectDataVal = new CreatePropertyRequest.SelectData()
selectDataVal.setAllowFreetext(property.selectData.isAllowingFreetext)
selectDataVal.setOptionsList(property.selectData.options.map(option => {
const optionVal = new CreatePropertyRequest.SelectData.SelectOption()
optionVal.setName(option.name)
if (option.description) {
optionVal.setDescription(option.description)
}
return optionVal
}))
req.setSelectData(selectDataVal)
}

const result = await APIServices.property.createProperty(req, getAuthenticatedGrpcMetadata())

const id = result.getPropertyId()

const newValue = { ...property, id }
const newValue = {
...property,
id
}
callback(newValue)
return newValue
},
Expand All @@ -146,19 +156,24 @@ export const usePropertyUpdateMutation = (callback: (property: Property) => void
if (property.setId) {
req.setSetId(property.setId)
}
const selectDataVal = new UpdatePropertyRequest.SelectData()
selectDataVal.setAllowFreetext(property.selectData.isAllowingFreetext)
/* This is done in a separate function
selectDataVal.set(properties.selectData.options.map(option => {
const optionVal = new UpdatePropertyRequest.SelectData.SelectOption()
optionVal.setName(option.name)
if (option.description) {
optionVal.setDescription(option.description)
if (property.fieldType === 'singleSelect' || property.fieldType === 'multiSelect') {
if (!property.selectData) {
throw Error('Select FieldType, but select data not set')
}
return optionVal
}))
*/
req.setSelectData(selectDataVal)
const selectDataVal = new UpdatePropertyRequest.SelectData()
selectDataVal.setAllowFreetext(property.selectData.isAllowingFreetext)
/* This is done in a separate function
selectDataVal.set(properties.selectData.options.map(option => {
const optionVal = new UpdatePropertyRequest.SelectData.SelectOption()
optionVal.setName(option.name)
if (option.description) {
optionVal.setDescription(option.description)
}
return optionVal
}))
*/
req.setSelectData(selectDataVal)
}

const result = await APIServices.property.updateProperty(req, getAuthenticatedGrpcMetadata())
if (!result.toObject()) {
Expand Down
100 changes: 79 additions & 21 deletions api-services/mutations/properties/property_value_mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import {
AttachPropertyValueRequest,
GetAttachedPropertyValuesRequest,
TaskPropertyMatcher,
PatientPropertyMatcher
PatientPropertyMatcher, GetAttachedPropertyValuesResponse
} from '@helpwave/proto-ts/services/property_svc/v1/property_value_svc_pb'
import {
Date as ProtoDate
} from '@helpwave/proto-ts/services/property_svc/v1/types_pb'
import { ArrayUtil } from '@helpwave/common/util/array'
import { APIServices } from '../../services'
import { getAuthenticatedGrpcMetadata } from '../../authentication/grpc_metadata'
import { QueryKeys } from '../query_keys'
import type { SubjectType } from '../../types/properties/property'
import type { SubjectType, FieldType } from '../../types/properties/property'
import type { AttachedProperty, DisplayableAttachedProperty } from '../../types/properties/attached_property'
import { GRPCConverter } from '../../util/util'
import type { Update } from '../../types/update'
import { emptyPropertyValue } from '../../types/properties/attached_property'

export const usePropertyWithValueListQuery = (subjectId: string | undefined, subjectType: SubjectType, wardId?: string) => {
return useQuery({
queryKey: [QueryKeys.properties, QueryKeys.attachedProperties, subjectId],
enabled: !!subjectId,
queryFn: async () => {
console.log('usePropertyWithValueListQuery', subjectId, subjectType)
if (!subjectId) {
return undefined
}
Expand All @@ -46,7 +48,9 @@ export const usePropertyWithValueListQuery = (subjectId: string | undefined, sub
const res = await APIServices.propertyValues.getAttachedPropertyValues(req, getAuthenticatedGrpcMetadata())

const results: DisplayableAttachedProperty[] = res.getValuesList().map(result => {
const dateValue: ProtoDate | undefined = result.getDateValue()
const selectValue = result.getSelectValue()
const valueCase = result.getValueCase()
const isValueNotSet = valueCase === GetAttachedPropertyValuesResponse.Value.ValueCase.VALUE_NOT_SET

return {
propertyId: result.getPropertyId(),
Expand All @@ -55,43 +59,97 @@ export const usePropertyWithValueListQuery = (subjectId: string | undefined, sub
name: result.getName(),
description: result.getDescription(),
fieldType: GRPCConverter.fieldTypeMapperFromGRPC(result.getFieldType()),
value: {
boolValue: result.getBoolValue() ?? false,
dateValue: dateValue?.getDate() ? dateValue.getDate()!.toDate() : new Date(),
numberValue: result.getNumberValue() ?? 0,
singleSelectValue: result.getSelectValue() ?? '',
dateTimeValue: result.getDateTimeValue()?.toDate() ?? new Date(),
textValue: result.getTextValue() ?? '',
multiSelectValue: [], // TODO update this when implemented on API
value: isValueNotSet ? emptyPropertyValue : {
textValue: valueCase !== GetAttachedPropertyValuesResponse.Value.ValueCase.TEXT_VALUE ? undefined : result.getTextValue(),
numberValue: valueCase !== GetAttachedPropertyValuesResponse.Value.ValueCase.NUMBER_VALUE ? undefined : result.getNumberValue(),
boolValue: valueCase !== GetAttachedPropertyValuesResponse.Value.ValueCase.BOOL_VALUE ? undefined : result.getBoolValue(),
dateValue: valueCase !== GetAttachedPropertyValuesResponse.Value.ValueCase.DATE_VALUE ? undefined : result.getDateValue()!.getDate()!.toDate(),
dateTimeValue: valueCase !== GetAttachedPropertyValuesResponse.Value.ValueCase.DATE_TIME_VALUE ? undefined : result.getDateTimeValue()!.toDate(),
singleSelectValue: valueCase !== GetAttachedPropertyValuesResponse.Value.ValueCase.SELECT_VALUE ? undefined : {
id: selectValue!.getId(),
name: selectValue!.getName(),
description: selectValue!.getDescription(),
},
multiSelectValue: (valueCase !== GetAttachedPropertyValuesResponse.Value.ValueCase.MULTI_SELECT_VALUE ? [] : result.getMultiSelectValue()!.getSelectValuesList()).map(value => ({
id: value.getId(),
name: value.getName(),
description: value.getDescription()
})) ?? [],
}
}
})

return results
},
})
}

type AttachedPropertyMutationUpdate<T extends AttachedProperty> = Update<T> & {
fieldType: FieldType
}

/**
* Mutation to insert or update a properties value for a properties attached to a subject
*/
export const useAttachedPropertyMutation = <T extends AttachedProperty>(callback: (property: T) => void = noop) => {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (property: T) => {
mutationFn: async (update: AttachedPropertyMutationUpdate<T>) => {
const req = new AttachPropertyValueRequest()
const { update: property, previous, fieldType } = update

req.setPropertyId(property.propertyId)
req.setSubjectId(property.subjectId)
req.setTextValue(property.value.textValue)
req.setNumberValue(property.value.numberValue)
req.setBoolValue(property.value.boolValue)
req.setDateValue(new ProtoDate().setDate(GRPCConverter.dateToTimestamp(property.value.dateValue)))
req.setDateTimeValue(GRPCConverter.dateToTimestamp(property.value.dateTimeValue))
req.setSelectValue(property.value.singleSelectValue)
.setSubjectId(property.subjectId)

switch (fieldType) {
case 'text':
if (property.value.textValue !== undefined) {
req.setTextValue(property.value.textValue)
}
break
case 'number':
if (property.value.numberValue !== undefined) {
req.setNumberValue(property.value.numberValue)
}
break
case 'checkbox':
if (property.value.boolValue !== undefined) {
req.setBoolValue(property.value.boolValue)
}
break
case 'date':
if (property.value.dateValue !== undefined) {
const protoDate = new ProtoDate().setDate(GRPCConverter.dateToTimestamp(property.value.dateValue))
req.setDateValue(protoDate)
}
break
case 'dateTime':
if (property.value.dateTimeValue !== undefined) {
req.setDateTimeValue(GRPCConverter.dateToTimestamp(property.value.dateTimeValue))
}
break
case 'singleSelect':
if (property.value.singleSelectValue !== undefined) {
req.setSelectValue(property.value.singleSelectValue.id)
}
break
case 'multiSelect':
if (property.value.multiSelectValue !== undefined) {
const previousOptions = previous?.value.multiSelectValue?.map(value => value.id) ?? []
const newOptions = property.value.multiSelectValue.map(value => value.id)
const addIds = ArrayUtil.difference(newOptions, previousOptions)
const deleteIds = ArrayUtil.difference(previousOptions, newOptions)

req.setMultiSelectValue(new AttachPropertyValueRequest.MultiSelectValue()
.setSelectValuesList(addIds)
.setRemoveSelectValuesList(deleteIds)
)
}
break
}

await APIServices.propertyValues.attachPropertyValue(req, getAuthenticatedGrpcMetadata())

console.log(req)
const newProperty: T = {
...property,
}
Expand Down
21 changes: 15 additions & 6 deletions api-services/mutations/tasks/patient_mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const usePatientDetailsQuery = (patientId: string | undefined) => {
const patient: PatientDetailsDTO = {
id: res.getId(),
note: res.getNotes(),
name: res.getName(),
name: res.getHumanReadableIdentifier(),
discharged: res.getIsDischarged(),
tasks: res.getTasksList().map(task => ({
id: task.getId(),
Expand Down Expand Up @@ -206,8 +206,14 @@ export const useRecentPatientsQuery = () => {
id: patient.getId(),
name: patient.getHumanReadableIdentifier(),
wardId,
bed: bed ? { id: bed.getId(), name: bed.getName() } : undefined,
room: room ? { id: room.getId(), name: room.getId() } : undefined
bed: bed ? {
id: bed.getId(),
name: bed.getName()
} : undefined,
room: room ? {
id: room.getId(),
name: room.getId()
} : undefined
})
}

Expand All @@ -221,8 +227,8 @@ export const usePatientCreateMutation = (organisationId: string) => {
return useMutation({
mutationFn: async (patient: PatientDTO) => {
const req = new CreatePatientRequest()
req.setNotes(patient.note)
req.setHumanReadableIdentifier(patient.name)
.setNotes(patient.note)
.setHumanReadableIdentifier(patient.name)
const res = await APIServices.patient.createPatient(req, getAuthenticatedGrpcMetadata(organisationId))

const id = res.getId()
Expand All @@ -231,7 +237,10 @@ export const usePatientCreateMutation = (organisationId: string) => {
throw new Error('create room failed')
}

return { ...patient, id }
return {
...patient,
id
}
},
onSuccess: () => {
queryClient.refetchQueries([QueryKeys.rooms]).catch(reason => console.error(reason))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { OrganizationMember } from '../../types/users/organization_member'

export const useMembersByOrganizationQuery = (organizationId: string | undefined) => {
return useQuery({
queryKey: [QueryKeys.organizations, organizationId],
queryKey: [QueryKeys.organizations, organizationId, 'members'],
enabled: !!organizationId,
queryFn: async () => {
if (!organizationId) {
Expand Down
Loading

0 comments on commit 60d21ad

Please sign in to comment.