oralify-backend
Version:
Express Node CRUD backend for Oralify
105 lines (89 loc) • 3.25 kB
text/typescript
import {Request, Response} from 'express';
import {supabase} from '../supabase';
// import { StreamChat } from "stream-chat";
import dotenv from 'dotenv';
import {SpecialityKey} from '../types/serviceType';
dotenv.config();
// const apikey = process.env.GETSTREAM_KEY as string;
// const apiSecret = process.env.GETSTREAM_SECRET as string;
// const serverClient = new StreamVideoServerClient(apikey, {secret:apiSecret});
/*const createVideocall = async (clientId:string, expertId:string, speciality:string) => {
// const channelID = `video-call-${clientId}-${expertId}`;
// const channel = chatClient.channel('messaging', channelID, {
// name: `video-call-${speciality}`,
// })
// await channel.create()
// await channel.addMembers([clientId, expertId])
// return channel
const exp = Math.round(new Date().getTime() / 1000) + 60 * 60;
const iat = Math.round(new Date().getTime() / 1000);
serverClient.createToken(expertId, exp, iat);
const callType = 'default';
const callId = speciality;
const call = serverClient.call(callType, callId);
call.create({ data: { created_by_id: expertId } })
// call.create({
// data: {
// created_by_id: expertId,
// members: [{ user_id: expertId, role: 'admin' }, { user_id: clientId }],
// },
// });
return call
}*/
export const createAppointment = async (
req: Request,
res: Response,
): Promise<void> => {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const milliseconds = String(now.getMilliseconds()).padStart(3, '0');
const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}+00`;
try {
const {clientId, expertId, startTime, endTime, speciality} = req.body;
const data = {
clientid: clientId,
expertid: expertId,
starttime: startTime,
endtime: endTime,
status: 'pending',
createtime: formattedDate,
speciality,
};
if (!Object.values(SpecialityKey).includes(speciality)) {
res
.status(400)
.json({
error: 'Invalid speciality',
message:
'Expected one of: ' + Object.values(SpecialityKey).join(', '),
});
return;
}
if (!clientId || !expertId || !startTime || !endTime) {
res.status(404).send('data is missing to create the appointment');
return;
}
const {error: errorReservation, data: dataReservation} = await supabase
.from('reservations')
.insert([data])
.select();
if (errorReservation) {
res.status(500).json({errorReservation});
}
// const channel = await createVideocall(clientId, expertId, speciality);
// const responseData = {
// ...data,
// channelId: channel.id,
// tokenClient: chatClient.createToken(clientId),
// tokenExpert: chatClient.createToken(expertId),
// }
res.status(200).json(dataReservation);
} catch (err) {
res.status(500).json({err});
}
};