Migrated from Redmine #1310 | Author: Ilya Radu
Status: New | Priority: Normal | Created: 2024-11-22
When I try to create a booking through the SimplyBook API, there is an error related to the client’s email value.
and gives the following error: Client email value is wrong.
// server.js
const express = require("express");
const axios = require("axios");
const dotenv = require("dotenv");
const cors = require("cors");
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors());
const API_KEY = process.env.SIMPLYBOOK_API_KEY;
const COMPANY_LOGIN = process.env.COMPANY_LOGIN;
const LOGIN_URL = "https://user-api.simplybook.me/login";
const API_URL = "https://user-api.simplybook.me";
const getAuthToken = async () => {
try {
const response = await axios.post(
LOGIN_URL,
{
method: "getToken",
params: {
companyLogin: COMPANY_LOGIN,
apiKey: API_KEY,
},
id: 1,
},
{
headers: {
"Content-Type": "application/json",
},
},
);
if (response.data && response.data.result) {
const token = response.data.result;
return token;
} else {
throw new Error("Failed to obtain authorization token from API response");
}
} catch (error) {
throw error;
}
};
app.get("/api/services", async (req, res) => {
try {
const token = await getAuthToken();
const response = await axios.post(
API_URL,
{
method: "getEventList",
params: {},
id: 1,
},
{
headers: {
"Content-Type": "application/json",
"X-Company-Login": COMPANY_LOGIN,
"X-Token": token,
},
},
);
if (response.data.error) {
return res.status(500).json({ message: "Error fetching services" });
}
res.status(200).json(response.data.result);
} catch (error) {
res.status(500).json({ message: "Error fetching services" });
}
});
app.get("/api/performers", async (req, res) => {
try {
const token = await getAuthToken();
const response = await axios.post(
API_URL,
{
method: "getUnitList",
params: {},
id: 1,
},
{
headers: {
"Content-Type": "application/json",
"X-Company-Login": COMPANY_LOGIN,
"X-Token": token,
},
},
);
if (response.data.error) {
return res.status(500).json({ message: "Error fetching performers" });
}
res.status(200).json(response.data.result);
} catch (error) {
res.status(500).json({ message: "Error fetching performers" });
}
});
app.get("/api/available-dates", async (req, res) => {
const { performerId } = req.query;
try {
const token = await getAuthToken();
const response = await axios.post(
API_URL,
{
method: "getFirstWorkingDay",
params: [performerId || null],
id: 1,
},
{
headers: {
"Content-Type": "application/json",
"X-Company-Login": COMPANY_LOGIN,
"X-Token": token,
},
},
);
if (response.data.error) {
return res.status(500).json({ message: "Error fetching available dates" });
}
res.status(200).json({ firstAvailableDate: response.data.result });
} catch (error) {
res.status(500).json({ message: "Error fetching available dates" });
}
});
app.get("/api/available-times", async (req, res) => {
const { date, serviceId, performerId } = req.query;
if (!date || !serviceId) {
return res.status(400).json({ message: "Parameters date and serviceId are required" });
}
try {
const token = await getAuthToken();
const response = await axios.post(
API_URL,
{
method: "getStartTimeMatrix",
params: [date, date, serviceId, performerId || null, 1],
id: 1,
},
{
headers: {
"Content-Type": "application/json",
"X-Company-Login": COMPANY_LOGIN,
"X-Token": token,
},
},
);
if (response.data.error) {
return res.status(500).json({ message: "Error fetching available times" });
}
const times = response.data.result[date] || [];
res.status(200).json({ availableTimes: times });
} catch (error) {
res.status(500).json({ message: "Error fetching available times" });
}
};
const addClient = async (client, clientData) => {
const response = await client.post("/", {
method: "addClient",
params: clientData,
id: 1,
});
if (response.data.error) {
throw new Error(response.data.error.message);
}
return response.data.result;
};
app.post("/api/booking", async (req, res) => {
const { name, email, phone, serviceId, performerId, date, time, additionalFields } = req.body;
if (!name || !email || !phone || !serviceId || !date || !time) {
return res.status(400).json({ message: "Invalid data" });
}
let additionalFieldValues = additionalFields || {};
try {
const token = await getAuthToken();
const client = axios.create({
baseURL: API_URL,
headers: {
"Content-Type": "application/json",
"X-Company-Login": COMPANY_LOGIN,
"X-Token": token,
},
});
const clientResult = await addClient(client, { name, email, phone });
const clientId = clientResult.id;
const bookingRequest = {
method: "book",
params: [
serviceId,
performerId || null,
`${date} ${time}`,
clientId,
additionalFieldValues,
1,
],
id: 1,
};
const bookingResponse = await client.post("/", bookingRequest);
if (bookingResponse.data.error) {
return res.status(500).json({
message: "Error during booking",
error: bookingResponse.data.error,
});
}
res.status(200).json({
message: "Booking successfully completed",
data: bookingResponse.data.result,
});
} catch (error) {
res.status(500).json({ message: "Error during booking", error: error.message });
}
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {});