add benefit eligibility setting to HR members
This commit is contained in:
@@ -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 },
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user