oralify-backend
Version:
Express Node CRUD backend for Oralify
73 lines (61 loc) • 1.73 kB
text/typescript
import {Request, Response} from 'express';
import {supabase} from '../supabase';
import {
AvailabilitySettingsType,
DayKey,
RecurrenceKey,
} from '../types/availabilityExpertTypes';
export const setAvailabilityExpert = async (
req: Request,
res: Response,
): Promise<void> => {
const {expertId, startTime, endTime, dayOfWeek, recurrence} = req.body;
if (!expertId) {
res.status(404).json({error: 'expertId required'});
return;
}
if (!startTime) {
res.status(404).json({error: 'startTime required'});
return;
}
if (!endTime) {
res.status(404).json({error: 'endTime required'});
return;
}
if (!Object.values(DayKey).includes(dayOfWeek)) {
res.status(400).json({
error: 'Invalid format',
message: 'Expected one of: ' + Object.values(DayKey).join(', '),
});
return;
}
if (!Object.values(RecurrenceKey).includes(recurrence)) {
res.status(400).json({
error: 'Invalid format',
message: 'Expected one of: ' + Object.values(RecurrenceKey).join(', '),
});
return;
}
/** TODO
* Generate timeslots from Avaialbity
* 1. Join tables to get all the services, and for each get the timeslots, adding splitTime if exists*/
/*const recceivedTimeslots = [{}]*/
const body: AvailabilitySettingsType = {
expertId,
startTime,
endTime,
dayOfWeek,
recurrence,
isAvailable: true,
};
const {error: availabilityError, data: availabilityData} = await supabase
.from('availabilityExperts')
.insert([body])
.select();
if (availabilityError) {
res.status(500).json({availabilityError});
}
res
.status(200)
.json({message: 'AvailabilityExpert created', availabilityData});
};