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

Commit

Permalink
feat: add created date and last updated date
Browse files Browse the repository at this point in the history
fix #1879
  • Loading branch information
jackcmeyer committed Mar 4, 2020
1 parent 258cab8 commit 16ab294
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/__tests__/clients/db/AppointmentRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -34,5 +35,16 @@ describe('Appointment Repository', () => {

await appointments.remove(await appointments.get(newAppointment.id))
})

it('should generate a timestamp for created date and last updated date', async () => {
const newAppointment = await AppointmentRepository.save({
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()
})
})
})
48 changes: 48 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 * as shortid from 'shortid'
import { fromUnixTime, 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 @@ -109,6 +110,29 @@ describe('patient repository', () => {

expect(shortid.isValid(newPatient.code)).toBeTruthy()
})

it('should generate a timestamp for created date and last updated date', async () => {
const newPatient = await PatientRepository.save({
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()
})

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 newPatient = await PatientRepository.save({
fullName: 'test1 test1',
createdDate: unexpectedTime,
lastUpdatedDate: unexpectedTime,
} as Patient)

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

describe('saveOrUpdate', () => {
Expand Down Expand Up @@ -156,6 +180,30 @@ describe('patient repository', () => {
expect(updatedPatient.fullName).toEqual(existingPatient.fullName)
expect(updatedPatient.givenName).toEqual('givenName')
})

it('should update the last updated date', async () => {
const existingPatient = await PatientRepository.save({
fullName: 'test7 test7',
} as Patient)
existingPatient.givenName = 'givenName'

const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient)

expect(isAfter(updatedPatient.lastUpdatedDate, updatedPatient.createdDate)).toBeTruthy()
expect(updatedPatient.lastUpdatedDate).not.toEqual(existingPatient.lastUpdatedDate)
})

it('should not update the created date', async () => {
const existingPatient = await PatientRepository.save({
fullName: 'test7 test7',
} as Patient)
existingPatient.givenName = 'givenName'
existingPatient.createdDate = getTime(new Date())

const updatedPatient = await PatientRepository.saveOrUpdate(existingPatient)

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

describe('delete', () => {
Expand Down
10 changes: 9 additions & 1 deletion src/clients/db/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ export default class Repository<T extends AbstractDBModel> {
}

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

const { id, rev, ...valuesToSave } = entity
const savedEntity = await this.db.put({ _id: uuidv4(), ...valuesToSave })
const savedEntity = await this.db.put({
_id: uuidv4(),
...valuesToSave,
createdDate: currentTime,
lastUpdatedDate: currentTime,
})
return this.find(savedEntity.id)
}

Expand All @@ -66,6 +73,7 @@ export default class Repository<T extends AbstractDBModel> {
_id: id,
_rev: rev,
...dataToSave,
lastUpdatedDate: getTime(new Date()),
}

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

0 comments on commit 16ab294

Please sign in to comment.