move most isikukood.js usage to utils
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { format } from 'date-fns';
|
||||
import Isikukood, { Gender } from 'isikukood';
|
||||
import { Tables } from '@/packages/supabase/src/database.types';
|
||||
import { DATE_FORMAT, DATE_TIME_FORMAT } from '@/lib/constants';
|
||||
import PersonalCode from '../utils';
|
||||
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
|
||||
@@ -73,15 +73,15 @@ export const getPatient = ({
|
||||
lastName: string,
|
||||
firstName: string,
|
||||
}) => {
|
||||
const isikukood = new Isikukood(idCode);
|
||||
const { dob, gender } = PersonalCode.parsePersonalCode(idCode);
|
||||
return `<Patsient>
|
||||
<IsikukoodiOID>1.3.6.1.4.1.28284.6.2.2.1</IsikukoodiOID>
|
||||
<Isikukood>${idCode}</Isikukood>
|
||||
<PerekonnaNimi>${lastName}</PerekonnaNimi>
|
||||
<EesNimi>${firstName}</EesNimi>
|
||||
<SynniAeg>${format(isikukood.getBirthday(), DATE_FORMAT)}</SynniAeg>
|
||||
<SynniAeg>${format(dob, DATE_FORMAT)}</SynniAeg>
|
||||
<SuguOID>1.3.6.1.4.1.28284.6.2.3.16.2</SuguOID>
|
||||
<Sugu>${isikukood.getGender() === Gender.MALE ? 'M' : 'N'}</Sugu>
|
||||
<Sugu>${gender === 'M' ? 'M' : 'N'}</Sugu>
|
||||
</Patsient>`;
|
||||
};
|
||||
|
||||
|
||||
61
lib/utils.ts
61
lib/utils.ts
@@ -91,8 +91,61 @@ export function getBmiBackgroundColor(bmiStatus: BmiCategory | null): string {
|
||||
}
|
||||
|
||||
export function getGenderStringFromPersonalCode(personalCode: string) {
|
||||
const person = new Isikukood(personalCode);
|
||||
if (person.getGender() === Gender.FEMALE) return 'common:female';
|
||||
if (person.getGender() === Gender.MALE) return 'common:male';
|
||||
return 'common:unknown';
|
||||
switch (PersonalCode.parsePersonalCode(personalCode).gender) {
|
||||
case 'F':
|
||||
return 'common:female';
|
||||
case 'M':
|
||||
return 'common:male';
|
||||
default:
|
||||
return 'common:unknown';
|
||||
}
|
||||
}
|
||||
|
||||
type AgeRange = '18-29' | '30-39' | '40-49' | '50-59' | '60';
|
||||
export default class PersonalCode {
|
||||
static getPersonalCode(personalCode: string | null) {
|
||||
if (!personalCode) {
|
||||
return null;
|
||||
}
|
||||
if (personalCode.toLowerCase().startsWith('ee')) {
|
||||
return personalCode.substring(2);
|
||||
}
|
||||
return personalCode;
|
||||
}
|
||||
|
||||
static parsePersonalCode(personalCode: string): {
|
||||
ageRange: AgeRange;
|
||||
gender: 'M' | 'F';
|
||||
dob: Date;
|
||||
age: number;
|
||||
} {
|
||||
const parsed = new Isikukood(personalCode);
|
||||
const ageRange = (() => {
|
||||
const age = parsed.getAge();
|
||||
if (age >= 18 && age <= 29) {
|
||||
return '18-29';
|
||||
}
|
||||
if (age >= 30 && age <= 39) {
|
||||
return '30-39';
|
||||
}
|
||||
if (age >= 40 && age <= 49) {
|
||||
return '40-49';
|
||||
}
|
||||
if (age >= 50 && age <= 59) {
|
||||
return '50-59';
|
||||
}
|
||||
if (age >= 60) {
|
||||
return '60';
|
||||
}
|
||||
throw new Error('Age range not supported');
|
||||
})();
|
||||
const gender = parsed.getGender() === Gender.MALE ? 'M' : 'F';
|
||||
|
||||
return {
|
||||
ageRange,
|
||||
gender,
|
||||
dob: parsed.getBirthday(),
|
||||
age: parsed.getAge(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user