MED-186: added upsert to balance if increased

MED-185: add wallet balance. to new employee
This commit is contained in:
Danel Kungla
2025-10-02 18:50:16 +03:00
parent 0c5d38f274
commit d8f314cb00
6 changed files with 244 additions and 73 deletions

View File

@@ -19,6 +19,7 @@ EMAIL_PORT= # or 465 for SSL
EMAIL_TLS= # or false for SSL (see provider documentation) EMAIL_TLS= # or false for SSL (see provider documentation)
NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY= NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=
MEDUSA_SECRET_API_KEY==
NEXT_PUBLIC_MONTONIO_ACCESS_KEY=7da5d7fa-3383-4997-9435-46aa818f4ead NEXT_PUBLIC_MONTONIO_ACCESS_KEY=7da5d7fa-3383-4997-9435-46aa818f4ead
MONTONIO_SECRET_KEY=rNZkzwxOiH93mzkdV53AvhSsbGidrgO2Kl5lE/IT7cvo MONTONIO_SECRET_KEY=rNZkzwxOiH93mzkdV53AvhSsbGidrgO2Kl5lE/IT7cvo

View File

@@ -98,13 +98,13 @@ To access admin pages follow these steps:
- Register new user - Register new user
- Go to Profile and add Multi-Factor Authentication - Go to Profile and add Multi-Factor Authentication
- Authenticate with mfa (at current time profile page prompts it again) - Authenticate with mfa (at current time profile page prompts it again)
- update your role. look at `supabase/sql/super-admin.sql` - update your `account.application_role` to `super_admin`.
- Sign out and Sign in - Sign out and Sign in
## Company User ## Company User
- With admin account go to `http://localhost:3000/admin/accounts` - With admin account go to `http://localhost:3000/admin/accounts`
- For Create Company Account to work you need to have rows in `medreport.roles` table. For that you can sql in `supabase/sql/super-admin.sql` - For Create Company Account to work you need to have rows in `medreport.roles` table.
## Start email server ## Start email server

View File

@@ -181,80 +181,74 @@ export function UpdateAccountForm({
)} )}
/> />
{!isEmailUser && ( <>
<> <FormField
name="city"
render={({ field }) => (
<FormItem>
<FormLabel>
<Trans i18nKey={'common:formField:city'} />
</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="flex flex-row justify-between gap-4">
<FormField <FormField
name="city" name="weight"
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem className="flex-1 basis-0">
<FormLabel> <FormLabel>
<Trans i18nKey={'common:formField:city'} /> <Trans i18nKey={'common:formField:weight'} />
</FormLabel> </FormLabel>
<FormControl> <FormControl>
<Input {...field} /> <Input
type="number"
placeholder="kg"
{...field}
value={field.value ?? ''}
onChange={(e) =>
field.onChange(
e.target.value === '' ? null : Number(e.target.value),
)
}
/>
</FormControl> </FormControl>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>
)} )}
/> />
<div className="flex flex-row justify-between gap-4"> <FormField
<FormField name="height"
name="weight" render={({ field }) => (
render={({ field }) => ( <FormItem className="flex-1 basis-0">
<FormItem className="flex-1 basis-0"> <FormLabel>
<FormLabel> <Trans i18nKey={'common:formField:height'} />
<Trans i18nKey={'common:formField:weight'} /> </FormLabel>
</FormLabel> <FormControl>
<FormControl> <Input
<Input placeholder="cm"
type="number" type="number"
placeholder="kg" {...field}
{...field} value={field.value ?? ''}
value={field.value ?? ''} onChange={(e) =>
onChange={(e) => field.onChange(
field.onChange( e.target.value === '' ? null : Number(e.target.value),
e.target.value === '' )
? null }
: Number(e.target.value), />
) </FormControl>
} <FormMessage />
/> </FormItem>
</FormControl> )}
<FormMessage /> />
</FormItem> </div>
)} </>
/>
<FormField
name="height"
render={({ field }) => (
<FormItem className="flex-1 basis-0">
<FormLabel>
<Trans i18nKey={'common:formField:height'} />
</FormLabel>
<FormControl>
<Input
placeholder="cm"
type="number"
{...field}
value={field.value ?? ''}
onChange={(e) =>
field.onChange(
e.target.value === ''
? null
: Number(e.target.value),
)
}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
</>
)}
<FormField <FormField
name="userConsent" name="userConsent"

View File

@@ -9,12 +9,12 @@ import { ShoppingCart } from 'lucide-react';
import { AppLogo } from '@kit/shared/components/app-logo'; import { AppLogo } from '@kit/shared/components/app-logo';
import { ProfileAccountDropdownContainer } from '@kit/shared/components/personal-account-dropdown-container'; import { ProfileAccountDropdownContainer } from '@kit/shared/components/personal-account-dropdown-container';
import { Search } from '@kit/shared/components/ui/search';
import { Button } from '@kit/ui/button'; import { Button } from '@kit/ui/button';
import { Card } from '@kit/ui/shadcn/card'; import { Card } from '@kit/ui/shadcn/card';
import { Trans } from '@kit/ui/trans'; import { Trans } from '@kit/ui/trans';
import { UserNotifications } from '../_components/user-notifications'; import { UserNotifications } from '../_components/user-notifications';
import { getAccountBalanceSummary } from '../_lib/server/balance-actions';
import { type UserWorkspace } from '../_lib/server/load-user-workspace'; import { type UserWorkspace } from '../_lib/server/load-user-workspace';
export async function HomeMenuNavigation(props: { export async function HomeMenuNavigation(props: {
@@ -23,6 +23,9 @@ export async function HomeMenuNavigation(props: {
}) { }) {
const { language } = await createI18nServerInstance(); const { language } = await createI18nServerInstance();
const { workspace, user, accounts } = props.workspace; const { workspace, user, accounts } = props.workspace;
const balanceSummary = workspace?.id
? await getAccountBalanceSummary(workspace.id)
: null;
const totalValue = props.cart?.total const totalValue = props.cart?.total
? formatCurrency({ ? formatCurrency({
currencyCode: props.cart.currency_code, currencyCode: props.cart.currency_code,
@@ -47,11 +50,16 @@ export async function HomeMenuNavigation(props: {
/> */} /> */}
<div className="flex items-center justify-end gap-3"> <div className="flex items-center justify-end gap-3">
{/* TODO: add wallet functionality
<Card className="px-6 py-2"> <Card className="px-6 py-2">
<span>€ {Number(0).toFixed(2).replace('.', ',')}</span> <span>
{formatCurrency({
value: balanceSummary?.totalBalance || 0,
locale: language,
currencyCode: 'EUR',
})}
</span>
</Card> </Card>
*/}
{hasCartItems && ( {hasCartItems && (
<Button <Button
className="relative mr-0 h-10 cursor-pointer border-1 px-4 py-2" className="relative mr-0 h-10 cursor-pointer border-1 px-4 py-2"

View File

@@ -1,4 +0,0 @@
ALTER TABLE medreport.connected_online_reservation
ADD CONSTRAINT fk_reservation_location_sync_id
FOREIGN KEY (location_sync_id)
REFERENCES medreport.connected_online_locations(sync_id);

View File

@@ -0,0 +1,172 @@
create or replace function medreport.upsert_health_benefits(
p_benefit_distribution_schedule_id uuid
)
returns void
language plpgsql
security definer
as $$
declare
member_record record;
expires_date timestamp with time zone;
v_company_id uuid;
v_benefit_amount numeric;
existing_entry_id uuid;
begin
-- Expires on first day of next year.
expires_date := date_trunc('year', now() + interval '1 year');
-- Get company_id and benefit_amount from benefit_distribution_schedule
select company_id, benefit_amount into v_company_id, v_benefit_amount
from medreport.benefit_distribution_schedule
where id = p_benefit_distribution_schedule_id;
-- Get all personal accounts that are members of this company
for member_record in
select distinct a.id as personal_account_id
from medreport.accounts a
join medreport.accounts_memberships am on a.id = am.user_id
where am.account_id = v_company_id
and a.is_personal_account = true
loop
-- Check if there is already a balance entry for this personal account from the same company in same month
select id into existing_entry_id
from medreport.account_balance_entries
where entry_type = 'benefit'
and account_id = member_record.personal_account_id
and source_company_id = v_company_id
and date_trunc('month', created_at) = date_trunc('month', now())
LIMIT 1;
if existing_entry_id is not null then
update medreport.account_balance_entries set
amount = v_benefit_amount,
expires_at = expires_date,
benefit_distribution_schedule_id = p_benefit_distribution_schedule_id
where id = existing_entry_id;
else
-- Insert new balance entry for personal account
insert into medreport.account_balance_entries (
account_id,
amount,
entry_type,
description,
source_company_id,
created_by,
expires_at,
benefit_distribution_schedule_id
) values (
member_record.personal_account_id,
v_benefit_amount,
'benefit',
'Health benefit from company',
v_company_id,
auth.uid(),
expires_date,
p_benefit_distribution_schedule_id
);
end if;
end loop;
end;
$$;
grant execute on function medreport.upsert_health_benefits(uuid) to authenticated, service_role;
create or replace function medreport.process_periodic_benefit_distributions()
returns void
language plpgsql
as $$
declare
schedule_record record;
next_distribution_date timestamp with time zone;
begin
-- Get all active schedules that are due for distribution
for schedule_record in
select *
from medreport.benefit_distribution_schedule
where is_active = true
and next_distribution_at <= now()
loop
-- Distribute benefits
perform medreport.upsert_health_benefits(
schedule_record.id
);
-- Calculate next distribution date
next_distribution_date := medreport.calculate_next_distribution_date(
schedule_record.benefit_occurrence,
now()
);
-- Update the schedule
update medreport.benefit_distribution_schedule
set
last_distributed_at = now(),
next_distribution_at = next_distribution_date,
updated_at = now()
where id = schedule_record.id;
end loop;
end;
$$;
create or replace function medreport.trigger_distribute_benefits()
returns trigger
language plpgsql
security definer
as $$
declare
v_benefit_distribution_schedule_id uuid;
begin
-- Only distribute if benefit_amount is set and greater than 0
if new.benefit_amount is not null and new.benefit_amount > 0 then
-- Create or update the distribution schedule for future distributions
v_benefit_distribution_schedule_id := medreport.upsert_benefit_distribution_schedule(
new.account_id,
new.benefit_amount,
coalesce(new.benefit_occurance, 'yearly')
);
-- Distribute benefits to all company members immediately
if new.benefit_amount > old.benefit_amount then
perform medreport.upsert_health_benefits(
v_benefit_distribution_schedule_id
);
end if;
else
-- If benefit_amount is 0 or null, deactivate the schedule
update medreport.benefit_distribution_schedule
set is_active = false, updated_at = now()
where company_id = new.account_id;
end if;
return new;
end;
$$;
drop function if exists medreport.distribute_health_benefits(uuid);
create or replace function medreport.trigger_benefits_on_new_membership()
returns trigger
language plpgsql
security definer
as $$
declare
v_schedule_id uuid;
begin
select bds.id into v_schedule_id
from medreport.benefit_distribution_schedule bds
where bds.company_id = new.account_id
limit 1;
if v_schedule_id is not NULL then
PERFORM medreport.upsert_health_benefits(v_schedule_id);
end if;
return new;
end;
$$;
create trigger trigger_insert_benefits_on_accounts_membership
after insert on medreport.accounts_memberships
for EACH row
execute function medreport.trigger_benefits_on_new_membership();