From 8827df18d7141b9b6f94d00fc99b1bf00fae66dc Mon Sep 17 00:00:00 2001 From: Jack Meyer Date: Wed, 4 Mar 2020 19:34:06 -0600 Subject: [PATCH] feat: change to use standard naming and use iso dates fix #1879 --- .../clients/db/AppointmentRepository.test.ts | 7 ++-- .../clients/db/PatientRepository.test.ts | 32 +++++++++---------- src/clients/db/Repository.ts | 9 +++--- src/model/AbstractDBModel.ts | 4 +-- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/__tests__/clients/db/AppointmentRepository.test.ts b/src/__tests__/clients/db/AppointmentRepository.test.ts index 45d6fede31..f05e4d4ebb 100644 --- a/src/__tests__/clients/db/AppointmentRepository.test.ts +++ b/src/__tests__/clients/db/AppointmentRepository.test.ts @@ -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 @@ -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() }) }) }) diff --git a/src/__tests__/clients/db/PatientRepository.test.ts b/src/__tests__/clients/db/PatientRepository.test.ts index f0e2224e9b..6d24adcb2a 100644 --- a/src/__tests__/clients/db/PatientRepository.test.ts +++ b/src/__tests__/clients/db/PatientRepository.test.ts @@ -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 @@ -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) }) }) @@ -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) }) }) diff --git a/src/clients/db/Repository.ts b/src/clients/db/Repository.ts index f88ae02a2a..f1d5f52877 100644 --- a/src/clients/db/Repository.ts +++ b/src/clients/db/Repository.ts @@ -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' @@ -48,14 +47,14 @@ export default class Repository { } async save(entity: T): Promise { - 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) } @@ -73,7 +72,7 @@ export default class Repository { _id: id, _rev: rev, ...dataToSave, - lastUpdatedDate: getTime(new Date()), + updatedAt: new Date().toISOString(), } await this.db.put(entityToUpdate) diff --git a/src/model/AbstractDBModel.ts b/src/model/AbstractDBModel.ts index 6bbac914bf..6b5a07ea06 100644 --- a/src/model/AbstractDBModel.ts +++ b/src/model/AbstractDBModel.ts @@ -1,6 +1,6 @@ export default interface AbstractDBModel { id: string rev: string - createdDate: number - lastUpdatedDate: number + createdAt: string + updatedAt: string }