diff --git a/package-lock.json b/package-lock.json index 9fabdc79..9588556c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cht-user-management", - "version": "1.0.11", + "version": "1.0.12", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "cht-user-management", - "version": "1.0.11", + "version": "1.0.12", "license": "ISC", "dependencies": { "@fastify/autoload": "^5.8.0", diff --git a/package.json b/package.json index 25239f56..a8bf3fb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cht-user-management", - "version": "1.0.11", + "version": "1.0.12", "main": "dist/index.js", "dependencies": { "@fastify/autoload": "^5.8.0", diff --git a/src/services/place.ts b/src/services/place.ts index 998c281e..a67208a3 100644 --- a/src/services/place.ts +++ b/src/services/place.ts @@ -138,18 +138,28 @@ export default class Place { }, {}); }; + const contactAttributes = (contactType: string) => { + const RESERVED_CONTACT_TYPES = ['district_hospital', 'health_center', 'clinic', 'person']; + + if (RESERVED_CONTACT_TYPES.includes(contactType)) { + return { type: contactType }; + } + + return { + type: 'contact', + contact_type: contactType, + }; + }; return { ...filteredProperties(this.properties), + ...contactAttributes(this.type.name), _id: this.isReplacement ? this.resolvedHierarchy[0]?.id : this.id, - type: 'contact', - contact_type: this.type.name, parent: this.resolvedHierarchy[1]?.id, user_attribution, contact: { ...filteredProperties(this.contact.properties), + ...contactAttributes(this.contact.type.contact_type), name: this.contact.name, - type: 'contact', - contact_type: this.contact.type.contact_type, user_attribution, } }; diff --git a/test/services/place.spec.ts b/test/services/place.spec.ts index 8c666edd..fc540057 100644 --- a/test/services/place.spec.ts +++ b/test/services/place.spec.ts @@ -1,7 +1,7 @@ import { expect } from 'chai'; import Place from '../../src/services/place'; -import { mockSimpleContactType } from '../mocks'; +import { mockSimpleContactType, mockValidContactType } from '../mocks'; import RemotePlaceResolver from '../../src/lib/remote-place-resolver'; describe('services/place.ts', () => { @@ -109,4 +109,30 @@ describe('services/place.ts', () => { const actual = place.generateUsername(); expect(actual).to.eq('migwani_itoloni'); }); + + it('asChtPayload uses contact_type by default', () => { + const contactType = mockValidContactType('string', undefined); + Object.freeze(contactType); + + const place = new Place(contactType); + const actual = place.asChtPayload('usr'); + expect(actual.type).to.eq('contact'); + expect(actual.contact.type).to.eq('contact'); + expect(actual.contact_type).to.eq(contactType.name); + }); + + it('#46 - asChtPayload should use type:health_center instead of contact_type:health_center', () => { + const contactType = mockValidContactType('string', undefined); + contactType.name = 'health_center'; + contactType.contact_type = 'person'; + Object.freeze(contactType); + + const place = new Place(contactType); + const actual = place.asChtPayload('usr'); + expect(actual.type).to.eq(contactType.name); + expect(actual.contact_type).to.be.undefined; + + expect(actual.contact.type).to.eq(contactType.contact_type); + expect(actual.contact.contact_type).to.be.undefined; + }); });