added API call to CRM when user registers

This commit is contained in:
2020-08-05 18:44:38 +02:00
parent f2bee18131
commit 91cba76bff
11 changed files with 140 additions and 36 deletions

View File

@@ -35,6 +35,9 @@ const dishSchema = mongoose.Schema({
peanuts: Boolean, peanuts: Boolean,
sesame: Boolean, sesame: Boolean,
}, },
ingredients: {
type: Array,
},
vegan: Boolean, vegan: Boolean,
vegetarian: Boolean, vegetarian: Boolean,
}); });

View File

@@ -32,6 +32,12 @@ const restaurantSchema = mongoose.Schema({
alcohol: Boolean, alcohol: Boolean,
delivery: Boolean, delivery: Boolean,
}, },
links: {
facebook: String,
twitter: String,
instagram: String,
www: String,
},
phone: Number, phone: Number,
hidden: Boolean, hidden: Boolean,
dishes: [mongoose.Types.ObjectId], dishes: [mongoose.Types.ObjectId],

View File

@@ -10,12 +10,23 @@ const userSchema = mongoose.Schema({
type: String, type: String,
required: true, required: true,
}, },
firstname: {
type: String,
required: true,
},
lastname: {
type: String,
required: true,
},
restaurantId: mongoose.Types.ObjectId, restaurantId: mongoose.Types.ObjectId,
subscriptionActive: { subscriptionActive: {
type: Boolean, type: Boolean,
required: true, required: true,
}, },
subscriptionDue: Date, subscriptionDue: {
type: String,
required: true,
},
}); });
export default mongoose.model("User", userSchema); export default mongoose.model("User", userSchema);

5
package-lock.json generated
View File

@@ -139,6 +139,11 @@
"negotiator": "0.6.2" "negotiator": "0.6.2"
} }
}, },
"agile_crm": {
"version": "1.2.5",
"resolved": "https://registry.npmjs.org/agile_crm/-/agile_crm-1.2.5.tgz",
"integrity": "sha1-gj+s18LUpzesJ2viFc9t6wwylVg="
},
"ansi-align": { "ansi-align": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/ansi-align/-/ansi-align-3.0.0.tgz", "resolved": "https://registry.npmjs.org/ansi-align/-/ansi-align-3.0.0.tgz",

View File

@@ -12,6 +12,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@material-ui/core": "^4.11.0", "@material-ui/core": "^4.11.0",
"agile_crm": "^1.2.5",
"bcrypt": "^5.0.0", "bcrypt": "^5.0.0",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"cookie-parser": "^1.4.5", "cookie-parser": "^1.4.5",

View File

@@ -5,7 +5,7 @@ import mongoose from "mongoose";
var router = express.Router(); var router = express.Router();
router.get("/", (req, res) => { router.get("/", (req, res) => {
Restaurant.find({ city: req.body.city }, (err, data) => { Restaurant.find({ city: req.query.city }, (err, data) => {
if (err) { if (err) {
res.sendStatus(404); res.sendStatus(404);
} else res.send(data); } else res.send(data);

View File

@@ -9,7 +9,7 @@ var router = express.Router();
// GET DISH BY ID // GET DISH BY ID
router.get("/", (req, res) => { router.get("/", (req, res) => {
Dish.findById(req.body.dishId, (err, data) => { Dish.findById(req.query.dishId, (err, data) => {
if (err) { if (err) {
res.sendStatus(404); res.sendStatus(404);
} else } else

View File

@@ -48,6 +48,10 @@ router.post("/", (req, res) => {
city: sanitizer.sanitize.keepUnicode(req.body.city), city: sanitizer.sanitize.keepUnicode(req.body.city),
imgUrl: services.saveImage(req.cookies["img"]), imgUrl: services.saveImage(req.cookies["img"]),
workingHours: req.body.workingHours, workingHours: req.body.workingHours,
description: sanitizer.sanitize.keepUnicode(req.body.description),
tags: req.body.tags,
links: req.body.links,
phone: req.body.phone,
hidden: req.body.hidden, hidden: req.body.hidden,
}); });
restaurant.save((err) => { restaurant.save((err) => {

View File

@@ -50,7 +50,7 @@ router.get("/autocomplete/", (req, res) => {
"name city", "name city",
(err, doc) => { (err, doc) => {
if (err) { if (err) {
res.send(404); res.sendStatus(404);
} else { } else {
doc.forEach((value) => { doc.forEach((value) => {
cities.add(value.city); cities.add(value.city);

View File

@@ -4,9 +4,17 @@ import User from "../models/users.js";
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
import * as services from "../services/services.js"; import * as services from "../services/services.js";
import * as config from "../config/index.js"; import * as config from "../config/index.js";
import AgileCRMManager from "agile_crm";
const { API_KEY, jwtSecret } = config; const { API_KEY, jwtSecret } = config;
var router = express.Router(); var router = express.Router();
var agileAPI = new AgileCRMManager("bankai", API_KEY, "bankai@bankai.pl");
var success = function (data) {
console.log("Task successfull");
};
var error = function (err) {
console.log("Task failed successfully");
};
router.post("/login", (req, res) => { router.post("/login", (req, res) => {
if (req.body.password && req.body.email) { if (req.body.password && req.body.email) {
@@ -19,12 +27,16 @@ router.post("/login", (req, res) => {
err, err,
result result
) { ) {
if (err) {
res.sendStatus(500);
} else {
if (result) { if (result) {
var token = services.generateAuthToken(user); var token = services.generateAuthToken(user);
res.header("x-auth-token", token).status(202).send(); res.header("x-auth-token", token).status(202).send();
} else { } else {
res.sendStatus(401); res.sendStatus(401);
} }
}
}); });
} }
}); });
@@ -49,7 +61,6 @@ router.post("/check", (req, res) => {
}); });
router.post("/register", (req, res) => { router.post("/register", (req, res) => {
if (req.body.key === API_KEY) {
services.checkEmailTaken(req.body.email, (result) => { services.checkEmailTaken(req.body.email, (result) => {
if (result) { if (result) {
res.sendStatus(409); res.sendStatus(409);
@@ -59,24 +70,23 @@ router.post("/register", (req, res) => {
_id: new mongoose.Types.ObjectId(), _id: new mongoose.Types.ObjectId(),
email: req.body.email, email: req.body.email,
password: hashedPass, password: hashedPass,
subscriptionActive: req.body.subscriptionActive, firstname: req.body.firstname,
subscriptionDue: services.dueDateBasedOnSubscription( lastname: req.body.lastname,
req.body.subscriptionActive subscriptionActive: true,
), subscriptionDue: services.halfYearFromNowDate(),
}); });
user.save((err) => { user.save((err) => {
if (err) { if (err) {
res.sendStatus(500); res.sendStatus(500);
} else { } else {
const contact = services.composeNewContact(user);
agileAPI.contactAPI.add(contact, success, error);
res.sendStatus(201); res.sendStatus(201);
} }
}); });
}); });
} }
}); });
} else {
res.sendStatus(404);
}
}); });
export default router; export default router;

View File

@@ -96,6 +96,7 @@ export function createDish(dish, cookie, generateId) {
peanuts: dish.allergens.peanuts, peanuts: dish.allergens.peanuts,
sesame: dish.allergens.sesame, sesame: dish.allergens.sesame,
}, },
ingredients: dish.ingredients,
vegan: dish.vegan, vegan: dish.vegan,
vegetarian: dish.vegetarian, vegetarian: dish.vegetarian,
}); });
@@ -117,6 +118,7 @@ export function createDish(dish, cookie, generateId) {
peanuts: dish.allergens.peanuts, peanuts: dish.allergens.peanuts,
sesame: dish.allergens.sesame, sesame: dish.allergens.sesame,
}, },
ingredients: dish.ingredients,
vegan: dish.vegan, vegan: dish.vegan,
vegetarian: dish.vegetarian, vegetarian: dish.vegetarian,
}); });
@@ -162,6 +164,18 @@ export function yearFromNowDate() {
return date.addDays(365); return date.addDays(365);
} }
export function halfYearFromNowDate() {
Date.prototype.addDays = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
var nowDate = new Date();
var resultDate = nowDate.addDays(183);
return toShortDate(resultDate);
}
export function hashPass(pass, callback) { export function hashPass(pass, callback) {
bcrypt.genSalt(10, (err, salt) => { bcrypt.genSalt(10, (err, salt) => {
if (err) callback(false); if (err) callback(false);
@@ -177,8 +191,58 @@ export function hashPass(pass, callback) {
export function dueDateBasedOnSubscription(subscriptionActive) { export function dueDateBasedOnSubscription(subscriptionActive) {
if (subscriptionActive === true) { if (subscriptionActive === true) {
return yearFromNowDate(); return halfYearFromNowDate();
} else { } else {
return new Date(); return new Date();
} }
} }
export function composeNewContact(request) {
const dateNow = new Date();
const contact = {
lead_score: "100",
tags: ["newUser"],
properties: [
{
type: "SYSTEM",
name: "first_name",
value: request.firstname,
},
{
type: "SYSTEM",
name: "last_name",
value: request.lastname,
},
{
type: "SYSTEM",
name: "email",
subtype: "work",
value: request.email,
},
{
type: "CUSTOM",
name: "UserID",
value: request._id,
},
{
type: "CUSTOM",
name: "Subscription Started",
value: toShortDate(dateNow),
},
{
type: "CUSTOM",
name: "Subscription Due",
value: request.subscriptionDue,
},
],
};
return contact;
}
function toShortDate(date) {
const shortDate =
date.getMonth() + "/" + date.getDay() + "/" + date.getFullYear();
return shortDate;
}