Merge branch 'develop' into MED-188
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
@@ -221,16 +221,6 @@ function useGetColumns(
|
|||||||
}
|
}
|
||||||
>
|
>
|
||||||
<RoleBadge role={role} />
|
<RoleBadge role={role} />
|
||||||
|
|
||||||
<If condition={isPrimaryOwner}>
|
|
||||||
<span
|
|
||||||
className={
|
|
||||||
'rounded-md bg-yellow-400 px-2.5 py-1 text-xs font-medium dark:text-black'
|
|
||||||
}
|
|
||||||
>
|
|
||||||
{t('primaryOwnerLabel')}
|
|
||||||
</span>
|
|
||||||
</If>
|
|
||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,16 +6,19 @@ import { Trans } from '@kit/ui/trans';
|
|||||||
type Role = string;
|
type Role = string;
|
||||||
|
|
||||||
const roles = {
|
const roles = {
|
||||||
owner: '',
|
owner: 'bg-yellow-400 text-black',
|
||||||
member:
|
member:
|
||||||
'bg-blue-50 hover:bg-blue-50 text-blue-500 dark:bg-blue-500/10 dark:hover:bg-blue-500/10',
|
'bg-blue-50 text-blue-500 dark:bg-blue-500/10 dark:hover:bg-blue-500/10',
|
||||||
};
|
};
|
||||||
|
|
||||||
const roleClassNameBuilder = cva('font-medium capitalize shadow-none', {
|
const roleClassNameBuilder = cva(
|
||||||
variants: {
|
'px-2.5 py-1 font-medium capitalize shadow-none',
|
||||||
role: roles,
|
{
|
||||||
|
variants: {
|
||||||
|
role: roles,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
export function RoleBadge({ role }: { role: Role }) {
|
export function RoleBadge({ role }: { role: Role }) {
|
||||||
// @ts-expect-error: hard to type this since users can add custom roles
|
// @ts-expect-error: hard to type this since users can add custom roles
|
||||||
|
|||||||
@@ -173,9 +173,6 @@ export const acceptInvitationAction = enhanceAction(
|
|||||||
throw new Error('Failed to accept invitation');
|
throw new Error('Failed to accept invitation');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure new account gets company benefits added to balance
|
|
||||||
await accountBalanceService.processPeriodicBenefitDistributions();
|
|
||||||
|
|
||||||
// Increase the seats for the account
|
// Increase the seats for the account
|
||||||
await perSeatBillingService.increaseSeats(accountId);
|
await perSeatBillingService.increaseSeats(accountId);
|
||||||
|
|
||||||
|
|||||||
@@ -341,6 +341,7 @@ export type Database = {
|
|||||||
Row: {
|
Row: {
|
||||||
account_id: string
|
account_id: string
|
||||||
amount: number
|
amount: number
|
||||||
|
benefit_distribution_schedule_id: string | null
|
||||||
created_at: string
|
created_at: string
|
||||||
created_by: string | null
|
created_by: string | null
|
||||||
description: string | null
|
description: string | null
|
||||||
@@ -348,14 +349,15 @@ export type Database = {
|
|||||||
expires_at: string | null
|
expires_at: string | null
|
||||||
id: string
|
id: string
|
||||||
is_active: boolean
|
is_active: boolean
|
||||||
is_analysis_order: boolean
|
is_analysis_order: boolean | null
|
||||||
is_analysis_package_order: boolean
|
is_analysis_package_order: boolean | null
|
||||||
reference_id: string | null
|
reference_id: string | null
|
||||||
source_company_id: string | null
|
source_company_id: string | null
|
||||||
}
|
}
|
||||||
Insert: {
|
Insert: {
|
||||||
account_id: string
|
account_id: string
|
||||||
amount: number
|
amount: number
|
||||||
|
benefit_distribution_schedule_id?: string | null
|
||||||
created_at?: string
|
created_at?: string
|
||||||
created_by?: string | null
|
created_by?: string | null
|
||||||
description?: string | null
|
description?: string | null
|
||||||
@@ -363,14 +365,15 @@ export type Database = {
|
|||||||
expires_at?: string | null
|
expires_at?: string | null
|
||||||
id?: string
|
id?: string
|
||||||
is_active?: boolean
|
is_active?: boolean
|
||||||
is_analysis_order?: boolean
|
is_analysis_order?: boolean | null
|
||||||
is_analysis_package_order?: boolean
|
is_analysis_package_order?: boolean | null
|
||||||
reference_id?: string | null
|
reference_id?: string | null
|
||||||
source_company_id?: string | null
|
source_company_id?: string | null
|
||||||
}
|
}
|
||||||
Update: {
|
Update: {
|
||||||
account_id?: string
|
account_id?: string
|
||||||
amount?: number
|
amount?: number
|
||||||
|
benefit_distribution_schedule_id?: string | null
|
||||||
created_at?: string
|
created_at?: string
|
||||||
created_by?: string | null
|
created_by?: string | null
|
||||||
description?: string | null
|
description?: string | null
|
||||||
@@ -378,8 +381,8 @@ export type Database = {
|
|||||||
expires_at?: string | null
|
expires_at?: string | null
|
||||||
id?: string
|
id?: string
|
||||||
is_active?: boolean
|
is_active?: boolean
|
||||||
is_analysis_order?: boolean
|
is_analysis_order?: boolean | null
|
||||||
is_analysis_package_order?: boolean
|
is_analysis_package_order?: boolean | null
|
||||||
reference_id?: string | null
|
reference_id?: string | null
|
||||||
source_company_id?: string | null
|
source_company_id?: string | null
|
||||||
}
|
}
|
||||||
@@ -2214,6 +2217,8 @@ export type Database = {
|
|||||||
p_account_id: string
|
p_account_id: string
|
||||||
p_amount: number
|
p_amount: number
|
||||||
p_description: string
|
p_description: string
|
||||||
|
p_is_analysis_order?: boolean
|
||||||
|
p_is_analysis_package_order?: boolean
|
||||||
p_reference_id?: string
|
p_reference_id?: string
|
||||||
}
|
}
|
||||||
Returns: boolean
|
Returns: boolean
|
||||||
@@ -2271,14 +2276,6 @@ export type Database = {
|
|||||||
updated_by: string | null
|
updated_by: string | null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
distribute_health_benefits: {
|
|
||||||
Args: {
|
|
||||||
p_benefit_amount: number
|
|
||||||
p_benefit_occurrence?: string
|
|
||||||
p_company_id: string
|
|
||||||
}
|
|
||||||
Returns: undefined
|
|
||||||
}
|
|
||||||
get_account_balance: {
|
get_account_balance: {
|
||||||
Args: { p_account_id: string }
|
Args: { p_account_id: string }
|
||||||
Returns: number
|
Returns: number
|
||||||
@@ -2317,14 +2314,12 @@ export type Database = {
|
|||||||
}[]
|
}[]
|
||||||
}
|
}
|
||||||
get_benefits_usages_for_company_members: {
|
get_benefits_usages_for_company_members: {
|
||||||
Args: {
|
Args: { p_account_id: string }
|
||||||
p_account_id: string
|
|
||||||
}
|
|
||||||
Returns: {
|
Returns: {
|
||||||
personal_account_id: string
|
|
||||||
benefit_amount: number
|
benefit_amount: number
|
||||||
benefit_unused_amount: number
|
benefit_unused_amount: number
|
||||||
}
|
personal_account_id: string
|
||||||
|
}[]
|
||||||
}
|
}
|
||||||
get_config: {
|
get_config: {
|
||||||
Args: Record<PropertyKey, never>
|
Args: Record<PropertyKey, never>
|
||||||
@@ -2530,6 +2525,10 @@ export type Database = {
|
|||||||
p_benefit_occurrence: string
|
p_benefit_occurrence: string
|
||||||
p_company_id: string
|
p_company_id: string
|
||||||
}
|
}
|
||||||
|
Returns: string
|
||||||
|
}
|
||||||
|
upsert_health_benefits: {
|
||||||
|
Args: { p_benefit_distribution_schedule_id: string }
|
||||||
Returns: undefined
|
Returns: undefined
|
||||||
}
|
}
|
||||||
upsert_order: {
|
upsert_order: {
|
||||||
@@ -2619,6 +2618,7 @@ export type Database = {
|
|||||||
| "settings.manage"
|
| "settings.manage"
|
||||||
| "members.manage"
|
| "members.manage"
|
||||||
| "invites.manage"
|
| "invites.manage"
|
||||||
|
| "benefit.manage"
|
||||||
application_role: "user" | "doctor" | "super_admin"
|
application_role: "user" | "doctor" | "super_admin"
|
||||||
billing_provider: "stripe" | "lemon-squeezy" | "paddle" | "montonio"
|
billing_provider: "stripe" | "lemon-squeezy" | "paddle" | "montonio"
|
||||||
connected_online_order_status:
|
connected_online_order_status:
|
||||||
@@ -8540,6 +8540,7 @@ export const Constants = {
|
|||||||
"settings.manage",
|
"settings.manage",
|
||||||
"members.manage",
|
"members.manage",
|
||||||
"invites.manage",
|
"invites.manage",
|
||||||
|
"benefit.manage",
|
||||||
],
|
],
|
||||||
application_role: ["user", "doctor", "super_admin"],
|
application_role: ["user", "doctor", "super_admin"],
|
||||||
billing_provider: ["stripe", "lemon-squeezy", "paddle", "montonio"],
|
billing_provider: ["stripe", "lemon-squeezy", "paddle", "montonio"],
|
||||||
|
|||||||
@@ -88,7 +88,7 @@
|
|||||||
},
|
},
|
||||||
"roles": {
|
"roles": {
|
||||||
"owner": {
|
"owner": {
|
||||||
"label": "Admin"
|
"label": "Haldur"
|
||||||
},
|
},
|
||||||
"member": {
|
"member": {
|
||||||
"label": "Liige"
|
"label": "Liige"
|
||||||
|
|||||||
@@ -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);
|
|
||||||
172
supabase/migrations/20251002170200_upsert_account_balance.sql
Normal file
172
supabase/migrations/20251002170200_upsert_account_balance.sql
Normal 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();
|
||||||
Reference in New Issue
Block a user