server v1.0.0

This commit is contained in:
2020-09-16 17:34:24 +02:00
parent a5232e7257
commit 27552e5eb0
13 changed files with 187 additions and 150 deletions

View File

@@ -35,7 +35,7 @@ router.post("/", async (req, res) => {
try {
const token = req.headers["x-auth-token"];
const user = validateUserToken(token);
const restaurant = createRestaurant(req.body);
const restaurant = await createRestaurant(req.body);
await restaurant.save();
await addRestaurantToUser(user, restaurant);
res.sendStatus(201);
@@ -44,6 +44,21 @@ router.post("/", async (req, res) => {
}
});
// UPDATE RESTAURANT
router.put("/", async (req, res) => {
try {
const token = req.headers["x-auth-token"];
const user = validateUserToken(token);
const oldRestaurant = await fetchRestaurant(req.body.restaurantId);
const newRestaurant = await createRestaurant(req.body, oldRestaurant);
await Restaurant.replaceOne({ _id: req.body.restaurantId }, newRestaurant);
res.send("Dane zostały zaktualizowane.");
} catch (error) {
handleError(error, res);
}
});
// GET ALL DISHES FROM A RESTAURANT ID
router.get("/dishes", async (req, res) => {

View File

@@ -1,8 +1,7 @@
import express from "express";
import * as services from "../services/services.js";
import Restaurant from "../models/restaurant.js";
import sanitizer from "string-sanitizer";
import restaurant from "../models/restaurant.js";
import { handleError } from "../services/services.js";
var router = express.Router();
@@ -18,9 +17,10 @@ router.get("/", (req, res) => {
$and: [
{ $or: [{ city: { $regex: regex } }, { name: { $regex: regex } }] },
{ hidden: false },
{ subscriptionActive: true },
],
},
"_id name city imgUrl workingHours description tags phone hidden",
"_id name city imgUrl workingHours description tags location links",
(err, results) => {
if (err) {
res.sendStatus(500);
@@ -36,6 +36,24 @@ router.get("/", (req, res) => {
}
});
// SEARCH RESTAURANTS BY LOCATION
router.get("/location", async (req, res) => {
try {
const location = {
coordinates: [req.query.lon, req.query.lat],
type: "Point",
};
const radius = parseInt(req.query.radius) * 1000;
const results = await Restaurant.find({
location: { $near: { $maxDistance: radius, $geometry: location } },
});
res.send(results);
} catch (error) {
handleError(error, res);
}
});
// AUTOCOMPLETE
router.get("/autocomplete/", (req, res) => {

View File

@@ -1,14 +1,16 @@
import express from "express";
import * as services from "../services/services.js";
import * as databaseServices from "../services/databaseServices.js";
var router = express.Router();
router.post("/", async (req, res) => {
try {
const decodedToken = services.validateUserToken(
req.headers["x-auth-token"]
const newDate = await databaseServices.renewSubscription(
req.body.restaurantId,
1
);
res.send(decodedToken);
res.send(`Subskrypcja przedłużona do: ${newDate}`);
} catch (error) {
services.handleError(error, res);
}

View File

@@ -1,10 +1,6 @@
import express from "express";
import { changeUserPass, fetchUser } from "../services/databaseServices.js";
import {
composeNewContact,
createUser,
prepareSafeUser,
} from "../services/dataPrepServices.js";
import { createUser, prepareSafeUser } from "../services/dataPrepServices.js";
import {
newError,
handleError,
@@ -14,13 +10,9 @@ import {
validateUserToken,
hashPass,
} from "../services/services.js";
import * as config from "../config/index.js";
import AgileCRMManager from "agile_crm";
import { resetPassword } from "../services/mailServices.js";
const { CRM_USER, CRM_EMAIL, CRM_KEY } = config;
var router = express.Router();
var agileAPI = new AgileCRMManager(CRM_USER, CRM_KEY, CRM_EMAIL);
// LOGIN
router.post("/login", async (req, res) => {
@@ -43,9 +35,9 @@ router.post("/register", async (req, res) => {
try {
await checkEmailTaken(req.body.email);
const user = await createUser(req);
await user.save();
const contact = composeNewContact(user);
agileAPI.contactAPI.add(contact, null, null);
await user.save().catch((e) => {
throw newError("Niewłaściwe dane.", 500);
});
res.sendStatus(201);
} catch (e) {
handleError(e, res);