diff --git a/package.json b/package.json index c8d444d596..067141828b 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "redux": "~4.0.5", "redux-thunk": "~2.3.0", "shortid": "^2.2.15", - "typescript": "~3.8.2" + "typescript": "~3.8.2", + "uuid": "^7.0.1" }, "repository": { "type": "git", diff --git a/src/__tests__/clients/db/AppointmentRepository.test.ts b/src/__tests__/clients/db/AppointmentRepository.test.ts index 139ae20189..0dfbe9bf59 100644 --- a/src/__tests__/clients/db/AppointmentRepository.test.ts +++ b/src/__tests__/clients/db/AppointmentRepository.test.ts @@ -1,7 +1,8 @@ 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 describe('Appointment Repository', () => { it('should create a repository with the database set to the appointments database', () => { @@ -24,12 +25,12 @@ describe('Appointment Repository', () => { }) describe('save', () => { - it('should create an id that is a timestamp', async () => { + it('should create an id that is a uuid', async () => { const newAppointment = await AppointmentRepository.save({ patientId: 'id', } as Appointment) - expect(fromUnixTime(parseInt(newAppointment.id, 10)).getTime() > 0).toBeTruthy() + expect(uuidV4Regex.test(newAppointment.id)).toBeTruthy() await appointments.remove(await appointments.get(newAppointment.id)) }) diff --git a/src/__tests__/clients/db/PatientRepository.test.ts b/src/__tests__/clients/db/PatientRepository.test.ts index 1ad6ac3770..b1de49819e 100644 --- a/src/__tests__/clients/db/PatientRepository.test.ts +++ b/src/__tests__/clients/db/PatientRepository.test.ts @@ -1,9 +1,10 @@ import { patients } from 'config/pouchdb' import PatientRepository from 'clients/db/PatientRepository' import Patient from 'model/Patient' -import { fromUnixTime } from 'date-fns' import * as shortid from 'shortid' +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 + async function removeAllDocs() { // eslint-disable-next-line const allDocs = await patients.allDocs({ include_docs: true }) @@ -93,12 +94,12 @@ describe('patient repository', () => { await removeAllDocs() }) - it('should generate an id that is a timestamp for the patient', async () => { + it('should generate an id that is a uuid for the patient', async () => { const newPatient = await PatientRepository.save({ fullName: 'test test', } as Patient) - expect(fromUnixTime(parseInt(newPatient.id, 10)).getTime() > 0).toBeTruthy() + expect(uuidV4Regex.test(newPatient.id)).toBeTruthy() }) it('should generate a patient code', async () => { diff --git a/src/clients/db/Repository.ts b/src/clients/db/Repository.ts index 643dad4e31..5fe3244f18 100644 --- a/src/clients/db/Repository.ts +++ b/src/clients/db/Repository.ts @@ -1,5 +1,6 @@ /* eslint "@typescript-eslint/camelcase": "off" */ import { getTime } from 'date-fns' +import { v4 as uuidv4 } from 'uuid' import AbstractDBModel from '../../model/AbstractDBModel' function mapRow(row: any): any { @@ -48,7 +49,7 @@ export default class Repository { async save(entity: T): Promise { const { id, rev, ...valuesToSave } = entity - const savedEntity = await this.db.put({ _id: getTime(new Date()).toString(), ...valuesToSave }) + const savedEntity = await this.db.put({ _id: uuidv4(), ...valuesToSave }) return this.find(savedEntity.id) }