Skip to content

Commit

Permalink
got build and queries working again! (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
ermish authored Jun 2, 2023
1 parent 2260053 commit 1559ddc
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 93 deletions.
8 changes: 5 additions & 3 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { initializeApp, applicationDefault, cert } from 'firebase-admin/app'
import serviceAccountCredentials
import { initializeApp, applicationDefault, cert, ServiceAccount } from 'firebase-admin/app'
import serviceAccountCredentialsJson
from '../findadoc-firebase-service-account-credentials.json'
import { getFirestore, Timestamp, FieldValue } from 'firebase-admin/firestore'

export async function initializeDb() {
const credentials = serviceAccountCredentialsJson as ServiceAccount

initializeApp({
credential: cert(serviceAccountCredentials)
credential: cert(credentials)
})
}
8 changes: 4 additions & 4 deletions src/databaseSeedTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ export const seedDatabase = () => {
const healthcareProfessionalForFacility = healthcareProfessionals[0]

facilities[0].healthcareProfessionals = [healthcareProfessionalForFacility]

async function test() {
const hpRef = db.collection('healthcareProfessionals')
// const hpRef = db.collection('healthcareProfessionals')

await healthcareProfessionals.forEach(hp => {
addHealthcareProfessional(hpRef, hp)
addHealthcareProfessional(hp)
})

const facilitiesRef = db.collection('facilities')

await facilities.forEach(facility => {
addFacility(facilitiesRef, facility)
addFacility(facility)
})
}
}
3 changes: 2 additions & 1 deletion src/fakeData/facilities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Facility } from '../typeDefs/dbSchema'
import { fakeHealthcareProfessionals } from './healthcareProfessional'

export const fakeFacilities = () => {
const facility : Facility = {
Expand All @@ -11,7 +12,7 @@ export const fakeFacilities = () => {
website: 'https://zoo.test.com',
mapsLink: ''
},
healthcareProfessionals: []
healthcareProfessionals: fakeHealthcareProfessionals()
}

return [facility]
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { startStandaloneServer } from '@apollo/server/standalone'
import loadSchema from './schema'
import resolvers from './resolvers'
import { initializeDb } from './database'
import { getFacilityById, getFacilities } from './services/facilityService'
import { getHealthcareProfessionalById, getHealthcareProfessionals } from './services/healthcareProfessionalService'
import { getFacilityById, searchFacilities } from './services/facilityService'
import { getHealthcareProfessionalById, searchHealthcareProfessionals } from './services/healthcareProfessionalService'
import {seedDatabase} from './databaseSeedTool'

const server = new ApolloServer({
Expand Down
20 changes: 10 additions & 10 deletions src/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import crypto from 'crypto'
// import { getDegreeById, getDegrees } from './services/degreeService'
import { getFacilityById, getFacilities } from './services/facilityService'
import { getHealthcareProfessionalById, getHealthcareProfessionals } from './services/healthcareProfessionalService'
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'
Expand All @@ -28,27 +28,27 @@ const resolvers = {

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

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

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

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

return matchingHealthcareProfessional
}
Expand Down
70 changes: 31 additions & 39 deletions src/services/facilityService.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,52 @@
// import { HealthcareProfessional, LocaleName, Degree,
// Specialty, SpecialtyName, SpokenLanguage, Insurance, Facility } from '../typeDefs/dbSchema'
import { getFirestore } from 'firebase-admin/firestore'
import { getHealthcareProfessionalsByIds } from './healthcareProfessionalService'
import { HealthcareProfessional, LocaleName, Degree,
Specialty, SpecialtyName, SpokenLanguage, Insurance, Facility } from '../typeDefs/dbSchema'
import { DocumentData, WhereFilterOp, getFirestore } from 'firebase-admin/firestore'
import { searchHealthcareProfessionals } from './healthcareProfessionalService'

export const getFacilityById = async (id: string) => {
export const getFacilityById = async (id: string) : Promise<Facility | null> => {
const db = getFirestore()
const facilityRef = db.collection('facilities')
const snapshot = await facilityRef.where('id', '=', id).get()
const facilities = []
const whereCondition = '=' as WhereFilterOp
const snapshot = await facilityRef.where('id', whereCondition, id).get()

snapshot.forEach(doc => {
facilities.push(doc.data())
})

for await (const facility of facilities) {
await hydrateFacility(facility)
if (snapshot.docs.length < 1) {
return null
}

return facilities
}
const convertedEntity = mapDbEntityTogqlEntity(snapshot.docs[0].data())

export async function addFacility(facilityRef, facility) {
facilityRef.add(transformFacilityForFirestore(facility))
return convertedEntity
}

function transformFacilityForFirestore(facility) {
const healthcareProfessionalIds = facility.healthcareProfessionals.map(hp => hp.id)

facility.healthcareProfessionals = healthcareProfessionalIds

return facility
export const addFacility = async (facility : Facility) : Promise<void> => {
//todo
}

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

const snapshot = await facilitiesRef.get()
const facilities = []
const facilities = [] as Facility[]

snapshot.forEach(doc => {
facilities.push(doc.data())
})
const convertedEntity = mapDbEntityTogqlEntity(doc.data())

for await (const facility of facilities) {
await hydrateFacility(facility)
}
facilities.push(convertedEntity)
})

return facilities
}

async function hydrateFacility(facility) {
const db = getFirestore()
const healthcareProfessionals = await getHealthcareProfessionalsByIds(facility.healthcareProfessionals)

facility.healthcareProfessionals = healthcareProfessionals

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

return gqlEntity
}
60 changes: 33 additions & 27 deletions src/services/healthcareProfessionalService.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
// import { HealthcareProfessional, LocaleName, Degree,
// Specialty, SpecialtyName, SpokenLanguage, Insurance } from '../typeDefs/dbSchema'
import { getFirestore } from 'firebase-admin/firestore'
import { HealthcareProfessional, LocaleName, Degree,
Specialty, SpecialtyName, SpokenLanguage, Insurance } from '../typeDefs/dbSchema'
import { DocumentData, WhereFilterOp, getFirestore } from 'firebase-admin/firestore'

export const getHealthcareProfessionalById = async (id: string) => {
export const getHealthcareProfessionalById = async (id: string) : Promise<HealthcareProfessional | null> => {
const db = getFirestore()
const hpRef = db.collection('healthcareProfessionals')
const snapshot = await hpRef.where('id', '=', id).get()
const healthcareProfessionals = []
const whereCondition = '=' as WhereFilterOp
const snapshot = await hpRef.where('id', whereCondition, id).get()

snapshot.forEach(doc => {
healthcareProfessionals.push(doc.data())
})
if (snapshot.docs.length < 1) {
return null
}

return healthcareProfessionals
const convertedEntity = mapDbEntityTogqlEntity(snapshot.docs[0].data())

return convertedEntity
}

export const addHealthcareProfessional = async (healthcareProfessionalsRef, healthcareProfessional) => {
healthcareProfessionalsRef.add(healthcareProfessional)
export const addHealthcareProfessional = async (healthcareProfessional : HealthcareProfessional) : Promise<void> => {

//todo
}

export const getHealthcareProfessionals = async () => {
export const searchHealthcareProfessionals = async (userSearchQuery : string[])
: Promise<HealthcareProfessional[]> => {
const db = getFirestore()
const hpRef = db.collection('healthcareProfessionals')
const snapshot = await hpRef.where('id', 'in', userSearchQuery).get()

const snapshot = await hpRef.get()
const healthcareProfessionals = []
const healthcareProfessionals = [] as HealthcareProfessional[]

snapshot.forEach(doc => {
healthcareProfessionals.push(doc.data())
const convertedEntity = mapDbEntityTogqlEntity(doc.data())

healthcareProfessionals.push(convertedEntity)
})

return healthcareProfessionals
}

export const getHealthcareProfessionalsByIds = async (ids : string[]) => {
const db = getFirestore()
const hpRef = db.collection('healthcareProfessionals')
const snapshot = await hpRef.where('id', 'in', ids).get()
const healthcareProfessionals = []

snapshot.forEach(doc => {
healthcareProfessionals.push(doc.data())
})

return healthcareProfessionals
const mapDbEntityTogqlEntity = (dbEntity : DocumentData) : HealthcareProfessional => {
const gqlEntity = {
id: dbEntity.id,
names: dbEntity.names,
degrees: dbEntity.degrees,
spokenLanguages: dbEntity.spokenLanguages,
specialties: dbEntity.specialties,
acceptedInsurance: dbEntity.acceptedInsurance
} satisfies HealthcareProfessional
return gqlEntity
}
14 changes: 7 additions & 7 deletions src/typeDefs/dbSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export type Facility = {
nameEn: string
nameJa: string
contact: Contact
healthcareProfessionals: [HealthcareProfessional]
healthcareProfessionals: HealthcareProfessional[]
}

export type HealthcareProfessional = {
id: string,
names: [LocaleName],
degrees: [Degree],
spokenLanguages: [SpokenLanguage],
specialties: [Specialty],
acceptedInsurance: [Insurance]
names: LocaleName[],
degrees: Degree[],
spokenLanguages: SpokenLanguage[],
specialties: Specialty[],
acceptedInsurance: Insurance[]
}

export type LocaleName = {
Expand All @@ -36,7 +36,7 @@ export enum Locale {

export type Specialty = {
id: string
names: [SpecialtyName]
names: SpecialtyName[]
}

export type SpecialtyName = {
Expand Down

0 comments on commit 1559ddc

Please sign in to comment.