oralify-backend
Version:
Express Node CRUD backend for Oralify
74 lines (63 loc) • 1.65 kB
text/typescript
import {Request, Response} from 'express';
import {supabase} from '../supabase';
import {
ServiceFormatKey,
ServiceType,
SpecialityKey,
} from '../types/serviceType';
export const setService = async (
req: Request,
res: Response,
): Promise<void> => {
try {
const {expertId, speciality, description, price, duration, format, name} =
req.body;
if (!Object.values(SpecialityKey).includes(speciality)) {
res.status(400).json({
error: 'Invalid speciality',
message: 'Expected one of: ' + Object.values(SpecialityKey).join(', '),
});
return;
}
if (!expertId) {
res.status(404).json({error: 'expertId required'});
return;
}
if (!price) {
res.status(404).json({error: 'price required'});
return;
}
if (!duration) {
res.status(404).json({error: 'duration required'});
return;
}
if (!Object.values(ServiceFormatKey).includes(format)) {
res.status(400).json({
error: 'Invalid format',
message:
'Expected one of: ' + Object.values(ServiceFormatKey).join(', '),
});
return;
}
const service: ServiceType = {
expertId,
speciality,
description,
price,
duration,
format,
name,
};
const {error: serviceError, data: serviceData} = await supabase
.from('services')
.insert([service])
.select();
if (serviceError) {
res.status(500).json({serviceError});
return;
}
res.status(200).json({message: 'Service created', serviceData});
} catch (err) {
res.status(500).json({err});
}
};