develop -> main
develop -> main
This commit is contained in:
@@ -43,6 +43,7 @@ MEDIPOST_ENABLE_DISPATCH_ON_MONTONIO_CALLBACK=true
|
||||
#MEDIPOST_ENABLE_DISPATCH_ON_MONTONIO_CALLBACK=false
|
||||
|
||||
# MEDUSA
|
||||
COMPANY_BENEFITS_PAYMENT_SECRET_KEY=NzcwMzE2NmEtOThiMS0xMWYwLWI4NjYtMDMwZDQzMjFhMjExCg==
|
||||
MEDUSA_BACKEND_URL=http://localhost:9000
|
||||
MEDUSA_BACKEND_PUBLIC_URL=http://localhost:9000
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { SIDEBAR_WIDTH_PROPERTY } from '@/packages/ui/src/shadcn/constants';
|
||||
|
||||
import { AppLogo } from '@kit/shared/components/app-logo';
|
||||
import { ProfileAccountDropdownContainer } from '@kit/shared/components/personal-account-dropdown-container';
|
||||
import { SIDEBAR_WIDTH_PROPERTY } from '@/packages/ui/src/shadcn/constants';
|
||||
|
||||
import type { UserWorkspace } from '../../home/(user)/_lib/server/load-user-workspace';
|
||||
|
||||
export function AdminMenuNavigation(props: {
|
||||
workspace: UserWorkspace;
|
||||
}) {
|
||||
export function AdminMenuNavigation(props: { workspace: UserWorkspace }) {
|
||||
const { accounts } = props.workspace;
|
||||
|
||||
return (
|
||||
@@ -17,9 +16,7 @@ export function AdminMenuNavigation(props: {
|
||||
|
||||
<div className="flex items-center justify-end gap-3">
|
||||
<div>
|
||||
<ProfileAccountDropdownContainer
|
||||
accounts={accounts}
|
||||
/>
|
||||
<ProfileAccountDropdownContainer accounts={accounts} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -3,8 +3,8 @@ import { cache } from 'react';
|
||||
import { AdminAccountPage } from '@kit/admin/components/admin-account-page';
|
||||
import { AdminGuard } from '@kit/admin/components/admin-guard';
|
||||
|
||||
import { getAccount } from '~/lib/services/account.service';
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
import { getAccount } from '~/lib/services/account.service';
|
||||
|
||||
interface Params {
|
||||
params: Promise<{
|
||||
|
||||
@@ -8,6 +8,7 @@ import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
import { AppBreadcrumbs } from '@kit/ui/app-breadcrumbs';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { PageBody, PageHeader } from '@kit/ui/page';
|
||||
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
|
||||
interface SearchParams {
|
||||
|
||||
@@ -5,8 +5,8 @@ import { cookies } from 'next/headers';
|
||||
import { Page, PageMobileNavigation, PageNavigation } from '@kit/ui/page';
|
||||
import { SidebarProvider } from '@kit/ui/shadcn-sidebar';
|
||||
|
||||
import { AdminSidebar } from '~/admin/_components/admin-sidebar';
|
||||
import { AdminMenuNavigation } from '~/admin/_components/admin-menu-navigation';
|
||||
import { AdminSidebar } from '~/admin/_components/admin-sidebar';
|
||||
import { AdminMobileNavigation } from '~/admin/_components/mobile-navigation';
|
||||
|
||||
import { loadUserWorkspace } from '../home/(user)/_lib/server/load-user-workspace';
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { AdminDashboard } from '@kit/admin/components/admin-dashboard';
|
||||
import { AdminGuard } from '@kit/admin/components/admin-guard';
|
||||
import { PageBody, PageHeader } from '@kit/ui/page';
|
||||
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
|
||||
function AdminPage() {
|
||||
|
||||
@@ -2,10 +2,45 @@ import axios from 'axios';
|
||||
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
|
||||
import type { ISearchLoadResponse } from '~/lib/types/connected-online';
|
||||
import { logSyncResult } from '~/lib/services/audit.service';
|
||||
import { SyncStatus } from '~/lib/types/audit';
|
||||
import type {
|
||||
ISearchLoadResponse,
|
||||
P_JobTitleTranslation,
|
||||
} from '~/lib/types/connected-online';
|
||||
|
||||
function createTranslationMap(translations: P_JobTitleTranslation[]) {
|
||||
const result: Map<
|
||||
number,
|
||||
Map<number, { textEN: string; textRU: string; textET: string }>
|
||||
> = new Map();
|
||||
|
||||
for (const translation of translations) {
|
||||
const { ClinicID, TextET, TextEN, TextRU, SyncID } = translation;
|
||||
|
||||
if (!result.has(ClinicID)) {
|
||||
result.set(ClinicID, new Map());
|
||||
}
|
||||
|
||||
result.get(ClinicID)!.set(SyncID, {
|
||||
textET: TextET,
|
||||
textEN: TextEN,
|
||||
textRU: TextRU,
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function getSpokenLanguages(spokenLanguages?: string) {
|
||||
if (!spokenLanguages || !spokenLanguages.length) return [];
|
||||
return spokenLanguages.split(',');
|
||||
}
|
||||
|
||||
export default async function syncConnectedOnline() {
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
const isProd = !['test', 'localhost'].some((pathString) =>
|
||||
process.env.NEXT_PUBLIC_SITE_URL?.includes(pathString),
|
||||
);
|
||||
|
||||
const baseUrl = process.env.CONNECTED_ONLINE_URL;
|
||||
|
||||
@@ -16,14 +51,19 @@ export default async function syncConnectedOnline() {
|
||||
const supabase = getSupabaseServerAdminClient();
|
||||
|
||||
try {
|
||||
const response = await axios.post<{ d: string }>(`${baseUrl}/Search_Load`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
const searchLoadResponse = await axios.post<{ d: string }>(
|
||||
`${baseUrl}/Search_Load`,
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
},
|
||||
param: "{'Value':'|et|-1'}", // get all available services in Estonian
|
||||
},
|
||||
param: "{'Value':'|et|-1'}", // get all available services in Estonian
|
||||
});
|
||||
);
|
||||
|
||||
const responseData: ISearchLoadResponse = JSON.parse(response.data.d);
|
||||
const responseData: ISearchLoadResponse = JSON.parse(
|
||||
searchLoadResponse.data.d,
|
||||
);
|
||||
|
||||
if (responseData?.ErrorCode !== 0) {
|
||||
throw new Error('Failed to get Connected Online data');
|
||||
@@ -43,18 +83,23 @@ export default async function syncConnectedOnline() {
|
||||
|
||||
let clinics;
|
||||
let services;
|
||||
let serviceProviders;
|
||||
let jobTitleTranslations;
|
||||
// Filter out "Dentas Demo OÜ" in prod or only sync "Dentas Demo OÜ" in any other environment
|
||||
if (isProd) {
|
||||
clinics = responseData.Data.T_Lic.filter((clinic) => clinic.ID !== 2);
|
||||
services = responseData.Data.T_Service.filter(
|
||||
(service) => service.ClinicID !== 2,
|
||||
);
|
||||
} else {
|
||||
clinics = responseData.Data.T_Lic.filter((clinic) => clinic.ID === 2);
|
||||
services = responseData.Data.T_Service.filter(
|
||||
(service) => service.ClinicID === 2,
|
||||
);
|
||||
}
|
||||
const isDemoClinic = (clinicId: number) =>
|
||||
isProd ? clinicId !== 2 : clinicId === 2;
|
||||
clinics = responseData.Data.T_Lic.filter(({ ID }) => isDemoClinic(ID));
|
||||
services = responseData.Data.T_Service.filter(({ ClinicID }) =>
|
||||
isDemoClinic(ClinicID),
|
||||
);
|
||||
serviceProviders = responseData.Data.T_Doctor.filter(({ ClinicID }) =>
|
||||
isDemoClinic(ClinicID),
|
||||
);
|
||||
jobTitleTranslations = createTranslationMap(
|
||||
responseData.Data.P_JobTitleTranslations.filter(({ ClinicID }) =>
|
||||
isDemoClinic(ClinicID),
|
||||
),
|
||||
);
|
||||
|
||||
const mappedClinics = clinics.map((clinic) => {
|
||||
return {
|
||||
@@ -64,6 +109,8 @@ export default async function syncConnectedOnline() {
|
||||
name: clinic.Name,
|
||||
personal_code_required: !!clinic.PersonalCodeRequired,
|
||||
phone_number: clinic.Phone || null,
|
||||
key: clinic.Key,
|
||||
address: clinic.Address,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -71,7 +118,7 @@ export default async function syncConnectedOnline() {
|
||||
return {
|
||||
id: service.ID,
|
||||
clinic_id: service.ClinicID,
|
||||
sync_id: service.SyncID,
|
||||
sync_id: Number(service.SyncID),
|
||||
name: service.Name,
|
||||
description: service.Description || null,
|
||||
price: service.Price,
|
||||
@@ -87,45 +134,133 @@ export default async function syncConnectedOnline() {
|
||||
};
|
||||
});
|
||||
|
||||
const mappedServiceProviders = serviceProviders.map((serviceProvider) => {
|
||||
const jobTitleTranslation = serviceProvider.JobTitleID
|
||||
? jobTitleTranslations
|
||||
.get(serviceProvider.ClinicID)
|
||||
?.get(serviceProvider.JobTitleID)
|
||||
: null;
|
||||
return {
|
||||
id: serviceProvider.ID,
|
||||
prefix: serviceProvider.Prefix,
|
||||
name: serviceProvider.Name,
|
||||
spoken_languages: getSpokenLanguages(serviceProvider.SpokenLanguages),
|
||||
job_title_et: jobTitleTranslation?.textET,
|
||||
job_title_en: jobTitleTranslation?.textEN,
|
||||
job_title_ru: jobTitleTranslation?.textRU,
|
||||
job_title_id: serviceProvider.JobTitleID,
|
||||
is_deleted: !!serviceProvider.Deleted,
|
||||
clinic_id: serviceProvider.ClinicID,
|
||||
};
|
||||
});
|
||||
|
||||
const { error: providersError } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_providers')
|
||||
.upsert(mappedClinics);
|
||||
|
||||
if (providersError) {
|
||||
return logSyncResult({
|
||||
operation: 'CONNECTED_ONLINE_SYNC',
|
||||
comment:
|
||||
'Error saving connected online providers: ' +
|
||||
JSON.stringify(providersError),
|
||||
status: SyncStatus.Fail,
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
}
|
||||
|
||||
const { error: servicesError } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_services')
|
||||
.upsert(mappedServices, { onConflict: 'id', ignoreDuplicates: false });
|
||||
.upsert(mappedServices, {
|
||||
onConflict: 'id',
|
||||
ignoreDuplicates: false,
|
||||
});
|
||||
|
||||
if (providersError || servicesError) {
|
||||
return supabase
|
||||
.schema('audit')
|
||||
.from('sync_entries')
|
||||
.insert({
|
||||
operation: 'CONNECTED_ONLINE_SYNC',
|
||||
comment: providersError
|
||||
? 'Error saving providers: ' + JSON.stringify(providersError)
|
||||
: 'Error saving services: ' + JSON.stringify(servicesError),
|
||||
status: 'FAIL',
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
if (servicesError) {
|
||||
return logSyncResult({
|
||||
operation: 'CONNECTED_ONLINE_SYNC',
|
||||
comment:
|
||||
'Error saving connected online services: ' +
|
||||
JSON.stringify(servicesError),
|
||||
status: SyncStatus.Fail,
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
}
|
||||
|
||||
await supabase.schema('audit').from('sync_entries').insert({
|
||||
const { error: serviceProvidersError } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_service_providers')
|
||||
.upsert(mappedServiceProviders, {
|
||||
onConflict: 'id',
|
||||
ignoreDuplicates: false,
|
||||
});
|
||||
|
||||
if (serviceProvidersError) {
|
||||
return logSyncResult({
|
||||
operation: 'CONNECTED_ONLINE_SYNC',
|
||||
comment:
|
||||
'Error saving service providers: ' +
|
||||
JSON.stringify(serviceProvidersError),
|
||||
status: SyncStatus.Fail,
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
}
|
||||
|
||||
for (const mappedClinic of mappedClinics) {
|
||||
const defaultLoadResponse = await axios.post<{ d: string }>(
|
||||
`${baseUrl}/Default_Load`,
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
},
|
||||
param: `{'Value':'${mappedClinic.key}|et'}`,
|
||||
},
|
||||
);
|
||||
|
||||
const defaultLoadResponseData = JSON.parse(defaultLoadResponse.data.d);
|
||||
|
||||
if (defaultLoadResponseData?.ErrorCode !== 0) {
|
||||
throw new Error('Failed to get Connected Online location data');
|
||||
}
|
||||
|
||||
const clinicLocations: {
|
||||
SyncID: number;
|
||||
Address: string;
|
||||
Name: string;
|
||||
}[] = defaultLoadResponseData.Data.T_SelectableLocation;
|
||||
|
||||
if (clinicLocations?.length) {
|
||||
const mappedLocations = clinicLocations.map(
|
||||
({ SyncID, Address, Name }) => ({
|
||||
address: Address,
|
||||
clinic_id: mappedClinic.id,
|
||||
sync_id: SyncID,
|
||||
name: Name,
|
||||
}),
|
||||
);
|
||||
|
||||
await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_locations')
|
||||
.insert(mappedLocations)
|
||||
.throwOnError();
|
||||
}
|
||||
}
|
||||
|
||||
await logSyncResult({
|
||||
operation: 'CONNECTED_ONLINE_SYNC',
|
||||
status: 'SUCCESS',
|
||||
status: SyncStatus.Success,
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
} catch (e) {
|
||||
await supabase
|
||||
.schema('audit')
|
||||
.from('sync_entries')
|
||||
.insert({
|
||||
operation: 'CONNECTED_ONLINE_SYNC',
|
||||
status: 'FAIL',
|
||||
comment: JSON.stringify(e),
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
await logSyncResult({
|
||||
operation: 'CONNECTED_ONLINE_SYNC',
|
||||
status: SyncStatus.Fail,
|
||||
comment: JSON.stringify(e),
|
||||
changed_by_role: 'service_role',
|
||||
});
|
||||
throw new Error(
|
||||
`Failed to sync Connected Online data, error: ${JSON.stringify(e)}`,
|
||||
);
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
import { HomeLayoutPageHeader } from '@/app/home/(user)/_components/home-page-header';
|
||||
import { loadCategory } from '@/app/home/(user)/_lib/server/load-category';
|
||||
|
||||
import { pathsConfig } from '@kit/shared/config';
|
||||
import { AppBreadcrumbs } from '@kit/ui/makerkit/app-breadcrumbs';
|
||||
import { PageBody } from '@kit/ui/page';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import BookingContainer from '~/home/(user)/_components/booking/booking-container';
|
||||
import { loadCurrentUserAccount } from '~/home/(user)/_lib/server/load-user-account';
|
||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
import {
|
||||
PageViewAction,
|
||||
createPageViewLog,
|
||||
} from '~/lib/services/audit/pageView.service';
|
||||
|
||||
export const generateMetadata = async () => {
|
||||
const i18n = await createI18nServerInstance();
|
||||
@@ -17,9 +25,30 @@ export const generateMetadata = async () => {
|
||||
};
|
||||
};
|
||||
|
||||
async function BookingHandlePage({ params }: { params: { handle: string } }) {
|
||||
const handle = await params.handle;
|
||||
async function BookingHandlePage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ handle: string }>;
|
||||
}) {
|
||||
const { handle } = await params;
|
||||
const { category } = await loadCategory({ handle });
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
|
||||
if (!category) {
|
||||
return <div>Category not found</div>;
|
||||
}
|
||||
|
||||
if (!account) {
|
||||
return redirect(pathsConfig.auth.signIn);
|
||||
}
|
||||
|
||||
await createPageViewLog({
|
||||
accountId: account.id,
|
||||
action: PageViewAction.VIEW_TTO_SERVICE_BOOKING,
|
||||
extraData: {
|
||||
handle,
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -30,10 +59,10 @@ async function BookingHandlePage({ params }: { params: { handle: string } }) {
|
||||
/>
|
||||
<HomeLayoutPageHeader
|
||||
title={<Trans i18nKey={'booking:title'} />}
|
||||
description={<Trans i18nKey={'booking:description'} />}
|
||||
description=""
|
||||
/>
|
||||
|
||||
<PageBody></PageBody>
|
||||
<BookingContainer category={category} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,100 +2,13 @@
|
||||
|
||||
import { MontonioOrderToken } from '@/app/home/(user)/_components/cart/types';
|
||||
import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-account';
|
||||
import { placeOrder, retrieveCart } from '@lib/data/cart';
|
||||
import { listProductTypes } from '@lib/data/products';
|
||||
import type { StoreOrder } from '@medusajs/types';
|
||||
import { retrieveCart } from '@lib/data/cart';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { z } from 'zod';
|
||||
|
||||
import type { AccountWithParams } from '@kit/accounts/types/accounts';
|
||||
import { createNotificationsApi } from '@kit/notifications/api';
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
import { handlePlaceOrder } from '../../../_lib/server/cart-actions';
|
||||
|
||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||
import { sendOrderToMedipost } from '~/lib/services/medipost/medipostPrivateMessage.service';
|
||||
import { getOrderedAnalysisIds } from '~/lib/services/medusaOrder.service';
|
||||
import {
|
||||
createAnalysisOrder,
|
||||
getAnalysisOrder,
|
||||
} from '~/lib/services/order.service';
|
||||
|
||||
const ANALYSIS_PACKAGES_TYPE_HANDLE = 'analysis-packages';
|
||||
const ANALYSIS_TYPE_HANDLE = 'synlab-analysis';
|
||||
const MONTONIO_PAID_STATUS = 'PAID';
|
||||
|
||||
const env = () =>
|
||||
z
|
||||
.object({
|
||||
emailSender: z
|
||||
.string({
|
||||
error: 'EMAIL_SENDER is required',
|
||||
})
|
||||
.min(1),
|
||||
siteUrl: z
|
||||
.string({
|
||||
error: 'NEXT_PUBLIC_SITE_URL is required',
|
||||
})
|
||||
.min(1),
|
||||
isEnabledDispatchOnMontonioCallback: z.boolean({
|
||||
error: 'MEDIPOST_ENABLE_DISPATCH_ON_MONTONIO_CALLBACK is required',
|
||||
}),
|
||||
})
|
||||
.parse({
|
||||
emailSender: process.env.EMAIL_SENDER,
|
||||
siteUrl: process.env.NEXT_PUBLIC_SITE_URL!,
|
||||
isEnabledDispatchOnMontonioCallback:
|
||||
process.env.MEDIPOST_ENABLE_DISPATCH_ON_MONTONIO_CALLBACK === 'true',
|
||||
});
|
||||
|
||||
const sendEmail = async ({
|
||||
account,
|
||||
email,
|
||||
analysisPackageName,
|
||||
partnerLocationName,
|
||||
language,
|
||||
}: {
|
||||
account: Pick<AccountWithParams, 'name' | 'id'>;
|
||||
email: string;
|
||||
analysisPackageName: string;
|
||||
partnerLocationName: string;
|
||||
language: string;
|
||||
}) => {
|
||||
const client = getSupabaseServerAdminClient();
|
||||
try {
|
||||
const { renderSynlabAnalysisPackageEmail } = await import(
|
||||
'@kit/email-templates'
|
||||
);
|
||||
const { getMailer } = await import('@kit/mailers');
|
||||
|
||||
const mailer = await getMailer();
|
||||
|
||||
const { html, subject } = await renderSynlabAnalysisPackageEmail({
|
||||
analysisPackageName,
|
||||
personName: account.name,
|
||||
partnerLocationName,
|
||||
language,
|
||||
});
|
||||
|
||||
await mailer
|
||||
.sendEmail({
|
||||
from: env().emailSender,
|
||||
to: email,
|
||||
subject,
|
||||
html,
|
||||
})
|
||||
.catch((error) => {
|
||||
throw new Error(`Failed to send email, message=${error}`);
|
||||
});
|
||||
await createNotificationsApi(client).createNotification({
|
||||
account_id: account.id,
|
||||
body: html,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to send email, message=${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
async function decodeOrderToken(orderToken: string) {
|
||||
const secretKey = process.env.MONTONIO_SECRET_KEY as string;
|
||||
|
||||
@@ -122,74 +35,6 @@ async function getCartByOrderToken(decoded: MontonioOrderToken) {
|
||||
return cart;
|
||||
}
|
||||
|
||||
async function getOrderResultParameters(medusaOrder: StoreOrder) {
|
||||
const { productTypes } = await listProductTypes();
|
||||
const analysisPackagesType = productTypes.find(
|
||||
({ metadata }) => metadata?.handle === ANALYSIS_PACKAGES_TYPE_HANDLE,
|
||||
);
|
||||
const analysisType = productTypes.find(
|
||||
({ metadata }) => metadata?.handle === ANALYSIS_TYPE_HANDLE,
|
||||
);
|
||||
|
||||
const analysisPackageOrderItem = medusaOrder.items?.find(
|
||||
({ product_type_id }) => product_type_id === analysisPackagesType?.id,
|
||||
);
|
||||
const analysisItems = medusaOrder.items?.filter(
|
||||
({ product_type_id }) => product_type_id === analysisType?.id,
|
||||
);
|
||||
|
||||
return {
|
||||
medusaOrderId: medusaOrder.id,
|
||||
email: medusaOrder.email,
|
||||
analysisPackageOrder: analysisPackageOrderItem
|
||||
? {
|
||||
partnerLocationName:
|
||||
(analysisPackageOrderItem?.metadata
|
||||
?.partner_location_name as string) ?? '',
|
||||
analysisPackageName: analysisPackageOrderItem?.title ?? '',
|
||||
}
|
||||
: null,
|
||||
analysisItemsOrder:
|
||||
Array.isArray(analysisItems) && analysisItems.length > 0
|
||||
? analysisItems.map(({ product }) => ({
|
||||
analysisName: product?.title ?? '',
|
||||
analysisId: (product?.metadata?.analysisIdOriginal as string) ?? '',
|
||||
}))
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
async function sendAnalysisPackageOrderEmail({
|
||||
account,
|
||||
email,
|
||||
analysisPackageOrder,
|
||||
}: {
|
||||
account: AccountWithParams;
|
||||
email: string;
|
||||
analysisPackageOrder: {
|
||||
partnerLocationName: string;
|
||||
analysisPackageName: string;
|
||||
};
|
||||
}) {
|
||||
const { language } = await createI18nServerInstance();
|
||||
const { analysisPackageName, partnerLocationName } = analysisPackageOrder;
|
||||
try {
|
||||
await sendEmail({
|
||||
account: { id: account.id, name: account.name },
|
||||
email,
|
||||
analysisPackageName,
|
||||
partnerLocationName,
|
||||
language,
|
||||
});
|
||||
console.info(`Successfully sent analysis package order email to ${email}`);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed to send analysis package order email to ${email}`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function processMontonioCallback(orderToken: string) {
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
@@ -199,63 +44,8 @@ export async function processMontonioCallback(orderToken: string) {
|
||||
try {
|
||||
const decoded = await decodeOrderToken(orderToken);
|
||||
const cart = await getCartByOrderToken(decoded);
|
||||
|
||||
const medusaOrder = await placeOrder(cart.id, {
|
||||
revalidateCacheTags: false,
|
||||
});
|
||||
const orderedAnalysisElements = await getOrderedAnalysisIds({
|
||||
medusaOrder,
|
||||
});
|
||||
|
||||
try {
|
||||
const existingAnalysisOrder = await getAnalysisOrder({
|
||||
medusaOrderId: medusaOrder.id,
|
||||
});
|
||||
console.info(
|
||||
`Analysis order already exists for medusaOrderId=${medusaOrder.id}, orderId=${existingAnalysisOrder.id}`,
|
||||
);
|
||||
return { success: true, orderId: existingAnalysisOrder.id };
|
||||
} catch {
|
||||
// ignored
|
||||
}
|
||||
|
||||
const orderId = await createAnalysisOrder({
|
||||
medusaOrder,
|
||||
orderedAnalysisElements,
|
||||
});
|
||||
const orderResult = await getOrderResultParameters(medusaOrder);
|
||||
|
||||
const { medusaOrderId, email, analysisPackageOrder, analysisItemsOrder } =
|
||||
orderResult;
|
||||
|
||||
if (email) {
|
||||
if (analysisPackageOrder) {
|
||||
await sendAnalysisPackageOrderEmail({
|
||||
account,
|
||||
email,
|
||||
analysisPackageOrder,
|
||||
});
|
||||
} else {
|
||||
console.info(`Order has no analysis package, skipping email.`);
|
||||
}
|
||||
|
||||
if (analysisItemsOrder) {
|
||||
// @TODO send email for separate analyses
|
||||
console.warn(
|
||||
`Order has analysis items, but no email template exists yet`,
|
||||
);
|
||||
} else {
|
||||
console.info(`Order has no analysis items, skipping email.`);
|
||||
}
|
||||
} else {
|
||||
console.error('Missing email to send order result email', orderResult);
|
||||
}
|
||||
|
||||
if (env().isEnabledDispatchOnMontonioCallback) {
|
||||
await sendOrderToMedipost({ medusaOrderId, orderedAnalysisElements });
|
||||
}
|
||||
|
||||
return { success: true, orderId };
|
||||
const result = await handlePlaceOrder({ cart });
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('Failed to place order', error);
|
||||
throw new Error(`Failed to place order, message=${error}`);
|
||||
|
||||
@@ -34,8 +34,15 @@ export default function MontonioCallbackClient({
|
||||
setHasProcessed(true);
|
||||
|
||||
try {
|
||||
const { orderId } = await processMontonioCallback(orderToken);
|
||||
router.push(`/home/order/${orderId}/confirmed`);
|
||||
const result = await processMontonioCallback(orderToken);
|
||||
if (result.success) {
|
||||
return router.push(`/home/order/${result.orderId}/confirmed`);
|
||||
}
|
||||
if (result.failedServiceBookings?.length) {
|
||||
router.push(
|
||||
`/home/cart/montonio-callback/error?reasonFailed=${result.failedServiceBookings.map(({ reason }) => reason).join(',')}`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to place order', error);
|
||||
router.push('/home/cart/montonio-callback/error');
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { use } from 'react';
|
||||
|
||||
import Link from 'next/link';
|
||||
|
||||
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
||||
import { PageBody, PageHeader } from '@/packages/ui/src/makerkit/page';
|
||||
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Alert, AlertDescription } from '@kit/ui/shadcn/alert';
|
||||
import { AlertTitle } from '@kit/ui/shadcn/alert';
|
||||
@@ -16,7 +19,15 @@ export async function generateMetadata() {
|
||||
};
|
||||
}
|
||||
|
||||
export default async function MontonioCheckoutCallbackErrorPage() {
|
||||
export default async function MontonioCheckoutCallbackErrorPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ reasonFailed: string }>;
|
||||
}) {
|
||||
const params = await searchParams;
|
||||
|
||||
const failedBookingData: string[] = toArray(params.reasonFailed?.split(','));
|
||||
|
||||
return (
|
||||
<div className={'flex h-full flex-1 flex-col'}>
|
||||
<PageHeader title={<Trans i18nKey="cart:montonioCallback.title" />} />
|
||||
@@ -28,9 +39,15 @@ export default async function MontonioCheckoutCallbackErrorPage() {
|
||||
</AlertTitle>
|
||||
|
||||
<AlertDescription>
|
||||
<p>
|
||||
{failedBookingData.length ? (
|
||||
failedBookingData.map((failureReason, index) => (
|
||||
<p key={index}>
|
||||
<Trans i18nKey={`checkout.error.${failureReason}`} />
|
||||
</p>
|
||||
))
|
||||
) : (
|
||||
<Trans i18nKey={'checkout.error.description'} />
|
||||
</p>
|
||||
)}
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { notFound } from 'next/navigation';
|
||||
|
||||
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
||||
import { PageBody, PageHeader } from '@/packages/ui/src/makerkit/page';
|
||||
import { retrieveCart } from '@lib/data/cart';
|
||||
@@ -8,9 +6,14 @@ import { listProductTypes } from '@lib/data/products';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
import { getCartReservations } from '~/lib/services/reservation.service';
|
||||
import { findProductTypeIdByHandle } from '~/lib/utils';
|
||||
|
||||
import Cart from '../../_components/cart';
|
||||
import CartTimer from '../../_components/cart/cart-timer';
|
||||
import { loadCurrentUserAccount } from '../../_lib/server/load-user-account';
|
||||
import { AccountBalanceService } from '@kit/accounts/services/account-balance.service';
|
||||
import { EnrichedCartItem } from '../../_components/cart/types';
|
||||
|
||||
export async function generateMetadata() {
|
||||
const { t } = await createI18nServerInstance();
|
||||
@@ -21,35 +24,48 @@ export async function generateMetadata() {
|
||||
}
|
||||
|
||||
async function CartPage() {
|
||||
const cart = await retrieveCart().catch((error) => {
|
||||
console.error('Failed to retrieve cart', error);
|
||||
return notFound();
|
||||
});
|
||||
const [
|
||||
cart,
|
||||
{ productTypes },
|
||||
{ account },
|
||||
] = await Promise.all([
|
||||
retrieveCart(),
|
||||
listProductTypes(),
|
||||
loadCurrentUserAccount(),
|
||||
]);
|
||||
|
||||
const { productTypes } = await listProductTypes();
|
||||
const analysisPackagesType = productTypes.find(
|
||||
({ metadata }) => metadata?.handle === 'analysis-packages',
|
||||
if (!account) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const balanceSummary = await new AccountBalanceService().getBalanceSummary(account.id);
|
||||
|
||||
const synlabAnalysisTypeId = findProductTypeIdByHandle(
|
||||
productTypes,
|
||||
'synlab-analysis',
|
||||
);
|
||||
const synlabAnalysisType = productTypes.find(
|
||||
({ metadata }) => metadata?.handle === 'synlab-analysis',
|
||||
const analysisPackagesTypeId = findProductTypeIdByHandle(
|
||||
productTypes,
|
||||
'analysis-packages',
|
||||
);
|
||||
|
||||
const synlabAnalyses =
|
||||
analysisPackagesType && synlabAnalysisType && cart?.items
|
||||
analysisPackagesTypeId && synlabAnalysisTypeId && cart?.items
|
||||
? cart.items.filter((item) => {
|
||||
const productTypeId = item.product?.type_id;
|
||||
if (!productTypeId) {
|
||||
return false;
|
||||
}
|
||||
return [analysisPackagesType.id, synlabAnalysisType.id].includes(
|
||||
return [analysisPackagesTypeId, synlabAnalysisTypeId].includes(
|
||||
productTypeId,
|
||||
);
|
||||
})
|
||||
: [];
|
||||
const ttoServiceItems =
|
||||
cart?.items?.filter(
|
||||
(item) => !synlabAnalyses.some((analysis) => analysis.id === item.id),
|
||||
) ?? [];
|
||||
|
||||
let ttoServiceItems: EnrichedCartItem[] = [];
|
||||
if (cart?.items?.length) {
|
||||
ttoServiceItems = await getCartReservations(cart);
|
||||
}
|
||||
const otherItemsSorted = ttoServiceItems.sort((a, b) =>
|
||||
(a.updated_at ?? '') > (b.updated_at ?? '') ? -1 : 1,
|
||||
);
|
||||
@@ -63,9 +79,11 @@ async function CartPage() {
|
||||
{isTimerShown && <CartTimer cartItem={item} />}
|
||||
</PageHeader>
|
||||
<Cart
|
||||
accountId={account.id}
|
||||
cart={cart}
|
||||
synlabAnalyses={synlabAnalyses}
|
||||
ttoServiceItems={ttoServiceItems}
|
||||
balanceSummary={balanceSummary}
|
||||
/>
|
||||
</PageBody>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
'use client';
|
||||
|
||||
import CartTotals from '@/app/home/(user)/_components/order/cart-totals';
|
||||
import OrderDetails from '@/app/home/(user)/_components/order/order-details';
|
||||
import OrderItems from '@/app/home/(user)/_components/order/order-items';
|
||||
import Divider from '@modules/common/components/divider';
|
||||
|
||||
import { PageBody, PageHeader } from '@kit/ui/page';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { StoreOrder } from '@medusajs/types';
|
||||
import { AnalysisOrder } from '~/lib/types/analysis-order';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { retrieveOrder } from '@lib/data/orders';
|
||||
import { GlobalLoader } from '@kit/ui/makerkit/global-loader';
|
||||
|
||||
function OrderConfirmedLoadingWrapper({
|
||||
medusaOrder: initialMedusaOrder,
|
||||
order,
|
||||
}: {
|
||||
medusaOrder: StoreOrder;
|
||||
order: AnalysisOrder;
|
||||
}) {
|
||||
const [medusaOrder, setMedusaOrder] = useState<StoreOrder>(initialMedusaOrder);
|
||||
const fetchingRef = useRef(false);
|
||||
|
||||
const paymentStatus = medusaOrder.payment_status;
|
||||
const medusaOrderId = order.medusa_order_id;
|
||||
|
||||
useEffect(() => {
|
||||
if (paymentStatus === 'captured') {
|
||||
return;
|
||||
}
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
if (fetchingRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
fetchingRef.current = true;
|
||||
const medusaOrder = await retrieveOrder(medusaOrderId, false);
|
||||
fetchingRef.current = false;
|
||||
|
||||
setMedusaOrder(medusaOrder);
|
||||
}, 2_000);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [paymentStatus, medusaOrderId]);
|
||||
|
||||
const isPaid = paymentStatus === 'captured';
|
||||
|
||||
if (!isPaid) {
|
||||
return (
|
||||
<PageBody>
|
||||
<div className="flex flex-col justify-start items-center h-full pt-[10vh]">
|
||||
<div>
|
||||
<GlobalLoader />
|
||||
</div>
|
||||
<h4 className="text-center">
|
||||
<Trans i18nKey="cart:orderConfirmed.paymentConfirmationLoading" />
|
||||
</h4>
|
||||
</div>
|
||||
</PageBody>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<PageBody>
|
||||
<PageHeader title={<Trans i18nKey="cart:orderConfirmed.title" />} />
|
||||
<Divider />
|
||||
<div className="small:grid-cols-[1fr_360px] grid grid-cols-1 gap-x-40 gap-y-6 lg:px-4">
|
||||
<OrderDetails order={order} />
|
||||
<Divider />
|
||||
<OrderItems medusaOrder={medusaOrder} />
|
||||
<CartTotals medusaOrder={medusaOrder} />
|
||||
</div>
|
||||
</PageBody>
|
||||
);
|
||||
}
|
||||
|
||||
export default OrderConfirmedLoadingWrapper;
|
||||
@@ -1,18 +1,13 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
import CartTotals from '@/app/home/(user)/_components/order/cart-totals';
|
||||
import OrderDetails from '@/app/home/(user)/_components/order/order-details';
|
||||
import OrderItems from '@/app/home/(user)/_components/order/order-items';
|
||||
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
||||
import { retrieveOrder } from '@lib/data/orders';
|
||||
import Divider from '@modules/common/components/divider';
|
||||
|
||||
import { pathsConfig } from '@kit/shared/config';
|
||||
import { PageBody, PageHeader } from '@kit/ui/page';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
import { getAnalysisOrder } from '~/lib/services/order.service';
|
||||
import OrderConfirmedLoadingWrapper from './order-confirmed-loading-wrapper';
|
||||
|
||||
export async function generateMetadata() {
|
||||
const { t } = await createI18nServerInstance();
|
||||
@@ -41,18 +36,7 @@ async function OrderConfirmedPage(props: {
|
||||
redirect(pathsConfig.app.myOrders);
|
||||
}
|
||||
|
||||
return (
|
||||
<PageBody>
|
||||
<PageHeader title={<Trans i18nKey="cart:orderConfirmed.title" />} />
|
||||
<Divider />
|
||||
<div className="small:grid-cols-[1fr_360px] grid grid-cols-1 gap-x-40 gap-y-6 lg:px-4">
|
||||
<OrderDetails order={order} />
|
||||
<Divider />
|
||||
<OrderItems medusaOrder={medusaOrder} />
|
||||
<CartTotals medusaOrder={medusaOrder} />
|
||||
</div>
|
||||
</PageBody>
|
||||
);
|
||||
return <OrderConfirmedLoadingWrapper medusaOrder={medusaOrder} order={order} />;
|
||||
}
|
||||
|
||||
export default withI18n(OrderConfirmedPage);
|
||||
|
||||
@@ -26,17 +26,7 @@ async function OrderConfirmedPage(props: {
|
||||
params: Promise<{ orderId: string }>;
|
||||
}) {
|
||||
const params = await props.params;
|
||||
|
||||
const order = await getAnalysisOrder({
|
||||
analysisOrderId: Number(params.orderId),
|
||||
}).catch(() => null);
|
||||
if (!order) {
|
||||
redirect(pathsConfig.app.myOrders);
|
||||
}
|
||||
|
||||
const medusaOrder = await retrieveOrder(order.medusa_order_id).catch(
|
||||
() => null,
|
||||
);
|
||||
const medusaOrder = await retrieveOrder(params.orderId).catch(() => null);
|
||||
if (!medusaOrder) {
|
||||
redirect(pathsConfig.app.myOrders);
|
||||
}
|
||||
@@ -46,7 +36,12 @@ async function OrderConfirmedPage(props: {
|
||||
<PageHeader title={<Trans i18nKey="cart:order.title" />} />
|
||||
<Divider />
|
||||
<div className="small:grid-cols-[1fr_360px] grid grid-cols-1 gap-x-40 gap-y-6 lg:px-4">
|
||||
<OrderDetails order={order} />
|
||||
<OrderDetails
|
||||
order={{
|
||||
id: medusaOrder.id,
|
||||
created_at: medusaOrder.created_at,
|
||||
}}
|
||||
/>
|
||||
<Divider />
|
||||
<OrderItems medusaOrder={medusaOrder} />
|
||||
<CartTotals medusaOrder={medusaOrder} />
|
||||
|
||||
@@ -11,12 +11,15 @@ import { PageBody } from '@kit/ui/makerkit/page';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
import { getAnalysisOrders } from '~/lib/services/order.service';
|
||||
import { getAnalysisOrders, getTtoOrders } from '~/lib/services/order.service';
|
||||
import { findProductTypeIdByHandle } from '~/lib/utils';
|
||||
import { listOrders } from '~/medusa/lib/data/orders';
|
||||
|
||||
import { HomeLayoutPageHeader } from '../../_components/home-page-header';
|
||||
import OrderBlock from '../../_components/orders/order-block';
|
||||
|
||||
const ORDERS_LIMIT = 50;
|
||||
|
||||
export async function generateMetadata() {
|
||||
const { t } = await createI18nServerInstance();
|
||||
|
||||
@@ -26,17 +29,25 @@ export async function generateMetadata() {
|
||||
}
|
||||
|
||||
async function OrdersPage() {
|
||||
const medusaOrders = await listOrders();
|
||||
const analysisOrders = await getAnalysisOrders();
|
||||
const { productTypes } = await listProductTypes();
|
||||
const [medusaOrders, analysisOrders, ttoOrders, { productTypes }] = await Promise.all([
|
||||
listOrders(ORDERS_LIMIT),
|
||||
getAnalysisOrders(),
|
||||
getTtoOrders(),
|
||||
listProductTypes(),
|
||||
]);
|
||||
|
||||
if (!medusaOrders || !productTypes) {
|
||||
if (!medusaOrders || !productTypes || !ttoOrders) {
|
||||
redirect(pathsConfig.auth.signIn);
|
||||
}
|
||||
|
||||
const analysisPackagesType = productTypes.find(
|
||||
({ metadata }) => metadata?.handle === 'analysis-packages',
|
||||
)!;
|
||||
const analysisPackagesTypeId = findProductTypeIdByHandle(
|
||||
productTypes,
|
||||
'analysis-package',
|
||||
);
|
||||
const ttoServiceTypeId = findProductTypeIdByHandle(
|
||||
productTypes,
|
||||
'tto-service',
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -45,34 +56,45 @@ async function OrdersPage() {
|
||||
description={<Trans i18nKey={'orders:description'} />}
|
||||
/>
|
||||
<PageBody>
|
||||
{analysisOrders.map((analysisOrder) => {
|
||||
const medusaOrder = medusaOrders.find(
|
||||
({ id }) => id === analysisOrder.medusa_order_id,
|
||||
{medusaOrders.map((medusaOrder) => {
|
||||
const analysisOrder = analysisOrders.find(
|
||||
({ medusa_order_id }) => medusa_order_id === medusaOrder.id,
|
||||
);
|
||||
|
||||
if (!medusaOrder) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const medusaOrderItems = medusaOrder.items || [];
|
||||
const medusaOrderItemsAnalysisPackages = medusaOrderItems.filter(
|
||||
(item) => item.product_type_id === analysisPackagesType?.id,
|
||||
(item) => item.product_type_id === analysisPackagesTypeId,
|
||||
);
|
||||
const medusaOrderItemsTtoServices = medusaOrderItems.filter(
|
||||
(item) => item.product_type_id === ttoServiceTypeId,
|
||||
);
|
||||
const medusaOrderItemsOther = medusaOrderItems.filter(
|
||||
(item) => item.product_type_id !== analysisPackagesType?.id,
|
||||
(item) =>
|
||||
!item.product_type_id ||
|
||||
![analysisPackagesTypeId, ttoServiceTypeId].includes(
|
||||
item.product_type_id,
|
||||
),
|
||||
);
|
||||
|
||||
return (
|
||||
<React.Fragment key={analysisOrder.id}>
|
||||
<React.Fragment key={medusaOrder.id}>
|
||||
<Divider className="my-6" />
|
||||
<OrderBlock
|
||||
medusaOrderId={medusaOrder.id}
|
||||
analysisOrder={analysisOrder}
|
||||
medusaOrderStatus={medusaOrder.status}
|
||||
itemsAnalysisPackage={medusaOrderItemsAnalysisPackages}
|
||||
itemsTtoService={medusaOrderItemsTtoServices}
|
||||
itemsOther={medusaOrderItemsOther}
|
||||
/>
|
||||
</React.Fragment>
|
||||
);
|
||||
})}
|
||||
{analysisOrders.length === 0 && (
|
||||
{analysisOrders.length === 0 && ttoOrders.length === 0 && (
|
||||
<h5 className="mt-6">
|
||||
<Trans i18nKey="orders:noOrders" />
|
||||
</h5>
|
||||
|
||||
@@ -16,6 +16,7 @@ import Dashboard from '../_components/dashboard';
|
||||
import DashboardCards from '../_components/dashboard-cards';
|
||||
import Recommendations from '../_components/recommendations';
|
||||
import RecommendationsSkeleton from '../_components/recommendations-skeleton';
|
||||
import { isValidOpenAiEnv } from '../_lib/server/is-valid-open-ai-env';
|
||||
import { loadCurrentUserAccount } from '../_lib/server/load-user-account';
|
||||
|
||||
export const generateMetadata = async () => {
|
||||
@@ -52,17 +53,16 @@ async function UserHomePage() {
|
||||
/>
|
||||
<PageBody>
|
||||
<Dashboard account={account} bmiThresholds={bmiThresholds} />
|
||||
{process.env.OPENAI_API_KEY &&
|
||||
process.env.PROMPT_ID_ANALYSIS_RECOMMENDATIONS && (
|
||||
<>
|
||||
<h4>
|
||||
<Trans i18nKey="dashboard:recommendations.title" />
|
||||
</h4>
|
||||
<Suspense fallback={<RecommendationsSkeleton />}>
|
||||
<Recommendations account={account} />
|
||||
</Suspense>
|
||||
</>
|
||||
)}
|
||||
{(await isValidOpenAiEnv()) && (
|
||||
<>
|
||||
<h4>
|
||||
<Trans i18nKey="dashboard:recommendations.title" />
|
||||
</h4>
|
||||
<Suspense fallback={<RecommendationsSkeleton />}>
|
||||
<Recommendations account={account} />
|
||||
</Suspense>
|
||||
</>
|
||||
)}
|
||||
</PageBody>
|
||||
</>
|
||||
);
|
||||
|
||||
40
app/home/(user)/_components/booking/booking-calendar.tsx
Normal file
40
app/home/(user)/_components/booking/booking-calendar.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
'use client';
|
||||
|
||||
import { isBefore, isSameDay } from 'date-fns';
|
||||
import { uniq } from 'lodash';
|
||||
|
||||
import { Calendar } from '@kit/ui/shadcn/calendar';
|
||||
import { Card } from '@kit/ui/shadcn/card';
|
||||
import { cn } from '@kit/ui/utils';
|
||||
|
||||
import { useBooking } from './booking.provider';
|
||||
|
||||
export default function BookingCalendar() {
|
||||
const { selectedDate, setSelectedDate, isLoadingTimeSlots, timeSlots } =
|
||||
useBooking();
|
||||
const availableDates = uniq(timeSlots?.map((timeSlot) => timeSlot.StartTime));
|
||||
|
||||
return (
|
||||
<Card className="mb-4">
|
||||
<Calendar
|
||||
mode="single"
|
||||
selected={selectedDate}
|
||||
onSelect={setSelectedDate}
|
||||
disabled={(date) => {
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
return (
|
||||
isBefore(date, today) ||
|
||||
!availableDates.some((dateWithBooking) =>
|
||||
isSameDay(date, dateWithBooking),
|
||||
)
|
||||
);
|
||||
}}
|
||||
className={cn('rounded-md border', {
|
||||
'pointer-events-none rounded-md border opacity-50':
|
||||
isLoadingTimeSlots,
|
||||
})}
|
||||
/>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
49
app/home/(user)/_components/booking/booking-container.tsx
Normal file
49
app/home/(user)/_components/booking/booking-container.tsx
Normal file
@@ -0,0 +1,49 @@
|
||||
'use client';
|
||||
|
||||
import { StoreProduct } from '@medusajs/types';
|
||||
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { EnrichedCartItem } from '../cart/types';
|
||||
import BookingCalendar from './booking-calendar';
|
||||
import { BookingProvider } from './booking.provider';
|
||||
import LocationSelector from './location-selector';
|
||||
import ServiceSelector from './service-selector';
|
||||
import TimeSlots from './time-slots';
|
||||
|
||||
const BookingContainer = ({
|
||||
category,
|
||||
cartItem,
|
||||
onComplete,
|
||||
}: {
|
||||
category: { products: StoreProduct[]; countryCode: string };
|
||||
cartItem?: EnrichedCartItem;
|
||||
onComplete?: () => void;
|
||||
}) => {
|
||||
const products = cartItem?.product ? [cartItem.product] : category.products;
|
||||
|
||||
if (!cartItem || !products?.length) {
|
||||
<p>
|
||||
<Trans i18nKey="booking:noProducts" />
|
||||
</p>;
|
||||
}
|
||||
|
||||
return (
|
||||
<BookingProvider category={{ products }} service={cartItem?.product}>
|
||||
<div className="xs:flex-row flex max-h-full flex-col gap-6">
|
||||
<div className="flex flex-col">
|
||||
<ServiceSelector products={products} />
|
||||
<BookingCalendar />
|
||||
<LocationSelector />
|
||||
</div>
|
||||
<TimeSlots
|
||||
countryCode={category.countryCode}
|
||||
cartItem={cartItem}
|
||||
onComplete={onComplete}
|
||||
/>
|
||||
</div>
|
||||
</BookingProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export default BookingContainer;
|
||||
77
app/home/(user)/_components/booking/booking.context.ts
Normal file
77
app/home/(user)/_components/booking/booking.context.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { createContext } from 'react';
|
||||
|
||||
import { StoreProduct } from '@medusajs/types';
|
||||
import { noop } from 'lodash';
|
||||
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
|
||||
export type Location = Tables<
|
||||
{ schema: 'medreport' },
|
||||
'connected_online_locations'
|
||||
>;
|
||||
|
||||
export type TimeSlotResponse = {
|
||||
timeSlots: TimeSlot[];
|
||||
locations: Location[];
|
||||
};
|
||||
|
||||
export type TimeSlot = {
|
||||
ClinicID: number;
|
||||
LocationID: number;
|
||||
UserID: number;
|
||||
SyncUserID: number;
|
||||
ServiceID: number;
|
||||
HKServiceID: number;
|
||||
StartTime: Date;
|
||||
EndTime: Date;
|
||||
PayorCode: string;
|
||||
serviceProvider?: ServiceProvider;
|
||||
syncedService?: SyncedService;
|
||||
} & { location?: Location };
|
||||
|
||||
export type ServiceProvider = {
|
||||
name: string;
|
||||
id: number;
|
||||
jobTitleEn: string | null;
|
||||
jobTitleEt: string | null;
|
||||
jobTitleRu: string | null;
|
||||
clinicId: number;
|
||||
};
|
||||
|
||||
export type SyncedService = Tables<
|
||||
{ schema: 'medreport' },
|
||||
'connected_online_services'
|
||||
> & {
|
||||
providerClinic: ProviderClinic;
|
||||
};
|
||||
|
||||
export type ProviderClinic = Tables<
|
||||
{ schema: 'medreport' },
|
||||
'connected_online_providers'
|
||||
> & { locations: Location[] };
|
||||
|
||||
const BookingContext = createContext<{
|
||||
timeSlots: TimeSlot[] | null;
|
||||
selectedService: StoreProduct | null;
|
||||
locations: Location[] | null;
|
||||
selectedLocationId: number | null;
|
||||
selectedDate?: Date;
|
||||
isLoadingTimeSlots?: boolean;
|
||||
setSelectedService: (selectedService: StoreProduct | null) => void;
|
||||
setSelectedLocationId: (selectedLocationId: number | null) => void;
|
||||
updateTimeSlots: (serviceIds: number[]) => Promise<void>;
|
||||
setSelectedDate: (selectedDate?: Date) => void;
|
||||
}>({
|
||||
timeSlots: null,
|
||||
selectedService: null,
|
||||
locations: null,
|
||||
selectedLocationId: null,
|
||||
selectedDate: new Date(),
|
||||
isLoadingTimeSlots: false,
|
||||
setSelectedService: (_) => _,
|
||||
setSelectedLocationId: (_) => _,
|
||||
updateTimeSlots: async (_) => noop(),
|
||||
setSelectedDate: (_) => _,
|
||||
});
|
||||
|
||||
export { BookingContext };
|
||||
80
app/home/(user)/_components/booking/booking.provider.tsx
Normal file
80
app/home/(user)/_components/booking/booking.provider.tsx
Normal file
@@ -0,0 +1,80 @@
|
||||
import React, { useContext, useEffect, useState } from 'react';
|
||||
|
||||
import { StoreProduct } from '@medusajs/types';
|
||||
|
||||
import { getAvailableTimeSlotsForDisplay } from '~/lib/services/connected-online.service';
|
||||
|
||||
import { BookingContext, Location, TimeSlot } from './booking.context';
|
||||
|
||||
export function useBooking() {
|
||||
const context = useContext(BookingContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error('useBooking must be used within a BookingProvider.');
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
export const BookingProvider: React.FC<{
|
||||
children: React.ReactElement;
|
||||
category: { products: StoreProduct[] };
|
||||
service?: StoreProduct;
|
||||
}> = ({ children, category, service }) => {
|
||||
const [selectedService, setSelectedService] = useState<StoreProduct | null>(
|
||||
(service ?? category?.products?.[0]) || null,
|
||||
);
|
||||
const [selectedLocationId, setSelectedLocationId] = useState<number | null>(
|
||||
null,
|
||||
);
|
||||
const [selectedDate, setSelectedDate] = useState<Date>();
|
||||
const [timeSlots, setTimeSlots] = useState<TimeSlot[] | null>(null);
|
||||
const [locations, setLocations] = useState<Location[] | null>(null);
|
||||
const [isLoadingTimeSlots, setIsLoadingTimeSlots] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const metadataServiceIds = selectedService?.metadata?.serviceIds as string;
|
||||
if (metadataServiceIds) {
|
||||
const json = JSON.parse(metadataServiceIds);
|
||||
if (Array.isArray(json)) {
|
||||
updateTimeSlots(json);
|
||||
}
|
||||
}
|
||||
}, [selectedService, selectedLocationId]);
|
||||
|
||||
const updateTimeSlots = async (serviceIds: number[]) => {
|
||||
setIsLoadingTimeSlots(true);
|
||||
try {
|
||||
console.log('serviceIds', serviceIds, selectedLocationId);
|
||||
const response = await getAvailableTimeSlotsForDisplay(
|
||||
serviceIds,
|
||||
selectedLocationId,
|
||||
);
|
||||
setTimeSlots(response.timeSlots);
|
||||
setLocations(response.locations);
|
||||
} catch (error) {
|
||||
setTimeSlots(null);
|
||||
} finally {
|
||||
setIsLoadingTimeSlots(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<BookingContext.Provider
|
||||
value={{
|
||||
timeSlots,
|
||||
locations,
|
||||
selectedService,
|
||||
selectedLocationId,
|
||||
setSelectedLocationId,
|
||||
selectedDate,
|
||||
isLoadingTimeSlots,
|
||||
setSelectedService,
|
||||
updateTimeSlots,
|
||||
setSelectedDate,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</BookingContext.Provider>
|
||||
);
|
||||
};
|
||||
55
app/home/(user)/_components/booking/location-selector.tsx
Normal file
55
app/home/(user)/_components/booking/location-selector.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
import { Label } from '@medusajs/ui';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import { RadioGroup, RadioGroupItem } from '@kit/ui/radio-group';
|
||||
import { Card } from '@kit/ui/shadcn/card';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { useBooking } from './booking.provider';
|
||||
|
||||
const LocationSelector = () => {
|
||||
const { t } = useTranslation();
|
||||
const { selectedLocationId, setSelectedLocationId, locations } = useBooking();
|
||||
|
||||
const onLocationSelect = (locationId: number | string | null) => {
|
||||
if (locationId === 'all') return setSelectedLocationId(null);
|
||||
setSelectedLocationId(Number(locationId));
|
||||
};
|
||||
|
||||
return (
|
||||
<Card className="mb-4 p-4">
|
||||
<h5 className="text-semibold mb-2">
|
||||
<Trans i18nKey="booking:locations" />
|
||||
</h5>
|
||||
<div className="flex flex-col">
|
||||
<RadioGroup
|
||||
className="mb-2 flex flex-col"
|
||||
onValueChange={(val) => onLocationSelect(val)}
|
||||
>
|
||||
<div className="flex items-center gap-2">
|
||||
<RadioGroupItem
|
||||
value={'all'}
|
||||
id={'all'}
|
||||
checked={selectedLocationId === null}
|
||||
/>
|
||||
<Label htmlFor={'all'}>{t('booking:showAllLocations')}</Label>
|
||||
</div>
|
||||
{locations?.map((location) => (
|
||||
<div key={location.sync_id} className="flex items-center gap-2">
|
||||
<RadioGroupItem
|
||||
value={location.sync_id.toString()}
|
||||
id={location.sync_id.toString()}
|
||||
checked={selectedLocationId === location.sync_id}
|
||||
/>
|
||||
<Label htmlFor={location.sync_id.toString()}>
|
||||
{location.name}
|
||||
</Label>
|
||||
</div>
|
||||
))}
|
||||
</RadioGroup>
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
|
||||
export default LocationSelector;
|
||||
85
app/home/(user)/_components/booking/service-selector.tsx
Normal file
85
app/home/(user)/_components/booking/service-selector.tsx
Normal file
@@ -0,0 +1,85 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
import { StoreProduct } from '@medusajs/types';
|
||||
import { ChevronDown } from 'lucide-react';
|
||||
|
||||
import { Card } from '@kit/ui/shadcn/card';
|
||||
import { Label } from '@kit/ui/shadcn/label';
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from '@kit/ui/shadcn/popover';
|
||||
import { RadioGroup, RadioGroupItem } from '@kit/ui/shadcn/radio-group';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { useBooking } from './booking.provider';
|
||||
|
||||
const ServiceSelector = ({ products }: { products: StoreProduct[] }) => {
|
||||
const { selectedService, setSelectedService } = useBooking();
|
||||
const [collapsed, setCollapsed] = useState(false);
|
||||
const [firstFourProducts] = useState<StoreProduct[]>(products?.slice(0, 4));
|
||||
|
||||
const onServiceSelect = async (productId: StoreProduct['id']) => {
|
||||
const product = products.find((p) => p.id === productId);
|
||||
setSelectedService(product ?? null);
|
||||
setCollapsed(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Card className="mb-4 p-4">
|
||||
<h5 className="text-semibold mb-2">
|
||||
<Trans i18nKey="booking:services" />
|
||||
</h5>
|
||||
<Popover open={collapsed} onOpenChange={setCollapsed}>
|
||||
<div className="flex flex-col">
|
||||
<RadioGroup
|
||||
defaultValue={selectedService?.id || ''}
|
||||
className="mb-2 flex flex-col"
|
||||
onValueChange={onServiceSelect}
|
||||
>
|
||||
{firstFourProducts?.map((product) => (
|
||||
<div key={product.id} className="flex items-center gap-2">
|
||||
<RadioGroupItem
|
||||
value={product.id}
|
||||
id={product.id}
|
||||
checked={selectedService?.id === product.id}
|
||||
/>
|
||||
<Label htmlFor={product.id}>{product.title}</Label>
|
||||
</div>
|
||||
))}
|
||||
</RadioGroup>
|
||||
{products.length > 4 && (
|
||||
<PopoverTrigger asChild>
|
||||
<div
|
||||
onClick={() => setCollapsed((_) => !_)}
|
||||
className="flex cursor-pointer items-center justify-between border-t py-1"
|
||||
>
|
||||
<span>
|
||||
<Trans i18nKey="booking:showAll" />
|
||||
</span>
|
||||
<ChevronDown />
|
||||
</div>
|
||||
</PopoverTrigger>
|
||||
)}
|
||||
</div>
|
||||
<PopoverContent sideOffset={10}>
|
||||
<RadioGroup onValueChange={onServiceSelect}>
|
||||
{products?.map((product) => (
|
||||
<div key={product.id + '-2'} className="flex items-center gap-2">
|
||||
<RadioGroupItem
|
||||
value={product.id}
|
||||
id={product.id + '-2'}
|
||||
checked={selectedService?.id === product.id}
|
||||
/>
|
||||
<Label htmlFor={product.id + '-2'}>{product.title}</Label>
|
||||
</div>
|
||||
))}
|
||||
</RadioGroup>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
|
||||
export default ServiceSelector;
|
||||
319
app/home/(user)/_components/booking/time-slots.tsx
Normal file
319
app/home/(user)/_components/booking/time-slots.tsx
Normal file
@@ -0,0 +1,319 @@
|
||||
import { useMemo, useState } from 'react';
|
||||
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
import { formatCurrency } from '@/packages/shared/src/utils';
|
||||
import { addHours, isAfter, isSameDay } from 'date-fns';
|
||||
import { orderBy } from 'lodash';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import { pathsConfig } from '@kit/shared/config';
|
||||
import { formatDateAndTime } from '@kit/shared/utils';
|
||||
import { Button } from '@kit/ui/shadcn/button';
|
||||
import { Card } from '@kit/ui/shadcn/card';
|
||||
import { toast } from '@kit/ui/sonner';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
import { cn } from '@kit/ui/utils';
|
||||
|
||||
import { updateReservationTime } from '~/lib/services/reservation.service';
|
||||
|
||||
import { createInitialReservationAction } from '../../_lib/server/actions';
|
||||
import { EnrichedCartItem } from '../cart/types';
|
||||
import { ServiceProvider, TimeSlot } from './booking.context';
|
||||
import { useBooking } from './booking.provider';
|
||||
|
||||
const getServiceProviderTitle = (
|
||||
currentLocale: string,
|
||||
serviceProvider?: ServiceProvider,
|
||||
) => {
|
||||
if (!serviceProvider) return null;
|
||||
if (currentLocale === 'en') return serviceProvider.jobTitleEn;
|
||||
if (currentLocale === 'ru') return serviceProvider.jobTitleRu;
|
||||
|
||||
return serviceProvider.jobTitleEt;
|
||||
};
|
||||
|
||||
const PAGE_SIZE = 7;
|
||||
|
||||
const TimeSlots = ({
|
||||
countryCode,
|
||||
cartItem,
|
||||
onComplete,
|
||||
}: {
|
||||
countryCode: string;
|
||||
cartItem?: EnrichedCartItem;
|
||||
onComplete?: () => void;
|
||||
}) => {
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
|
||||
const {
|
||||
t,
|
||||
i18n: { language: currentLocale },
|
||||
} = useTranslation();
|
||||
|
||||
const booking = useBooking();
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
const selectedDate = booking.selectedDate ?? new Date();
|
||||
|
||||
const filteredBookings = useMemo(
|
||||
() =>
|
||||
orderBy(
|
||||
booking?.timeSlots?.filter(({ StartTime }) => {
|
||||
const firstAvailableTimeToSelect = isSameDay(selectedDate, new Date())
|
||||
? addHours(new Date(), 0.5)
|
||||
: selectedDate;
|
||||
return isAfter(StartTime, firstAvailableTimeToSelect);
|
||||
}) ?? [],
|
||||
'StartTime',
|
||||
'asc',
|
||||
),
|
||||
[booking.timeSlots, selectedDate],
|
||||
);
|
||||
|
||||
const totalPages = Math.ceil(filteredBookings.length / PAGE_SIZE);
|
||||
|
||||
const paginatedBookings = useMemo(() => {
|
||||
const startIndex = (currentPage - 1) * PAGE_SIZE;
|
||||
const endIndex = startIndex + PAGE_SIZE;
|
||||
return filteredBookings.slice(startIndex, endIndex);
|
||||
}, [filteredBookings, currentPage, PAGE_SIZE]);
|
||||
|
||||
const handlePageChange = (page: number) => {
|
||||
setCurrentPage(page);
|
||||
};
|
||||
|
||||
const generatePageNumbers = () => {
|
||||
const pages = [];
|
||||
const maxVisiblePages = 5;
|
||||
|
||||
if (totalPages <= maxVisiblePages) {
|
||||
for (let i = 1; i <= totalPages; i++) {
|
||||
pages.push(i);
|
||||
}
|
||||
} else {
|
||||
if (currentPage <= 3) {
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
pages.push(i);
|
||||
}
|
||||
pages.push('...');
|
||||
pages.push(totalPages);
|
||||
} else if (currentPage >= totalPages - 2) {
|
||||
pages.push(1);
|
||||
pages.push('...');
|
||||
for (let i = totalPages - 3; i <= totalPages; i++) {
|
||||
pages.push(i);
|
||||
}
|
||||
} else {
|
||||
pages.push(1);
|
||||
pages.push('...');
|
||||
for (let i = currentPage - 1; i <= currentPage + 1; i++) {
|
||||
pages.push(i);
|
||||
}
|
||||
pages.push('...');
|
||||
pages.push(totalPages);
|
||||
}
|
||||
}
|
||||
|
||||
return pages;
|
||||
};
|
||||
|
||||
if (!booking?.timeSlots?.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const handleBookTime = async (timeSlot: TimeSlot, comments?: string) => {
|
||||
const selectedService = booking.selectedService;
|
||||
|
||||
const selectedVariant = selectedService?.variants?.[0];
|
||||
|
||||
const syncedService = timeSlot.syncedService;
|
||||
if (!syncedService || !selectedVariant) {
|
||||
return toast.error(t('booking:serviceNotFound'));
|
||||
}
|
||||
|
||||
const bookTimePromise = createInitialReservationAction(
|
||||
selectedVariant,
|
||||
countryCode,
|
||||
Number(syncedService.id),
|
||||
syncedService?.clinic_id,
|
||||
timeSlot.UserID,
|
||||
timeSlot.SyncUserID,
|
||||
timeSlot.StartTime,
|
||||
booking.selectedLocationId ? booking.selectedLocationId : null,
|
||||
comments,
|
||||
).then(() => {
|
||||
if (onComplete) {
|
||||
onComplete();
|
||||
}
|
||||
router.push(pathsConfig.app.cart);
|
||||
});
|
||||
|
||||
toast.promise(() => bookTimePromise, {
|
||||
success: <Trans i18nKey={'booking:bookTimeSuccess'} />,
|
||||
error: <Trans i18nKey={'booking:bookTimeError'} />,
|
||||
loading: <Trans i18nKey={'booking:bookTimeLoading'} />,
|
||||
});
|
||||
};
|
||||
|
||||
const handleChangeTime = async (
|
||||
timeSlot: TimeSlot,
|
||||
reservationId: number,
|
||||
cartId: string,
|
||||
) => {
|
||||
const syncedService = timeSlot.syncedService;
|
||||
if (!syncedService) {
|
||||
return toast.error(t('booking:serviceNotFound'));
|
||||
}
|
||||
|
||||
const bookTimePromise = updateReservationTime({
|
||||
reservationId,
|
||||
newStartTime: timeSlot.StartTime,
|
||||
newServiceId: Number(syncedService.id),
|
||||
newAppointmentUserId: timeSlot.UserID,
|
||||
newSyncUserId: timeSlot.SyncUserID,
|
||||
newLocationId: booking.selectedLocationId
|
||||
? booking.selectedLocationId
|
||||
: null,
|
||||
cartId,
|
||||
});
|
||||
|
||||
toast.promise(() => bookTimePromise, {
|
||||
success: <Trans i18nKey={'booking:bookTimeSuccess'} />,
|
||||
error: <Trans i18nKey={'booking:bookTimeError'} />,
|
||||
loading: <Trans i18nKey={'booking:bookTimeLoading'} />,
|
||||
});
|
||||
|
||||
if (onComplete) {
|
||||
onComplete();
|
||||
}
|
||||
};
|
||||
|
||||
const handleTimeSelect = async (timeSlot: TimeSlot) => {
|
||||
if (cartItem?.reservation.id) {
|
||||
return handleChangeTime(
|
||||
timeSlot,
|
||||
cartItem.reservation.id,
|
||||
cartItem.cart_id,
|
||||
);
|
||||
}
|
||||
|
||||
return handleBookTime(timeSlot);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex w-full flex-col gap-4">
|
||||
<div className="flex h-full w-full flex-col gap-2 overflow-auto">
|
||||
{paginatedBookings.map((timeSlot, index) => {
|
||||
const isEHIF = timeSlot.HKServiceID > 0;
|
||||
const serviceProviderTitle = getServiceProviderTitle(
|
||||
currentLocale,
|
||||
timeSlot.serviceProvider,
|
||||
);
|
||||
const price =
|
||||
booking.selectedService?.variants?.[0]?.calculated_price
|
||||
?.calculated_amount ?? cartItem?.unit_price;
|
||||
return (
|
||||
<Card
|
||||
className="xs:flex xs:justify-between grid w-full justify-center-safe gap-3 p-4"
|
||||
key={index}
|
||||
>
|
||||
<div>
|
||||
<span>{formatDateAndTime(timeSlot.StartTime.toString())}</span>
|
||||
<div className="flex">
|
||||
<h5
|
||||
className={cn(
|
||||
(serviceProviderTitle || isEHIF) &&
|
||||
"after:mx-2 after:content-['·']",
|
||||
)}
|
||||
>
|
||||
{timeSlot.serviceProvider?.name}
|
||||
</h5>
|
||||
{serviceProviderTitle && (
|
||||
<span
|
||||
className={cn(isEHIF && "after:mx-2 after:content-['·']")}
|
||||
>
|
||||
{serviceProviderTitle}
|
||||
</span>
|
||||
)}
|
||||
{isEHIF && <span>{t('booking:ehifBooking')}</span>}
|
||||
</div>
|
||||
<div className="flex text-xs">{timeSlot.location?.address}</div>
|
||||
</div>
|
||||
<div className="flex-end not-last:xs:justify-center flex items-center justify-between gap-2">
|
||||
<span className="text-sm font-semibold">
|
||||
{formatCurrency({
|
||||
currencyCode: 'EUR',
|
||||
locale: 'et-EE',
|
||||
value: price ?? '',
|
||||
})}
|
||||
</span>
|
||||
<Button onClick={() => handleTimeSelect(timeSlot)} size="sm">
|
||||
<Trans i18nKey="common:book" />
|
||||
</Button>
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
})}
|
||||
|
||||
{!paginatedBookings.length && (
|
||||
<div className="wrap text-muted-foreground flex size-full content-center-safe justify-center-safe">
|
||||
<p>{t('booking:noResults')}</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{totalPages > 1 && (
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="text-muted-foreground text-sm">
|
||||
{t('common:pageOfPages', {
|
||||
page: currentPage,
|
||||
total: totalPages,
|
||||
})}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-1">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => handlePageChange(currentPage - 1)}
|
||||
disabled={currentPage === 1}
|
||||
>
|
||||
<Trans i18nKey="common:previous" defaultValue="Previous" />
|
||||
</Button>
|
||||
|
||||
{generatePageNumbers().map((page, index) => (
|
||||
<Button
|
||||
key={index}
|
||||
variant={page === currentPage ? 'default' : 'outline'}
|
||||
size="sm"
|
||||
onClick={() =>
|
||||
typeof page === 'number' && handlePageChange(page)
|
||||
}
|
||||
disabled={page === '...'}
|
||||
className={cn(
|
||||
'min-w-[2rem]',
|
||||
page === '...' && 'cursor-default hover:bg-transparent',
|
||||
)}
|
||||
>
|
||||
{page}
|
||||
</Button>
|
||||
))}
|
||||
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => handlePageChange(currentPage + 1)}
|
||||
disabled={currentPage === totalPages}
|
||||
>
|
||||
<Trans i18nKey="common:next" defaultValue="Next" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default TimeSlots;
|
||||
146
app/home/(user)/_components/cart/cart-service-item.tsx
Normal file
146
app/home/(user)/_components/cart/cart-service-item.tsx
Normal file
@@ -0,0 +1,146 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
import { formatCurrency, formatDateAndTime } from '@/packages/shared/src/utils';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import { Button } from '@kit/ui/button';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@kit/ui/dialog';
|
||||
import { TableCell, TableRow } from '@kit/ui/table';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import BookingContainer from '../booking/booking-container';
|
||||
import CartItemDelete from './cart-item-delete';
|
||||
import { EnrichedCartItem } from './types';
|
||||
|
||||
const EditCartServiceItemModal = ({
|
||||
item,
|
||||
onComplete,
|
||||
}: {
|
||||
item: EnrichedCartItem | null;
|
||||
onComplete: () => void;
|
||||
}) => {
|
||||
if (!item) return null;
|
||||
|
||||
return (
|
||||
<Dialog defaultOpen>
|
||||
<DialogContent className="xs:max-w-[90vw] flex max-h-screen max-w-full flex-col items-center gap-4 space-y-4 overflow-y-scroll">
|
||||
<DialogHeader className="items-center text-center">
|
||||
<DialogTitle>
|
||||
<Trans i18nKey="cart:editServiceItem.title" />
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
<Trans i18nKey="cart:editServiceItem.description" />
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div>
|
||||
{item.product && item.reservation.countryCode ? (
|
||||
<BookingContainer
|
||||
category={{
|
||||
products: [item.product],
|
||||
countryCode: item.reservation.countryCode,
|
||||
}}
|
||||
cartItem={item}
|
||||
onComplete={onComplete}
|
||||
/>
|
||||
) : (
|
||||
<p>
|
||||
<Trans i18nKey="booking:noProducts" />
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
||||
|
||||
export default function CartServiceItem({
|
||||
item,
|
||||
currencyCode,
|
||||
isUnavailable,
|
||||
}: {
|
||||
item: EnrichedCartItem;
|
||||
currencyCode: string;
|
||||
isUnavailable?: boolean;
|
||||
}) {
|
||||
const [editingItem, setEditingItem] = useState<EnrichedCartItem | null>(null);
|
||||
const {
|
||||
i18n: { language },
|
||||
} = useTranslation();
|
||||
|
||||
return (
|
||||
<>
|
||||
<TableRow className="w-full" data-testid="product-row">
|
||||
<TableCell className="w-[100%] px-4 text-left sm:px-6">
|
||||
<p
|
||||
className="txt-medium-plus text-ui-fg-base"
|
||||
data-testid="product-title"
|
||||
>
|
||||
{item.product_title}
|
||||
</p>
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="px-4 sm:px-6">
|
||||
{formatDateAndTime(item.reservation.startTime.toString())}
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="px-4 sm:px-6">
|
||||
{item.reservation.location?.address ?? '-'}
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="px-4 sm:px-6">{item.quantity}</TableCell>
|
||||
|
||||
<TableCell className="min-w-[80px] px-4 sm:px-6">
|
||||
{formatCurrency({
|
||||
value: item.unit_price,
|
||||
currencyCode,
|
||||
locale: language,
|
||||
})}
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="min-w-[80px] px-4 text-right sm:px-6">
|
||||
{formatCurrency({
|
||||
value: item.total,
|
||||
currencyCode,
|
||||
locale: language,
|
||||
})}
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="px-4 text-right sm:px-6">
|
||||
<span className="flex justify-end gap-x-1">
|
||||
<Button size="sm" onClick={() => setEditingItem(item)}>
|
||||
<Trans i18nKey="common:change" />
|
||||
</Button>
|
||||
</span>
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="px-4 text-right sm:px-6">
|
||||
<span className="flex w-[60px] justify-end gap-x-1">
|
||||
<CartItemDelete id={item.id} />
|
||||
</span>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
{isUnavailable && (
|
||||
<TableRow>
|
||||
<TableCell
|
||||
colSpan={8}
|
||||
className="text-destructive px-4 text-left sm:px-6"
|
||||
>
|
||||
<Trans i18nKey="booking:timeSlotUnavailable" />
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
<EditCartServiceItemModal
|
||||
item={editingItem}
|
||||
onComplete={() => setEditingItem(null)}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
72
app/home/(user)/_components/cart/cart-service-items.tsx
Normal file
72
app/home/(user)/_components/cart/cart-service-items.tsx
Normal file
@@ -0,0 +1,72 @@
|
||||
import { StoreCart } from '@medusajs/types';
|
||||
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@kit/ui/table';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import CartServiceItem from './cart-service-item';
|
||||
import { EnrichedCartItem } from './types';
|
||||
|
||||
export default function CartServiceItems({
|
||||
cart,
|
||||
items,
|
||||
productColumnLabelKey,
|
||||
unavailableLineItemIds,
|
||||
}: {
|
||||
cart: StoreCart;
|
||||
items: EnrichedCartItem[];
|
||||
productColumnLabelKey: string;
|
||||
unavailableLineItemIds?: string[];
|
||||
}) {
|
||||
if (!items || items.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Table className="border-separate rounded-lg border">
|
||||
<TableHeader className="text-ui-fg-subtle txt-medium-plus">
|
||||
<TableRow>
|
||||
<TableHead className="px-4 sm:px-6">
|
||||
<Trans i18nKey={productColumnLabelKey} />
|
||||
</TableHead>
|
||||
<TableHead className="px-4 sm:px-6">
|
||||
<Trans i18nKey="cart:table.time" />
|
||||
</TableHead>
|
||||
<TableHead className="px-4 sm:px-6">
|
||||
<Trans i18nKey="cart:table.location" />
|
||||
</TableHead>
|
||||
<TableHead className="px-4 sm:px-6">
|
||||
<Trans i18nKey="cart:table.quantity" />
|
||||
</TableHead>
|
||||
<TableHead className="min-w-[100px] px-4 sm:px-6">
|
||||
<Trans i18nKey="cart:table.price" />
|
||||
</TableHead>
|
||||
<TableHead className="min-w-[100px] px-4 text-right sm:px-6">
|
||||
<Trans i18nKey="cart:table.total" />
|
||||
</TableHead>
|
||||
<TableHead className="px-4 sm:px-6"></TableHead>
|
||||
<TableHead className="px-4 sm:px-6"></TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{items
|
||||
.sort((a, b) =>
|
||||
(a.created_at ?? '') > (b.created_at ?? '') ? -1 : 1,
|
||||
)
|
||||
.map((item) => (
|
||||
<CartServiceItem
|
||||
key={item.id}
|
||||
item={item}
|
||||
currencyCode={cart.currency_code}
|
||||
isUnavailable={unavailableLineItemIds?.includes(item.id)}
|
||||
/>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
);
|
||||
}
|
||||
@@ -2,9 +2,7 @@
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
import { handleNavigateToPayment } from '@/lib/services/medusaCart.service';
|
||||
import { formatCurrency } from '@/packages/shared/src/utils';
|
||||
import { initiatePaymentSession } from '@lib/data/cart';
|
||||
import { StoreCart, StoreCartLineItem } from '@medusajs/types';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
@@ -15,28 +13,41 @@ import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import AnalysisLocation from './analysis-location';
|
||||
import CartItems from './cart-items';
|
||||
import CartServiceItems from './cart-service-items';
|
||||
import DiscountCode from './discount-code';
|
||||
import { initiatePayment } from '../../_lib/server/cart-actions';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { AccountBalanceSummary } from '@kit/accounts/services/account-balance.service';
|
||||
import { EnrichedCartItem } from './types';
|
||||
|
||||
const IS_DISCOUNT_SHOWN = true as boolean;
|
||||
|
||||
export default function Cart({
|
||||
accountId,
|
||||
cart,
|
||||
synlabAnalyses,
|
||||
ttoServiceItems,
|
||||
balanceSummary,
|
||||
}: {
|
||||
accountId: string;
|
||||
cart: StoreCart | null;
|
||||
synlabAnalyses: StoreCartLineItem[];
|
||||
ttoServiceItems: StoreCartLineItem[];
|
||||
ttoServiceItems: EnrichedCartItem[];
|
||||
balanceSummary: AccountBalanceSummary | null;
|
||||
}) {
|
||||
const {
|
||||
i18n: { language },
|
||||
} = useTranslation();
|
||||
|
||||
const [isInitiatingSession, setIsInitiatingSession] = useState(false);
|
||||
const router = useRouter();
|
||||
const [unavailableLineItemIds, setUnavailableLineItemIds] =
|
||||
useState<string[]>();
|
||||
|
||||
const items = cart?.items ?? [];
|
||||
const hasCartItems = cart && Array.isArray(items) && items.length > 0;
|
||||
|
||||
if (!cart || items.length === 0) {
|
||||
if (!hasCartItems) {
|
||||
return (
|
||||
<div className="content-container py-5 lg:px-4">
|
||||
<div>
|
||||
@@ -56,24 +67,38 @@ export default function Cart({
|
||||
);
|
||||
}
|
||||
|
||||
async function initiatePayment() {
|
||||
async function initiateSession() {
|
||||
setIsInitiatingSession(true);
|
||||
const response = await initiatePaymentSession(cart!, {
|
||||
provider_id: 'pp_montonio_montonio',
|
||||
});
|
||||
if (response.payment_collection) {
|
||||
const { payment_sessions } = response.payment_collection;
|
||||
const paymentSessionId = payment_sessions![0]!.id;
|
||||
const url = await handleNavigateToPayment({ language, paymentSessionId });
|
||||
window.location.href = url;
|
||||
} else {
|
||||
|
||||
try {
|
||||
const { url, isFullyPaidByBenefits, orderId, unavailableLineItemIds } = await initiatePayment({
|
||||
accountId,
|
||||
balanceSummary: balanceSummary!,
|
||||
cart: cart!,
|
||||
language,
|
||||
});
|
||||
if (unavailableLineItemIds) {
|
||||
setUnavailableLineItemIds(unavailableLineItemIds);
|
||||
}
|
||||
if (url) {
|
||||
window.location.href = url;
|
||||
} else if (isFullyPaidByBenefits) {
|
||||
if (typeof orderId !== 'number') {
|
||||
throw new Error('Order ID is missing');
|
||||
}
|
||||
router.push(`/home/order/${orderId}/confirmed`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to initiate payment', error);
|
||||
setIsInitiatingSession(false);
|
||||
}
|
||||
}
|
||||
|
||||
const hasCartItems = Array.isArray(cart.items) && cart.items.length > 0;
|
||||
const isLocationsShown = synlabAnalyses.length > 0;
|
||||
|
||||
const companyBenefitsTotal = balanceSummary?.totalBalance ?? 0;
|
||||
const montonioTotal = cart && companyBenefitsTotal > 0 ? cart.total - companyBenefitsTotal : cart.total;
|
||||
|
||||
return (
|
||||
<div className="small:grid-cols-[1fr_360px] grid grid-cols-1 gap-x-40 lg:px-4">
|
||||
<div className="flex flex-col gap-y-6 bg-white">
|
||||
@@ -82,10 +107,11 @@ export default function Cart({
|
||||
items={synlabAnalyses}
|
||||
productColumnLabelKey="cart:items.synlabAnalyses.productColumnLabel"
|
||||
/>
|
||||
<CartItems
|
||||
<CartServiceItems
|
||||
cart={cart}
|
||||
items={ttoServiceItems}
|
||||
productColumnLabelKey="cart:items.ttoServices.productColumnLabel"
|
||||
unavailableLineItemIds={unavailableLineItemIds}
|
||||
/>
|
||||
</div>
|
||||
{hasCartItems && (
|
||||
@@ -106,7 +132,7 @@ export default function Cart({
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-x-4 px-4 py-2 sm:justify-end sm:px-6 sm:py-4">
|
||||
<div className="flex gap-x-4 px-4 pt-2 sm:justify-end sm:px-6 sm:pt-4">
|
||||
<div className="w-full sm:mr-[42px] sm:w-auto">
|
||||
<p className="text-muted-foreground ml-0 text-sm font-bold">
|
||||
<Trans i18nKey="cart:order.promotionsTotal" />
|
||||
@@ -122,6 +148,24 @@ export default function Cart({
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
{companyBenefitsTotal > 0 && (
|
||||
<div className="flex gap-x-4 px-4 py-2 sm:justify-end sm:px-6 sm:py-4">
|
||||
<div className="w-full sm:mr-[42px] sm:w-auto">
|
||||
<p className="text-muted-foreground ml-0 text-sm font-bold">
|
||||
<Trans i18nKey="cart:order.companyBenefitsTotal" />
|
||||
</p>
|
||||
</div>
|
||||
<div className={`sm:mr-[112px] sm:w-[50px]`}>
|
||||
<p className="text-right text-sm">
|
||||
{formatCurrency({
|
||||
value: (companyBenefitsTotal > cart.total ? cart.total : companyBenefitsTotal),
|
||||
currencyCode: cart.currency_code,
|
||||
locale: language,
|
||||
})}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex gap-x-4 px-4 sm:justify-end sm:px-6">
|
||||
<div className="w-full sm:mr-[42px] sm:w-auto">
|
||||
<p className="ml-0 text-sm font-bold">
|
||||
@@ -131,7 +175,7 @@ export default function Cart({
|
||||
<div className={`sm:mr-[112px] sm:w-[50px]`}>
|
||||
<p className="text-right text-sm">
|
||||
{formatCurrency({
|
||||
value: cart.total,
|
||||
value: montonioTotal < 0 ? 0 : montonioTotal,
|
||||
currencyCode: cart.currency_code,
|
||||
locale: language,
|
||||
})}
|
||||
@@ -175,7 +219,7 @@ export default function Cart({
|
||||
<div>
|
||||
<Button
|
||||
className="h-10"
|
||||
onClick={initiatePayment}
|
||||
onClick={initiateSession}
|
||||
disabled={isInitiatingSession}
|
||||
>
|
||||
{isInitiatingSession && (
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { StoreCartLineItem } from "@medusajs/types";
|
||||
import { Reservation } from "~/lib/types/reservation";
|
||||
|
||||
export interface MontonioOrderToken {
|
||||
uuid: string;
|
||||
accessKey: string;
|
||||
@@ -10,6 +13,12 @@ export interface MontonioOrderToken {
|
||||
| 'PENDING'
|
||||
| 'EXPIRED'
|
||||
| 'REFUNDED';
|
||||
| 'PAID'
|
||||
| 'FAILED'
|
||||
| 'CANCELLED'
|
||||
| 'PENDING'
|
||||
| 'EXPIRED'
|
||||
| 'REFUNDED';
|
||||
paymentMethod: string;
|
||||
grandTotal: number;
|
||||
currency: string;
|
||||
@@ -20,3 +29,10 @@ export interface MontonioOrderToken {
|
||||
iat: number;
|
||||
exp: number;
|
||||
}
|
||||
|
||||
export enum CartItemType {
|
||||
analysisOrders = 'analysisOrders',
|
||||
ttoServices = 'ttoServices',
|
||||
}
|
||||
|
||||
export type EnrichedCartItem = StoreCartLineItem & { reservation: Reservation };
|
||||
|
||||
@@ -3,12 +3,33 @@ import Link from 'next/link';
|
||||
import { ChevronRight, HeartPulse } from 'lucide-react';
|
||||
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Card, CardDescription, CardFooter, CardHeader } from '@kit/ui/card';
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardFooter,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@kit/ui/card';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
import { formatCurrency } from '@/packages/shared/src/utils';
|
||||
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';
|
||||
import { cn } from '@kit/ui/lib/utils';
|
||||
import { loadCurrentUserAccount } from '../_lib/server/load-user-account';
|
||||
import { getAccountBalanceSummary } from '../_lib/server/balance-actions';
|
||||
|
||||
export default async function DashboardCards() {
|
||||
const { language } = await createI18nServerInstance();
|
||||
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
const balanceSummary = account ? await getAccountBalanceSummary(account.id) : null;
|
||||
|
||||
export default function DashboardCards() {
|
||||
return (
|
||||
<div className="flex gap-4">
|
||||
<div
|
||||
className={cn(
|
||||
'grid grid-cols-1 gap-4',
|
||||
'md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
|
||||
)}>
|
||||
<Card
|
||||
variant="gradient-success"
|
||||
className="xs:w-1/2 flex w-full flex-col justify-between sm:w-auto"
|
||||
@@ -38,6 +59,34 @@ export default function DashboardCards() {
|
||||
</CardDescription>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
|
||||
<Card className="flex flex-col justify-center">
|
||||
<CardHeader>
|
||||
<CardTitle size="h5">
|
||||
<Trans i18nKey="dashboard:heroCard.benefits.title" />
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent>
|
||||
<Figure>
|
||||
{formatCurrency({
|
||||
value: balanceSummary?.totalBalance || 0,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
})}
|
||||
</Figure>
|
||||
<CardDescription>
|
||||
<Trans
|
||||
i18nKey="dashboard:heroCard.benefits.validUntil"
|
||||
values={{ date: '31.12.2025' }}
|
||||
/>
|
||||
</CardDescription>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Figure(props: React.PropsWithChildren) {
|
||||
return <div className={'text-3xl font-bold'}>{props.children}</div>;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
import Link from 'next/link';
|
||||
|
||||
import { Database } from '@/packages/supabase/src/database.types';
|
||||
import { BlendingModeIcon, RulerHorizontalIcon } from '@radix-ui/react-icons';
|
||||
import { isNil } from 'lodash';
|
||||
import {
|
||||
@@ -15,7 +14,7 @@ import {
|
||||
User,
|
||||
} from 'lucide-react';
|
||||
|
||||
import type { AccountWithParams } from '@kit/accounts/types/accounts';
|
||||
import type { AccountWithParams, BmiThresholds } from '@kit/accounts/types/accounts';
|
||||
import { pathsConfig } from '@kit/shared/config';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import {
|
||||
@@ -138,10 +137,7 @@ export default function Dashboard({
|
||||
bmiThresholds,
|
||||
}: {
|
||||
account: AccountWithParams;
|
||||
bmiThresholds: Omit<
|
||||
Database['medreport']['Tables']['bmi_thresholds']['Row'],
|
||||
'id'
|
||||
>[];
|
||||
bmiThresholds: Omit<BmiThresholds, 'id'>[];
|
||||
}) {
|
||||
const height = account.accountParams?.height || 0;
|
||||
const weight = account.accountParams?.weight || 0;
|
||||
|
||||
@@ -103,7 +103,6 @@ export default function OrderAnalysesCards({
|
||||
{title}
|
||||
{description && (
|
||||
<>
|
||||
{' '}
|
||||
<InfoTooltip
|
||||
content={
|
||||
<div className="flex flex-col gap-2">
|
||||
|
||||
@@ -8,6 +8,11 @@ import { useTranslation } from 'react-i18next';
|
||||
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
const PaymentProviderIds = {
|
||||
COMPANY_BENEFITS: "pp_company-benefits_company-benefits",
|
||||
MONTONIO: "pp_montonio_montonio",
|
||||
};
|
||||
|
||||
export default function CartTotals({
|
||||
medusaOrder,
|
||||
}: {
|
||||
@@ -20,11 +25,16 @@ export default function CartTotals({
|
||||
currency_code,
|
||||
total,
|
||||
subtotal,
|
||||
tax_total,
|
||||
discount_total,
|
||||
gift_card_total,
|
||||
payment_collections,
|
||||
} = medusaOrder;
|
||||
|
||||
const montonioPayment = payment_collections?.[0]?.payments
|
||||
?.find(({ provider_id }) => provider_id === PaymentProviderIds.MONTONIO);
|
||||
const companyBenefitsPayment = payment_collections?.[0]?.payments
|
||||
?.find(({ provider_id }) => provider_id === PaymentProviderIds.COMPANY_BENEFITS);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="txt-medium text-ui-fg-subtle flex flex-col gap-y-2">
|
||||
@@ -86,8 +96,11 @@ export default function CartTotals({
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
</div>
|
||||
|
||||
<div className="my-4 h-px w-full border-b border-gray-200" />
|
||||
|
||||
<div className="text-ui-fg-base txt-medium mb-2 flex items-center justify-between">
|
||||
<span className="font-bold">
|
||||
<Trans i18nKey="cart:order.total" />
|
||||
@@ -104,7 +117,42 @@ export default function CartTotals({
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
<div className="mt-4 h-px w-full border-b border-gray-200" />
|
||||
|
||||
<div className="my-4 h-px w-full border-b border-gray-200" />
|
||||
|
||||
<div className="txt-medium text-ui-fg-subtle flex flex-col gap-y-2">
|
||||
{companyBenefitsPayment && (
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="flex items-center gap-x-1">
|
||||
<Trans i18nKey="cart:order.benefitsTotal" />
|
||||
</span>
|
||||
<span data-testid="cart-subtotal" data-value={companyBenefitsPayment.amount || 0}>
|
||||
-{' '}
|
||||
{formatCurrency({
|
||||
value: companyBenefitsPayment.amount ?? 0,
|
||||
currencyCode: currency_code,
|
||||
locale: language,
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{montonioPayment && (
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="flex items-center gap-x-1">
|
||||
<Trans i18nKey="cart:order.montonioTotal" />
|
||||
</span>
|
||||
<span data-testid="cart-subtotal" data-value={montonioPayment.amount || 0}>
|
||||
-{' '}
|
||||
{formatCurrency({
|
||||
value: montonioPayment.amount ?? 0,
|
||||
currencyCode: currency_code,
|
||||
locale: language,
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,16 +2,18 @@ import { formatDate } from 'date-fns';
|
||||
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import type { AnalysisOrder } from '~/lib/types/analysis-order';
|
||||
|
||||
export default function OrderDetails({ order }: { order: AnalysisOrder }) {
|
||||
export default function OrderDetails({
|
||||
order,
|
||||
}: {
|
||||
order: { id: string; created_at: string | Date };
|
||||
}) {
|
||||
return (
|
||||
<div className="flex flex-col gap-y-2">
|
||||
<div>
|
||||
<span className="font-bold">
|
||||
<Trans i18nKey="cart:orderConfirmed.orderNumber" />:{' '}
|
||||
</span>
|
||||
<span>{order.medusa_order_id}</span>
|
||||
<span className="break-all">{order.id}</span>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
|
||||
@@ -5,51 +5,77 @@ import { Eye } from 'lucide-react';
|
||||
|
||||
import { Trans } from '@kit/ui/makerkit/trans';
|
||||
|
||||
import type { AnalysisOrder } from '~/lib/types/analysis-order';
|
||||
import type { AnalysisOrder } from '~/lib/types/order';
|
||||
|
||||
import OrderItemsTable from './order-items-table';
|
||||
|
||||
export default function OrderBlock({
|
||||
analysisOrder,
|
||||
medusaOrderStatus,
|
||||
itemsAnalysisPackage,
|
||||
itemsTtoService,
|
||||
itemsOther,
|
||||
medusaOrderId,
|
||||
}: {
|
||||
analysisOrder: AnalysisOrder;
|
||||
analysisOrder?: AnalysisOrder;
|
||||
medusaOrderStatus: string;
|
||||
itemsAnalysisPackage: StoreOrderLineItem[];
|
||||
itemsTtoService: StoreOrderLineItem[];
|
||||
itemsOther: StoreOrderLineItem[];
|
||||
medusaOrderId: string;
|
||||
}) {
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<h4>
|
||||
<Trans
|
||||
i18nKey="analysis-results:orderTitle"
|
||||
values={{ orderNumber: analysisOrder.medusa_order_id }}
|
||||
values={{ orderNumber: medusaOrderId }}
|
||||
/>
|
||||
{` (${analysisOrder.id})`}
|
||||
</h4>
|
||||
<div className="flex gap-2">
|
||||
<h5>
|
||||
<Trans i18nKey={`orders:status.${analysisOrder.status}`} />
|
||||
</h5>
|
||||
<Link
|
||||
href={`/home/order/${analysisOrder.id}`}
|
||||
className="text-small-regular flex items-center justify-between"
|
||||
>
|
||||
<button className="text-ui-fg-subtle hover:text-ui-fg-base flex cursor-pointer gap-x-1">
|
||||
<Eye />
|
||||
</button>
|
||||
</Link>
|
||||
</div>
|
||||
{analysisOrder && (
|
||||
<div className="flex gap-2">
|
||||
<h5>
|
||||
<Trans i18nKey={`orders:status.${analysisOrder.status}`} />
|
||||
</h5>
|
||||
<Link
|
||||
href={`/home/order/${analysisOrder.id}`}
|
||||
className="text-small-regular flex items-center justify-between"
|
||||
>
|
||||
<button className="text-ui-fg-subtle hover:text-ui-fg-base flex cursor-pointer gap-x-1">
|
||||
<Eye />
|
||||
</button>
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex flex-col gap-4">
|
||||
<OrderItemsTable
|
||||
items={itemsAnalysisPackage}
|
||||
title="orders:table.analysisPackage"
|
||||
analysisOrder={analysisOrder}
|
||||
/>
|
||||
{analysisOrder && (
|
||||
<OrderItemsTable
|
||||
items={itemsAnalysisPackage}
|
||||
title="orders:table.analysisPackage"
|
||||
order={{
|
||||
medusaOrderId: analysisOrder.medusa_order_id,
|
||||
id: analysisOrder.id,
|
||||
status: analysisOrder.status,
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{itemsTtoService && (
|
||||
<OrderItemsTable
|
||||
items={itemsTtoService}
|
||||
title="orders:table.ttoService"
|
||||
type="ttoService"
|
||||
order={{
|
||||
status: medusaOrderStatus.toUpperCase(),
|
||||
medusaOrderId,
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
<OrderItemsTable
|
||||
items={itemsOther}
|
||||
title="orders:table.otherOrders"
|
||||
analysisOrder={analysisOrder}
|
||||
order={{
|
||||
status: analysisOrder?.status,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -4,7 +4,6 @@ import { useRouter } from 'next/navigation';
|
||||
|
||||
import { StoreOrderLineItem } from '@medusajs/types';
|
||||
import { formatDate } from 'date-fns';
|
||||
import { Eye } from 'lucide-react';
|
||||
|
||||
import { pathsConfig } from '@kit/shared/config';
|
||||
import { Button } from '@kit/ui/button';
|
||||
@@ -18,18 +17,22 @@ import {
|
||||
} from '@kit/ui/table';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import type { AnalysisOrder } from '~/lib/types/analysis-order';
|
||||
import type { Order } from '~/lib/types/order';
|
||||
|
||||
import { logAnalysisResultsNavigateAction } from './actions';
|
||||
|
||||
export type OrderItemType = 'analysisOrder' | 'ttoService';
|
||||
|
||||
export default function OrderItemsTable({
|
||||
items,
|
||||
title,
|
||||
analysisOrder,
|
||||
order,
|
||||
type = 'analysisOrder',
|
||||
}: {
|
||||
items: StoreOrderLineItem[];
|
||||
title: string;
|
||||
analysisOrder: AnalysisOrder;
|
||||
order: Order;
|
||||
type?: OrderItemType;
|
||||
}) {
|
||||
const router = useRouter();
|
||||
|
||||
@@ -37,9 +40,15 @@ export default function OrderItemsTable({
|
||||
return null;
|
||||
}
|
||||
|
||||
const openAnalysisResults = async () => {
|
||||
await logAnalysisResultsNavigateAction(analysisOrder.medusa_order_id);
|
||||
router.push(`${pathsConfig.app.analysisResults}/${analysisOrder.id}`);
|
||||
const isAnalysisOrder = type === 'analysisOrder';
|
||||
|
||||
const openDetailedView = async () => {
|
||||
if (isAnalysisOrder && order?.medusaOrderId && order?.id) {
|
||||
await logAnalysisResultsNavigateAction(order.medusaOrderId);
|
||||
router.push(`${pathsConfig.app.analysisResults}/${order.id}`);
|
||||
} else {
|
||||
router.push(`${pathsConfig.app.myOrders}/${order.medusaOrderId}`);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -55,7 +64,7 @@ export default function OrderItemsTable({
|
||||
<TableHead className="px-6">
|
||||
<Trans i18nKey="orders:table.status" />
|
||||
</TableHead>
|
||||
<TableHead className="px-6"></TableHead>
|
||||
{isAnalysisOrder && <TableHead className="px-6"></TableHead>}
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
@@ -76,11 +85,13 @@ export default function OrderItemsTable({
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="min-w-[180px] px-6">
|
||||
<Trans i18nKey={`orders:status.${analysisOrder.status}`} />
|
||||
<Trans
|
||||
i18nKey={`orders:status.${type}.${order?.status ?? 'CONFIRMED'}`}
|
||||
/>
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="px-6 text-right">
|
||||
<Button size="sm" onClick={openAnalysisResults}>
|
||||
<Button size="sm" onClick={openDetailedView}>
|
||||
<Trans i18nKey="analysis-results:view" />
|
||||
</Button>
|
||||
</TableCell>
|
||||
|
||||
@@ -4,17 +4,20 @@ import React from 'react';
|
||||
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
import { createPath, pathsConfig } from '@/packages/shared/src/config';
|
||||
import { pathsConfig } from '@/packages/shared/src/config';
|
||||
import { StoreProduct } from '@medusajs/types';
|
||||
import { ComponentInstanceIcon } from '@radix-ui/react-icons';
|
||||
|
||||
import { cn } from '@kit/ui/shadcn';
|
||||
import { Card, CardDescription, CardTitle } from '@kit/ui/shadcn/card';
|
||||
import { Card, CardDescription } from '@kit/ui/shadcn/card';
|
||||
|
||||
export interface ServiceCategory {
|
||||
name: string;
|
||||
handle: string;
|
||||
color: string;
|
||||
description: string;
|
||||
products: StoreProduct[];
|
||||
countryCode: string;
|
||||
}
|
||||
|
||||
const ServiceCategories = ({
|
||||
|
||||
43
app/home/(user)/_lib/server/actions.ts
Normal file
43
app/home/(user)/_lib/server/actions.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
'use server';
|
||||
|
||||
import { updateLineItem } from '@lib/data/cart';
|
||||
import { StoreProductVariant } from '@medusajs/types';
|
||||
|
||||
import { handleAddToCart } from '~/lib/services/medusaCart.service';
|
||||
import { createInitialReservation } from '~/lib/services/reservation.service';
|
||||
|
||||
export async function createInitialReservationAction(
|
||||
selectedVariant: Pick<StoreProductVariant, 'id'>,
|
||||
countryCode: string,
|
||||
serviceId: number,
|
||||
clinicId: number,
|
||||
appointmentUserId: number,
|
||||
syncUserId: number,
|
||||
startTime: Date,
|
||||
locationId: number | null,
|
||||
comments?: string,
|
||||
) {
|
||||
const { addedItem } = await handleAddToCart({
|
||||
selectedVariant,
|
||||
countryCode,
|
||||
});
|
||||
|
||||
if (addedItem) {
|
||||
const reservation = await createInitialReservation({
|
||||
serviceId,
|
||||
clinicId,
|
||||
appointmentUserId,
|
||||
syncUserID: syncUserId,
|
||||
startTime,
|
||||
medusaLineItemId: addedItem.id,
|
||||
locationId,
|
||||
comments,
|
||||
});
|
||||
|
||||
await updateLineItem({
|
||||
lineId: addedItem.id,
|
||||
quantity: addedItem.quantity,
|
||||
metadata: { connectedOnlineReservationId: reservation.id },
|
||||
});
|
||||
}
|
||||
}
|
||||
13
app/home/(user)/_lib/server/balance-actions.ts
Normal file
13
app/home/(user)/_lib/server/balance-actions.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
'use server';
|
||||
|
||||
import { AccountBalanceService, AccountBalanceSummary } from '@kit/accounts/services/account-balance.service';
|
||||
|
||||
export async function getAccountBalanceSummary(accountId: string): Promise<AccountBalanceSummary | null> {
|
||||
try {
|
||||
const service = new AccountBalanceService();
|
||||
return await service.getBalanceSummary(accountId);
|
||||
} catch (error) {
|
||||
console.error('Error getting account balance summary:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
347
app/home/(user)/_lib/server/cart-actions.ts
Normal file
347
app/home/(user)/_lib/server/cart-actions.ts
Normal file
@@ -0,0 +1,347 @@
|
||||
'use server';
|
||||
|
||||
import { z } from 'zod';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
import type { StoreCart, StoreOrder } from "@medusajs/types";
|
||||
|
||||
import { initiateMultiPaymentSession, placeOrder } from "@lib/data/cart";
|
||||
import type { AccountBalanceSummary } from "@kit/accounts/services/account-balance.service";
|
||||
import { handleNavigateToPayment } from "~/lib/services/medusaCart.service";
|
||||
import { loadCurrentUserAccount } from "./load-user-account";
|
||||
import { getOrderedAnalysisIds } from "~/lib/services/medusaOrder.service";
|
||||
import { createAnalysisOrder, getAnalysisOrder } from "~/lib/services/order.service";
|
||||
import { listProductTypes } from "@lib/data";
|
||||
import { sendOrderToMedipost } from "~/lib/services/medipost/medipostPrivateMessage.service";
|
||||
import { AccountWithParams } from "@/packages/features/accounts/src/types/accounts";
|
||||
import { createI18nServerInstance } from "~/lib/i18n/i18n.server";
|
||||
import { getSupabaseServerAdminClient } from "@/packages/supabase/src/clients/server-admin-client";
|
||||
import { createNotificationsApi } from "@/packages/features/notifications/src/server/api";
|
||||
import { FailureReason } from '~/lib/types/connected-online';
|
||||
import { getOrderedTtoServices } from '~/lib/services/reservation.service';
|
||||
import { bookAppointment } from '~/lib/services/connected-online.service';
|
||||
|
||||
const ANALYSIS_PACKAGES_TYPE_HANDLE = 'analysis-packages';
|
||||
const ANALYSIS_TYPE_HANDLE = 'synlab-analysis';
|
||||
|
||||
const env = () =>
|
||||
z
|
||||
.object({
|
||||
emailSender: z
|
||||
.string({
|
||||
error: 'EMAIL_SENDER is required',
|
||||
})
|
||||
.min(1),
|
||||
siteUrl: z
|
||||
.string({
|
||||
error: 'NEXT_PUBLIC_SITE_URL is required',
|
||||
})
|
||||
.min(1),
|
||||
isEnabledDispatchOnMontonioCallback: z.boolean({
|
||||
error: 'MEDIPOST_ENABLE_DISPATCH_ON_MONTONIO_CALLBACK is required',
|
||||
}),
|
||||
medusaBackendPublicUrl: z.string({
|
||||
error: 'MEDUSA_BACKEND_PUBLIC_URL is required',
|
||||
}).min(1),
|
||||
companyBenefitsPaymentSecretKey: z.string({
|
||||
error: 'COMPANY_BENEFITS_PAYMENT_SECRET_KEY is required',
|
||||
}).min(1),
|
||||
})
|
||||
.parse({
|
||||
emailSender: process.env.EMAIL_SENDER,
|
||||
siteUrl: process.env.NEXT_PUBLIC_SITE_URL!,
|
||||
isEnabledDispatchOnMontonioCallback:
|
||||
process.env.MEDIPOST_ENABLE_DISPATCH_ON_MONTONIO_CALLBACK === 'true',
|
||||
medusaBackendPublicUrl: process.env.MEDUSA_BACKEND_PUBLIC_URL!,
|
||||
companyBenefitsPaymentSecretKey: process.env.COMPANY_BENEFITS_PAYMENT_SECRET_KEY!,
|
||||
});
|
||||
|
||||
export const initiatePayment = async ({
|
||||
accountId,
|
||||
balanceSummary,
|
||||
cart,
|
||||
language,
|
||||
}: {
|
||||
accountId: string;
|
||||
balanceSummary: AccountBalanceSummary;
|
||||
cart: StoreCart;
|
||||
language: string;
|
||||
}) => {
|
||||
try {
|
||||
const {
|
||||
montonioPaymentSessionId,
|
||||
companyBenefitsPaymentSessionId,
|
||||
totalByMontonio,
|
||||
totalByBenefits,
|
||||
isFullyPaidByBenefits,
|
||||
} = await initiateMultiPaymentSession(cart, balanceSummary.totalBalance);
|
||||
|
||||
if (!isFullyPaidByBenefits) {
|
||||
if (!montonioPaymentSessionId) {
|
||||
throw new Error('Montonio payment session ID is missing');
|
||||
}
|
||||
const props = await handleNavigateToPayment({
|
||||
language,
|
||||
paymentSessionId: montonioPaymentSessionId,
|
||||
amount: totalByMontonio,
|
||||
currencyCode: cart.currency_code,
|
||||
cartId: cart.id,
|
||||
});
|
||||
return { ...props, isFullyPaidByBenefits };
|
||||
} else {
|
||||
// place order if all paid already
|
||||
const { orderId } = await handlePlaceOrder({ cart });
|
||||
|
||||
const companyBenefitsOrderToken = jwt.sign({
|
||||
accountId,
|
||||
companyBenefitsPaymentSessionId,
|
||||
orderId,
|
||||
totalByBenefits,
|
||||
}, env().companyBenefitsPaymentSecretKey, {
|
||||
algorithm: 'HS256',
|
||||
});
|
||||
const webhookResponse = await fetch(`${env().medusaBackendPublicUrl}/hooks/payment/company-benefits_company-benefits`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
orderToken: companyBenefitsOrderToken,
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
if (!webhookResponse.ok) {
|
||||
throw new Error('Failed to send company benefits webhook');
|
||||
}
|
||||
return { isFullyPaidByBenefits, orderId, unavailableLineItemIds: [] };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error initiating payment', error);
|
||||
}
|
||||
|
||||
return { url: null, isFullyPaidByBenefits: false, orderId: null, unavailableLineItemIds: [] };
|
||||
}
|
||||
|
||||
export async function handlePlaceOrder({
|
||||
cart,
|
||||
}: {
|
||||
cart: StoreCart;
|
||||
}) {
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found in context');
|
||||
}
|
||||
|
||||
try {
|
||||
const medusaOrder = await placeOrder(cart.id, {
|
||||
revalidateCacheTags: false,
|
||||
});
|
||||
const orderedAnalysisElements = await getOrderedAnalysisIds({
|
||||
medusaOrder,
|
||||
});
|
||||
|
||||
const orderContainsSynlabItems = !!orderedAnalysisElements?.length;
|
||||
|
||||
try {
|
||||
const existingAnalysisOrder = await getAnalysisOrder({
|
||||
medusaOrderId: medusaOrder.id,
|
||||
});
|
||||
console.info(
|
||||
`Analysis order already exists for medusaOrderId=${medusaOrder.id}, orderId=${existingAnalysisOrder.id}`,
|
||||
);
|
||||
return { success: true, orderId: existingAnalysisOrder.id };
|
||||
} catch {
|
||||
// ignored
|
||||
}
|
||||
|
||||
let orderId: number | undefined = undefined;
|
||||
if (orderContainsSynlabItems) {
|
||||
orderId = await createAnalysisOrder({
|
||||
medusaOrder,
|
||||
orderedAnalysisElements,
|
||||
});
|
||||
}
|
||||
|
||||
const orderResult = await getOrderResultParameters(medusaOrder);
|
||||
|
||||
const { medusaOrderId, email, analysisPackageOrder, analysisItemsOrder } =
|
||||
orderResult;
|
||||
|
||||
const orderedTtoServices = await getOrderedTtoServices({ medusaOrder });
|
||||
let bookServiceResults: {
|
||||
success: boolean;
|
||||
reason?: FailureReason;
|
||||
serviceId?: number;
|
||||
}[] = [];
|
||||
if (orderedTtoServices?.length) {
|
||||
const bookingPromises = orderedTtoServices.map((service) =>
|
||||
bookAppointment(
|
||||
service.service_id,
|
||||
service.clinic_id,
|
||||
service.service_user_id,
|
||||
service.sync_user_id,
|
||||
service.start_time,
|
||||
),
|
||||
);
|
||||
bookServiceResults = await Promise.all(bookingPromises);
|
||||
}
|
||||
|
||||
if (email) {
|
||||
if (analysisPackageOrder) {
|
||||
await sendAnalysisPackageOrderEmail({
|
||||
account,
|
||||
email,
|
||||
analysisPackageOrder,
|
||||
});
|
||||
} else {
|
||||
console.info(`Order has no analysis package, skipping email.`);
|
||||
}
|
||||
|
||||
if (analysisItemsOrder) {
|
||||
// @TODO send email for separate analyses
|
||||
console.warn(
|
||||
`Order has analysis items, but no email template exists yet`,
|
||||
);
|
||||
} else {
|
||||
console.info(`Order has no analysis items, skipping email.`);
|
||||
}
|
||||
} else {
|
||||
console.error('Missing email to send order result email', orderResult);
|
||||
}
|
||||
|
||||
if (env().isEnabledDispatchOnMontonioCallback) {
|
||||
await sendOrderToMedipost({ medusaOrderId, orderedAnalysisElements });
|
||||
}
|
||||
|
||||
if (bookServiceResults.some(({ success }) => success === false)) {
|
||||
const failedServiceBookings = bookServiceResults.filter(
|
||||
({ success }) => success === false,
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
failedServiceBookings,
|
||||
orderId,
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true, orderId };
|
||||
} catch (error) {
|
||||
console.error('Failed to place order', error);
|
||||
throw new Error(`Failed to place order, message=${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function sendAnalysisPackageOrderEmail({
|
||||
account,
|
||||
email,
|
||||
analysisPackageOrder,
|
||||
}: {
|
||||
account: AccountWithParams;
|
||||
email: string;
|
||||
analysisPackageOrder: {
|
||||
partnerLocationName: string;
|
||||
analysisPackageName: string;
|
||||
};
|
||||
}) {
|
||||
const { language } = await createI18nServerInstance();
|
||||
const { analysisPackageName, partnerLocationName } = analysisPackageOrder;
|
||||
try {
|
||||
await sendEmail({
|
||||
account: { id: account.id, name: account.name },
|
||||
email,
|
||||
analysisPackageName,
|
||||
partnerLocationName,
|
||||
language,
|
||||
});
|
||||
console.info(`Successfully sent analysis package order email to ${email}`);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed to send analysis package order email to ${email}`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function getOrderResultParameters(medusaOrder: StoreOrder) {
|
||||
const { productTypes } = await listProductTypes();
|
||||
const analysisPackagesType = productTypes.find(
|
||||
({ metadata }) => metadata?.handle === ANALYSIS_PACKAGES_TYPE_HANDLE,
|
||||
);
|
||||
const analysisType = productTypes.find(
|
||||
({ metadata }) => metadata?.handle === ANALYSIS_TYPE_HANDLE,
|
||||
);
|
||||
|
||||
const analysisPackageOrderItem = medusaOrder.items?.find(
|
||||
({ product_type_id }) => product_type_id === analysisPackagesType?.id,
|
||||
);
|
||||
const analysisItems = medusaOrder.items?.filter(
|
||||
({ product_type_id }) => product_type_id === analysisType?.id,
|
||||
);
|
||||
|
||||
return {
|
||||
medusaOrderId: medusaOrder.id,
|
||||
email: medusaOrder.email,
|
||||
analysisPackageOrder: analysisPackageOrderItem
|
||||
? {
|
||||
partnerLocationName:
|
||||
(analysisPackageOrderItem?.metadata
|
||||
?.partner_location_name as string) ?? '',
|
||||
analysisPackageName: analysisPackageOrderItem?.title ?? '',
|
||||
}
|
||||
: null,
|
||||
analysisItemsOrder:
|
||||
Array.isArray(analysisItems) && analysisItems.length > 0
|
||||
? analysisItems.map(({ product }) => ({
|
||||
analysisName: product?.title ?? '',
|
||||
analysisId: (product?.metadata?.analysisIdOriginal as string) ?? '',
|
||||
}))
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
const sendEmail = async ({
|
||||
account,
|
||||
email,
|
||||
analysisPackageName,
|
||||
partnerLocationName,
|
||||
language,
|
||||
}: {
|
||||
account: Pick<AccountWithParams, 'name' | 'id'>;
|
||||
email: string;
|
||||
analysisPackageName: string;
|
||||
partnerLocationName: string;
|
||||
language: string;
|
||||
}) => {
|
||||
const client = getSupabaseServerAdminClient();
|
||||
try {
|
||||
const { renderSynlabAnalysisPackageEmail } = await import(
|
||||
'@kit/email-templates'
|
||||
);
|
||||
const { getMailer } = await import('@kit/mailers');
|
||||
|
||||
const mailer = await getMailer();
|
||||
|
||||
const { html, subject } = await renderSynlabAnalysisPackageEmail({
|
||||
analysisPackageName,
|
||||
personName: account.name,
|
||||
partnerLocationName,
|
||||
language,
|
||||
});
|
||||
|
||||
await mailer
|
||||
.sendEmail({
|
||||
from: env().emailSender,
|
||||
to: email,
|
||||
subject,
|
||||
html,
|
||||
})
|
||||
.catch((error) => {
|
||||
throw new Error(`Failed to send email, message=${error}`);
|
||||
});
|
||||
await createNotificationsApi(client).createNotification({
|
||||
account_id: account.id,
|
||||
body: html,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to send email, message=${error}`);
|
||||
}
|
||||
};
|
||||
12
app/home/(user)/_lib/server/is-valid-open-ai-env.ts
Normal file
12
app/home/(user)/_lib/server/is-valid-open-ai-env.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import OpenAI from 'openai';
|
||||
|
||||
export const isValidOpenAiEnv = async () => {
|
||||
try {
|
||||
const client = new OpenAI();
|
||||
await client.models.list();
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.log('No openAI env');
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -45,10 +45,6 @@ async function analysesLoader() {
|
||||
})
|
||||
: null;
|
||||
|
||||
const serviceCategories = productCategories.filter(
|
||||
({ parent_category }) => parent_category?.handle === 'tto-categories',
|
||||
);
|
||||
|
||||
return {
|
||||
analyses:
|
||||
categoryProducts?.response.products
|
||||
|
||||
@@ -1,20 +1,30 @@
|
||||
import { cache } from 'react';
|
||||
|
||||
import { getProductCategories } from '@lib/data';
|
||||
import { getProductCategories, listProducts } from '@lib/data';
|
||||
|
||||
import { ServiceCategory } from '../../_components/service-categories';
|
||||
|
||||
async function categoryLoader({
|
||||
handle,
|
||||
}: {
|
||||
handle: string;
|
||||
}): Promise<{ category: ServiceCategory | null }> {
|
||||
const response = await getProductCategories({
|
||||
handle,
|
||||
fields: '*products, is_active, metadata',
|
||||
});
|
||||
import { loadCountryCodes } from './load-analyses';
|
||||
|
||||
async function categoryLoader({ handle }: { handle: string }) {
|
||||
const [response, countryCodes] = await Promise.all([
|
||||
getProductCategories({
|
||||
handle,
|
||||
limit: 1,
|
||||
}),
|
||||
loadCountryCodes(),
|
||||
]);
|
||||
const category = response.product_categories[0];
|
||||
const countryCode = countryCodes[0]!;
|
||||
|
||||
if (!response.product_categories?.[0]?.id) {
|
||||
return { category: null };
|
||||
}
|
||||
|
||||
const {
|
||||
response: { products: categoryProducts },
|
||||
} = await listProducts({
|
||||
countryCode,
|
||||
queryParams: { limit: 100, category_id: response.product_categories[0].id },
|
||||
});
|
||||
|
||||
return {
|
||||
category: {
|
||||
@@ -25,6 +35,8 @@ async function categoryLoader({
|
||||
description: category?.description || '',
|
||||
handle: category?.handle || '',
|
||||
name: category?.name || '',
|
||||
countryCode,
|
||||
products: categoryProducts,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,38 +10,36 @@ async function ttoServicesLoader() {
|
||||
});
|
||||
|
||||
const heroCategories = response.product_categories?.filter(
|
||||
({ parent_category, is_active, metadata }) =>
|
||||
parent_category?.handle === 'tto-categories' &&
|
||||
is_active &&
|
||||
metadata?.isHero,
|
||||
({ parent_category, metadata }) =>
|
||||
parent_category?.handle === 'tto-categories' && metadata?.isHero,
|
||||
);
|
||||
|
||||
const ttoCategories = response.product_categories?.filter(
|
||||
({ parent_category, is_active, metadata }) =>
|
||||
parent_category?.handle === 'tto-categories' &&
|
||||
is_active &&
|
||||
!metadata?.isHero,
|
||||
({ parent_category, metadata }) =>
|
||||
parent_category?.handle === 'tto-categories' && !metadata?.isHero,
|
||||
);
|
||||
|
||||
return {
|
||||
heroCategories:
|
||||
heroCategories.map<ServiceCategory>(
|
||||
({ name, handle, metadata, description }) => ({
|
||||
heroCategories.map<Omit<ServiceCategory, 'countryCode'>>(
|
||||
({ name, handle, metadata, description, products }) => ({
|
||||
name,
|
||||
handle,
|
||||
color:
|
||||
typeof metadata?.color === 'string' ? metadata.color : 'primary',
|
||||
description,
|
||||
products: products ?? [],
|
||||
}),
|
||||
) ?? [],
|
||||
ttoCategories:
|
||||
ttoCategories.map<ServiceCategory>(
|
||||
({ name, handle, metadata, description }) => ({
|
||||
ttoCategories.map<Omit<ServiceCategory, 'countryCode'>>(
|
||||
({ name, handle, metadata, description, products }) => ({
|
||||
name,
|
||||
handle,
|
||||
color:
|
||||
typeof metadata?.color === 'string' ? metadata.color : 'primary',
|
||||
description,
|
||||
products: products ?? [],
|
||||
}),
|
||||
) ?? [],
|
||||
};
|
||||
|
||||
@@ -1,23 +1,14 @@
|
||||
import React from 'react';
|
||||
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
import { formatCurrency } from '@/packages/shared/src/utils';
|
||||
import { Database } from '@/packages/supabase/src/database.types';
|
||||
import { PiggyBankIcon, Settings } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import { createPath, pathsConfig } from '@kit/shared/config';
|
||||
import { Card, CardTitle } from '@kit/ui/card';
|
||||
import { cn } from '@kit/ui/lib/utils';
|
||||
import { Button } from '@kit/ui/shadcn/button';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
interface TeamAccountBenefitStatisticsProps {
|
||||
employeeCount: number;
|
||||
accountSlug: string;
|
||||
companyParams: Database['medreport']['Tables']['company_params']['Row'];
|
||||
}
|
||||
import { TeamAccountBenefitExpensesOverview } from '../_lib/server/load-team-account-benefit-expenses-overview';
|
||||
import { AccountBenefitStatistics } from '../_lib/server/load-team-account-benefit-statistics';
|
||||
|
||||
const StatisticsCard = ({ children }: { children: React.ReactNode }) => {
|
||||
return <Card className="p-4">{children}</Card>;
|
||||
@@ -46,126 +37,90 @@ const StatisticsValue = ({ children }: { children: React.ReactNode }) => {
|
||||
};
|
||||
|
||||
const TeamAccountBenefitStatistics = ({
|
||||
employeeCount,
|
||||
accountSlug,
|
||||
companyParams,
|
||||
}: TeamAccountBenefitStatisticsProps) => {
|
||||
accountBenefitStatistics,
|
||||
expensesOverview,
|
||||
}: {
|
||||
accountBenefitStatistics: AccountBenefitStatistics;
|
||||
expensesOverview: TeamAccountBenefitExpensesOverview;
|
||||
}) => {
|
||||
const {
|
||||
i18n: { language },
|
||||
} = useTranslation();
|
||||
|
||||
return (
|
||||
<div className="flex h-full w-full flex-col gap-2 sm:flex-row">
|
||||
<Card className="relative flex flex-row">
|
||||
<div className="p-6">
|
||||
<Button
|
||||
onClick={() =>
|
||||
redirect(createPath(pathsConfig.app.accountBilling, accountSlug))
|
||||
}
|
||||
variant="outline"
|
||||
className="absolute top-1 right-1 p-3"
|
||||
>
|
||||
<Settings />
|
||||
</Button>
|
||||
<div className="flex h-16 w-16 items-center justify-center rounded-full bg-orange-100">
|
||||
<PiggyBankIcon className="h-[32px] w-[32px] stroke-orange-400 stroke-2" />
|
||||
</div>
|
||||
<p className="mt-4 text-sm font-medium">
|
||||
<Trans i18nKey="teams:benefitStatistics.budget.title" />
|
||||
</p>
|
||||
<h3 className="text-2xl">
|
||||
<Trans
|
||||
i18nKey="teams:benefitStatistics.budget.balance"
|
||||
values={{
|
||||
balance: formatCurrency({
|
||||
value: 11800,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
}),
|
||||
}}
|
||||
/>
|
||||
</h3>
|
||||
<StatisticsDescription>
|
||||
<Trans
|
||||
i18nKey="teams:benefitStatistics.budget.volume"
|
||||
values={{
|
||||
volume: formatCurrency({
|
||||
value:
|
||||
(Number(companyParams.benefit_amount) || 0) * employeeCount,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
}),
|
||||
}}
|
||||
/>
|
||||
</StatisticsDescription>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
<div className="grid flex-2 grid-cols-2 gap-2 sm:grid-cols-3 sm:grid-rows-2">
|
||||
<StatisticsCard>
|
||||
<StatisticsCardTitle className="text-lg font-bold">
|
||||
<Trans i18nKey="teams:benefitStatistics.data.serviceSum" />
|
||||
<Trans i18nKey="teams:benefitStatistics.budget.membersCount" />
|
||||
</StatisticsCardTitle>
|
||||
<StatisticsValue>1800 €</StatisticsValue>
|
||||
<StatisticsValue>
|
||||
{accountBenefitStatistics.companyAccountsCount}
|
||||
</StatisticsValue>
|
||||
</StatisticsCard>
|
||||
|
||||
<StatisticsCard>
|
||||
<StatisticsCardTitle className="text-lg font-bold">
|
||||
<Trans i18nKey="teams:benefitStatistics.data.totalSum" />
|
||||
</StatisticsCardTitle>
|
||||
<StatisticsValue>
|
||||
{formatCurrency({
|
||||
value: accountBenefitStatistics.orders.totalSum,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
})}
|
||||
</StatisticsValue>
|
||||
</StatisticsCard>
|
||||
|
||||
<StatisticsCard>
|
||||
<StatisticsCardTitle className="text-lg font-bold">
|
||||
<Trans i18nKey="teams:benefitStatistics.data.currentMonthUsageTotal" />
|
||||
</StatisticsCardTitle>
|
||||
<StatisticsValue>
|
||||
{formatCurrency({
|
||||
value: expensesOverview.currentMonthUsageTotal,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
})}
|
||||
</StatisticsValue>
|
||||
</StatisticsCard>
|
||||
|
||||
<StatisticsCard>
|
||||
<StatisticsCardTitle>
|
||||
<Trans i18nKey="teams:benefitStatistics.data.analysis" />
|
||||
</StatisticsCardTitle>
|
||||
<StatisticsValue>200 €</StatisticsValue>
|
||||
<StatisticsValue>
|
||||
{formatCurrency({
|
||||
value: accountBenefitStatistics.orders.analysesSum,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
})}
|
||||
</StatisticsValue>
|
||||
<StatisticsDescription>
|
||||
<Trans
|
||||
i18nKey="teams:benefitStatistics.data.reservations"
|
||||
values={{ value: 36 }}
|
||||
/>
|
||||
</StatisticsDescription>
|
||||
</StatisticsCard>
|
||||
<StatisticsCard>
|
||||
<StatisticsCardTitle>
|
||||
<Trans i18nKey="teams:benefitStatistics.data.doctorsAndSpecialists" />
|
||||
</StatisticsCardTitle>
|
||||
<StatisticsValue>200 €</StatisticsValue>
|
||||
<StatisticsDescription>
|
||||
<Trans
|
||||
i18nKey="teams:benefitStatistics.data.reservations"
|
||||
values={{ value: 44 }}
|
||||
/>
|
||||
</StatisticsDescription>
|
||||
</StatisticsCard>
|
||||
<StatisticsCard>
|
||||
<StatisticsCardTitle>
|
||||
<Trans i18nKey="teams:benefitStatistics.data.researches" />
|
||||
</StatisticsCardTitle>
|
||||
<StatisticsValue>200 €</StatisticsValue>
|
||||
<StatisticsDescription>
|
||||
<Trans
|
||||
i18nKey="teams:benefitStatistics.data.reservations"
|
||||
values={{ value: 40 }}
|
||||
/>
|
||||
</StatisticsDescription>
|
||||
</StatisticsCard>
|
||||
<StatisticsCard>
|
||||
<StatisticsCardTitle>
|
||||
<Trans i18nKey="teams:benefitStatistics.data.eclinic" />
|
||||
</StatisticsCardTitle>
|
||||
<StatisticsValue>200 €</StatisticsValue>
|
||||
<StatisticsDescription>
|
||||
<Trans
|
||||
i18nKey="teams:benefitStatistics.data.reservations"
|
||||
values={{ value: 34 }}
|
||||
values={{ value: accountBenefitStatistics.orders.analysesCount }}
|
||||
/>
|
||||
</StatisticsDescription>
|
||||
</StatisticsCard>
|
||||
|
||||
<StatisticsCard>
|
||||
<StatisticsCardTitle>
|
||||
<Trans i18nKey="teams:benefitStatistics.data.healthResearchPlans" />
|
||||
<Trans i18nKey="teams:benefitStatistics.data.analysisPackages" />
|
||||
</StatisticsCardTitle>
|
||||
<StatisticsValue>200 €</StatisticsValue>
|
||||
<StatisticsValue>
|
||||
{formatCurrency({
|
||||
value: accountBenefitStatistics.orders.analysisPackagesSum,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
})}
|
||||
</StatisticsValue>
|
||||
<StatisticsDescription>
|
||||
<Trans
|
||||
i18nKey="teams:benefitStatistics.data.serviceUsage"
|
||||
values={{ value: 46 }}
|
||||
i18nKey="teams:benefitStatistics.data.analysisPackagesCount"
|
||||
values={{
|
||||
value: accountBenefitStatistics.orders.analysisPackagesCount,
|
||||
}}
|
||||
/>
|
||||
</StatisticsDescription>
|
||||
</StatisticsCard>
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Database } from '@/packages/supabase/src/database.types';
|
||||
import { Card } from '@kit/ui/card';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
import { cn } from '@kit/ui/utils';
|
||||
import type { BmiThresholds } from '@kit/accounts/types/accounts';
|
||||
|
||||
import { getAccountHealthDetailsFields } from '../_lib/server/load-team-account-health-details';
|
||||
import { TeamAccountStatisticsProps } from './team-account-statistics';
|
||||
@@ -15,10 +16,7 @@ const TeamAccountHealthDetails = ({
|
||||
members,
|
||||
}: {
|
||||
memberParams: TeamAccountStatisticsProps['memberParams'];
|
||||
bmiThresholds: Omit<
|
||||
Database['medreport']['Tables']['bmi_thresholds']['Row'],
|
||||
'id'
|
||||
>[];
|
||||
bmiThresholds: Omit<BmiThresholds, 'id'>[];
|
||||
members: Database['medreport']['Functions']['get_account_members']['Returns'];
|
||||
}) => {
|
||||
const accountHealthDetailsFields = getAccountHealthDetailsFields(
|
||||
|
||||
@@ -52,7 +52,6 @@ function SidebarContainer(props: {
|
||||
<SidebarContent>
|
||||
<SidebarNavigation config={config} />
|
||||
</SidebarContent>
|
||||
|
||||
</Sidebar>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,28 +14,21 @@ import { createPath, pathsConfig } from '@kit/shared/config';
|
||||
import { Card } from '@kit/ui/card';
|
||||
import { Trans } from '@kit/ui/makerkit/trans';
|
||||
import { Button } from '@kit/ui/shadcn/button';
|
||||
import { Calendar, DateRange } from '@kit/ui/shadcn/calendar';
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from '@kit/ui/shadcn/popover';
|
||||
import { DateRange } from '@kit/ui/shadcn/calendar';
|
||||
|
||||
import { AccountBenefitStatistics } from '../_lib/server/load-team-account-benefit-statistics';
|
||||
import TeamAccountBenefitStatistics from './team-account-benefit-statistics';
|
||||
import TeamAccountHealthDetails from './team-account-health-details';
|
||||
import type { Account, AccountParams, BmiThresholds } from '@/packages/features/accounts/src/types/accounts';
|
||||
import { TeamAccountBenefitExpensesOverview } from '../_lib/server/load-team-account-benefit-expenses-overview';
|
||||
|
||||
export interface TeamAccountStatisticsProps {
|
||||
teamAccount: Database['medreport']['Tables']['accounts']['Row'];
|
||||
memberParams: Pick<
|
||||
Database['medreport']['Tables']['account_params']['Row'],
|
||||
'weight' | 'height'
|
||||
>[];
|
||||
bmiThresholds: Omit<
|
||||
Database['medreport']['Tables']['bmi_thresholds']['Row'],
|
||||
'id'
|
||||
>[];
|
||||
teamAccount: Account;
|
||||
memberParams: Pick<AccountParams, 'weight' | 'height'>[];
|
||||
bmiThresholds: Omit<BmiThresholds, 'id'>[];
|
||||
members: Database['medreport']['Functions']['get_account_members']['Returns'];
|
||||
companyParams: Database['medreport']['Tables']['company_params']['Row'];
|
||||
accountBenefitStatistics: AccountBenefitStatistics;
|
||||
expensesOverview: TeamAccountBenefitExpensesOverview;
|
||||
}
|
||||
|
||||
export default function TeamAccountStatistics({
|
||||
@@ -43,11 +36,13 @@ export default function TeamAccountStatistics({
|
||||
memberParams,
|
||||
bmiThresholds,
|
||||
members,
|
||||
companyParams,
|
||||
accountBenefitStatistics,
|
||||
expensesOverview,
|
||||
}: TeamAccountStatisticsProps) {
|
||||
const currentDate = new Date();
|
||||
const [date, setDate] = useState<DateRange | undefined>({
|
||||
from: new Date(),
|
||||
to: new Date(),
|
||||
from: new Date(currentDate.getFullYear(), currentDate.getMonth(), 1),
|
||||
to: new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0),
|
||||
});
|
||||
const {
|
||||
i18n: { language },
|
||||
@@ -58,7 +53,7 @@ export default function TeamAccountStatistics({
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="mt-4 flex items-center justify-between">
|
||||
<div className="mt-4 flex flex-col gap-4 sm:gap-0 sm:flex-row items-center justify-between">
|
||||
<h4 className="font-bold">
|
||||
<Trans
|
||||
i18nKey={'teams:home.headerTitle'}
|
||||
@@ -66,28 +61,16 @@ export default function TeamAccountStatistics({
|
||||
/>
|
||||
</h4>
|
||||
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button variant="outline" data-empty={!date}>
|
||||
<CalendarIcon />
|
||||
{date?.from && date?.to ? (
|
||||
`${format(date.from, 'd MMMM yyyy', dateFormatOptions)} - ${format(date.to, 'd MMMM yyyy', dateFormatOptions)}`
|
||||
) : (
|
||||
<span>
|
||||
<Trans i18nKey="common:formField.selectDate" />
|
||||
</span>
|
||||
)}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent className="w-auto p-0">
|
||||
<Calendar
|
||||
mode="range"
|
||||
selected={date}
|
||||
onSelect={setDate}
|
||||
locale={language === 'et' ? et : enGB}
|
||||
/>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
<Button variant="outline" data-empty={!date}>
|
||||
<CalendarIcon />
|
||||
{date?.from && date?.to ? (
|
||||
`${format(date.from, 'd MMMM yyyy', dateFormatOptions)} - ${format(date.to, 'd MMMM yyyy', dateFormatOptions)}`
|
||||
) : (
|
||||
<span>
|
||||
<Trans i18nKey="common:formField.selectDate" />
|
||||
</span>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div
|
||||
@@ -95,11 +78,7 @@ export default function TeamAccountStatistics({
|
||||
'animate-in fade-in flex flex-col space-y-4 pb-36 duration-500'
|
||||
}
|
||||
>
|
||||
<TeamAccountBenefitStatistics
|
||||
employeeCount={members.length}
|
||||
accountSlug={teamAccount.slug || ''}
|
||||
companyParams={companyParams}
|
||||
/>
|
||||
<TeamAccountBenefitStatistics accountBenefitStatistics={accountBenefitStatistics} expensesOverview={expensesOverview} />
|
||||
|
||||
<h5 className="mt-4 mb-2">
|
||||
<Trans i18nKey="teams:home.healthDetails" />
|
||||
@@ -148,7 +127,7 @@ export default function TeamAccountStatistics({
|
||||
redirect(
|
||||
createPath(
|
||||
pathsConfig.app.accountBilling,
|
||||
teamAccount.slug || '',
|
||||
teamAccount.slug!,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { getSupabaseServerClient } from "@/packages/supabase/src/clients/server-client";
|
||||
import { loadCompanyPersonalAccountsBalanceEntries } from "./load-team-account-benefit-statistics";
|
||||
|
||||
export interface TeamAccountBenefitExpensesOverview {
|
||||
benefitAmount: number | null;
|
||||
benefitOccurrence: 'yearly' | 'monthly' | 'quarterly' | null;
|
||||
currentMonthUsageTotal: number;
|
||||
managementFee: number;
|
||||
managementFeeTotal: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
const MANAGEMENT_FEE = 5.50;
|
||||
|
||||
const MONTHS = 12;
|
||||
const QUARTERS = 4;
|
||||
|
||||
export async function loadTeamAccountBenefitExpensesOverview({
|
||||
companyId,
|
||||
employeeCount,
|
||||
}: {
|
||||
companyId: string;
|
||||
employeeCount: number;
|
||||
}): Promise<TeamAccountBenefitExpensesOverview> {
|
||||
const supabase = getSupabaseServerClient();
|
||||
const { data, error } = await supabase
|
||||
.schema('medreport')
|
||||
.from('benefit_distribution_schedule')
|
||||
.select('*')
|
||||
.eq('company_id', companyId)
|
||||
.eq('is_active', true)
|
||||
.single();
|
||||
|
||||
let benefitAmount: TeamAccountBenefitExpensesOverview['benefitAmount'] = null;
|
||||
let benefitOccurrence: TeamAccountBenefitExpensesOverview['benefitOccurrence'] = null;
|
||||
if (error) {
|
||||
console.warn('Failed to load team account benefit expenses overview');
|
||||
} else {
|
||||
benefitAmount = data.benefit_amount as TeamAccountBenefitExpensesOverview['benefitAmount'];
|
||||
benefitOccurrence = data.benefit_occurrence as TeamAccountBenefitExpensesOverview['benefitOccurrence'];
|
||||
}
|
||||
|
||||
const { purchaseEntriesTotal } = await loadCompanyPersonalAccountsBalanceEntries({ accountId: companyId });
|
||||
|
||||
return {
|
||||
benefitAmount,
|
||||
benefitOccurrence,
|
||||
currentMonthUsageTotal: purchaseEntriesTotal,
|
||||
managementFee: MANAGEMENT_FEE,
|
||||
managementFeeTotal: MANAGEMENT_FEE * employeeCount,
|
||||
total: (() => {
|
||||
if (typeof benefitAmount !== 'number') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const currentDate = new Date();
|
||||
const createdAt = new Date(data.created_at);
|
||||
const isCreatedThisYear = createdAt.getFullYear() === currentDate.getFullYear();
|
||||
if (benefitOccurrence === 'yearly') {
|
||||
return benefitAmount * employeeCount;
|
||||
} else if (benefitOccurrence === 'monthly') {
|
||||
const monthsLeft = isCreatedThisYear
|
||||
? MONTHS - createdAt.getMonth()
|
||||
: MONTHS;
|
||||
return benefitAmount * employeeCount * monthsLeft;
|
||||
} else if (benefitOccurrence === 'quarterly') {
|
||||
const quartersLeft = isCreatedThisYear
|
||||
? QUARTERS - Math.ceil(createdAt.getMonth() / 3)
|
||||
: QUARTERS;
|
||||
return benefitAmount * employeeCount * quartersLeft;
|
||||
}
|
||||
return 0;
|
||||
})(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
'use server';
|
||||
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
|
||||
export interface AccountBenefitStatistics {
|
||||
benefitDistributionSchedule: {
|
||||
amount: number;
|
||||
};
|
||||
companyAccountsCount: number;
|
||||
periodTotal: number;
|
||||
orders: {
|
||||
totalSum: number;
|
||||
|
||||
analysesCount: number;
|
||||
analysesSum: number;
|
||||
|
||||
analysisPackagesCount: number;
|
||||
analysisPackagesSum: number;
|
||||
}
|
||||
}
|
||||
|
||||
export const loadCompanyPersonalAccountsBalanceEntries = async ({
|
||||
accountId,
|
||||
}: {
|
||||
accountId: string;
|
||||
}) => {
|
||||
const supabase = getSupabaseServerAdminClient();
|
||||
|
||||
const { count, data: accountMemberships } = await supabase
|
||||
.schema('medreport')
|
||||
.from('accounts_memberships')
|
||||
.select('user_id', { count: 'exact' })
|
||||
.eq('account_id', accountId)
|
||||
.throwOnError();
|
||||
|
||||
const { data: accountBalanceEntries } = await supabase
|
||||
.schema('medreport')
|
||||
.from('account_balance_entries')
|
||||
.select('*')
|
||||
.eq('is_active', true)
|
||||
.in('account_id', accountMemberships.map(({ user_id }) => user_id))
|
||||
.throwOnError();
|
||||
|
||||
const purchaseEntries = accountBalanceEntries.filter(({ entry_type }) => entry_type === 'purchase');
|
||||
const analysesEntries = purchaseEntries.filter(({ is_analysis_order }) => is_analysis_order);
|
||||
const analysisPackagesEntries = purchaseEntries.filter(({ is_analysis_package_order }) => is_analysis_package_order);
|
||||
|
||||
return {
|
||||
accountBalanceEntries,
|
||||
analysesEntries,
|
||||
analysisPackagesEntries,
|
||||
companyAccountsCount: count || 0,
|
||||
purchaseEntries,
|
||||
purchaseEntriesTotal: purchaseEntries.reduce((acc, { amount }) => acc + Math.abs(amount || 0), 0),
|
||||
};
|
||||
}
|
||||
|
||||
export const loadAccountBenefitStatistics = async (
|
||||
accountId: string,
|
||||
): Promise<AccountBenefitStatistics> => {
|
||||
const supabase = getSupabaseServerAdminClient();
|
||||
|
||||
const {
|
||||
analysesEntries,
|
||||
analysisPackagesEntries,
|
||||
companyAccountsCount,
|
||||
purchaseEntriesTotal,
|
||||
} = await loadCompanyPersonalAccountsBalanceEntries({ accountId });
|
||||
|
||||
const { data: benefitDistributionSchedule } = await supabase
|
||||
.schema('medreport')
|
||||
.from('benefit_distribution_schedule')
|
||||
.select('*')
|
||||
.eq('company_id', accountId)
|
||||
.eq('is_active', true)
|
||||
.single();
|
||||
|
||||
const scheduleAmount = benefitDistributionSchedule?.benefit_amount || 0;
|
||||
return {
|
||||
companyAccountsCount,
|
||||
benefitDistributionSchedule: {
|
||||
amount: benefitDistributionSchedule?.benefit_amount || 0,
|
||||
},
|
||||
periodTotal: scheduleAmount * companyAccountsCount,
|
||||
orders: {
|
||||
totalSum: purchaseEntriesTotal,
|
||||
|
||||
analysesCount: analysesEntries.length,
|
||||
analysesSum: analysesEntries.reduce((acc, { amount }) => acc + Math.abs(amount || 0), 0),
|
||||
|
||||
analysisPackagesCount: analysisPackagesEntries.length,
|
||||
analysisPackagesSum: analysisPackagesEntries.reduce((acc, { amount }) => acc + Math.abs(amount || 0), 0),
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
} from '~/lib/utils';
|
||||
|
||||
import { TeamAccountStatisticsProps } from '../../_components/team-account-statistics';
|
||||
import type { BmiThresholds } from '@kit/accounts/types/accounts';
|
||||
|
||||
interface AccountHealthDetailsField {
|
||||
title: string;
|
||||
@@ -25,10 +26,7 @@ interface AccountHealthDetailsField {
|
||||
|
||||
export const getAccountHealthDetailsFields = (
|
||||
memberParams: TeamAccountStatisticsProps['memberParams'],
|
||||
bmiThresholds: Omit<
|
||||
Database['medreport']['Tables']['bmi_thresholds']['Row'],
|
||||
'id'
|
||||
>[],
|
||||
bmiThresholds: Omit<BmiThresholds, 'id'>[],
|
||||
members: Database['medreport']['Functions']['get_account_members']['Returns'],
|
||||
): AccountHealthDetailsField[] => {
|
||||
const averageWeight =
|
||||
@@ -82,7 +80,7 @@ export const getAccountHealthDetailsFields = (
|
||||
},
|
||||
{
|
||||
title: 'teams:healthDetails.bmi',
|
||||
value: averageBMI,
|
||||
value: averageBMI!,
|
||||
Icon: TrendingUp,
|
||||
iconBg: getBmiBackgroundColor(bmiStatus),
|
||||
},
|
||||
|
||||
@@ -30,7 +30,9 @@ const HealthBenefitFields = () => {
|
||||
<Select {...field} onValueChange={field.onChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue
|
||||
placeholder={<Trans i18nKey="common:formField:occurrence" />}
|
||||
placeholder={
|
||||
<Trans i18nKey="common:formField:occurrence" />
|
||||
}
|
||||
/>
|
||||
</SelectTrigger>
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
import { UpdateHealthBenefitSchema } from '@/packages/billing/core/src/schema';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useForm } from 'react-hook-form';
|
||||
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Form } from '@kit/ui/form';
|
||||
import { Spinner } from '@kit/ui/makerkit/spinner';
|
||||
import { toast } from '@kit/ui/shadcn/sonner';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { cn } from '~/lib/utils';
|
||||
|
||||
import { updateHealthBenefit } from '../_lib/server/server-actions';
|
||||
import HealthBenefitFields from './health-benefit-fields';
|
||||
import { Account, CompanyParams } from '@/packages/features/accounts/src/types/accounts';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
const HealthBenefitFormClient = ({
|
||||
account,
|
||||
companyParams,
|
||||
}: {
|
||||
account: Account;
|
||||
companyParams: CompanyParams;
|
||||
}) => {
|
||||
const { t } = useTranslation('account');
|
||||
const router = useRouter();
|
||||
|
||||
const [currentCompanyParams, setCurrentCompanyParams] =
|
||||
useState<CompanyParams>(companyParams);
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
|
||||
const form = useForm({
|
||||
resolver: zodResolver(UpdateHealthBenefitSchema),
|
||||
mode: 'onChange',
|
||||
defaultValues: {
|
||||
occurrence: currentCompanyParams.benefit_occurance || 'yearly',
|
||||
amount: currentCompanyParams.benefit_amount || 0,
|
||||
},
|
||||
});
|
||||
|
||||
const isDirty = form.formState.isDirty;
|
||||
|
||||
const onSubmit = (data: { occurrence: string; amount: number }) => {
|
||||
const promise = async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
await updateHealthBenefit({ ...data, accountId: account.id });
|
||||
setCurrentCompanyParams((prev) => ({
|
||||
...prev,
|
||||
benefit_amount: data.amount,
|
||||
benefit_occurance: data.occurrence,
|
||||
}));
|
||||
} finally {
|
||||
form.reset(data);
|
||||
setIsLoading(false);
|
||||
router.refresh();
|
||||
}
|
||||
};
|
||||
|
||||
toast.promise(promise, {
|
||||
success: t('account:healthBenefitForm.updateSuccess'),
|
||||
error: 'error',
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
className="flex flex-col gap-6"
|
||||
onSubmit={form.handleSubmit(onSubmit)}
|
||||
>
|
||||
<HealthBenefitFields />
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
className="relative"
|
||||
disabled={!isDirty || isLoading}
|
||||
>
|
||||
{isLoading && (
|
||||
<div className="absolute inset-0 flex items-center justify-center">
|
||||
<Spinner />
|
||||
</div>
|
||||
)}
|
||||
<div className={cn({ invisible: isLoading })}>
|
||||
<Trans i18nKey="account:saveChanges" />
|
||||
</div>
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
|
||||
export default HealthBenefitFormClient;
|
||||
|
||||
|
||||
@@ -1,138 +1,70 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
|
||||
import { UpdateHealthBenefitSchema } from '@/packages/billing/core/src/schema';
|
||||
import { Database } from '@/packages/supabase/src/database.types';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { PiggyBankIcon } from 'lucide-react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Form } from '@kit/ui/form';
|
||||
import { Spinner } from '@kit/ui/makerkit/spinner';
|
||||
import { Separator } from '@kit/ui/shadcn/separator';
|
||||
import { toast } from '@kit/ui/shadcn/sonner';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { cn } from '~/lib/utils';
|
||||
|
||||
import { updateHealthBenefit } from '../_lib/server/server-actions';
|
||||
import HealthBenefitFields from './health-benefit-fields';
|
||||
import HealthBenefitFormClient from './health-benefit-form-client';
|
||||
import YearlyExpensesOverview from './yearly-expenses-overview';
|
||||
import { TeamAccountBenefitExpensesOverview } from '../../_lib/server/load-team-account-benefit-expenses-overview';
|
||||
import { Account, CompanyParams } from '@/packages/features/accounts/src/types/accounts';
|
||||
|
||||
const HealthBenefitForm = ({
|
||||
const HealthBenefitForm = async ({
|
||||
account,
|
||||
companyParams,
|
||||
employeeCount,
|
||||
expensesOverview,
|
||||
}: {
|
||||
account: Database['medreport']['Tables']['accounts']['Row'];
|
||||
companyParams: Database['medreport']['Tables']['company_params']['Row'];
|
||||
account: Account;
|
||||
companyParams: CompanyParams;
|
||||
employeeCount: number;
|
||||
expensesOverview: TeamAccountBenefitExpensesOverview;
|
||||
}) => {
|
||||
const [currentCompanyParams, setCurrentCompanyParams] =
|
||||
useState<Database['medreport']['Tables']['company_params']['Row']>(
|
||||
companyParams,
|
||||
);
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
const form = useForm({
|
||||
resolver: zodResolver(UpdateHealthBenefitSchema),
|
||||
mode: 'onChange',
|
||||
defaultValues: {
|
||||
occurrence: currentCompanyParams.benefit_occurance || 'yearly',
|
||||
amount: currentCompanyParams.benefit_amount || 0,
|
||||
},
|
||||
});
|
||||
const isDirty = form.formState.isDirty;
|
||||
|
||||
const onSubmit = (data: { occurrence: string; amount: number }) => {
|
||||
const promise = async () => {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
await updateHealthBenefit({ ...data, accountId: account.id });
|
||||
setCurrentCompanyParams((prev) => ({
|
||||
...prev,
|
||||
benefit_amount: data.amount,
|
||||
benefit_occurance: data.occurrence,
|
||||
}));
|
||||
} finally {
|
||||
form.reset(data);
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
toast.promise(promise, {
|
||||
success: 'Andmed uuendatud',
|
||||
error: 'error',
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
className="flex flex-col gap-6 px-6 text-left"
|
||||
onSubmit={form.handleSubmit(onSubmit)}
|
||||
>
|
||||
<div className="mt-8 flex items-center justify-between">
|
||||
<div>
|
||||
<h4>
|
||||
<Trans
|
||||
i18nKey="billing:pageTitle"
|
||||
values={{ companyName: account.name }}
|
||||
/>
|
||||
</h4>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
<Trans i18nKey="billing:description" />
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
type="submit"
|
||||
className="relative"
|
||||
disabled={!isDirty || isLoading}
|
||||
>
|
||||
{isLoading && (
|
||||
<div className="absolute inset-0 flex items-center justify-center">
|
||||
<Spinner />
|
||||
</div>
|
||||
)}
|
||||
<div className={cn({ invisible: isLoading })}>
|
||||
<Trans i18nKey="account:saveChanges" />
|
||||
</div>
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex flex-row gap-6">
|
||||
<div className="border-border w-1/3 rounded-lg border">
|
||||
<div className="p-6">
|
||||
<div className="flex h-16 w-16 items-center justify-center rounded-full bg-orange-100">
|
||||
<PiggyBankIcon className="h-[32px] w-[32px] stroke-orange-400 stroke-2" />
|
||||
</div>
|
||||
<p className="mt-4 text-sm font-medium">
|
||||
<Trans i18nKey="billing:healthBenefitForm.description" />
|
||||
</p>
|
||||
<p className="pt-2 text-2xl font-semibold">
|
||||
{currentCompanyParams.benefit_amount || 0} €
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
<div className="p-6">
|
||||
<HealthBenefitFields />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex-1">
|
||||
<YearlyExpensesOverview
|
||||
employeeCount={employeeCount}
|
||||
companyParams={currentCompanyParams}
|
||||
<div className="flex flex-col gap-6 px-6 text-left">
|
||||
<div className="mt-8 flex items-center justify-between">
|
||||
<div>
|
||||
<h4>
|
||||
<Trans
|
||||
i18nKey="billing:pageTitle"
|
||||
values={{ companyName: account.name }}
|
||||
/>
|
||||
<p className="text-muted-foreground mt-2 text-sm">
|
||||
<Trans i18nKey="billing:healthBenefitForm.info" />
|
||||
</h4>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col-reverse sm:flex-row gap-6">
|
||||
<div className="border-border w-full sm:w-1/3 rounded-lg border">
|
||||
<div className="p-6">
|
||||
<div className="flex h-16 w-16 items-center justify-center rounded-full bg-orange-100">
|
||||
<PiggyBankIcon className="h-[32px] w-[32px] stroke-orange-400 stroke-2" />
|
||||
</div>
|
||||
<p className="mt-4 text-sm font-medium">
|
||||
<Trans i18nKey="billing:healthBenefitForm.description" />
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Separator />
|
||||
|
||||
<div className="p-6">
|
||||
<HealthBenefitFormClient
|
||||
account={account}
|
||||
companyParams={companyParams}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
|
||||
<div className="flex-1 space-y-6">
|
||||
<YearlyExpensesOverview
|
||||
employeeCount={employeeCount}
|
||||
expensesOverview={expensesOverview}
|
||||
/>
|
||||
|
||||
<p className="text-muted-foreground text-sm">
|
||||
<Trans i18nKey="billing:healthBenefitForm.info" />
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,50 +1,19 @@
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { Database } from '@/packages/supabase/src/database.types';
|
||||
'use client';
|
||||
|
||||
import { Trans } from '@kit/ui/makerkit/trans';
|
||||
import { Separator } from '@kit/ui/separator';
|
||||
import { formatCurrency } from '@/packages/shared/src/utils';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { TeamAccountBenefitExpensesOverview } from '../../_lib/server/load-team-account-benefit-expenses-overview';
|
||||
|
||||
const YearlyExpensesOverview = ({
|
||||
employeeCount = 0,
|
||||
companyParams,
|
||||
expensesOverview,
|
||||
}: {
|
||||
employeeCount?: number;
|
||||
companyParams: Database['medreport']['Tables']['company_params']['Row'];
|
||||
expensesOverview: TeamAccountBenefitExpensesOverview;
|
||||
}) => {
|
||||
const monthlyExpensePerEmployee = useMemo(() => {
|
||||
if (!companyParams.benefit_amount) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
switch (companyParams.benefit_occurance) {
|
||||
case 'yearly':
|
||||
return (companyParams.benefit_amount / 12).toFixed(2);
|
||||
case 'quarterly':
|
||||
return (companyParams.benefit_amount / 3).toFixed(2);
|
||||
case 'monthly':
|
||||
return companyParams.benefit_amount.toFixed(2);
|
||||
default:
|
||||
return '0.00';
|
||||
}
|
||||
}, [companyParams]);
|
||||
|
||||
const maxYearlyExpensePerEmployee = useMemo(() => {
|
||||
if (!companyParams.benefit_amount) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
switch (companyParams.benefit_occurance) {
|
||||
case 'yearly':
|
||||
return companyParams.benefit_amount.toFixed(2);
|
||||
case 'quarterly':
|
||||
return (companyParams.benefit_amount * 3).toFixed(2);
|
||||
case 'monthly':
|
||||
return (companyParams.benefit_amount * 12).toFixed(2);
|
||||
default:
|
||||
return '0.00';
|
||||
}
|
||||
}, [companyParams]);
|
||||
const { i18n: { language } } = useTranslation();
|
||||
|
||||
return (
|
||||
<div className="border-border rounded-lg border p-6">
|
||||
@@ -53,41 +22,56 @@ const YearlyExpensesOverview = ({
|
||||
</h5>
|
||||
<div className="mt-5 flex justify-between">
|
||||
<p className="text-sm font-medium">
|
||||
<Trans i18nKey="billing:expensesOverview.monthly" />
|
||||
<Trans i18nKey="billing:expensesOverview.employeeCount" />
|
||||
</p>
|
||||
<span className="text-primary text-sm font-bold">
|
||||
{monthlyExpensePerEmployee} €
|
||||
{employeeCount}
|
||||
</span>
|
||||
</div>
|
||||
<div className="mt-3 flex justify-between">
|
||||
<p className="text-sm font-medium">
|
||||
<Trans i18nKey="billing:expensesOverview.yearly" />
|
||||
</p>
|
||||
<span className="text-sm font-medium">
|
||||
{maxYearlyExpensePerEmployee} €
|
||||
</span>
|
||||
</div>
|
||||
<div className="mt-5 mb-3 flex justify-between">
|
||||
<p className="text-sm font-medium">
|
||||
<Trans
|
||||
i18nKey="billing:expensesOverview.total"
|
||||
values={{ employeeCount: employeeCount || 0 }}
|
||||
i18nKey="billing:expensesOverview.managementFeeTotal"
|
||||
values={{
|
||||
managementFee: formatCurrency({
|
||||
value: expensesOverview.managementFee,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
}),
|
||||
}}
|
||||
/>
|
||||
</p>
|
||||
<span className="text-sm font-medium">
|
||||
{(Number(maxYearlyExpensePerEmployee) * employeeCount).toFixed(2)} €
|
||||
{formatCurrency({
|
||||
value: expensesOverview.managementFeeTotal,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
<div className="mt-3 mb-4 flex justify-between">
|
||||
<p className="text-sm font-medium">
|
||||
<Trans i18nKey="billing:expensesOverview.currentMonthUsageTotal" />
|
||||
</p>
|
||||
<span className="text-sm font-medium">
|
||||
{formatCurrency({
|
||||
value: expensesOverview.currentMonthUsageTotal,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
<Separator />
|
||||
<div className="mt-4 flex justify-between">
|
||||
<p className="font-semibold">
|
||||
<Trans i18nKey="billing:expensesOverview.sum" />
|
||||
<Trans i18nKey="billing:expensesOverview.total" />
|
||||
</p>
|
||||
<span className="font-semibold">
|
||||
{companyParams.benefit_amount
|
||||
? companyParams.benefit_amount * employeeCount
|
||||
: 0}{' '}
|
||||
€
|
||||
{formatCurrency({
|
||||
value: expensesOverview.total,
|
||||
locale: language,
|
||||
currencyCode: 'EUR',
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -7,6 +7,7 @@ import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
|
||||
import HealthBenefitForm from './_components/health-benefit-form';
|
||||
import { loadTeamAccountBenefitExpensesOverview } from '../_lib/server/load-team-account-benefit-expenses-overview';
|
||||
|
||||
interface TeamAccountBillingPageProps {
|
||||
params: Promise<{ account: string }>;
|
||||
@@ -27,8 +28,14 @@ async function TeamAccountBillingPage({ params }: TeamAccountBillingPageProps) {
|
||||
const api = createTeamAccountsApi(client);
|
||||
|
||||
const account = await api.getTeamAccount(accountSlug);
|
||||
const companyParams = await api.getTeamAccountParams(account.id);
|
||||
const { members } = await api.getMembers(accountSlug);
|
||||
const [expensesOverview, companyParams] = await Promise.all([
|
||||
loadTeamAccountBenefitExpensesOverview({
|
||||
companyId: account.id,
|
||||
employeeCount: members.length,
|
||||
}),
|
||||
api.getTeamAccountParams(account.id),
|
||||
]);
|
||||
|
||||
return (
|
||||
<PageBody>
|
||||
@@ -36,6 +43,7 @@ async function TeamAccountBillingPage({ params }: TeamAccountBillingPageProps) {
|
||||
account={account}
|
||||
companyParams={companyParams}
|
||||
employeeCount={members.length}
|
||||
expensesOverview={expensesOverview}
|
||||
/>
|
||||
</PageBody>
|
||||
);
|
||||
|
||||
@@ -5,6 +5,7 @@ import { SupabaseClient } from '@supabase/supabase-js';
|
||||
import { Database } from '@/packages/supabase/src/database.types';
|
||||
|
||||
import { loadTeamWorkspace } from '~/home/[account]/_lib/server/team-account-workspace.loader';
|
||||
import { getSupabaseServerAdminClient } from '@/packages/supabase/src/clients/server-admin-client';
|
||||
|
||||
/**
|
||||
* Load data for the members page
|
||||
@@ -15,11 +16,13 @@ export async function loadMembersPageData(
|
||||
client: SupabaseClient<Database>,
|
||||
slug: string,
|
||||
) {
|
||||
const workspace = await loadTeamWorkspace(slug);
|
||||
return Promise.all([
|
||||
loadAccountMembers(client, slug),
|
||||
loadInvitations(client, slug),
|
||||
canAddMember,
|
||||
loadTeamWorkspace(slug),
|
||||
workspace,
|
||||
loadAccountMembersBenefitsUsage(getSupabaseServerAdminClient(), workspace.account.id),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -60,6 +63,27 @@ async function loadAccountMembers(
|
||||
return data ?? [];
|
||||
}
|
||||
|
||||
export async function loadAccountMembersBenefitsUsage(
|
||||
client: SupabaseClient<Database>,
|
||||
accountId: string,
|
||||
): Promise<{
|
||||
personal_account_id: string;
|
||||
benefit_amount: number;
|
||||
}[]> {
|
||||
const { data, error } = await client
|
||||
.schema('medreport')
|
||||
.rpc('get_benefits_usages_for_company_members', {
|
||||
p_account_id: accountId,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error('Failed to load account members benefits usage', error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return data ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Load account invitations
|
||||
* @param client
|
||||
|
||||
@@ -42,7 +42,7 @@ async function TeamAccountMembersPage({ params }: TeamAccountMembersPageProps) {
|
||||
const client = getSupabaseServerClient();
|
||||
const slug = (await params).account;
|
||||
|
||||
const [members, invitations, canAddMember, { user, account }] =
|
||||
const [members, invitations, canAddMember, { user, account }, membersBenefitsUsage] =
|
||||
await loadMembersPageData(client, slug);
|
||||
|
||||
const canManageRoles = account.permissions.includes('roles.manage');
|
||||
@@ -54,8 +54,10 @@ async function TeamAccountMembersPage({ params }: TeamAccountMembersPageProps) {
|
||||
return (
|
||||
<>
|
||||
<TeamAccountLayoutPageHeader
|
||||
title={<Trans i18nKey={'common:routes.members'} />}
|
||||
description={<AppBreadcrumbs values={{ [account.slug]: account.name }}/>}
|
||||
title={<Trans i18nKey={'common:routes.companyMembers'} />}
|
||||
description={
|
||||
<AppBreadcrumbs values={{ [account.slug]: account.name }} />
|
||||
}
|
||||
/>
|
||||
|
||||
<PageBody>
|
||||
@@ -96,6 +98,7 @@ async function TeamAccountMembersPage({ params }: TeamAccountMembersPageProps) {
|
||||
members={members}
|
||||
isPrimaryOwner={isPrimaryOwner}
|
||||
canManageRoles={canManageRoles}
|
||||
membersBenefitsUsage={membersBenefitsUsage}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -17,6 +17,8 @@ import {
|
||||
} from '~/lib/services/audit/pageView.service';
|
||||
|
||||
import { Dashboard } from './_components/dashboard';
|
||||
import { loadAccountBenefitStatistics } from './_lib/server/load-team-account-benefit-statistics';
|
||||
import { loadTeamAccountBenefitExpensesOverview } from './_lib/server/load-team-account-benefit-expenses-overview';
|
||||
|
||||
interface TeamAccountHomePageProps {
|
||||
params: Promise<{ account: string }>;
|
||||
@@ -39,9 +41,11 @@ function TeamAccountHomePage({ params }: TeamAccountHomePageProps) {
|
||||
const teamAccount = use(teamAccountsApi.getTeamAccount(account));
|
||||
const { memberParams, members } = use(teamAccountsApi.getMembers(account));
|
||||
const bmiThresholds = use(userAnalysesApi.fetchBmiThresholds());
|
||||
const companyParams = use(
|
||||
teamAccountsApi.getTeamAccountParams(teamAccount.id),
|
||||
);
|
||||
const accountBenefitStatistics = use(loadAccountBenefitStatistics(teamAccount.id));
|
||||
const expensesOverview = use(loadTeamAccountBenefitExpensesOverview({
|
||||
companyId: teamAccount.id,
|
||||
employeeCount: members.length,
|
||||
}));
|
||||
|
||||
use(
|
||||
createPageViewLog({
|
||||
@@ -57,7 +61,8 @@ function TeamAccountHomePage({ params }: TeamAccountHomePageProps) {
|
||||
memberParams={memberParams}
|
||||
bmiThresholds={bmiThresholds}
|
||||
members={members}
|
||||
companyParams={companyParams}
|
||||
accountBenefitStatistics={accountBenefitStatistics}
|
||||
expensesOverview={expensesOverview}
|
||||
/>
|
||||
</PageBody>
|
||||
);
|
||||
|
||||
@@ -48,7 +48,9 @@ async function TeamAccountSettingsPage(props: TeamAccountSettingsPageProps) {
|
||||
<>
|
||||
<TeamAccountLayoutPageHeader
|
||||
title={<Trans i18nKey={'teams:settings.pageTitle'} />}
|
||||
description={<AppBreadcrumbs values={{ [account.slug]: account.name }} />}
|
||||
description={
|
||||
<AppBreadcrumbs values={{ [account.slug]: account.name }} />
|
||||
}
|
||||
/>
|
||||
|
||||
<PageBody>
|
||||
|
||||
@@ -17,6 +17,7 @@ import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
import { toTitleCase } from '~/lib/utils';
|
||||
|
||||
interface JoinTeamAccountPageProps {
|
||||
searchParams: Promise<{
|
||||
@@ -110,12 +111,12 @@ async function JoinTeamAccountPage(props: JoinTeamAccountPageProps) {
|
||||
// once the user accepts the invitation, we redirect them to the account home page
|
||||
const membershipConfirmation = pathsConfig.auth.membershipConfirmation;
|
||||
|
||||
const email = auth.data.email ?? '';
|
||||
const fullName = toTitleCase(auth.data.user_metadata.full_name ?? '');
|
||||
|
||||
return (
|
||||
<AuthLayoutShell Logo={AppLogo}>
|
||||
<AcceptInvitationContainer
|
||||
email={email}
|
||||
fullName={fullName || auth.data.email || ''}
|
||||
inviteToken={token}
|
||||
invitation={invitation}
|
||||
paths={{
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
'use server';
|
||||
|
||||
import { RequestStatus } from '@/lib/types/audit';
|
||||
import { RequestStatus, SyncStatus } from '@/lib/types/audit';
|
||||
import { ConnectedOnlineMethodName } from '@/lib/types/connected-online';
|
||||
import { ExternalApi } from '@/lib/types/external';
|
||||
import { MedipostAction } from '@/lib/types/medipost';
|
||||
import { getSupabaseServerAdminClient } from '@/packages/supabase/src/clients/server-admin-client';
|
||||
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||
|
||||
export default async function logRequestResult(
|
||||
/* personalCode: string, */ requestApi: keyof typeof ExternalApi,
|
||||
requestApi: keyof typeof ExternalApi,
|
||||
requestApiMethod: `${ConnectedOnlineMethodName}` | `${MedipostAction}`,
|
||||
status: RequestStatus,
|
||||
comment?: string,
|
||||
@@ -16,11 +15,10 @@ export default async function logRequestResult(
|
||||
serviceId?: number,
|
||||
serviceProviderId?: number,
|
||||
) {
|
||||
const { error } = await getSupabaseServerClient()
|
||||
const { error } = await getSupabaseServerAdminClient()
|
||||
.schema('audit')
|
||||
.from('request_entries')
|
||||
.insert({
|
||||
/* personal_code: personalCode, */
|
||||
request_api: requestApi,
|
||||
request_api_method: requestApiMethod,
|
||||
requested_start_date: startTime,
|
||||
@@ -69,3 +67,29 @@ export async function getMedipostDispatchTries(medusaOrderId: string) {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function logSyncResult({
|
||||
operation,
|
||||
comment,
|
||||
status,
|
||||
changed_by_role,
|
||||
}: {
|
||||
operation: string;
|
||||
comment?: string;
|
||||
status: SyncStatus;
|
||||
changed_by_role: string;
|
||||
}) {
|
||||
const { error } = await getSupabaseServerAdminClient()
|
||||
.schema('audit')
|
||||
.from('sync_entries')
|
||||
.insert({
|
||||
operation,
|
||||
comment,
|
||||
status,
|
||||
changed_by_role,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw new Error('Failed to insert log entry, error: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
40
lib/services/audit/cartEntries.ts
Normal file
40
lib/services/audit/cartEntries.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
export const createCartEntriesLog = async ({
|
||||
operation,
|
||||
accountId,
|
||||
cartId,
|
||||
variantId,
|
||||
comment,
|
||||
}: {
|
||||
operation: string;
|
||||
accountId: string;
|
||||
cartId: string;
|
||||
variantId?: string;
|
||||
comment?: string;
|
||||
}) => {
|
||||
try {
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
error: userError,
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
if (userError || !user) {
|
||||
console.error('No authenticated user found; skipping audit insert');
|
||||
return;
|
||||
}
|
||||
|
||||
return supabase.schema('audit').from('cart_entries').insert({
|
||||
operation,
|
||||
account_id: accountId,
|
||||
cart_id: cartId,
|
||||
changed_by: user.id,
|
||||
variant_id: variantId,
|
||||
comment,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to insert doctor page view log', error);
|
||||
}
|
||||
};
|
||||
@@ -6,6 +6,7 @@ export enum PageViewAction {
|
||||
REGISTRATION_SUCCESS = 'REGISTRATION_SUCCESS',
|
||||
VIEW_ORDER_ANALYSIS = 'VIEW_ORDER_ANALYSIS',
|
||||
VIEW_TEAM_ACCOUNT_DASHBOARD = 'VIEW_TEAM_ACCOUNT_DASHBOARD',
|
||||
VIEW_TTO_SERVICE_BOOKING = 'VIEW_TTO_SERVICE_BOOKING',
|
||||
}
|
||||
|
||||
export const createPageViewLog = async ({
|
||||
@@ -37,6 +38,7 @@ export const createPageViewLog = async ({
|
||||
account_id: accountId,
|
||||
action,
|
||||
changed_by: user.id,
|
||||
extra_data: extraData,
|
||||
})
|
||||
.throwOnError();
|
||||
} catch (error) {
|
||||
|
||||
@@ -7,19 +7,30 @@ import {
|
||||
BookTimeResponse,
|
||||
ConfirmedLoadResponse,
|
||||
ConnectedOnlineMethodName,
|
||||
FailureReason,
|
||||
} from '@/lib/types/connected-online';
|
||||
import { ExternalApi } from '@/lib/types/external';
|
||||
import { Tables } from '@/packages/supabase/src/database.types';
|
||||
import { createClient } from '@/utils/supabase/server';
|
||||
import axios from 'axios';
|
||||
import { uniq, uniqBy } from 'lodash';
|
||||
|
||||
import { renderBookTimeFailedEmail } from '@kit/email-templates';
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import { TimeSlotResponse } from '~/home/(user)/_components/booking/booking.context';
|
||||
|
||||
import { sendEmailFromTemplate } from './mailer.service';
|
||||
|
||||
export async function getAvailableAppointmentsForService(
|
||||
serviceId: number,
|
||||
key: string,
|
||||
locationId: number | null,
|
||||
startTime?: Date,
|
||||
maxDays?: number,
|
||||
) {
|
||||
try {
|
||||
const showTimesFrom = startTime ? { StartTime: startTime } : {};
|
||||
|
||||
const start = startTime ? { StartTime: startTime } : {};
|
||||
const response = await axios.post(
|
||||
`${process.env.CONNECTED_ONLINE_URL!}/${ConnectedOnlineMethodName.GetAvailabilities}`,
|
||||
{
|
||||
@@ -28,9 +39,11 @@ export async function getAvailableAppointmentsForService(
|
||||
},
|
||||
param: JSON.stringify({
|
||||
ServiceID: serviceId,
|
||||
Key: '7T624nlu',
|
||||
Key: key,
|
||||
Lang: 'et',
|
||||
...showTimesFrom,
|
||||
MaxDays: maxDays ?? 120,
|
||||
LocationId: locationId ?? -1,
|
||||
...start,
|
||||
}),
|
||||
},
|
||||
);
|
||||
@@ -80,157 +93,210 @@ export async function getAvailableAppointmentsForService(
|
||||
}
|
||||
|
||||
export async function bookAppointment(
|
||||
serviceSyncId: number,
|
||||
serviceId: number,
|
||||
clinicId: number,
|
||||
appointmentUserId: number,
|
||||
syncUserID: number,
|
||||
startTime: string,
|
||||
locationId = 0,
|
||||
comments = '',
|
||||
isEarlierTimeRequested = false,
|
||||
earlierTimeRequestComment = '',
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
const logger = await getLogger();
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
let reason = FailureReason.BOOKING_FAILED;
|
||||
try {
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
logger.info(
|
||||
`Booking time slot ${JSON.stringify({ serviceId, clinicId, startTime, userId: user?.id })}`,
|
||||
);
|
||||
|
||||
if (!user?.id) {
|
||||
throw new Error('User not authenticated');
|
||||
}
|
||||
|
||||
const formattedStartTime = startTime.replace('T', ' ');
|
||||
|
||||
const [
|
||||
{ data: dbClinic, error: clinicError },
|
||||
{ data: dbService, error: serviceError },
|
||||
{ data: account, error: accountError },
|
||||
{ data: dbReservation, error: dbReservationError },
|
||||
] = await Promise.all([
|
||||
supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_providers')
|
||||
.select('*')
|
||||
.eq('id', clinicId)
|
||||
.limit(1),
|
||||
.single(),
|
||||
supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_services')
|
||||
.select('*')
|
||||
.eq('sync_id', serviceSyncId)
|
||||
.eq('id', serviceId)
|
||||
.eq('clinic_id', clinicId)
|
||||
.limit(1),
|
||||
.single(),
|
||||
supabase
|
||||
.schema('medreport')
|
||||
.from('accounts')
|
||||
.select('name, last_name, personal_code, phone, email')
|
||||
.eq('is_personal_account', true)
|
||||
.eq('primary_owner_user_id', user.id)
|
||||
.single(),
|
||||
supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.select('id')
|
||||
.eq('clinic_id', clinicId)
|
||||
.eq('service_id', serviceId)
|
||||
.eq('start_time', formattedStartTime)
|
||||
.eq('user_id', user.id)
|
||||
.eq('status', 'PENDING')
|
||||
.single(),
|
||||
]);
|
||||
|
||||
if (!dbClinic?.length || !dbService?.length) {
|
||||
return logRequestResult(
|
||||
ExternalApi.ConnectedOnline,
|
||||
ConnectedOnlineMethodName.BookTime,
|
||||
RequestStatus.Fail,
|
||||
dbClinic?.length
|
||||
? `Could not find clinic with id ${clinicId}, error: ${JSON.stringify(clinicError)}`
|
||||
: `Could not find service with sync id ${serviceSyncId} and clinic id ${clinicId}, error: ${JSON.stringify(serviceError)}`,
|
||||
startTime,
|
||||
serviceSyncId,
|
||||
clinicId,
|
||||
);
|
||||
if (!dbClinic || !dbService) {
|
||||
const errorMessage = dbClinic
|
||||
? `Could not find clinic with id ${clinicId}, error: ${JSON.stringify(clinicError)}`
|
||||
: `Could not find service with sync id ${serviceId} and clinic id ${clinicId}, error: ${JSON.stringify(serviceError)}`;
|
||||
logger.error(errorMessage);
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
if (clinicError || serviceError || accountError) {
|
||||
const stringifiedErrors = JSON.stringify({
|
||||
clinicError,
|
||||
serviceError,
|
||||
accountError,
|
||||
});
|
||||
const errorMessage = `Failed to book time, error: ${stringifiedErrors}`;
|
||||
logger.error(errorMessage);
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
if (!dbReservation) {
|
||||
const errorMessage = `No reservation found in db with data ${JSON.stringify({ clinicId, serviceId, startTime, userId: user.id })}, got error ${JSON.stringify(dbReservationError)}`;
|
||||
logger.error(errorMessage);
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
const clinic: Tables<
|
||||
{ schema: 'medreport' },
|
||||
'connected_online_providers'
|
||||
> = dbClinic![0];
|
||||
> = dbClinic;
|
||||
const service: Tables<
|
||||
{ schema: 'medreport' },
|
||||
'connected_online_services'
|
||||
> = dbService![0];
|
||||
> = dbService;
|
||||
|
||||
// TODO the dummy data needs to be replaced with real values once they're present on the user/account
|
||||
const response = await axios.post(
|
||||
const connectedOnlineBookingResponse = await axios.post(
|
||||
`${process.env.CONNECTED_ONLINE_URL!}/${ConnectedOnlineMethodName.BookTime}`,
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
},
|
||||
param: JSON.stringify({
|
||||
EarlierTime: isEarlierTimeRequested, // once we have the e-shop functionality we can let the user select if she would like to be offered earlier time slots if they become available
|
||||
EarlierTimeComment: earlierTimeRequestComment,
|
||||
ClinicID: clinic.id,
|
||||
ServiceID: service.id,
|
||||
ClinicServiceID: service.sync_id,
|
||||
ServiceID: service.sync_id,
|
||||
ClinicServiceID: service.id,
|
||||
UserID: appointmentUserId,
|
||||
SyncUserID: syncUserID,
|
||||
StartTime: startTime,
|
||||
FirstName: 'Test',
|
||||
LastName: 'User',
|
||||
PersonalCode: '4',
|
||||
Email: user.email,
|
||||
Phone: 'phone',
|
||||
FirstName: account.name,
|
||||
LastName: account.last_name,
|
||||
PersonalCode: account.personal_code,
|
||||
Email: account.email ?? user.email,
|
||||
Phone: account.phone,
|
||||
Comments: comments,
|
||||
Location: locationId,
|
||||
FreeCode: '',
|
||||
AddToBasket: false,
|
||||
Key: '7T624nlu',
|
||||
Lang: 'et', // update when integrated into app, if needed
|
||||
Key: dbClinic.key,
|
||||
Lang: 'et',
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
const responseData: BookTimeResponse = JSON.parse(response.data.d);
|
||||
const connectedOnlineBookingResponseData: BookTimeResponse = JSON.parse(
|
||||
connectedOnlineBookingResponse.data.d,
|
||||
);
|
||||
|
||||
if (responseData?.ErrorCode !== 0 || !responseData.Value) {
|
||||
return logRequestResult(
|
||||
ExternalApi.ConnectedOnline,
|
||||
ConnectedOnlineMethodName.BookTime,
|
||||
RequestStatus.Fail,
|
||||
JSON.stringify(responseData),
|
||||
startTime,
|
||||
service.id,
|
||||
clinicId,
|
||||
const errorCode = connectedOnlineBookingResponseData?.ErrorCode;
|
||||
if (errorCode !== 0 || !connectedOnlineBookingResponseData.Value) {
|
||||
const errorMessage = `Received invalid result from external api, error: ${JSON.stringify(connectedOnlineBookingResponseData)}`;
|
||||
logger.error(errorMessage);
|
||||
if (process.env.SUPPORT_EMAIL) {
|
||||
await sendEmailFromTemplate(
|
||||
renderBookTimeFailedEmail,
|
||||
{ reservationId: dbReservation.id, error: errorMessage },
|
||||
process.env.SUPPORT_EMAIL,
|
||||
);
|
||||
}
|
||||
await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.update({
|
||||
status: 'REJECTED',
|
||||
})
|
||||
.eq('id', dbReservation.id)
|
||||
.throwOnError();
|
||||
|
||||
if (errorCode === 1) {
|
||||
reason = FailureReason.TIME_SLOT_UNAVAILABLE;
|
||||
}
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
const responseParts = connectedOnlineBookingResponseData.Value.split(',');
|
||||
|
||||
const { data: updatedReservation, error } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.update({
|
||||
booking_code: responseParts[1],
|
||||
requires_payment: !!responseParts[0],
|
||||
status: 'CONFIRMED',
|
||||
})
|
||||
.eq('id', dbReservation.id)
|
||||
.select('id')
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
throw new Error(
|
||||
JSON.stringify({ connectedOnlineBookingResponseData, error }),
|
||||
);
|
||||
}
|
||||
|
||||
const responseParts = responseData.Value.split(',');
|
||||
|
||||
const { error } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.insert({
|
||||
booking_code: responseParts[1],
|
||||
clinic_id: clinic.id,
|
||||
comments,
|
||||
lang: 'et', // change later, if needed
|
||||
service_id: service.id,
|
||||
service_user_id: appointmentUserId,
|
||||
start_time: startTime,
|
||||
sync_user_id: syncUserID,
|
||||
requires_payment: !!responseParts[0],
|
||||
user_id: user.id,
|
||||
});
|
||||
logger.info(
|
||||
'Booked time, updated reservation with id ' + updatedReservation?.id,
|
||||
);
|
||||
|
||||
await logRequestResult(
|
||||
ExternalApi.ConnectedOnline,
|
||||
ConnectedOnlineMethodName.BookTime,
|
||||
RequestStatus.Success,
|
||||
JSON.stringify(responseData),
|
||||
startTime,
|
||||
JSON.stringify(connectedOnlineBookingResponseData),
|
||||
startTime.toString(),
|
||||
service.id,
|
||||
clinicId,
|
||||
);
|
||||
|
||||
if (error) {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
|
||||
return responseData.Value;
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return logRequestResult(
|
||||
logger.error(`Failed to book time, error: ${JSON.stringify(error)}`);
|
||||
await logRequestResult(
|
||||
ExternalApi.ConnectedOnline,
|
||||
ConnectedOnlineMethodName.BookTime,
|
||||
RequestStatus.Fail,
|
||||
JSON.stringify(error),
|
||||
startTime,
|
||||
serviceSyncId,
|
||||
startTime.toString(),
|
||||
serviceId,
|
||||
clinicId,
|
||||
);
|
||||
return { success: false, reason };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,8 +336,83 @@ export async function getConfirmedService(reservationCode: string) {
|
||||
ExternalApi.ConnectedOnline,
|
||||
ConnectedOnlineMethodName.ConfirmedLoad,
|
||||
RequestStatus.Fail,
|
||||
JSON.stringify(error),
|
||||
error?.toString(),
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAvailableTimeSlotsForDisplay(
|
||||
serviceIds: number[],
|
||||
locationId: number | null,
|
||||
date?: Date,
|
||||
): Promise<TimeSlotResponse> {
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
const { data: syncedServices } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_services')
|
||||
.select(
|
||||
'*, providerClinic:clinic_id(*,locations:connected_online_locations(*))',
|
||||
)
|
||||
.in('id', serviceIds)
|
||||
.throwOnError();
|
||||
|
||||
const timeSlotPromises = [];
|
||||
for (const syncedService of syncedServices) {
|
||||
const timeSlotsPromise = getAvailableAppointmentsForService(
|
||||
syncedService.id,
|
||||
syncedService.providerClinic.key,
|
||||
locationId,
|
||||
date,
|
||||
);
|
||||
timeSlotPromises.push(timeSlotsPromise);
|
||||
}
|
||||
|
||||
const timeSlots = await Promise.all(timeSlotPromises);
|
||||
|
||||
const mappedTimeSlots = [];
|
||||
for (const timeSlotGroup of timeSlots) {
|
||||
const { data: serviceProviders } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_service_providers')
|
||||
.select(
|
||||
'name, id, jobTitleEn: job_title_en, jobTitleEt: job_title_et, jobTitleRu: job_title_ru, clinicId: clinic_id',
|
||||
)
|
||||
.in(
|
||||
'clinic_id',
|
||||
uniq(timeSlotGroup?.T_Booking.map(({ ClinicID }) => ClinicID)),
|
||||
)
|
||||
.throwOnError();
|
||||
|
||||
const timeSlots =
|
||||
timeSlotGroup?.T_Booking?.map((item) => {
|
||||
return {
|
||||
...item,
|
||||
serviceProvider: serviceProviders.find(
|
||||
({ id }) => id === item.UserID,
|
||||
),
|
||||
syncedService: syncedServices.find(
|
||||
(syncedService) => syncedService.sync_id === item.ServiceID,
|
||||
),
|
||||
location: syncedServices
|
||||
.find(
|
||||
({ providerClinic }) =>
|
||||
providerClinic.id === Number(item.ClinicID),
|
||||
)
|
||||
?.providerClinic?.locations?.find(
|
||||
(location) => location.sync_id === item.LocationID,
|
||||
),
|
||||
};
|
||||
}) ?? [];
|
||||
mappedTimeSlots.push(...timeSlots);
|
||||
}
|
||||
|
||||
return {
|
||||
timeSlots: mappedTimeSlots,
|
||||
locations: uniqBy(
|
||||
syncedServices.flatMap(({ providerClinic }) => providerClinic.locations),
|
||||
'id',
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ import axios from 'axios';
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
|
||||
import type { AnalysisOrder } from '~/lib/types/analysis-order';
|
||||
import type { AnalysisResponseElement } from '~/lib/types/analysis-response-element';
|
||||
import type { AnalysisOrder } from '~/lib/types/order';
|
||||
|
||||
import { getAccountAdmin } from '../account.service';
|
||||
import { getAnalyses } from '../analyses.service';
|
||||
@@ -28,7 +28,7 @@ import {
|
||||
upsertAnalysisResponseElement,
|
||||
} from '../analysis-order.service';
|
||||
import { logMedipostDispatch } from '../audit.service';
|
||||
import { getAnalysisOrder, updateAnalysisOrderStatus } from '../order.service';
|
||||
import { getAnalysisOrder } from '../order.service';
|
||||
import { parseXML } from '../util/xml.service';
|
||||
import { MedipostValidationError } from './MedipostValidationError';
|
||||
import {
|
||||
@@ -430,17 +430,19 @@ export async function readPrivateMessageResponse({
|
||||
medipostExternalOrderId,
|
||||
});
|
||||
if (status.isPartial) {
|
||||
await updateAnalysisOrderStatus({
|
||||
medusaOrderId,
|
||||
orderStatus: 'PARTIAL_ANALYSIS_RESPONSE',
|
||||
});
|
||||
await createUserAnalysesApi(getSupabaseServerAdminClient())
|
||||
.updateAnalysisOrderStatus({
|
||||
medusaOrderId,
|
||||
orderStatus: 'PARTIAL_ANALYSIS_RESPONSE',
|
||||
});
|
||||
hasAnalysisResponse = true;
|
||||
hasPartialAnalysisResponse = true;
|
||||
} else if (status.isCompleted) {
|
||||
await updateAnalysisOrderStatus({
|
||||
medusaOrderId,
|
||||
orderStatus: 'FULL_ANALYSIS_RESPONSE',
|
||||
});
|
||||
await createUserAnalysesApi(getSupabaseServerAdminClient())
|
||||
.updateAnalysisOrderStatus({
|
||||
medusaOrderId,
|
||||
orderStatus: 'FULL_ANALYSIS_RESPONSE',
|
||||
});
|
||||
if (IS_ENABLED_DELETE_PRIVATE_MESSAGE) {
|
||||
await deletePrivateMessage(privateMessageId);
|
||||
}
|
||||
@@ -622,5 +624,9 @@ export async function sendOrderToMedipost({
|
||||
hasAnalysisResults: false,
|
||||
medusaOrderId,
|
||||
});
|
||||
await updateAnalysisOrderStatus({ medusaOrderId, orderStatus: 'PROCESSING' });
|
||||
await createUserAnalysesApi(getSupabaseServerAdminClient())
|
||||
.updateAnalysisOrderStatus({
|
||||
medusaOrderId,
|
||||
orderStatus: 'PROCESSING',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,9 +5,16 @@ import { MontonioOrderHandlerService } from '@/packages/billing/montonio/src';
|
||||
import { addToCart, deleteLineItem, retrieveCart } from '@lib/data/cart';
|
||||
import { getCartId } from '@lib/data/cookies';
|
||||
import { StoreCartLineItem, StoreProductVariant } from '@medusajs/types';
|
||||
import { isSameMinute } from 'date-fns';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
import {
|
||||
cancelReservation,
|
||||
getOrderedTtoServices,
|
||||
} from '~/lib/services/reservation.service';
|
||||
|
||||
import { createCartEntriesLog } from './audit/cartEntries';
|
||||
import { getAvailableAppointmentsForService } from './connected-online.service';
|
||||
|
||||
const env = () =>
|
||||
z
|
||||
@@ -35,64 +42,60 @@ export async function handleAddToCart({
|
||||
selectedVariant: Pick<StoreProductVariant, 'id'>;
|
||||
countryCode: string;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
const quantity = 1;
|
||||
const cart = await addToCart({
|
||||
const { newCart, addedItem } = await addToCart({
|
||||
variantId: selectedVariant.id,
|
||||
quantity,
|
||||
countryCode,
|
||||
});
|
||||
|
||||
const { error } = await supabase.schema('audit').from('cart_entries').insert({
|
||||
variant_id: selectedVariant.id,
|
||||
await createCartEntriesLog({
|
||||
variantId: selectedVariant.id,
|
||||
operation: 'ADD_TO_CART',
|
||||
account_id: account.id,
|
||||
cart_id: cart.id,
|
||||
changed_by: user.id,
|
||||
accountId: account.id,
|
||||
cartId: newCart.id,
|
||||
});
|
||||
if (error) {
|
||||
throw new Error('Error logging cart entry: ' + error.message);
|
||||
}
|
||||
|
||||
return cart;
|
||||
return { cart: newCart, addedItem };
|
||||
}
|
||||
|
||||
export async function handleDeleteCartItem({ lineId }: { lineId: string }) {
|
||||
await deleteLineItem(lineId);
|
||||
await cancelReservation(lineId);
|
||||
|
||||
const supabase = getSupabaseServerClient();
|
||||
const cartId = await getCartId();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
const { error } = await supabase.schema('audit').from('cart_entries').insert({
|
||||
variant_id: lineId,
|
||||
await createCartEntriesLog({
|
||||
variantId: lineId,
|
||||
operation: 'REMOVE_FROM_CART',
|
||||
account_id: account.id,
|
||||
cart_id: cartId!,
|
||||
changed_by: user.id,
|
||||
accountId: account.id,
|
||||
cartId: cartId!,
|
||||
});
|
||||
if (error) {
|
||||
throw new Error('Error logging cart entry: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleNavigateToPayment({
|
||||
language,
|
||||
paymentSessionId,
|
||||
amount,
|
||||
currencyCode,
|
||||
cartId,
|
||||
}: {
|
||||
language: string;
|
||||
paymentSessionId: string;
|
||||
amount: number;
|
||||
currencyCode: string;
|
||||
cartId: string;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
@@ -101,29 +104,52 @@ export async function handleNavigateToPayment({
|
||||
if (!cart) {
|
||||
throw new Error('No cart found');
|
||||
}
|
||||
const orderedTtoServices = await getOrderedTtoServices({ cart });
|
||||
|
||||
if (orderedTtoServices?.length) {
|
||||
const unavailableLineItemIds: string[] = [];
|
||||
for (const ttoService of orderedTtoServices) {
|
||||
const availabilities = await getAvailableAppointmentsForService(
|
||||
ttoService.service_id,
|
||||
ttoService.provider.key,
|
||||
ttoService.location_sync_id,
|
||||
new Date(ttoService.start_time),
|
||||
1,
|
||||
);
|
||||
const isAvailable = availabilities?.T_Booking?.length
|
||||
? availabilities.T_Booking.find((timeSlot) =>
|
||||
isSameMinute(ttoService.start_time, timeSlot.StartTime),
|
||||
)
|
||||
: false;
|
||||
|
||||
if (!isAvailable) {
|
||||
unavailableLineItemIds.push(ttoService.medusa_cart_line_item_id!);
|
||||
}
|
||||
}
|
||||
|
||||
if (unavailableLineItemIds.length) {
|
||||
return { unavailableLineItemIds };
|
||||
}
|
||||
}
|
||||
|
||||
const paymentLink =
|
||||
await new MontonioOrderHandlerService().getMontonioPaymentLink({
|
||||
notificationUrl: `${env().medusaBackendPublicUrl}/hooks/payment/montonio_montonio`,
|
||||
returnUrl: `${env().siteUrl}/home/cart/montonio-callback`,
|
||||
amount: cart.total,
|
||||
currency: cart.currency_code.toUpperCase(),
|
||||
amount,
|
||||
currency: currencyCode.toUpperCase(),
|
||||
description: `Order from Medreport`,
|
||||
locale: language,
|
||||
merchantReference: `${account.id}:${paymentSessionId}:${cart.id}`,
|
||||
merchantReference: `${account.id}:${paymentSessionId}:${cartId}`,
|
||||
});
|
||||
|
||||
const { error } = await supabase.schema('audit').from('cart_entries').insert({
|
||||
await createCartEntriesLog({
|
||||
operation: 'NAVIGATE_TO_PAYMENT',
|
||||
account_id: account.id,
|
||||
cart_id: cart.id,
|
||||
changed_by: user.id,
|
||||
accountId: account.id,
|
||||
cartId: cart.id,
|
||||
});
|
||||
if (error) {
|
||||
throw new Error('Error logging cart entry: ' + error.message);
|
||||
}
|
||||
|
||||
return paymentLink;
|
||||
return { url: paymentLink };
|
||||
}
|
||||
|
||||
export async function handleLineItemTimeout({
|
||||
@@ -131,21 +157,16 @@ export async function handleLineItemTimeout({
|
||||
}: {
|
||||
lineItem: StoreCartLineItem;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
await deleteLineItem(lineItem.id);
|
||||
|
||||
const { error } = await supabase.schema('audit').from('cart_entries').insert({
|
||||
await createCartEntriesLog({
|
||||
operation: 'LINE_ITEM_TIMEOUT',
|
||||
account_id: account.id,
|
||||
cart_id: lineItem.cart_id,
|
||||
changed_by: user.id,
|
||||
accountId: account.id,
|
||||
cartId: lineItem.cart_id,
|
||||
});
|
||||
if (error) {
|
||||
throw new Error('Error logging cart entry: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { Tables } from '@kit/supabase/database';
|
||||
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import type { AnalysisOrder } from '../types/analysis-order';
|
||||
import type { AnalysisOrder, TTOOrder } from '../types/order';
|
||||
|
||||
export async function createAnalysisOrder({
|
||||
medusaOrder,
|
||||
@@ -51,48 +51,6 @@ export async function createAnalysisOrder({
|
||||
return orderResult.data.id;
|
||||
}
|
||||
|
||||
export async function updateAnalysisOrder({
|
||||
orderId,
|
||||
orderStatus,
|
||||
}: {
|
||||
orderId: number;
|
||||
orderStatus: Tables<{ schema: 'medreport' }, 'analysis_orders'>['status'];
|
||||
}) {
|
||||
console.info(`Updating order id=${orderId} status=${orderStatus}`);
|
||||
await getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.update({
|
||||
status: orderStatus,
|
||||
})
|
||||
.eq('id', orderId)
|
||||
.throwOnError();
|
||||
}
|
||||
|
||||
export async function updateAnalysisOrderStatus({
|
||||
orderId,
|
||||
medusaOrderId,
|
||||
orderStatus,
|
||||
}: {
|
||||
orderId?: number;
|
||||
medusaOrderId?: string;
|
||||
orderStatus: Tables<{ schema: 'medreport' }, 'analysis_orders'>['status'];
|
||||
}) {
|
||||
const orderIdParam = orderId;
|
||||
const medusaOrderIdParam = medusaOrderId;
|
||||
if (!orderIdParam && !medusaOrderIdParam) {
|
||||
throw new Error('Either orderId or medusaOrderId must be provided');
|
||||
}
|
||||
await getSupabaseServerAdminClient()
|
||||
.schema('medreport')
|
||||
.rpc('update_analysis_order_status', {
|
||||
order_id: orderIdParam ?? -1,
|
||||
status_param: orderStatus,
|
||||
medusa_order_id_param: medusaOrderIdParam ?? '',
|
||||
})
|
||||
.throwOnError();
|
||||
}
|
||||
|
||||
export async function getAnalysisOrder({
|
||||
medusaOrderId,
|
||||
analysisOrderId,
|
||||
@@ -171,3 +129,39 @@ export async function getAnalysisOrdersAdmin({
|
||||
.throwOnError();
|
||||
return orders.data;
|
||||
}
|
||||
|
||||
export async function getTtoOrders({
|
||||
orderStatus,
|
||||
lineItemIds,
|
||||
}: {
|
||||
orderStatus?: TTOOrder['status'];
|
||||
lineItemIds?: string[];
|
||||
} = {}) {
|
||||
const client = getSupabaseServerClient();
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
} = await client.auth.getUser();
|
||||
if (!user) {
|
||||
throw new Error('Unauthorized');
|
||||
}
|
||||
|
||||
const query = client
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.select('*')
|
||||
.eq('user_id', user.id);
|
||||
|
||||
if (orderStatus) {
|
||||
query.eq('status', orderStatus);
|
||||
}
|
||||
|
||||
if (lineItemIds?.length) {
|
||||
query.in('medusa_cart_line_item_id', lineItemIds);
|
||||
}
|
||||
|
||||
const orders = await query
|
||||
.order('created_at', { ascending: false })
|
||||
.throwOnError();
|
||||
return orders.data;
|
||||
}
|
||||
|
||||
343
lib/services/reservation.service.ts
Normal file
343
lib/services/reservation.service.ts
Normal file
@@ -0,0 +1,343 @@
|
||||
'use server';
|
||||
|
||||
import { revalidatePath } from 'next/cache';
|
||||
|
||||
import { listRegions } from '@lib/data/regions';
|
||||
import { StoreCart, StoreOrder } from '@medusajs/types';
|
||||
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import { EnrichedCartItem } from '../../app/home/(user)/_components/cart/types';
|
||||
import { loadCurrentUserAccount } from '../../app/home/(user)/_lib/server/load-user-account';
|
||||
import { createCartEntriesLog } from './audit/cartEntries';
|
||||
import { handleDeleteCartItem } from './medusaCart.service';
|
||||
|
||||
type Locations = Tables<{ schema: 'medreport' }, 'connected_online_locations'>;
|
||||
type Services = Tables<{ schema: 'medreport' }, 'connected_online_services'>;
|
||||
type ServiceProviders = Tables<
|
||||
{ schema: 'medreport' },
|
||||
'connected_online_service_providers'
|
||||
>;
|
||||
|
||||
export async function getCartReservations(
|
||||
medusaCart: StoreCart,
|
||||
): Promise<EnrichedCartItem[]> {
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
const cartLineItemIds = medusaCart.items?.map(({ id }) => id);
|
||||
|
||||
if (!cartLineItemIds?.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const { data: reservations } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.select(
|
||||
'id, startTime:start_time, service:service_id, location:location_sync_id, serviceProvider:service_user_id, medusaCartLineItemId:medusa_cart_line_item_id',
|
||||
)
|
||||
.in('medusa_cart_line_item_id', cartLineItemIds)
|
||||
.throwOnError();
|
||||
|
||||
const locationSyncIds: number[] =
|
||||
reservations
|
||||
?.filter((reservation) => !!reservation.location)
|
||||
.map((reservation) => reservation.location!) ?? [];
|
||||
const serviceIds =
|
||||
reservations?.map((reservation) => reservation.service) ?? [];
|
||||
const serviceProviderIds =
|
||||
reservations.map((reservation) => reservation.serviceProvider) ?? [];
|
||||
|
||||
let locations:
|
||||
| {
|
||||
syncId: Locations['sync_id'];
|
||||
name: Locations['name'];
|
||||
address: Locations['address'];
|
||||
}[]
|
||||
| null = null;
|
||||
if (locationSyncIds.length) {
|
||||
({ data: locations } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_locations')
|
||||
.select('syncId:sync_id, name, address')
|
||||
.in('sync_id', locationSyncIds)
|
||||
.throwOnError());
|
||||
}
|
||||
|
||||
let services:
|
||||
| {
|
||||
id: Services['id'];
|
||||
name: Services['name'];
|
||||
}[]
|
||||
| null = null;
|
||||
if (serviceIds.length) {
|
||||
({ data: services } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_services')
|
||||
.select('name, id')
|
||||
.in('id', serviceIds)
|
||||
.throwOnError());
|
||||
}
|
||||
|
||||
let serviceProviders:
|
||||
| {
|
||||
id: ServiceProviders['id'];
|
||||
name: ServiceProviders['name'];
|
||||
jobTitleEt: ServiceProviders['job_title_et'];
|
||||
jobTitleEn: ServiceProviders['job_title_en'];
|
||||
jobTitleRu: ServiceProviders['job_title_ru'];
|
||||
spokenLanguages: ServiceProviders['spoken_languages'];
|
||||
}[]
|
||||
| null = null;
|
||||
if (serviceProviderIds.length) {
|
||||
({ data: serviceProviders } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_service_providers')
|
||||
.select(
|
||||
'id, name, jobTitleEt:job_title_et, jobTitleEn:job_title_en, jobTitleRu:job_title_ru, spokenLanguages:spoken_languages',
|
||||
)
|
||||
.in('id', serviceProviderIds)
|
||||
.throwOnError());
|
||||
}
|
||||
|
||||
const results = [];
|
||||
for (const reservation of reservations) {
|
||||
if (reservation.medusaCartLineItemId === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const cartLineItem = medusaCart.items?.find(
|
||||
(item) => item.id === reservation.medusaCartLineItemId,
|
||||
);
|
||||
|
||||
if (!cartLineItem) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const location = locations?.find(
|
||||
(location) => location.syncId === reservation.location,
|
||||
);
|
||||
const service = services?.find(
|
||||
(service) => service.id === reservation.service,
|
||||
);
|
||||
const serviceProvider = serviceProviders?.find(
|
||||
(serviceProvider) => serviceProvider.id === reservation.serviceProvider,
|
||||
);
|
||||
|
||||
const countryCodes = await listRegions().then((regions) =>
|
||||
regions?.map((r) => r.countries?.map((c) => c.iso_2)).flat(),
|
||||
);
|
||||
|
||||
const enrichedReservation = {
|
||||
...reservation,
|
||||
location,
|
||||
service,
|
||||
serviceProvider,
|
||||
};
|
||||
|
||||
results.push({
|
||||
...cartLineItem,
|
||||
reservation: {
|
||||
...enrichedReservation,
|
||||
medusaCartLineItemId: reservation.medusaCartLineItemId!,
|
||||
countryCode: countryCodes[0],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function createInitialReservation({
|
||||
serviceId,
|
||||
clinicId,
|
||||
appointmentUserId,
|
||||
syncUserID,
|
||||
startTime,
|
||||
medusaLineItemId,
|
||||
locationId,
|
||||
comments = '',
|
||||
}: {
|
||||
serviceId: number;
|
||||
clinicId: number;
|
||||
appointmentUserId: number;
|
||||
syncUserID: number;
|
||||
startTime: Date;
|
||||
medusaLineItemId: string;
|
||||
locationId?: number | null;
|
||||
comments?: string;
|
||||
}) {
|
||||
const logger = await getLogger();
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
const userId = user?.id;
|
||||
|
||||
if (!userId) {
|
||||
throw new Error('User not authenticated');
|
||||
}
|
||||
|
||||
logger.info(
|
||||
'Creating reservation' +
|
||||
JSON.stringify({ serviceId, clinicId, startTime, userId }),
|
||||
);
|
||||
|
||||
try {
|
||||
const { data: createdReservation } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.insert({
|
||||
clinic_id: clinicId,
|
||||
comments,
|
||||
lang: 'et',
|
||||
service_id: serviceId,
|
||||
service_user_id: appointmentUserId,
|
||||
start_time: startTime.toString(),
|
||||
sync_user_id: syncUserID,
|
||||
user_id: userId,
|
||||
status: 'PENDING',
|
||||
medusa_cart_line_item_id: medusaLineItemId,
|
||||
location_sync_id: locationId,
|
||||
})
|
||||
.select('id')
|
||||
.single()
|
||||
.throwOnError();
|
||||
|
||||
logger.info(
|
||||
`Created reservation ${JSON.stringify({ createdReservation, userId })}`,
|
||||
);
|
||||
|
||||
return createdReservation;
|
||||
} catch (e) {
|
||||
logger.error(
|
||||
`Failed to create initial reservation ${JSON.stringify({ serviceId, clinicId, startTime })} ${e}`,
|
||||
);
|
||||
await handleDeleteCartItem({ lineId: medusaLineItemId });
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
export async function cancelReservation(medusaLineItemId: string) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
return supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.update({
|
||||
status: 'CANCELLED',
|
||||
})
|
||||
.eq('medusa_cart_line_item_id', medusaLineItemId)
|
||||
.throwOnError();
|
||||
}
|
||||
|
||||
export async function getOrderedTtoServices({
|
||||
cart,
|
||||
medusaOrder,
|
||||
}: {
|
||||
cart?: StoreCart;
|
||||
medusaOrder?: StoreOrder;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
if (!medusaOrder && !cart) {
|
||||
throw new Error('No cart or medusa order provided');
|
||||
}
|
||||
|
||||
const ttoReservationIds: number[] =
|
||||
(medusaOrder?.items ?? cart?.items)
|
||||
?.filter(({ metadata }) => !!metadata?.connectedOnlineReservationId)
|
||||
.map(({ metadata }) => Number(metadata!.connectedOnlineReservationId)) ??
|
||||
[];
|
||||
|
||||
const { data: orderedTtoServices } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.select('*, provider:connected_online_providers(key)')
|
||||
.in('id', ttoReservationIds)
|
||||
.throwOnError();
|
||||
|
||||
return orderedTtoServices;
|
||||
}
|
||||
|
||||
export async function updateReservationTime({
|
||||
reservationId,
|
||||
newStartTime,
|
||||
newServiceId,
|
||||
newAppointmentUserId,
|
||||
newSyncUserId,
|
||||
newLocationId, // TODO stop allowing null when Connected starts returning the correct ids instead of -1
|
||||
cartId,
|
||||
}: {
|
||||
reservationId: number;
|
||||
newStartTime: Date;
|
||||
newServiceId: number;
|
||||
newAppointmentUserId: number;
|
||||
newSyncUserId: number;
|
||||
newLocationId: number | null;
|
||||
cartId: string;
|
||||
}) {
|
||||
const logger = await getLogger();
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
const userId = user?.id;
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
|
||||
if (!userId || !account) {
|
||||
throw new Error('User not authenticated');
|
||||
}
|
||||
|
||||
const reservationData = JSON.stringify({
|
||||
reservationId,
|
||||
newStartTime,
|
||||
newServiceId,
|
||||
newAppointmentUserId,
|
||||
newSyncUserId,
|
||||
newLocationId,
|
||||
userId,
|
||||
cartId,
|
||||
});
|
||||
|
||||
logger.info('Updating reservation' + reservationData);
|
||||
try {
|
||||
await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.update({
|
||||
service_id: newServiceId,
|
||||
service_user_id: newAppointmentUserId,
|
||||
sync_user_id: newSyncUserId,
|
||||
start_time: newStartTime.toString(),
|
||||
location_sync_id: newLocationId,
|
||||
})
|
||||
.eq('id', reservationId)
|
||||
.eq('user_id', user.id)
|
||||
.throwOnError();
|
||||
|
||||
logger.info(`Successfully updated reservation ${reservationData}`);
|
||||
await createCartEntriesLog({
|
||||
operation: 'CHANGE_RESERVATION',
|
||||
accountId: account.id,
|
||||
cartId: cartId,
|
||||
comment: `${reservationData}`,
|
||||
});
|
||||
revalidatePath('/home/cart', 'layout');
|
||||
} catch (e) {
|
||||
logger.error(`Failed to update reservation ${reservationData}`);
|
||||
await createCartEntriesLog({
|
||||
operation: 'CHANGE_RESERVATION',
|
||||
accountId: account.id,
|
||||
cartId: cartId,
|
||||
comment: `${e}`,
|
||||
});
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
import type { Tables } from '@kit/supabase/database';
|
||||
|
||||
export type AnalysisOrder = Tables<{ schema: 'medreport' }, 'analysis_orders'>;
|
||||
@@ -10,13 +10,15 @@ export type BookTimeResponse = z.infer<typeof BookTimeResponseSchema>;
|
||||
|
||||
export enum ConnectedOnlineMethodName {
|
||||
SearchLoad = 'Search_Load',
|
||||
DefaultLoad = 'Default_Load',
|
||||
ConfirmedCancel = 'Confirmed_Cancel',
|
||||
GetAvailabilities = 'GetAvailabilities',
|
||||
BookTime = 'BookTime',
|
||||
ConfirmedLoad = 'Confirmed_Load',
|
||||
}
|
||||
|
||||
export const AvailableAppointmentTBookingSchema = z.object({
|
||||
ClinicID: z.string(),
|
||||
ClinicID: z.number(),
|
||||
LocationID: z.number(),
|
||||
UserID: z.number(),
|
||||
SyncUserID: z.number(),
|
||||
@@ -225,6 +227,18 @@ export const ConfirmedLoadResponseSchema = z.object({
|
||||
});
|
||||
export type ConfirmedLoadResponse = z.infer<typeof ConfirmedLoadResponseSchema>;
|
||||
|
||||
export type P_JobTitleTranslation = {
|
||||
ID: number;
|
||||
SyncID: number;
|
||||
TextEN: string;
|
||||
TextET: string;
|
||||
TextFI: string;
|
||||
TextRU: string;
|
||||
TextLT: string;
|
||||
ClinicID: number;
|
||||
Deleted: number;
|
||||
};
|
||||
|
||||
export interface ISearchLoadResponse {
|
||||
Value: string;
|
||||
Data: {
|
||||
@@ -232,9 +246,11 @@ export interface ISearchLoadResponse {
|
||||
ID: number;
|
||||
Name: string;
|
||||
OnlineCanSelectWorker: boolean;
|
||||
Address: string;
|
||||
Email: string | null;
|
||||
PersonalCodeRequired: boolean;
|
||||
Phone: string | null;
|
||||
Key: string;
|
||||
}[];
|
||||
T_Service: {
|
||||
ID: number;
|
||||
@@ -253,7 +269,14 @@ export interface ISearchLoadResponse {
|
||||
RequiresPayment: boolean;
|
||||
SyncID: string;
|
||||
}[];
|
||||
T_Doctor: TDoctor[];
|
||||
P_JobTitleTranslations: P_JobTitleTranslation[];
|
||||
};
|
||||
ErrorCode: number;
|
||||
ErrorMessage: string;
|
||||
}
|
||||
|
||||
export enum FailureReason {
|
||||
BOOKING_FAILED = 'BOOKING_FAILED',
|
||||
TIME_SLOT_UNAVAILABLE = 'TIME_SLOT_UNAVAILABLE',
|
||||
}
|
||||
|
||||
12
lib/types/order.ts
Normal file
12
lib/types/order.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { Tables } from '@kit/supabase/database';
|
||||
|
||||
export type AnalysisOrder = Tables<{ schema: 'medreport' }, 'analysis_orders'>;
|
||||
export type TTOOrder = Tables<
|
||||
{ schema: 'medreport' },
|
||||
'connected_online_reservation'
|
||||
>;
|
||||
export type Order = {
|
||||
medusaOrderId?: string;
|
||||
id?: number;
|
||||
status?: string;
|
||||
};
|
||||
35
lib/types/reservation.ts
Normal file
35
lib/types/reservation.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import z from 'zod';
|
||||
|
||||
export const LocationSchema = z.object({
|
||||
syncId: z.number(),
|
||||
name: z.string(),
|
||||
address: z.string().nullable(),
|
||||
});
|
||||
export type Location = z.infer<typeof LocationSchema>;
|
||||
|
||||
export const ServiceSchema = z.object({
|
||||
name: z.string(),
|
||||
id: z.number(),
|
||||
});
|
||||
export type Service = z.infer<typeof ServiceSchema>;
|
||||
|
||||
export const ServiceProviderSchema = z.object({
|
||||
id: z.number(),
|
||||
name: z.string(),
|
||||
jobTitleEt: z.string().nullable(),
|
||||
jobTitleEn: z.string().nullable(),
|
||||
jobTitleRu: z.string().nullable(),
|
||||
spokenLanguages: z.array(z.string()).nullable(),
|
||||
});
|
||||
export type ServiceProvider = z.infer<typeof ServiceProviderSchema>;
|
||||
|
||||
export const ReservationSchema = z.object({
|
||||
startTime: z.string(),
|
||||
service: ServiceSchema.optional(),
|
||||
location: LocationSchema.optional(),
|
||||
serviceProvider: ServiceProviderSchema.optional(),
|
||||
medusaCartLineItemId: z.string(),
|
||||
id: z.number(),
|
||||
countryCode: z.string().optional(),
|
||||
});
|
||||
export type Reservation = z.infer<typeof ReservationSchema>;
|
||||
14
lib/utils.ts
14
lib/utils.ts
@@ -1,9 +1,9 @@
|
||||
import { Database } from '@/packages/supabase/src/database.types';
|
||||
import { type ClassValue, clsx } from 'clsx';
|
||||
import Isikukood, { Gender } from 'isikukood';
|
||||
import { twMerge } from 'tailwind-merge';
|
||||
|
||||
import { BmiCategory } from './types/bmi';
|
||||
import type { BmiThresholds } from '@kit/accounts/types/accounts';
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
@@ -45,10 +45,7 @@ export const bmiFromMetric = (kg: number, cm: number) => {
|
||||
};
|
||||
|
||||
export function getBmiStatus(
|
||||
thresholds: Omit<
|
||||
Database['medreport']['Tables']['bmi_thresholds']['Row'],
|
||||
'id'
|
||||
>[],
|
||||
thresholds: Omit<BmiThresholds, 'id'>[],
|
||||
params: { age: number; height: number; weight: number },
|
||||
): BmiCategory | null {
|
||||
const age = params.age;
|
||||
@@ -143,3 +140,10 @@ export default class PersonalCode {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const findProductTypeIdByHandle = (
|
||||
productTypes: { metadata?: Record<string, unknown> | null; id: string }[],
|
||||
handle: string,
|
||||
) => {
|
||||
return productTypes.find(({ metadata }) => metadata?.handle === handle)?.id;
|
||||
};
|
||||
|
||||
@@ -37,10 +37,11 @@ interface MontonioOrderToken {
|
||||
exp: number;
|
||||
}
|
||||
|
||||
const { secretKey } = MontonioServerEnvSchema.parse({
|
||||
apiUrl: process.env.MONTONIO_API_URL,
|
||||
secretKey: process.env.MONTONIO_SECRET_KEY,
|
||||
});
|
||||
const env = () =>
|
||||
MontonioServerEnvSchema.parse({
|
||||
apiUrl: process.env.MONTONIO_API_URL,
|
||||
secretKey: process.env.MONTONIO_SECRET_KEY,
|
||||
});
|
||||
|
||||
export class MontonioWebhookHandlerService
|
||||
implements BillingWebhookHandlerService
|
||||
@@ -50,6 +51,7 @@ export class MontonioWebhookHandlerService
|
||||
|
||||
async verifyWebhookSignature(request: Request) {
|
||||
const logger = await getLogger();
|
||||
const { secretKey } = env();
|
||||
|
||||
let token: string;
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
Body,
|
||||
Head,
|
||||
Html,
|
||||
Preview,
|
||||
Tailwind,
|
||||
Text,
|
||||
render,
|
||||
} from '@react-email/components';
|
||||
|
||||
import { BodyStyle } from '../components/body-style';
|
||||
import { EmailContent } from '../components/content';
|
||||
import { EmailHeader } from '../components/header';
|
||||
import { EmailHeading } from '../components/heading';
|
||||
import { EmailWrapper } from '../components/wrapper';
|
||||
|
||||
export async function renderBookTimeFailedEmail({
|
||||
reservationId,
|
||||
error,
|
||||
}: {
|
||||
reservationId: number;
|
||||
error: string;
|
||||
}) {
|
||||
const subject = 'Aja broneerimine ei õnnestunud';
|
||||
|
||||
const html = await render(
|
||||
<Html>
|
||||
<Head>
|
||||
<BodyStyle />
|
||||
</Head>
|
||||
|
||||
<Preview>{subject}</Preview>
|
||||
|
||||
<Tailwind>
|
||||
<Body>
|
||||
<EmailWrapper>
|
||||
<EmailContent>
|
||||
<EmailHeader>
|
||||
<EmailHeading>{subject}</EmailHeading>
|
||||
</EmailHeader>
|
||||
<Text className="text-[16px] leading-[24px] text-[#242424]">
|
||||
Tere
|
||||
</Text>
|
||||
|
||||
<Text>
|
||||
Broneeringu {reservationId} Connected Online'i saatmine ei
|
||||
õnnestunud, kliendile tuleb teha tagasimakse.
|
||||
</Text>
|
||||
<Text>Saadud error: {error}</Text>
|
||||
</EmailContent>
|
||||
</EmailWrapper>
|
||||
</Body>
|
||||
</Tailwind>
|
||||
</Html>,
|
||||
);
|
||||
|
||||
return {
|
||||
html,
|
||||
subject,
|
||||
};
|
||||
}
|
||||
@@ -10,3 +10,4 @@ export * from './emails/all-results-received.email';
|
||||
export * from './emails/order-processing.email';
|
||||
export * from './emails/patient-first-results-received.email';
|
||||
export * from './emails/patient-full-results-received.email';
|
||||
export * from './emails/book-time-failed.email';
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
"./personal-account-settings": "./src/components/personal-account-settings/index.ts",
|
||||
"./components": "./src/components/index.ts",
|
||||
"./hooks/*": "./src/hooks/*.ts",
|
||||
"./services/*": "./src/server/services/*.ts",
|
||||
"./api": "./src/server/api.ts",
|
||||
"./types/*": "./src/types/*.ts"
|
||||
},
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import type { AccountBalanceEntry } from '../../types/account-balance-entry';
|
||||
|
||||
export type AccountBalanceSummary = {
|
||||
totalBalance: number;
|
||||
expiringSoon: number;
|
||||
recentEntries: AccountBalanceEntry[];
|
||||
};
|
||||
|
||||
export class AccountBalanceService {
|
||||
private supabase: ReturnType<typeof getSupabaseServerClient>;
|
||||
|
||||
constructor() {
|
||||
this.supabase = getSupabaseServerClient();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current balance for a specific account
|
||||
*/
|
||||
async getAccountBalance(accountId: string): Promise<number> {
|
||||
const { data, error } = await this.supabase
|
||||
.schema('medreport')
|
||||
.rpc('get_account_balance', {
|
||||
p_account_id: accountId,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error('Error getting account balance:', error);
|
||||
throw new Error('Failed to get account balance');
|
||||
}
|
||||
|
||||
return data || 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get balance entries for an account with pagination
|
||||
*/
|
||||
async getAccountBalanceEntries(
|
||||
accountId: string,
|
||||
options: {
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
entryType?: string;
|
||||
includeInactive?: boolean;
|
||||
} = {}
|
||||
): Promise<{
|
||||
entries: AccountBalanceEntry[];
|
||||
total: number;
|
||||
}> {
|
||||
const { limit = 50, offset = 0, entryType, includeInactive = false } = options;
|
||||
|
||||
let query = this.supabase
|
||||
.schema('medreport')
|
||||
.from('account_balance_entries')
|
||||
.select('*', { count: 'exact' })
|
||||
.eq('account_id', accountId)
|
||||
.order('created_at', { ascending: false })
|
||||
.range(offset, offset + limit - 1);
|
||||
|
||||
if (!includeInactive) {
|
||||
query = query.eq('is_active', true);
|
||||
}
|
||||
|
||||
if (entryType) {
|
||||
query = query.eq('entry_type', entryType);
|
||||
}
|
||||
|
||||
const { data, error, count } = await query;
|
||||
|
||||
if (error) {
|
||||
console.error('Error getting account balance entries:', error);
|
||||
throw new Error('Failed to get account balance entries');
|
||||
}
|
||||
|
||||
return {
|
||||
entries: data || [],
|
||||
total: count || 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get balance summary for dashboard display
|
||||
*/
|
||||
async getBalanceSummary(accountId: string): Promise<AccountBalanceSummary> {
|
||||
const [balance, entries] = await Promise.all([
|
||||
this.getAccountBalance(accountId),
|
||||
this.getAccountBalanceEntries(accountId, { limit: 5 }),
|
||||
]);
|
||||
|
||||
// Calculate expiring balance (next 30 days)
|
||||
const thirtyDaysFromNow = new Date();
|
||||
thirtyDaysFromNow.setDate(thirtyDaysFromNow.getDate() + 30);
|
||||
|
||||
const { data: expiringData, error: expiringError } = await this.supabase
|
||||
.schema('medreport')
|
||||
.from('account_balance_entries')
|
||||
.select('amount')
|
||||
.eq('account_id', accountId)
|
||||
.eq('is_active', true)
|
||||
.not('expires_at', 'is', null)
|
||||
.lte('expires_at', thirtyDaysFromNow.toISOString());
|
||||
|
||||
if (expiringError) {
|
||||
console.error('Error getting expiring balance:', expiringError);
|
||||
}
|
||||
|
||||
const expiringSoon = expiringData?.reduce((sum, entry) => sum + (entry.amount || 0), 0) || 0;
|
||||
|
||||
return {
|
||||
totalBalance: balance,
|
||||
expiringSoon,
|
||||
recentEntries: entries.entries,
|
||||
};
|
||||
}
|
||||
|
||||
async processPeriodicBenefitDistributions(): Promise<void> {
|
||||
console.info('Processing periodic benefit distributions...');
|
||||
const { error } = await this.supabase.schema('medreport').rpc('process_periodic_benefit_distributions');
|
||||
if (error) {
|
||||
console.error('Error processing periodic benefit distributions:', error);
|
||||
throw new Error('Failed to process periodic benefit distributions');
|
||||
}
|
||||
console.info('Periodic benefit distributions processed successfully');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import type { Database } from '@kit/supabase/database';
|
||||
|
||||
export type AccountBalanceEntry = Database['medreport']['Tables']['account_balance_entries']['Row'];
|
||||
@@ -1,23 +1,25 @@
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
export type ApplicationRole =
|
||||
Database['medreport']['Tables']['accounts']['Row']['application_role'];
|
||||
export type ApplicationRole = Account['application_role'];
|
||||
export enum ApplicationRoleEnum {
|
||||
User = 'user',
|
||||
Doctor = 'doctor',
|
||||
SuperAdmin = 'super_admin',
|
||||
}
|
||||
|
||||
export type AccountWithParams =
|
||||
Database['medreport']['Tables']['accounts']['Row'] & {
|
||||
accountParams:
|
||||
| (Pick<
|
||||
Database['medreport']['Tables']['account_params']['Row'],
|
||||
'weight' | 'height'
|
||||
> & {
|
||||
isSmoker:
|
||||
| Database['medreport']['Tables']['account_params']['Row']['is_smoker']
|
||||
| null;
|
||||
})
|
||||
| null;
|
||||
};
|
||||
export type AccountParams =
|
||||
Database['medreport']['Tables']['account_params']['Row'];
|
||||
|
||||
export type Account = Database['medreport']['Tables']['accounts']['Row'];
|
||||
export type AccountWithParams = Account & {
|
||||
accountParams:
|
||||
| (Pick<AccountParams, 'weight' | 'height'> & {
|
||||
isSmoker: AccountParams['is_smoker'] | null;
|
||||
})
|
||||
| null;
|
||||
};
|
||||
|
||||
export type CompanyParams =
|
||||
Database['medreport']['Tables']['company_params']['Row'];
|
||||
|
||||
export type BmiThresholds = Database['medreport']['Tables']['bmi_thresholds']['Row'];
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
"devDependencies": {
|
||||
"@hookform/resolvers": "^5.0.1",
|
||||
"@kit/next": "workspace:*",
|
||||
"@kit/accounts": "workspace:*",
|
||||
"@kit/shared": "workspace:*",
|
||||
"@kit/supabase": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
|
||||
@@ -11,7 +11,7 @@ import { EllipsisVertical } from 'lucide-react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import type { Account } from '@kit/accounts/types/accounts';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Checkbox } from '@kit/ui/checkbox';
|
||||
import {
|
||||
@@ -44,8 +44,6 @@ import { AdminDeleteUserDialog } from './admin-delete-user-dialog';
|
||||
import { AdminImpersonateUserDialog } from './admin-impersonate-user-dialog';
|
||||
import { AdminResetPasswordDialog } from './admin-reset-password-dialog';
|
||||
|
||||
type Account = Database['medreport']['Tables']['accounts']['Row'];
|
||||
|
||||
const FiltersSchema = z.object({
|
||||
type: z.enum(['all', 'team', 'personal']),
|
||||
query: z.string().optional(),
|
||||
|
||||
@@ -4,10 +4,10 @@ import Link from 'next/link';
|
||||
|
||||
import { ColumnDef } from '@tanstack/react-table';
|
||||
|
||||
import { formatDateAndTime } from '@kit/shared/utils';
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import { DataTable } from '@kit/ui/enhanced-data-table';
|
||||
import { ProfileAvatar } from '@kit/ui/profile-avatar';
|
||||
import { formatDateAndTime } from '@kit/shared/utils';
|
||||
|
||||
type Memberships =
|
||||
Database['medreport']['Functions']['get_account_members']['Returns'][number];
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import { ApplicationRole } from '@kit/accounts/types/accounts';
|
||||
|
||||
const ConfirmationSchema = z.object({
|
||||
confirmation: z.custom<string>((value) => value === 'CONFIRM'),
|
||||
@@ -19,9 +19,7 @@ export const DeleteAccountSchema = ConfirmationSchema.extend({
|
||||
accountId: z.string().uuid(),
|
||||
});
|
||||
|
||||
type ApplicationRoleType =
|
||||
Database['medreport']['Tables']['accounts']['Row']['application_role'];
|
||||
export const UpdateAccountRoleSchema = z.object({
|
||||
accountId: z.string().uuid(),
|
||||
role: z.string() as z.ZodType<ApplicationRoleType>,
|
||||
role: z.string() as z.ZodType<ApplicationRole>,
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'server-only';
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import type { ApplicationRole } from '@kit/accounts/types/accounts';
|
||||
|
||||
export function createAdminAccountsService(client: SupabaseClient<Database>) {
|
||||
return new AdminAccountsService(client);
|
||||
@@ -25,7 +26,7 @@ class AdminAccountsService {
|
||||
|
||||
async updateRole(
|
||||
accountId: string,
|
||||
role: Database['medreport']['Tables']['accounts']['Row']['application_role'],
|
||||
role: ApplicationRole,
|
||||
) {
|
||||
const { error } = await this.adminClient
|
||||
.schema('medreport')
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
"@kit/supabase": "workspace:*",
|
||||
"@kit/tsconfig": "workspace:*",
|
||||
"@kit/ui": "workspace:*",
|
||||
"@kit/user-analyses": "workspace:*",
|
||||
"@makerkit/data-loader-supabase-core": "^0.0.10",
|
||||
"@makerkit/data-loader-supabase-nextjs": "^1.2.5",
|
||||
"@supabase/supabase-js": "2.49.4",
|
||||
|
||||
@@ -5,6 +5,7 @@ import { isBefore } from 'date-fns';
|
||||
import { renderDoctorSummaryReceivedEmail } from '@kit/email-templates';
|
||||
import { getFullName } from '@kit/shared/utils';
|
||||
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
import { createUserAnalysesApi } from '@kit/user-analyses/api';
|
||||
|
||||
import { sendEmailFromTemplate } from '../../../../../../../lib/services/mailer.service';
|
||||
import { AnalysisResultDetails } from '../schema/doctor-analysis-detail-view.schema';
|
||||
@@ -641,7 +642,14 @@ export async function submitFeedback(
|
||||
}
|
||||
|
||||
if (status === 'COMPLETED') {
|
||||
const [{ data: recipient }, { data: analysisOrder }] = await Promise.all([
|
||||
const { data: analysisOrder } = await supabase
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.select('medusa_order_id, id')
|
||||
.eq('id', analysisOrderId)
|
||||
.limit(1)
|
||||
.throwOnError();
|
||||
const [{ data: recipient }] = await Promise.all([
|
||||
supabase
|
||||
.schema('medreport')
|
||||
.from('accounts')
|
||||
@@ -649,19 +657,10 @@ export async function submitFeedback(
|
||||
.eq('is_personal_account', true)
|
||||
.eq('primary_owner_user_id', userId)
|
||||
.throwOnError(),
|
||||
supabase
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.select('medusa_order_id, id')
|
||||
.eq('id', analysisOrderId)
|
||||
.limit(1)
|
||||
.throwOnError(),
|
||||
supabase
|
||||
.schema('medreport')
|
||||
.from('analysis_orders')
|
||||
.update({ status: 'COMPLETED' })
|
||||
.eq('id', analysisOrderId)
|
||||
.throwOnError(),
|
||||
createUserAnalysesApi(supabase).updateAnalysisOrderStatus({
|
||||
orderId: analysisOrderId,
|
||||
orderStatus: 'COMPLETED',
|
||||
}),
|
||||
]);
|
||||
|
||||
if (!recipient?.[0]?.email) {
|
||||
|
||||
@@ -168,7 +168,12 @@ export async function addToCart({
|
||||
})
|
||||
.catch(medusaError);
|
||||
|
||||
return cart;
|
||||
const newCart = await getOrSetCart(countryCode);
|
||||
const addedItem = newCart.items?.filter(
|
||||
(item) => !cart.items?.some((oldCartItem) => oldCartItem.id === item.id),
|
||||
)?.[0];
|
||||
|
||||
return { newCart, addedItem };
|
||||
}
|
||||
|
||||
export async function updateLineItem({
|
||||
@@ -253,6 +258,37 @@ export async function setShippingMethod({
|
||||
.catch(medusaError);
|
||||
}
|
||||
|
||||
export async function initiateMultiPaymentSession(
|
||||
cart: HttpTypes.StoreCart,
|
||||
benefitsAmount: number,
|
||||
) {
|
||||
const headers = {
|
||||
...(await getAuthHeaders()),
|
||||
};
|
||||
|
||||
return sdk.client.fetch<unknown>(`/store/multi-payment`, {
|
||||
method: 'POST',
|
||||
body: { cartId: cart.id, benefitsAmount },
|
||||
headers,
|
||||
})
|
||||
.then(async (response) => {
|
||||
console.info('Payment session initiated:', response);
|
||||
const cartCacheTag = await getCacheTag('carts');
|
||||
revalidateTag(cartCacheTag);
|
||||
return response as {
|
||||
montonioPaymentSessionId: string | null;
|
||||
companyBenefitsPaymentSessionId: string | null;
|
||||
totalByBenefits: number;
|
||||
totalByMontonio: number;
|
||||
isFullyPaidByBenefits: boolean;
|
||||
};
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error('Error initiating payment session:', e, JSON.stringify(Object.keys(e)));
|
||||
return medusaError(e);
|
||||
});
|
||||
}
|
||||
|
||||
export async function initiatePaymentSession(
|
||||
cart: HttpTypes.StoreCart,
|
||||
data: HttpTypes.StoreInitializePaymentSession,
|
||||
|
||||
@@ -21,7 +21,6 @@ export const listCategories = async (query?: Record<string, any>) => {
|
||||
...query,
|
||||
},
|
||||
next,
|
||||
cache: 'force-cache',
|
||||
},
|
||||
)
|
||||
.then(({ product_categories }) => product_categories);
|
||||
@@ -57,7 +56,6 @@ export const getProductCategories = async ({
|
||||
limit,
|
||||
},
|
||||
next,
|
||||
//cache: "force-cache",
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@ import { HttpTypes } from '@medusajs/types';
|
||||
|
||||
import { getAuthHeaders, getCacheOptions } from './cookies';
|
||||
|
||||
export const retrieveOrder = async (id: string) => {
|
||||
export const retrieveOrder = async (id: string, allowCache = true) => {
|
||||
const headers = {
|
||||
...(await getAuthHeaders()),
|
||||
};
|
||||
@@ -24,7 +24,7 @@ export const retrieveOrder = async (id: string) => {
|
||||
},
|
||||
headers,
|
||||
next,
|
||||
cache: 'force-cache',
|
||||
...(allowCache ? { cache: 'force-cache' } : {}),
|
||||
})
|
||||
.then(({ order }) => order)
|
||||
.catch((err) => medusaError(err));
|
||||
@@ -61,11 +61,6 @@ export const listOrders = async (
|
||||
};
|
||||
|
||||
export const createTransferRequest = async (
|
||||
state: {
|
||||
success: boolean;
|
||||
error: string | null;
|
||||
order: HttpTypes.StoreOrder | null;
|
||||
},
|
||||
formData: FormData,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
|
||||
@@ -15,7 +15,7 @@ export const listRegions = async () => {
|
||||
.fetch<{ regions: HttpTypes.StoreRegion[] }>(`/store/regions`, {
|
||||
method: 'GET',
|
||||
next,
|
||||
cache: 'force-cache',
|
||||
// cache: 'force-cache',
|
||||
})
|
||||
.then(({ regions }) => regions)
|
||||
.catch(medusaError);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import Image from 'next/image';
|
||||
|
||||
import { useDismissNotification } from '@kit/notifications/hooks';
|
||||
import { Heading } from '@kit/ui/heading';
|
||||
import { If } from '@kit/ui/if';
|
||||
import { Separator } from '@kit/ui/separator';
|
||||
@@ -12,7 +11,7 @@ import { SignOutInvitationButton } from './sign-out-invitation-button';
|
||||
|
||||
export function AcceptInvitationContainer(props: {
|
||||
inviteToken: string;
|
||||
email: string;
|
||||
fullName: string;
|
||||
|
||||
invitation: {
|
||||
id: string;
|
||||
@@ -76,7 +75,7 @@ export function AcceptInvitationContainer(props: {
|
||||
/>
|
||||
|
||||
<InvitationSubmitButton
|
||||
email={props.email}
|
||||
fullName={props.fullName}
|
||||
accountName={props.invitation.account.name}
|
||||
/>
|
||||
</form>
|
||||
|
||||
@@ -7,7 +7,7 @@ import { Trans } from '@kit/ui/trans';
|
||||
|
||||
export function InvitationSubmitButton(props: {
|
||||
accountName: string;
|
||||
email: string;
|
||||
fullName: string;
|
||||
}) {
|
||||
const { pending } = useFormStatus();
|
||||
|
||||
@@ -17,7 +17,7 @@ export function InvitationSubmitButton(props: {
|
||||
i18nKey={pending ? 'teams:joiningTeam' : 'teams:continueAs'}
|
||||
values={{
|
||||
accountName: props.accountName,
|
||||
email: props.email,
|
||||
fullName: props.fullName,
|
||||
}}
|
||||
/>
|
||||
</Button>
|
||||
|
||||
@@ -20,6 +20,7 @@ import { If } from '@kit/ui/if';
|
||||
import { Input } from '@kit/ui/input';
|
||||
import { ProfileAvatar } from '@kit/ui/profile-avatar';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
import { formatCurrency } from '@kit/shared/utils';
|
||||
|
||||
import { RemoveMemberDialog } from './remove-member-dialog';
|
||||
import { RoleBadge } from './role-badge';
|
||||
@@ -42,6 +43,10 @@ type AccountMembersTableProps = {
|
||||
userRoleHierarchy: number;
|
||||
isPrimaryOwner: boolean;
|
||||
canManageRoles: boolean;
|
||||
membersBenefitsUsage: {
|
||||
personal_account_id: string;
|
||||
benefit_amount: number;
|
||||
}[];
|
||||
};
|
||||
|
||||
export function AccountMembersTable({
|
||||
@@ -51,6 +56,7 @@ export function AccountMembersTable({
|
||||
isPrimaryOwner,
|
||||
userRoleHierarchy,
|
||||
canManageRoles,
|
||||
membersBenefitsUsage,
|
||||
}: AccountMembersTableProps) {
|
||||
const [search, setSearch] = useState('');
|
||||
const { t } = useTranslation('teams');
|
||||
@@ -73,6 +79,7 @@ export function AccountMembersTable({
|
||||
currentUserId,
|
||||
currentAccountId,
|
||||
currentRoleHierarchy: userRoleHierarchy,
|
||||
membersBenefitsUsage,
|
||||
});
|
||||
|
||||
const filteredMembers = members
|
||||
@@ -122,9 +129,13 @@ function useGetColumns(
|
||||
currentUserId: string;
|
||||
currentAccountId: string;
|
||||
currentRoleHierarchy: number;
|
||||
membersBenefitsUsage: {
|
||||
personal_account_id: string;
|
||||
benefit_amount: number;
|
||||
}[];
|
||||
},
|
||||
): ColumnDef<Members[0]>[] {
|
||||
const { t } = useTranslation('teams');
|
||||
const { t, i18n: { language } } = useTranslation('teams');
|
||||
|
||||
return useMemo(
|
||||
() => [
|
||||
@@ -168,6 +179,23 @@ function useGetColumns(
|
||||
return row.original.personal_code ?? '-';
|
||||
},
|
||||
},
|
||||
{
|
||||
header: t('distributedBenefitsAmount'),
|
||||
cell: ({ row }) => {
|
||||
const benefitAmount = params.membersBenefitsUsage.find(
|
||||
(usage) => usage.personal_account_id === row.original.id
|
||||
)?.benefit_amount;
|
||||
if (typeof benefitAmount !== 'number') {
|
||||
return '-';
|
||||
}
|
||||
|
||||
return formatCurrency({
|
||||
currencyCode: 'EUR',
|
||||
locale: language,
|
||||
value: benefitAmount,
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
header: t('roleLabel'),
|
||||
cell: ({ row }) => {
|
||||
@@ -175,7 +203,7 @@ function useGetColumns(
|
||||
const isPrimaryOwner = primary_owner_user_id === user_id;
|
||||
|
||||
return (
|
||||
<span className={'flex items-center space-x-1'}>
|
||||
<span className={'flex items-center space-x-1 flex-wrap space-y-1 sm:space-y-0 sm:flex-nowrap'}>
|
||||
<RoleBadge role={role} />
|
||||
|
||||
<If condition={isPrimaryOwner}>
|
||||
|
||||
@@ -13,6 +13,8 @@ import { TeamAccountDangerZone } from './team-account-danger-zone';
|
||||
import { UpdateTeamAccountImage } from './update-team-account-image-container';
|
||||
import { UpdateTeamAccountNameForm } from './update-team-account-name-form';
|
||||
|
||||
const SHOW_TEAM_LOGO = false as boolean;
|
||||
|
||||
export function TeamAccountSettingsContainer(props: {
|
||||
account: {
|
||||
name: string;
|
||||
@@ -32,21 +34,23 @@ export function TeamAccountSettingsContainer(props: {
|
||||
}) {
|
||||
return (
|
||||
<div className={'flex w-full flex-col space-y-4'}>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>
|
||||
<Trans i18nKey={'teams:settings.teamLogo'} />
|
||||
</CardTitle>
|
||||
{SHOW_TEAM_LOGO && (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>
|
||||
<Trans i18nKey={'teams:settings.teamLogo'} />
|
||||
</CardTitle>
|
||||
|
||||
<CardDescription>
|
||||
<Trans i18nKey={'teams:settings.teamLogoDescription'} />
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardDescription>
|
||||
<Trans i18nKey={'teams:settings.teamLogoDescription'} />
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent>
|
||||
<UpdateTeamAccountImage account={props.account} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
<CardContent>
|
||||
<UpdateTeamAccountImage account={props.account} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
|
||||
@@ -18,6 +18,7 @@ import { RenewInvitationSchema } from '../../schema/renew-invitation.schema';
|
||||
import { UpdateInvitationSchema } from '../../schema/update-invitation.schema';
|
||||
import { createAccountInvitationsService } from '../services/account-invitations.service';
|
||||
import { createAccountPerSeatBillingService } from '../services/account-per-seat-billing.service';
|
||||
import { AccountBalanceService } from '@kit/accounts/services/account-balance.service';
|
||||
|
||||
/**
|
||||
* @name createInvitationsAction
|
||||
@@ -148,6 +149,7 @@ export const updateInvitationAction = enhanceAction(
|
||||
export const acceptInvitationAction = enhanceAction(
|
||||
async (data: FormData, user) => {
|
||||
const client = getSupabaseServerClient();
|
||||
const accountBalanceService = new AccountBalanceService();
|
||||
|
||||
const { inviteToken, nextPath } = AcceptInvitationSchema.parse(
|
||||
Object.fromEntries(data),
|
||||
@@ -171,6 +173,9 @@ export const acceptInvitationAction = enhanceAction(
|
||||
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
|
||||
await perSeatBillingService.increaseSeats(accountId);
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
type Account = Database['medreport']['Tables']['accounts']['Row'];
|
||||
import type { Account } from '@kit/accounts/types/accounts';
|
||||
|
||||
export function createAccountWebhooksService() {
|
||||
return new AccountWebhooksService();
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { UuringuVastus } from '@kit/shared/types/medipost-analysis';
|
||||
import { toArray } from '@kit/shared/utils';
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
import type { AnalysisOrder } from '../types/analysis-orders';
|
||||
import type { AnalysisOrder, AnalysisOrderStatus } from '../types/analysis-orders';
|
||||
import type {
|
||||
AnalysisResultDetailsElement,
|
||||
AnalysisResultDetailsMapped,
|
||||
@@ -450,6 +450,32 @@ class UserAnalysesApi {
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
async updateAnalysisOrderStatus({
|
||||
orderId,
|
||||
medusaOrderId,
|
||||
orderStatus,
|
||||
}: {
|
||||
orderId?: number;
|
||||
medusaOrderId?: string;
|
||||
orderStatus: AnalysisOrderStatus;
|
||||
}) {
|
||||
const orderIdParam = orderId;
|
||||
const medusaOrderIdParam = medusaOrderId;
|
||||
|
||||
console.info(`Updating order id=${orderId} medusaOrderId=${medusaOrderId} status=${orderStatus}`);
|
||||
if (!orderIdParam && !medusaOrderIdParam) {
|
||||
throw new Error('Either orderId or medusaOrderId must be provided');
|
||||
}
|
||||
await this.client
|
||||
.schema('medreport')
|
||||
.rpc('update_analysis_order_status', {
|
||||
order_id: orderIdParam ?? -1,
|
||||
status_param: orderStatus,
|
||||
medusa_order_id_param: medusaOrderIdParam ?? '',
|
||||
})
|
||||
.throwOnError();
|
||||
}
|
||||
}
|
||||
|
||||
export function createUserAnalysesApi(client: SupabaseClient<Database>) {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Tables } from '@kit/supabase/database';
|
||||
|
||||
export type AnalysisOrder = Tables<{ schema: 'medreport' }, 'analysis_orders'>;
|
||||
export type AnalysisOrderStatus = AnalysisOrder['status'];
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { adminNavigationConfig } from './admin-navigation.config';
|
||||
import appConfig from './app.config';
|
||||
import authConfig from './auth.config';
|
||||
import billingConfig from './billing.config';
|
||||
import { adminNavigationConfig } from './admin-navigation.config';
|
||||
import {
|
||||
DynamicAuthConfig,
|
||||
getCachedAuthConfig,
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user