diff --git a/src/__tests__/clients/db/AppointmentRepository.test.ts b/src/__tests__/clients/db/AppointmentRepository.test.ts index 0dfbe9bf59..45d6fede31 100644 --- a/src/__tests__/clients/db/AppointmentRepository.test.ts +++ b/src/__tests__/clients/db/AppointmentRepository.test.ts @@ -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 @@ -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() + }) }) }) diff --git a/src/__tests__/clients/db/PatientRepository.test.ts b/src/__tests__/clients/db/PatientRepository.test.ts index b1de49819e..42486b2507 100644 --- a/src/__tests__/clients/db/PatientRepository.test.ts +++ b/src/__tests__/clients/db/PatientRepository.test.ts @@ -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 @@ -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', () => { @@ -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', () => { diff --git a/src/clients/db/Repository.ts b/src/clients/db/Repository.ts index 5fe3244f18..f88ae02a2a 100644 --- a/src/clients/db/Repository.ts +++ b/src/clients/db/Repository.ts @@ -48,8 +48,15 @@ export default class Repository { } async save(entity: T): Promise { + 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) } @@ -66,6 +73,7 @@ export default class Repository { _id: id, _rev: rev, ...dataToSave, + lastUpdatedDate: getTime(new Date()), } await this.db.put(entityToUpdate) diff --git a/src/model/AbstractDBModel.ts b/src/model/AbstractDBModel.ts index 0dc80f4b4b..6bbac914bf 100644 --- a/src/model/AbstractDBModel.ts +++ b/src/model/AbstractDBModel.ts @@ -1,4 +1,6 @@ export default interface AbstractDBModel { id: string rev: string + createdDate: number + lastUpdatedDate: number }