Skip to content

Commit

Permalink
feat: add mutations for addFacility and addHealthcareProfessional (#203)
Browse files Browse the repository at this point in the history
* feat: implement mutation for addFacility

* feat: implement mutation for addHealthcareProfessional

* refactor: fix healthcareProfessionalService types

* refactor: fix types and move function
  • Loading branch information
theyokohamalife authored Aug 8, 2023
1 parent c259d3c commit b022309
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 140 deletions.
106 changes: 34 additions & 72 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,76 @@
import crypto from 'crypto'
// import { getDegreeById, getDegrees } from './services/degreeService'
import { getFacilityById, searchFacilities } from './services/facilityService'
import { getHealthcareProfessionalById, searchHealthcareProfessionals } from './services/healthcareProfessionalService'
// import { getPhysicalAddressById, getPhysicalAddresses } from './services/physicalAddressService'
// import { getSpecialtyById, getSpecialties } from './services/specialtyService'
// import { getSpokenLanguageByIso, getSpokenLanguages } from './services/spokenLanguageService'
import { addFacility, getFacilityById, searchFacilities } from './services/facilityService'
import { addHealthcareProfessional,
getHealthcareProfessionalById,
searchHealthcareProfessionals } from './services/healthcareProfessionalService'
import {
Contact,
Degree,
Facility,
Insurance,
HealthcareProfessional,
Specialty,
LocaleNameInput,
HealthcareProfessionalInput,
SpokenLanguage
SpokenLanguage,
LocaleName,
DegreeInput,
SpecialtyInput,
SpokenLanguageInput,
FacilityInput,
ContactInput
} from './typeDefs/gqlTypes'

const resolvers = {
Query: {
// degrees: () => {
// const matchingDegrees = getDegrees()

// return matchingDegrees
// },
// degree: (_parent: Degree, args: { id: string; }) => {
// const matchingDegree = getDegreeById(args.id)

// return matchingDegree
// },
facilities: async () => {
// TODO: add a validation step for incoming parameters
const matchingFacilities = await searchFacilities(['1'])

return matchingFacilities
},
facility: async (_parent: Facility, args: { id: string; }) => {
// TODO: add a validation step for incoming parameters
const matchingFacility = await getFacilityById(args.id)

return matchingFacility
},
healthcareProfessionals: async () => {
// TODO: add a validation step for incoming parameters
const matchingProfessionals = await searchHealthcareProfessionals(['1'])

return matchingProfessionals
},
healthcareProfessional: async (_parent: HealthcareProfessional, args: { id: string; }) => {
// TODO: add a validation step for incoming parameters
const matchingHealthcareProfessional = await getHealthcareProfessionalById(args.id)

return matchingHealthcareProfessional
}
// physicalAddress: (_parent: HealthcareProfessional, args: { id: string; }) => getPhysicalAddressById(args.id),
// physicalAddresses: () => getPhysicalAddresses(),
// specialties: () => {
// // TODO: add a validation step for incoming parameters
// const matchingSpecialties = getSpecialties()

// return matchingSpecialties
// },
// specialty: (_parent: Specialty, args: { id: string; }) => {
// // TODO: add a validation step for incoming parameters
// const matchingSpecialty = getSpecialtyById(args.id)

// return matchingSpecialty
// },
// spokenLanguages: () => getSpokenLanguages(),
// spokenLanguage: (_parent: SpokenLanguage, args: {iso639_3: string;}) => getSpokenLanguageByIso(args.iso639_3)
},
Mutation: {
createHealthcareProfessional: (_parent: HealthcareProfessionalInput, args: {
id: string,
names: Array<LocaleNameInput>,
degrees: Array<Degree>,
spokenLanguages: Array<SpokenLanguage>,
specialties: Array<Specialty>,
acceptedInsurance: Array<Insurance>
}) => {
const id = crypto.randomUUID()

const {
names,
degrees,
spokenLanguages,
specialties,
acceptedInsurance
} = args
createFacility: async (_parent: FacilityInput, args: {
input: {
contact: ContactInput,
healthcareProfessionals: HealthcareProfessionalInput[],
nameEn: string,
nameJa: string,
}
}) => {
const newFacility = await addFacility(args.input)

// TODO: Eventually this should check if a specialty already exists in the DB
// and match it to that if it does.
specialties.map((specialty: { id: string }) => {
if (!specialty.id) {
// eslint-disable-next-line no-param-reassign
specialty.id = crypto.randomUUID()
}
return specialty.id
})
return newFacility
},
createHealthcareProfessional: async (_parent: HealthcareProfessionalInput, args: {
input:{
acceptedInsurance: Insurance[],
degrees: DegreeInput[],
names: LocaleNameInput[]
specialties: SpecialtyInput[]
spokenLanguages: SpokenLanguageInput[]

const healthcareProfessional = {
id,
names,
degrees,
spokenLanguages,
specialties,
acceptedInsurance
}
}) => {
const newHealthcareProfessional = await addHealthcareProfessional(args.input)

return healthcareProfessional
return newHealthcareProfessional
}

}
}

Expand Down
59 changes: 52 additions & 7 deletions src/services/facilityService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { HealthcareProfessional, LocaleName, Degree,
Specialty, SpecialtyName, SpokenLanguage, Insurance, Facility } from '../typeDefs/dbSchema'
import { HealthcareProfessional, LocaleName, Contact, Degree,
Specialty, SpokenLanguage, Insurance, Facility } from '../typeDefs/dbSchema'
import { DocumentData, WhereFilterOp, getFirestore } from 'firebase-admin/firestore'
import { searchHealthcareProfessionals } from './healthcareProfessionalService'
import { FacilityInput, PhysicalAddress, ContactInput, HealthcareProfessionalInput } from '../typeDefs/gqlTypes'
import { mapAndValidateHealthcareProInput } from './healthcareProfessionalService'

export const getFacilityById = async (id: string) : Promise<Facility | null> => {
const db = getFirestore()
Expand All @@ -18,14 +19,33 @@ export const getFacilityById = async (id: string) : Promise<Facility | null> =>
return convertedEntity
}

export const addFacility = async (facility : Facility) : Promise<void> => {
//todo
export const addFacility = async (input: FacilityInput) : Promise<Facility> => {
const db = getFirestore()

const facilityRef = db.collection('facilities')

const healthcareProfessionalIds = await mapAndValidateHealthcareProInput(
input.healthcareProfessionals as HealthcareProfessionalInput[]
)

const newFacility = {
contact: validateContactInput(input.contact as Contact),
healthcareProfessionalIds: healthcareProfessionalIds,
healthcareProfessionals: [],
nameEn: validateNameEnInput(input.nameEn as string),
nameJa: validateNameJaInput(input.nameJa as string)
} satisfies Facility

await facilityRef.add(newFacility)

return newFacility as Facility
}

export const searchFacilities = async (userSearchQuery : string[]) : Promise<Facility[]> => {
const db = getFirestore()
const hpRef = db.collection('healthcareProfessionals')
const hpRef = db.collection('facilities')
// make this a real query
// this is still incomplete
const snapshot = await hpRef.where('id', 'in', userSearchQuery).get()

const facilities = [] as Facility[]
Expand All @@ -41,12 +61,37 @@ export const searchFacilities = async (userSearchQuery : string[]) : Promise<Fac

const mapDbEntityTogqlEntity = (dbEntity : DocumentData) : Facility => {
const gqlEntity = {
id: dbEntity.id,
nameEn: dbEntity.nameEn,
nameJa: dbEntity.nameJa,
contact: dbEntity.contact,
healthcareProfessionalIds: dbEntity.healthcareProfessionalIds,
healthcareProfessionals: dbEntity.healthcareProfessionals
} satisfies Facility

return gqlEntity
}

function validateContactInput(contactInput: Contact) : Contact {
const facilityContact = {
address: contactInput.address as PhysicalAddress,
email: contactInput.email as string,
mapsLink: contactInput.mapsLink as string,
phone: contactInput.phone as string,
website: contactInput.website as string
}

return facilityContact
}

function validateNameEnInput(nameEnInput: string) : string {
const nameEn = nameEnInput

return nameEn
}

function validateNameJaInput(nameJaInput: string) : string {
const nameJa = nameJaInput

return nameJa
}

118 changes: 114 additions & 4 deletions src/services/healthcareProfessionalService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { HealthcareProfessional, LocaleName, Degree,
Specialty, SpecialtyName, SpokenLanguage, Insurance } from '../typeDefs/dbSchema'
import { DocumentData, WhereFilterOp, getFirestore } from 'firebase-admin/firestore'
import { DocumentData, DocumentReference, WhereFilterOp, getFirestore } from 'firebase-admin/firestore'
import { DegreeInput,
HealthcareProfessionalInput,
LocaleNameInput,
SpecialtyInput,
SpecialtyNameInput,
SpokenLanguageInput } from '../typeDefs/gqlTypes'

export const getHealthcareProfessionalById = async (id: string) : Promise<HealthcareProfessional | null> => {
const db = getFirestore()
Expand All @@ -17,9 +23,26 @@ export const getHealthcareProfessionalById = async (id: string) : Promise<Health
return convertedEntity
}

export const addHealthcareProfessional = async (healthcareProfessional : HealthcareProfessional) : Promise<void> => {
export const addHealthcareProfessional = async (input : HealthcareProfessionalInput) : Promise<string[]> => {
const db = getFirestore()

const healthcareProRef = db.collection('healthcareProfessionals')

const newHealthcareProfessional = {
acceptedInsurance: mapAndValidateInsurance(input.acceptedInsurance as []),
degrees: mapAndValidateDegrees(input.degrees as DegreeInput[]),
names: mapAndValidateNames(input.names as LocaleNameInput[]),
specialties: mapAndValidateSpecialties(input.specialties as SpecialtyInput[]),
spokenLanguages: mapAndValidateLanguages(input.spokenLanguages as SpokenLanguageInput[])
}

const idList: string[] = []

const docRef = await healthcareProRef.add(newHealthcareProfessional)

//todo
idList.push(docRef.id)

return idList
}

export const searchHealthcareProfessionals = async (userSearchQuery : string[])
Expand All @@ -41,7 +64,6 @@ export const searchHealthcareProfessionals = async (userSearchQuery : string[])

const mapDbEntityTogqlEntity = (dbEntity : DocumentData) : HealthcareProfessional => {
const gqlEntity = {
id: dbEntity.id,
names: dbEntity.names,
degrees: dbEntity.degrees,
spokenLanguages: dbEntity.spokenLanguages,
Expand All @@ -51,3 +73,91 @@ const mapDbEntityTogqlEntity = (dbEntity : DocumentData) : HealthcareProfessiona

return gqlEntity
}

export const mapAndValidateHealthcareProInput =
(healthcareProInput: HealthcareProfessionalInput[]) : Promise<string[]> => healthcareProInput.map(
(professional: HealthcareProfessionalInput) => addHealthcareProfessional(professional)
)[0]

function mapAndValidateDegrees(degreesInput: DegreeInput[]) {
// TODO: Write conditional to check if already exists
// TODO: This should save to the degrees collection and return an array of IDs
const degrees = degreesInput.map((degree: DegreeInput) => {
const newDegree = {nameJa: degree.nameJa,
nameEn: degree.nameEn,
abbreviation: degree.abbreviation}

return newDegree
})

return degrees
}

function mapAndValidateNames(namesInput: LocaleNameInput[]) {
// TODO: Write conditional to check if already exists
const names = namesInput.map((name: LocaleNameInput) => {
const newLocaleName = {
lastName: name.lastName,
firstName: name.firstName,
middleName: name.middleName,
locale: name.locale
}

return newLocaleName
})

return names
}

function mapAndValidateSpecialties(specialtiesInput: SpecialtyInput[]) {
// TODO: Write conditional to check if already exists
// TODO: This should save to the specialties collection and return an array of IDs
const specialties = specialtiesInput.map((specialty: SpecialtyInput) => {
const newSpecialty = {

names: mapAndValidateSpecialtyNames(specialty.names as SpecialtyNameInput[])
}

return newSpecialty
})

return specialties
}

function mapAndValidateSpecialtyNames(specialtyNamesInput: SpecialtyNameInput[]) {
// TODO: Write conditional to check if already exists
const specialtyNames = specialtyNamesInput.map((name: SpecialtyNameInput) => {
const newSpecialtyName = {
name: name.name,
locale: name.locale
}

return newSpecialtyName
})

return specialtyNames
}

function mapAndValidateLanguages(languagesInput: SpokenLanguageInput[]) {
// TODO: Write conditional to check if already exists
const languages = languagesInput.map((language: SpokenLanguageInput) => {
const newLanguage = {
iso639_3: language.iso639_3,
nameJa: language.nameJa,
nameEn: language.nameEn,
nameNative: language.nameNative
}

return newLanguage
})

return languages
}

function mapAndValidateInsurance(insuranceInput: Insurance[]) {
// TODO: Write conditional to check if already exists

const insurance = insuranceInput

return insurance
}
Loading

0 comments on commit b022309

Please sign in to comment.