add cart functionality for tto services
This commit is contained in:
@@ -11,7 +11,6 @@ import {
|
||||
} from '@/lib/types/connected-online';
|
||||
import { ExternalApi } from '@/lib/types/external';
|
||||
import { Tables } from '@/packages/supabase/src/database.types';
|
||||
import { StoreOrder } from '@medusajs/types';
|
||||
import axios from 'axios';
|
||||
import { uniq, uniqBy } from 'lodash';
|
||||
|
||||
@@ -20,14 +19,15 @@ 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';
|
||||
import { handleDeleteCartItem } from './medusaCart.service';
|
||||
|
||||
export async function getAvailableAppointmentsForService(
|
||||
serviceId: number,
|
||||
key: string,
|
||||
locationId: number | null,
|
||||
startTime?: Date,
|
||||
maxDays?: number
|
||||
) {
|
||||
try {
|
||||
const start = startTime ? { StartTime: startTime } : {};
|
||||
@@ -41,7 +41,7 @@ export async function getAvailableAppointmentsForService(
|
||||
ServiceID: serviceId,
|
||||
Key: key,
|
||||
Lang: 'et',
|
||||
MaxDays: 120,
|
||||
MaxDays: maxDays ?? 120,
|
||||
LocationId: locationId ?? -1,
|
||||
...start,
|
||||
}),
|
||||
@@ -202,8 +202,8 @@ export async function bookAppointment(
|
||||
},
|
||||
param: JSON.stringify({
|
||||
ClinicID: clinic.id,
|
||||
ServiceID: service.id,
|
||||
ClinicServiceID: service.sync_id,
|
||||
ServiceID: service.sync_id,
|
||||
ClinicServiceID: service.id,
|
||||
UserID: appointmentUserId,
|
||||
SyncUserID: syncUserID,
|
||||
StartTime: startTime,
|
||||
@@ -416,102 +416,3 @@ export async function getAvailableTimeSlotsForDisplay(
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
export async function createInitialReservation(
|
||||
serviceId: number,
|
||||
clinicId: number,
|
||||
appointmentUserId: number,
|
||||
syncUserID: number,
|
||||
startTime: Date,
|
||||
medusaLineItemId: string,
|
||||
locationId?: number | null,
|
||||
comments = '',
|
||||
) {
|
||||
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({
|
||||
medusaOrder,
|
||||
}: {
|
||||
medusaOrder: StoreOrder;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
|
||||
const ttoReservationIds: number[] =
|
||||
medusaOrder.items
|
||||
?.filter(({ metadata }) => !!metadata?.connectedOnlineReservationId)
|
||||
.map(({ metadata }) => Number(metadata!.connectedOnlineReservationId)) ??
|
||||
[];
|
||||
|
||||
const { data: orderedTtoServices } = await supabase
|
||||
.schema('medreport')
|
||||
.from('connected_online_reservation')
|
||||
.select('*')
|
||||
.in('id', ttoReservationIds)
|
||||
.throwOnError();
|
||||
|
||||
return orderedTtoServices;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user