Skip to content

Commit

Permalink
just use the client we already get
Browse files Browse the repository at this point in the history
  • Loading branch information
mojotalantikite committed Sep 14, 2023
1 parent e3ff72e commit 8b5a9c1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
14 changes: 8 additions & 6 deletions services/app-api/src/handlers/proto_to_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type {
RateTable,
} from '@prisma/client'
import type { ContractType } from '../domain-models/contractAndRates'
import { PrismaClient } from '@prisma/client'
import type { PrismaClient } from '@prisma/client'
import type { ContractTable, ContractRevisionTable } from '@prisma/client'
import type { StoreError } from '../postgres/storeError'
import { NotFoundError, isStoreError } from '../postgres/storeError'
Expand All @@ -40,7 +40,10 @@ import { toDomain } from '../../../app-web/src/common-code/proto/healthPlanFormD
import type { HealthPlanFormDataType } from '../../../app-web/src/common-code/healthPlanFormDataType'
import { findContractWithHistory } from '../postgres/contractAndRates'

export const getDatabaseConnection = async (): Promise<Store> => {
export const getDatabaseConnection = async (): Promise<{
store: Store
prismaClient: PrismaClient
}> => {
const dbURL = process.env.DATABASE_URL
const secretsManagerSecret = process.env.SECRETS_MANAGER_SECRET

Expand Down Expand Up @@ -68,7 +71,7 @@ export const getDatabaseConnection = async (): Promise<Store> => {
}
const store = NewPostgresStore(pgResult)

return store
return { store, prismaClient: pgResult }
}

export const getRevisions = async (
Expand Down Expand Up @@ -266,15 +269,14 @@ export const main: Handler = async (): Promise<APIGatewayProxyResultV2> => {
}

// setup db connections and get revisions
const store = await getDatabaseConnection()
const client = new PrismaClient()
const { store, prismaClient } = await getDatabaseConnection()
const revisions = await getRevisions(store)

// go through the list of revisons and migrate
console.info(`Found ${revisions.length} revisions to migrate...`)
const revisionsWithErrors = []
for (const revision of revisions) {
const migrateResult = await migrateRevision(client, revision)
const migrateResult = await migrateRevision(prismaClient, revision)
if (migrateResult instanceof Error) {
recordException(migrateResult, serviceName, 'migrateRevision')
revisionsWithErrors.push(revision.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ async function migrateContractRevision(
})

return createdContractRevision
} catch (error) {
} catch (err) {
return new Error(
`Error creating contract revision for ID ${revision.id}: ${error.message}`
`Error creating contract revision for ID ${revision.id}: ${err.message}`
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ async function migrateDocuments(
}

return results
} catch (error) {
const err = new Error(
`Error migrating documents: ${JSON.stringify(error)}`
} catch (err) {
const error = new Error(
`Error migrating documents: ${JSON.stringify(err)}`
)
return err
return error
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ async function migrateAssociations(
}
}
}
} catch (error) {
console.error(`Error migrating associations: ${JSON.stringify(error)}`)
} catch (err) {
return new Error('Error migrating associations')
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function prepopulateUpdateInfo(
): Promise<string | Error> {
try {
const user = await client.user.findFirst({
where: { email: { equals: revision.submittedBy ?? '' } },
where: { email: { equals: revision.submittedBy ?? undefined } },
})

if (!user) {
Expand Down

0 comments on commit 8b5a9c1

Please sign in to comment.