move most isikukood.js usage to utils

This commit is contained in:
2025-09-08 23:42:50 +03:00
parent ccdfd5872b
commit 0081e8948b
5 changed files with 75 additions and 51 deletions

View File

@@ -16,7 +16,6 @@ import {
} from 'lucide-react';
import { pathsConfig } from '@kit/shared/config';
import { getPersonParameters } from '@kit/shared/utils';
import { Button } from '@kit/ui/button';
import {
Card,
@@ -30,7 +29,7 @@ import { cn } from '@kit/ui/utils';
import { isNil } from 'lodash';
import { BmiCategory } from '~/lib/types/bmi';
import {
import PersonalCode, {
bmiFromMetric,
getBmiBackgroundColor,
getBmiStatus,
@@ -145,21 +144,19 @@ export default function Dashboard({
'id'
>[];
}) {
const params = getPersonParameters(account.personal_code!);
const bmiStatus = getBmiStatus(bmiThresholds, {
age: params?.age || 0,
height: account.accountParams?.height || 0,
weight: account.accountParams?.weight || 0,
});
const height = account.accountParams?.height || 0;
const weight = account.accountParams?.weight || 0;
const { age = 0, gender } = PersonalCode.parsePersonalCode(account.personal_code!);
const bmiStatus = getBmiStatus(bmiThresholds, { age, height, weight });
return (
<>
<div className="xs:grid-cols-2 grid auto-rows-fr gap-3 sm:grid-cols-4 lg:grid-cols-5">
{cards({
gender: params?.gender,
age: params?.age,
height: account.accountParams?.height,
weight: account.accountParams?.weight,
gender,
age,
height,
weight,
bmiStatus,
smoking: account.accountParams?.isSmoker,
}).map(

View File

@@ -1,5 +1,4 @@
import { cache } from 'react';
import Isikukood, { Gender } from 'isikukood';
import { listProductTypes, listProducts } from "@lib/data/products";
import { listRegions } from '@lib/data/regions';
@@ -8,6 +7,7 @@ import type { StoreProduct } from '@medusajs/types';
import { loadCurrentUserAccount } from './load-user-account';
import { AccountWithParams } from '@/packages/features/accounts/src/server/api';
import { AnalysisPackageWithVariant } from '@kit/shared/components/select-analysis-package';
import PersonalCode from '~/lib/utils';
async function countryCodesLoader() {
const countryCodes = await listRegions().then((regions) =>
@@ -32,27 +32,8 @@ function userSpecificVariantLoader({
if (!personalCode) {
throw new Error('Personal code not found');
}
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';
const { gender, ageRange } = PersonalCode.parsePersonalCode(personalCode);
return ({
product,

View File

@@ -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>`;
};

View File

@@ -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(),
}
}
}

View File

@@ -3,6 +3,7 @@ import { SupabaseClient } from '@supabase/supabase-js';
import { Database } from '@kit/supabase/database';
import { AnalysisResultDetails, UserAnalysis } from '../types/accounts';
import PersonalCode from '~/lib/utils';
export type AccountWithParams =
Database['medreport']['Tables']['accounts']['Row'] & {
@@ -71,15 +72,7 @@ class AccountsApi {
const { personal_code, ...rest } = data;
return {
...rest,
personal_code: (() => {
if (!personal_code) {
return null;
}
if (personal_code.toLowerCase().startsWith('ee')) {
return personal_code.substring(2);
}
return personal_code;
})(),
personal_code: PersonalCode.getPersonalCode(personal_code),
};
}