add benefit eligibility setting to HR members
This commit is contained in:
@@ -65,18 +65,17 @@ async function loadAccountMembers(
|
||||
|
||||
const members = data ?? [];
|
||||
|
||||
return members
|
||||
.sort((prev, next) => {
|
||||
if (prev.primary_owner_user_id === prev.user_id) {
|
||||
return -1;
|
||||
}
|
||||
return members.sort((prev, next) => {
|
||||
if (prev.primary_owner_user_id === prev.user_id) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (prev.role_hierarchy_level < next.role_hierarchy_level) {
|
||||
return -1;
|
||||
}
|
||||
if (prev.role_hierarchy_level < next.role_hierarchy_level) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
});
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
export async function loadAccountMembersBenefitsUsage(
|
||||
@@ -100,11 +99,7 @@ export async function loadAccountMembersBenefitsUsage(
|
||||
return [];
|
||||
}
|
||||
|
||||
return (data ?? []) as unknown as {
|
||||
personal_account_id: string;
|
||||
benefit_amount: number;
|
||||
benefit_unused_amount: number;
|
||||
}[];
|
||||
return data ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -52,7 +52,7 @@ async function TeamAccountMembersPage({ params }: TeamAccountMembersPageProps) {
|
||||
|
||||
const canManageRoles = account.permissions.includes('roles.manage');
|
||||
const canManageInvitations = account.permissions.includes('invites.manage');
|
||||
const canManageBenefit = account.permissions.include('benefit.manage');
|
||||
const canUpdateBenefit = account.permissions.includes('benefit.manage');
|
||||
|
||||
const isPrimaryOwner = account.primary_owner_user_id === user.id;
|
||||
const currentUserRoleHierarchy = account.role_hierarchy_level;
|
||||
@@ -104,6 +104,7 @@ async function TeamAccountMembersPage({ params }: TeamAccountMembersPageProps) {
|
||||
members={members}
|
||||
isPrimaryOwner={isPrimaryOwner}
|
||||
canManageRoles={canManageRoles}
|
||||
canUpdateBenefit={canUpdateBenefit}
|
||||
membersBenefitsUsage={membersBenefitsUsage}
|
||||
/>
|
||||
</CardContent>
|
||||
|
||||
@@ -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
|
||||
@@ -265,6 +269,7 @@ function ActionsDropdown({
|
||||
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;
|
||||
@@ -316,6 +321,12 @@ function ActionsDropdown({
|
||||
<Trans i18nKey={'teams:removeMember'} />
|
||||
</DropdownMenuItem>
|
||||
</If>
|
||||
|
||||
<If condition={permissions.canUpdateBenefit}>
|
||||
<DropdownMenuItem onClick={() => setIsUpdatingBenefit(true)}>
|
||||
<Trans i18nKey={'teams:updateBenefit'} />
|
||||
</DropdownMenuItem>
|
||||
</If>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
|
||||
@@ -348,6 +359,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,66 @@ 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)
|
||||
.eq('type', 'benefit')
|
||||
.single();
|
||||
|
||||
logger.info(
|
||||
ctx,
|
||||
'Changing employee benefit to ',
|
||||
!data?.is_eligible_for_benefits,
|
||||
);
|
||||
|
||||
if (error) {
|
||||
logger.error(ctx, 'Error on receiving balance entry', error);
|
||||
}
|
||||
|
||||
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(ctx, `Error on updating balance entry`, error);
|
||||
}
|
||||
|
||||
const { data: scheduleData, error: scheduleError } = await client
|
||||
.schema('medreport')
|
||||
.from('benefit_distribution_schedule')
|
||||
.select('id')
|
||||
.eq('company_id', accountId)
|
||||
.single();
|
||||
|
||||
if (scheduleError) {
|
||||
logger.error(ctx, `Error on getting company benefit schedule`, error);
|
||||
}
|
||||
|
||||
if (scheduleData?.id) {
|
||||
await accountBalanceService.upsertHealthBenefitsBySchedule(
|
||||
scheduleData.id,
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ schema: UpdateEmployeeBenefitSchema },
|
||||
);
|
||||
|
||||
@@ -557,6 +557,7 @@ export type Database = {
|
||||
created_by: string | null
|
||||
has_seen_confirmation: boolean
|
||||
id: string
|
||||
is_eligible_for_benefits: boolean
|
||||
updated_at: string
|
||||
updated_by: string | null
|
||||
user_id: string
|
||||
@@ -568,6 +569,7 @@ export type Database = {
|
||||
created_by?: string | null
|
||||
has_seen_confirmation?: boolean
|
||||
id?: string
|
||||
is_eligible_for_benefits?: boolean
|
||||
updated_at?: string
|
||||
updated_by?: string | null
|
||||
user_id: string
|
||||
@@ -579,6 +581,7 @@ export type Database = {
|
||||
created_by?: string | null
|
||||
has_seen_confirmation?: boolean
|
||||
id?: string
|
||||
is_eligible_for_benefits?: boolean
|
||||
updated_at?: string
|
||||
updated_by?: string | null
|
||||
user_id?: string
|
||||
@@ -2303,6 +2306,7 @@ export type Database = {
|
||||
created_at: string
|
||||
email: string
|
||||
id: string
|
||||
is_eligible_for_benefits: boolean
|
||||
name: string
|
||||
personal_code: string
|
||||
picture_url: string
|
||||
|
||||
@@ -96,6 +96,13 @@
|
||||
"updateRoleLoadingMessage": "Rolli uuendatakse...",
|
||||
"updateRoleSuccessMessage": "Roll edukalt uuendatud",
|
||||
"updatingRoleErrorMessage": "Vabandust, tekkis viga. Palun proovi uuesti.",
|
||||
"updateBenefit": "Uuenda toetust",
|
||||
"updateBenefitHeading": "Uuenda toetus staatust",
|
||||
"removeBenefitDescription": "Eemalda töötaja toetusest",
|
||||
"allowBenefitDescription": "Luba töötajale toetus",
|
||||
"removeBenefitSubmitLabel": "Eemalda toetus",
|
||||
"allowBenefitSubmitLabel": "Luba toetus",
|
||||
"updateBenefiErrorMessage": "Vabandus, tekkis viga. Palun proovi uuesti.",
|
||||
"updateMemberRoleModalHeading": "Uuenda töötaja rolli",
|
||||
"updateMemberRoleModalDescription": "Muuda valitud töötaja rolli. Roll määrab töötaja õigused.",
|
||||
"roleMustBeDifferent": "Roll peab erinema praegusest",
|
||||
|
||||
@@ -1,2 +1,56 @@
|
||||
insert into medreport.role_permissions (role, permission) values
|
||||
('owner', 'benefit.manage');
|
||||
('owner', 'benefit.manage');
|
||||
|
||||
ALTER TABLE medreport.accounts_memberships
|
||||
ADD COLUMN is_eligible_for_benefits boolean NOT NULL DEFAULT true;
|
||||
|
||||
DROP FUNCTION IF EXISTS medreport.get_account_members(text);
|
||||
|
||||
CREATE OR REPLACE FUNCTION medreport.get_account_members(account_slug text)
|
||||
RETURNS TABLE(
|
||||
id uuid,
|
||||
user_id uuid,
|
||||
account_id uuid,
|
||||
role character varying,
|
||||
role_hierarchy_level integer,
|
||||
primary_owner_user_id uuid,
|
||||
name text,
|
||||
email character varying,
|
||||
personal_code text,
|
||||
picture_url character varying,
|
||||
created_at timestamp with time zone,
|
||||
updated_at timestamp with time zone,
|
||||
is_eligible_for_benefits boolean
|
||||
)
|
||||
LANGUAGE plpgsql
|
||||
SET search_path TO ''
|
||||
AS $function$begin
|
||||
return QUERY
|
||||
select
|
||||
acc.id,
|
||||
am.user_id,
|
||||
am.account_id,
|
||||
am.account_role,
|
||||
r.hierarchy_level,
|
||||
a.primary_owner_user_id,
|
||||
TRIM(CONCAT(acc.name, ' ', acc.last_name)) as name,
|
||||
acc.email,
|
||||
acc.personal_code,
|
||||
acc.picture_url,
|
||||
am.created_at,
|
||||
am.updated_at,
|
||||
am.is_eligible_for_benefits
|
||||
from
|
||||
medreport.accounts_memberships am
|
||||
join medreport.accounts a on a.id = am.account_id
|
||||
join medreport.accounts acc on acc.id = am.user_id
|
||||
join medreport.roles r on r.name = am.account_role
|
||||
where
|
||||
a.slug = account_slug;
|
||||
|
||||
end;$function$
|
||||
;
|
||||
|
||||
grant
|
||||
execute on function medreport.get_account_members (text) to authenticated,
|
||||
service_role;
|
||||
|
||||
Reference in New Issue
Block a user