@@ -93,7 +93,6 @@ export class DeathRegistrationDialogComponent {
9393 return ;
9494 }
9595
96- this . removeExistingDerivedConditions ( ) ;
9796 this . patientService . deletePatientDeathRecord ( this . data . patient . id ) ;
9897
9998 const revivedPatient : Patient = {
@@ -183,8 +182,6 @@ export class DeathRegistrationDialogComponent {
183182 return ;
184183 }
185184
186- this . removeExistingDerivedConditions ( ) ;
187-
188185 const enrichedPart1 = await this . createDerivedConditions (
189186 serializedPart1 . map ( entry => ( { ...entry } ) ) ,
190187 deceasedDateTime
@@ -248,12 +245,14 @@ export class DeathRegistrationDialogComponent {
248245 this . deceasedTime = existingDateTime ;
249246
250247 const part1LineCodes : Part1LineCode [ ] = [ 'a' , 'b' , 'c' , 'd' ] ;
248+ const part1Diagnoses = this . getBundleDiagnoses ( this . data . existingRecord , 'part1' ) ;
249+ const part2Diagnoses = this . getBundleDiagnoses ( this . data . existingRecord , 'part2' ) ;
250+
251251 this . part1Lines = part1LineCodes . map ( lineCode => {
252- const existingLine = this . getBundleDiagnoses ( this . data . existingRecord ! , 'part1' ) . find ( line => line . line === lineCode ) ;
252+ const existingLine = part1Diagnoses . find ( line => line . line === lineCode ) ;
253253 return existingLine ? this . mapDiagnosisToLine ( existingLine , lineCode ) : this . createPart1Line ( lineCode ) ;
254254 } ) ;
255255
256- const part2Diagnoses = this . getBundleDiagnoses ( this . data . existingRecord , 'part2' ) ;
257256 this . part2Lines = part2Diagnoses . length > 0
258257 ? part2Diagnoses . map ( line => this . mapDiagnosisToLine ( line ) )
259258 : [ this . createPart2Line ( ) ] ;
@@ -284,7 +283,7 @@ export class DeathRegistrationDialogComponent {
284283 return {
285284 line,
286285 sourceType : diagnosis . sourceType ,
287- selectedConditionId : diagnosis . sourceConditionId || '' ,
286+ selectedConditionId : this . resolveConditionId ( diagnosis ) ,
288287 selectedConcept : diagnosis . snomedConceptId
289288 ? { code : diagnosis . snomedConceptId , display : diagnosis . snomedDisplay || diagnosis . text }
290289 : null ,
@@ -294,6 +293,22 @@ export class DeathRegistrationDialogComponent {
294293 } ;
295294 }
296295
296+ private resolveConditionId ( diagnosis : DeathRecordDiagnosis ) : string {
297+ if ( diagnosis . sourceConditionId && this . data . conditions . some ( c => c . id === diagnosis . sourceConditionId ) ) {
298+ return diagnosis . sourceConditionId ;
299+ }
300+ if ( diagnosis . snomedConceptId ) {
301+ const match = this . data . conditions . find ( c => {
302+ const coding = this . patientService . getConditionSnomedCoding ( c ) ;
303+ return coding ?. code === diagnosis . snomedConceptId ;
304+ } ) ;
305+ if ( match ) {
306+ return match . id ;
307+ }
308+ }
309+ return diagnosis . sourceConditionId || '' ;
310+ }
311+
297312 private async serializePart1 ( ) : Promise < Array < DeathRecordDiagnosis & { line : Part1LineCode } > > {
298313 const results : Array < DeathRecordDiagnosis & { line : Part1LineCode } > = [ ] ;
299314
@@ -418,30 +433,15 @@ export class DeathRegistrationDialogComponent {
418433 }
419434 ] ;
420435
421- await this . patientService . addPatientConditionAllowDuplicatesEnriched ( this . data . patient . id , condition ) ;
422- diagnosis . derivedConditionId = condition . id ;
436+ const savedCondition = await this . patientService . addPatientConditionAllowDuplicatesEnriched ( this . data . patient . id , condition ) ;
437+ diagnosis . sourceType = 'existing-condition' ;
438+ diagnosis . sourceConditionId = savedCondition . id ;
439+ diagnosis . derivedConditionId = undefined ;
423440 }
424441
425442 return diagnoses ;
426443 }
427444
428- private removeExistingDerivedConditions ( ) : void {
429- if ( ! this . data . existingRecord ) {
430- return ;
431- }
432-
433- const previousDiagnoses = [
434- ...this . getBundleDiagnoses ( this . data . existingRecord , 'part1' ) ,
435- ...this . getBundleDiagnoses ( this . data . existingRecord , 'part2' )
436- ] ;
437-
438- previousDiagnoses . forEach ( diagnosis => {
439- if ( diagnosis . derivedConditionId ) {
440- this . patientService . deletePatientCondition ( this . data . patient . id , diagnosis . derivedConditionId ) ;
441- }
442- } ) ;
443- }
444-
445445 private getConditionById ( conditionId ?: string ) : Condition | undefined {
446446 if ( ! conditionId ) {
447447 return undefined ;
@@ -450,6 +450,17 @@ export class DeathRegistrationDialogComponent {
450450 return this . data . conditions . find ( condition => condition . id === conditionId ) ;
451451 }
452452
453+ getConditionDisplayText ( condition : Condition ) : string {
454+ if ( condition . code ?. text ) {
455+ return condition . code . text ;
456+ }
457+ const snomed = this . patientService . getConditionSnomedCoding ( condition ) ;
458+ if ( snomed ?. display ) {
459+ return snomed . display ;
460+ }
461+ return condition . code ?. coding ?. find ( c => c . display ) ?. display || condition . id || '' ;
462+ }
463+
453464 private getConditionSnomed ( condition : Condition ) : { code ?: string ; display ?: string } {
454465 const coding = this . patientService . getConditionSnomedCoding ( condition ) || condition . code ?. coding ?. [ 0 ] ;
455466
@@ -506,7 +517,7 @@ export class DeathRegistrationDialogComponent {
506517
507518 const entries = [
508519 { fullUrl : `urn:uuid:${ compositionId } ` , resource : compositionResource } ,
509- { fullUrl : `urn:uuid: ${ this . data . patient . id } ` , resource : patientResource } ,
520+ { fullUrl : `Patient/ ${ this . data . patient . id } ` , resource : patientResource } ,
510521 { fullUrl : `urn:uuid:${ practitionerId } ` , resource : practitionerResource } ,
511522 { fullUrl : `urn:uuid:${ procedureId } ` , resource : procedureResource } ,
512523 ...part1Observations . map ( resource => ( { fullUrl : `urn:uuid:${ resource . id } ` , resource } ) ) ,
@@ -570,7 +581,7 @@ export class DeathRegistrationDialogComponent {
570581 text : 'Death certification'
571582 } ,
572583 subject : {
573- reference : `urn:uuid: ${ this . data . patient . id } `
584+ reference : `Patient/ ${ this . data . patient . id } `
574585 } ,
575586 performedDateTime : deceasedDateTime ,
576587 performer : [
@@ -609,7 +620,7 @@ export class DeathRegistrationDialogComponent {
609620 text : 'Death certificate'
610621 } ,
611622 subject : {
612- reference : `urn:uuid: ${ this . data . patient . id } `
623+ reference : `Patient/ ${ this . data . patient . id } `
613624 } ,
614625 date : deceasedDateTime ,
615626 author : [
@@ -672,7 +683,7 @@ export class DeathRegistrationDialogComponent {
672683 ]
673684 } ,
674685 subject : {
675- reference : `urn:uuid: ${ this . data . patient . id } `
686+ reference : `Patient/ ${ this . data . patient . id } `
676687 } ,
677688 performer : [
678689 {
@@ -737,7 +748,7 @@ export class DeathRegistrationDialogComponent {
737748 ]
738749 } ,
739750 subject : {
740- reference : `urn:uuid: ${ this . data . patient . id } `
751+ reference : `Patient/ ${ this . data . patient . id } `
741752 } ,
742753 performer : [
743754 {
0 commit comments