Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(patients): add saveOrUpdate function in repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Jan 20, 2020
1 parent 1e06ddd commit c9a6913
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/__tests__/clients/db/PatientRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { patients } from 'config/pouchdb'
import PatientRepository from 'clients/db/PatientRepository'
import Patient from 'model/Patient'
import { fromUnixTime } from 'date-fns'
import { updatePatientStart } from 'patients/patient-slice'

describe('patient repository', () => {
describe('find', () => {
Expand Down Expand Up @@ -105,6 +106,57 @@ describe('patient repository', () => {
})
})

describe('saveOrUpdate', () => {
// it('should save the patient if an id was not on the entity', async () => {
// const newPatient = await PatientRepository.saveOrUpdate({
// fullName: 'test1 test1',
// } as Patient)

// expect(newPatient.id).toBeDefined()

// await patients.remove(await patients.get(newPatient.id))
// })

it('should update the patient if one was already existing', async () => {
const existingPatient = await PatientRepository.save({
fullName: 'test test',
} as Patient)

const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient)

expect(updatedPatient.id).toEqual(existingPatient.id)

await patients.remove(await patients.get(existingPatient.id))
})

it('should update the existing fields', async () => {
const existingPatient = await PatientRepository.save({
fullName: 'test test',
} as Patient)
existingPatient.fullName = 'changed'

const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient)

expect(updatedPatient.fullName).toEqual('changed')

await patients.remove(await patients.get(existingPatient.id))
})

it('should add new fields without changing existing fields', async () => {
const existingPatient = await PatientRepository.save({
fullName: 'test test',
} as Patient)
existingPatient.givenName = 'givenName'

const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient)

expect(updatedPatient.fullName).toEqual(existingPatient.fullName)
expect(updatedPatient.givenName).toEqual('givenName')

await patients.remove(await patients.get(existingPatient.id))
})
})

describe('delete', () => {
it('should delete the patient', async () => {
const patientToDelete = await PatientRepository.save({
Expand Down
21 changes: 21 additions & 0 deletions src/clients/db/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ export default class Repository<T extends AbstractDBModel> {
return this.find(savedEntity.id)
}

async saveOrUpdate(entity: T): Promise<T> {
if (!entity.id) {
return this.save(entity)
}

try {
const existingEntity = await this.find(entity.id)
const { id, rev, ...restOfDoc } = existingEntity
const entityToUpdate = {
_id: id,
_rev: rev,
...restOfDoc,
...entity,
}
await this.db.put(entityToUpdate)
return this.find(entity.id)
} catch (error) {
return this.save(entity)
}
}

async delete(entity: T): Promise<T> {
const e = entity as any
return mapDocument(this.db.remove(e.id, e.rev))
Expand Down

0 comments on commit c9a6913

Please sign in to comment.