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

View File

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

View File

@@ -1,7 +1,7 @@
import { format } from 'date-fns'; import { format } from 'date-fns';
import Isikukood, { Gender } from 'isikukood';
import { Tables } from '@/packages/supabase/src/database.types'; import { Tables } from '@/packages/supabase/src/database.types';
import { DATE_FORMAT, DATE_TIME_FORMAT } from '@/lib/constants'; import { DATE_FORMAT, DATE_TIME_FORMAT } from '@/lib/constants';
import PersonalCode from '../utils';
const isProd = process.env.NODE_ENV === 'production'; const isProd = process.env.NODE_ENV === 'production';
@@ -73,15 +73,15 @@ export const getPatient = ({
lastName: string, lastName: string,
firstName: string, firstName: string,
}) => { }) => {
const isikukood = new Isikukood(idCode); const { dob, gender } = PersonalCode.parsePersonalCode(idCode);
return `<Patsient> return `<Patsient>
<IsikukoodiOID>1.3.6.1.4.1.28284.6.2.2.1</IsikukoodiOID> <IsikukoodiOID>1.3.6.1.4.1.28284.6.2.2.1</IsikukoodiOID>
<Isikukood>${idCode}</Isikukood> <Isikukood>${idCode}</Isikukood>
<PerekonnaNimi>${lastName}</PerekonnaNimi> <PerekonnaNimi>${lastName}</PerekonnaNimi>
<EesNimi>${firstName}</EesNimi> <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> <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>`; </Patsient>`;
}; };

View File

@@ -91,8 +91,61 @@ export function getBmiBackgroundColor(bmiStatus: BmiCategory | null): string {
} }
export function getGenderStringFromPersonalCode(personalCode: string) { export function getGenderStringFromPersonalCode(personalCode: string) {
const person = new Isikukood(personalCode); switch (PersonalCode.parsePersonalCode(personalCode).gender) {
if (person.getGender() === Gender.FEMALE) return 'common:female'; case 'F':
if (person.getGender() === Gender.MALE) return 'common:male'; return 'common:female';
return 'common:unknown'; 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 { Database } from '@kit/supabase/database';
import { AnalysisResultDetails, UserAnalysis } from '../types/accounts'; import { AnalysisResultDetails, UserAnalysis } from '../types/accounts';
import PersonalCode from '~/lib/utils';
export type AccountWithParams = export type AccountWithParams =
Database['medreport']['Tables']['accounts']['Row'] & { Database['medreport']['Tables']['accounts']['Row'] & {
@@ -71,15 +72,7 @@ class AccountsApi {
const { personal_code, ...rest } = data; const { personal_code, ...rest } = data;
return { return {
...rest, ...rest,
personal_code: (() => { personal_code: PersonalCode.getPersonalCode(personal_code),
if (!personal_code) {
return null;
}
if (personal_code.toLowerCase().startsWith('ee')) {
return personal_code.substring(2);
}
return personal_code;
})(),
}; };
} }