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

Commit

Permalink
feat: change to use standard naming and use iso dates
Browse files Browse the repository at this point in the history
fix #1879
  • Loading branch information
jackcmeyer committed Mar 5, 2020
1 parent 92b970f commit 8827df1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
7 changes: 2 additions & 5 deletions src/__tests__/clients/db/AppointmentRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import AppointmentRepository from 'clients/db/AppointmentRepository'
import { appointments } from 'config/pouchdb'
import Appointment from 'model/Appointment'
import { fromUnixTime } from 'date-fns'

const uuidV4Regex = /^[A-F\d]{8}-[A-F\d]{4}-4[A-F\d]{3}-[89AB][A-F\d]{3}-[A-F\d]{12}$/i

Expand Down Expand Up @@ -41,10 +40,8 @@ describe('Appointment Repository', () => {
patientId: 'id',
} as Appointment)

expect(newAppointment.createdDate).toBeDefined()
expect(fromUnixTime(newAppointment.createdDate).getTime() > 0).toBeTruthy()
expect(newAppointment.lastUpdatedDate).toBeDefined()
expect(fromUnixTime(newAppointment.lastUpdatedDate).getTime() > 0).toBeTruthy()
expect(newAppointment.createdAt).toBeDefined()
expect(newAppointment.updatedAt).toBeDefined()
})
})
})
32 changes: 16 additions & 16 deletions src/__tests__/clients/db/PatientRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { patients } from 'config/pouchdb'
import PatientRepository from 'clients/db/PatientRepository'
import Patient from 'model/Patient'
import * as shortid from 'shortid'
import { fromUnixTime, getTime, isAfter } from 'date-fns'
import { getTime, isAfter } from 'date-fns'

const uuidV4Regex = /^[A-F\d]{8}-[A-F\d]{4}-4[A-F\d]{3}-[89AB][A-F\d]{3}-[A-F\d]{12}$/i

Expand Down Expand Up @@ -116,22 +116,20 @@ describe('patient repository', () => {
fullName: 'test1 test1',
} as Patient)

expect(newPatient.createdDate).toBeDefined()
expect(fromUnixTime(newPatient.createdDate).getTime() > 0).toBeTruthy()
expect(newPatient.lastUpdatedDate).toBeDefined()
expect(fromUnixTime(newPatient.lastUpdatedDate).getTime() > 0).toBeTruthy()
expect(newPatient.createdAt).toBeDefined()
expect(newPatient.updatedAt).toBeDefined()
})

it('should override the created date and last updated date even if one was passed in', async () => {
const unexpectedTime = getTime(new Date(2020, 2, 1))
const unexpectedTime = new Date(2020, 2, 1).toISOString()
const newPatient = await PatientRepository.save({
fullName: 'test1 test1',
createdDate: unexpectedTime,
lastUpdatedDate: unexpectedTime,
createdAt: unexpectedTime,
updatedAt: unexpectedTime,
} as Patient)

expect(newPatient.createdDate).not.toEqual(unexpectedTime)
expect(newPatient.lastUpdatedDate).not.toEqual(unexpectedTime)
expect(newPatient.createdAt).not.toEqual(unexpectedTime)
expect(newPatient.updatedAt).not.toEqual(unexpectedTime)
})
})

Expand Down Expand Up @@ -182,23 +180,25 @@ describe('patient repository', () => {
})

it('should update the last updated date', async () => {
const time = getTime(new Date(2020, 1, 1))
await patients.put({ _id: 'id2222222', createdDate: time, lastUpdatedDate: time })
const time = new Date(2020, 1, 1).toISOString()
await patients.put({ _id: 'id2222222', createdAt: time, updatedAt: time })
const existingPatient = await PatientRepository.find('id2222222')

const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient)

expect(isAfter(updatedPatient.lastUpdatedDate, updatedPatient.createdDate)).toBeTruthy()
expect(updatedPatient.lastUpdatedDate).not.toEqual(existingPatient.lastUpdatedDate)
expect(
isAfter(new Date(updatedPatient.updatedAt), new Date(updatedPatient.createdAt)),
).toBeTruthy()
expect(updatedPatient.updatedAt).not.toEqual(existingPatient.updatedAt)
})

it('should not update the created date', async () => {
const time = getTime(new Date(2020, 1, 1))
await patients.put({ _id: 'id111111', createdDate: time, lastUpdatedDate: time })
await patients.put({ _id: 'id111111', createdAt: time, updatedAt: time })
const existingPatient = await PatientRepository.find('id111111')
const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient)

expect(updatedPatient.createdDate).toEqual(existingPatient.createdDate)
expect(updatedPatient.createdAt).toEqual(existingPatient.createdAt)
})
})

Expand Down
9 changes: 4 additions & 5 deletions src/clients/db/Repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint "@typescript-eslint/camelcase": "off" */
import { getTime } from 'date-fns'
import { v4 as uuidv4 } from 'uuid'
import AbstractDBModel from '../../model/AbstractDBModel'

Expand Down Expand Up @@ -48,14 +47,14 @@ export default class Repository<T extends AbstractDBModel> {
}

async save(entity: T): Promise<T> {
const currentTime = getTime(new Date())
const currentTime = new Date().toISOString()

const { id, rev, ...valuesToSave } = entity
const savedEntity = await this.db.put({
_id: uuidv4(),
...valuesToSave,
createdDate: currentTime,
lastUpdatedDate: currentTime,
createdAt: currentTime,
updatedAt: currentTime,
})
return this.find(savedEntity.id)
}
Expand All @@ -73,7 +72,7 @@ export default class Repository<T extends AbstractDBModel> {
_id: id,
_rev: rev,
...dataToSave,
lastUpdatedDate: getTime(new Date()),
updatedAt: new Date().toISOString(),
}

await this.db.put(entityToUpdate)
Expand Down
4 changes: 2 additions & 2 deletions src/model/AbstractDBModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default interface AbstractDBModel {
id: string
rev: string
createdDate: number
lastUpdatedDate: number
createdAt: string
updatedAt: string
}

0 comments on commit 8827df1

Please sign in to comment.