MED-188: add health benefit eligibility management
MED-188: add health benefit eligibility management
This commit is contained in:
@@ -120,6 +120,22 @@ export class AccountBalanceService {
|
||||
};
|
||||
}
|
||||
|
||||
async upsertHealthBenefitsBySchedule(
|
||||
benefitDistributionScheduleId: string,
|
||||
): Promise<void> {
|
||||
console.info('Updating health benefits...');
|
||||
const { error } = await this.supabase
|
||||
.schema('medreport')
|
||||
.rpc('upsert_health_benefits', {
|
||||
p_benefit_distribution_schedule_id: benefitDistributionScheduleId,
|
||||
});
|
||||
if (error) {
|
||||
console.error('Error Updating health benefits.', error);
|
||||
throw new Error('Failed Updating health benefits.');
|
||||
}
|
||||
console.info('Updating health benefits successfully');
|
||||
}
|
||||
|
||||
async processPeriodicBenefitDistributions(): Promise<void> {
|
||||
console.info('Processing periodic benefit distributions...');
|
||||
const { error } = await this.supabase
|
||||
|
||||
@@ -25,6 +25,7 @@ import { Trans } from '@kit/ui/trans';
|
||||
import { RemoveMemberDialog } from './remove-member-dialog';
|
||||
import { RoleBadge } from './role-badge';
|
||||
import { TransferOwnershipDialog } from './transfer-ownership-dialog';
|
||||
import UpdateEmployeeBenefitDialog from './update-employee-benefit-dialog';
|
||||
import { UpdateMemberRoleDialog } from './update-member-role-dialog';
|
||||
|
||||
type Members =
|
||||
@@ -34,6 +35,7 @@ interface Permissions {
|
||||
canUpdateRole: (roleHierarchy: number) => boolean;
|
||||
canRemoveFromAccount: (roleHierarchy: number) => boolean;
|
||||
canTransferOwnership: boolean;
|
||||
canUpdateBenefit: boolean;
|
||||
}
|
||||
|
||||
type AccountMembersTableProps = {
|
||||
@@ -43,6 +45,7 @@ type AccountMembersTableProps = {
|
||||
userRoleHierarchy: number;
|
||||
isPrimaryOwner: boolean;
|
||||
canManageRoles: boolean;
|
||||
canUpdateBenefit: boolean;
|
||||
membersBenefitsUsage: {
|
||||
personal_account_id: string;
|
||||
benefit_amount: number;
|
||||
@@ -57,6 +60,7 @@ export function AccountMembersTable({
|
||||
isPrimaryOwner,
|
||||
userRoleHierarchy,
|
||||
canManageRoles,
|
||||
canUpdateBenefit,
|
||||
membersBenefitsUsage,
|
||||
}: AccountMembersTableProps) {
|
||||
const [search, setSearch] = useState('');
|
||||
@@ -74,6 +78,7 @@ export function AccountMembersTable({
|
||||
);
|
||||
},
|
||||
canTransferOwnership: isPrimaryOwner,
|
||||
canUpdateBenefit,
|
||||
};
|
||||
|
||||
const columns = useGetColumns(permissions, {
|
||||
@@ -211,8 +216,7 @@ function useGetColumns(
|
||||
{
|
||||
header: t('roleLabel'),
|
||||
cell: ({ row }) => {
|
||||
const { role, primary_owner_user_id, user_id } = row.original;
|
||||
const isPrimaryOwner = primary_owner_user_id === user_id;
|
||||
const { role } = row.original;
|
||||
|
||||
return (
|
||||
<span
|
||||
@@ -238,7 +242,6 @@ function useGetColumns(
|
||||
<ActionsDropdown
|
||||
permissions={permissions}
|
||||
member={row.original}
|
||||
currentUserId={params.currentUserId}
|
||||
currentTeamAccountId={params.currentAccountId}
|
||||
currentRoleHierarchy={params.currentRoleHierarchy}
|
||||
/>
|
||||
@@ -252,27 +255,21 @@ function useGetColumns(
|
||||
function ActionsDropdown({
|
||||
permissions,
|
||||
member,
|
||||
currentUserId,
|
||||
currentTeamAccountId,
|
||||
currentRoleHierarchy,
|
||||
}: {
|
||||
permissions: Permissions;
|
||||
member: Members[0];
|
||||
currentUserId: string;
|
||||
currentTeamAccountId: string;
|
||||
currentRoleHierarchy: number;
|
||||
}) {
|
||||
const [isRemoving, setIsRemoving] = useState(false);
|
||||
const [isTransferring, setIsTransferring] = useState(false);
|
||||
const [isUpdatingRole, setIsUpdatingRole] = useState(false);
|
||||
const [isUpdatingBenefit, setIsUpdatingBenefit] = useState(false);
|
||||
|
||||
const isCurrentUser = member.user_id === currentUserId;
|
||||
const isPrimaryOwner = member.primary_owner_user_id === member.user_id;
|
||||
|
||||
if (isCurrentUser || isPrimaryOwner) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const memberRoleHierarchy = member.role_hierarchy_level;
|
||||
const canUpdateRole = permissions.canUpdateRole(memberRoleHierarchy);
|
||||
|
||||
@@ -299,23 +296,29 @@ function ActionsDropdown({
|
||||
</DropdownMenuTrigger>
|
||||
|
||||
<DropdownMenuContent>
|
||||
<If condition={canUpdateRole}>
|
||||
<If condition={canUpdateRole && !isPrimaryOwner}>
|
||||
<DropdownMenuItem onClick={() => setIsUpdatingRole(true)}>
|
||||
<Trans i18nKey={'teams:updateRole'} />
|
||||
</DropdownMenuItem>
|
||||
</If>
|
||||
|
||||
<If condition={permissions.canTransferOwnership}>
|
||||
<If condition={permissions.canTransferOwnership && !isPrimaryOwner}>
|
||||
<DropdownMenuItem onClick={() => setIsTransferring(true)}>
|
||||
<Trans i18nKey={'teams:transferOwnership'} />
|
||||
</DropdownMenuItem>
|
||||
</If>
|
||||
|
||||
<If condition={canRemoveFromAccount}>
|
||||
<If condition={canRemoveFromAccount && !isPrimaryOwner}>
|
||||
<DropdownMenuItem onClick={() => setIsRemoving(true)}>
|
||||
<Trans i18nKey={'teams:removeMember'} />
|
||||
</DropdownMenuItem>
|
||||
</If>
|
||||
|
||||
<If condition={permissions.canUpdateBenefit}>
|
||||
<DropdownMenuItem onClick={() => setIsUpdatingBenefit(true)}>
|
||||
<Trans i18nKey={'teams:updateBenefit'} />
|
||||
</DropdownMenuItem>
|
||||
</If>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
|
||||
@@ -348,6 +351,16 @@ function ActionsDropdown({
|
||||
userId={member.user_id}
|
||||
/>
|
||||
</If>
|
||||
|
||||
<If condition={isUpdatingBenefit}>
|
||||
<UpdateEmployeeBenefitDialog
|
||||
isOpen
|
||||
setIsOpen={setIsUpdatingBenefit}
|
||||
accountId={member.account_id}
|
||||
userId={member.user_id}
|
||||
isEligibleForBenefits={member.is_eligible_for_benefits}
|
||||
/>
|
||||
</If>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import React, { useState, useTransition } from 'react';
|
||||
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
import { Alert, AlertDescription } from '@kit/ui/alert';
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
} from '@kit/ui/alert-dialog';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { If } from '@kit/ui/if';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { updateEmployeeBenefitAction } from '../../server/actions/team-members-server-actions';
|
||||
|
||||
const UpdateEmployeeBenefitDialog = ({
|
||||
isOpen,
|
||||
setIsOpen,
|
||||
accountId,
|
||||
userId,
|
||||
isEligibleForBenefits,
|
||||
}: {
|
||||
isOpen: boolean;
|
||||
setIsOpen: (isOpen: boolean) => void;
|
||||
accountId: string;
|
||||
userId: string;
|
||||
isEligibleForBenefits: boolean;
|
||||
}) => {
|
||||
const router = useRouter();
|
||||
const [isSubmitting, startTransition] = useTransition();
|
||||
const [error, setError] = useState<boolean>();
|
||||
const updateEmployeeBenefit = () => {
|
||||
startTransition(async () => {
|
||||
try {
|
||||
await updateEmployeeBenefitAction({ accountId, userId });
|
||||
|
||||
setIsOpen(false);
|
||||
|
||||
router.refresh();
|
||||
} catch {
|
||||
setError(true);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<AlertDialog open={isOpen} onOpenChange={setIsOpen}>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>
|
||||
<Trans i18nKey="team:updateBenefitHeading" />
|
||||
</AlertDialogTitle>
|
||||
|
||||
<AlertDialogDescription>
|
||||
{isEligibleForBenefits ? (
|
||||
<Trans i18nKey="team:removeBenefitDescription" />
|
||||
) : (
|
||||
<Trans i18nKey="team:allowBenefitDescription" />
|
||||
)}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
|
||||
<If condition={error}>
|
||||
<Alert variant="destructive">
|
||||
<AlertDescription>
|
||||
<Trans i18nKey="teams:updateBenefiErrorMessage" />
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
</If>
|
||||
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>
|
||||
<Trans i18nKey="common:cancel" />
|
||||
</AlertDialogCancel>
|
||||
|
||||
<Button
|
||||
data-test="update-member-benefit"
|
||||
variant="destructive"
|
||||
disabled={isSubmitting}
|
||||
onClick={updateEmployeeBenefit}
|
||||
>
|
||||
{isEligibleForBenefits ? (
|
||||
<Trans i18nKey="teams:removeBenefitSubmitLabel" />
|
||||
) : (
|
||||
<Trans i18nKey="teams:allowBenefitSubmitLabel" />
|
||||
)}
|
||||
</Button>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default UpdateEmployeeBenefitDialog;
|
||||
@@ -0,0 +1,6 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const UpdateEmployeeBenefitSchema = z.object({
|
||||
accountId: z.string().uuid(),
|
||||
userId: z.string().uuid(),
|
||||
});
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { revalidatePath } from 'next/cache';
|
||||
|
||||
import { AccountBalanceService } from '@kit/accounts/services/account-balance.service';
|
||||
import { enhanceAction } from '@kit/next/actions';
|
||||
import { createOtpApi } from '@kit/otp';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
@@ -10,6 +11,7 @@ import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import { RemoveMemberSchema } from '../../schema/remove-member.schema';
|
||||
import { TransferOwnershipConfirmationSchema } from '../../schema/transfer-ownership-confirmation.schema';
|
||||
import { UpdateEmployeeBenefitSchema } from '../../schema/update-employee-benefit.schema';
|
||||
import { UpdateMemberRoleSchema } from '../../schema/update-member-role.schema';
|
||||
import { createAccountMembersService } from '../services/account-members.service';
|
||||
|
||||
@@ -144,3 +146,64 @@ export const transferOwnershipAction = enhanceAction(
|
||||
schema: TransferOwnershipConfirmationSchema,
|
||||
},
|
||||
);
|
||||
|
||||
export const updateEmployeeBenefitAction = enhanceAction(
|
||||
async ({ accountId, userId }) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const logger = await getLogger();
|
||||
const accountBalanceService = new AccountBalanceService();
|
||||
|
||||
const ctx = {
|
||||
name: 'teams.updateEmployeeBenefit',
|
||||
userId,
|
||||
accountId,
|
||||
};
|
||||
|
||||
const { data, error } = await client
|
||||
.schema('medreport')
|
||||
.from('accounts_memberships')
|
||||
.select('id,is_eligible_for_benefits')
|
||||
.eq('user_id', userId)
|
||||
.eq('account_id', accountId)
|
||||
.single();
|
||||
|
||||
logger.info(
|
||||
{ ...ctx, isEligible: !data?.is_eligible_for_benefits, id: data?.id },
|
||||
'Changing employee benefit',
|
||||
);
|
||||
|
||||
if (error) {
|
||||
logger.error({ error }, 'Error on receiving balance entry');
|
||||
}
|
||||
|
||||
if (data) {
|
||||
const { error } = await client
|
||||
.schema('medreport')
|
||||
.from('accounts_memberships')
|
||||
.update({ is_eligible_for_benefits: !data.is_eligible_for_benefits })
|
||||
.eq('id', data.id);
|
||||
|
||||
if (error) {
|
||||
logger.error({ error }, `Error on updating balance entry`);
|
||||
}
|
||||
|
||||
const { data: scheduleData, error: scheduleError } = await client
|
||||
.schema('medreport')
|
||||
.from('benefit_distribution_schedule')
|
||||
.select('id')
|
||||
.eq('company_id', accountId)
|
||||
.single();
|
||||
|
||||
if (scheduleError) {
|
||||
logger.error({ error }, 'Error on getting company benefit schedule');
|
||||
}
|
||||
|
||||
if (scheduleData?.id) {
|
||||
await accountBalanceService.upsertHealthBenefitsBySchedule(
|
||||
scheduleData.id,
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ schema: UpdateEmployeeBenefitSchema },
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user