added API call to CRM when user registers
This commit is contained in:
@@ -35,6 +35,9 @@ const dishSchema = mongoose.Schema({
|
||||
peanuts: Boolean,
|
||||
sesame: Boolean,
|
||||
},
|
||||
ingredients: {
|
||||
type: Array,
|
||||
},
|
||||
vegan: Boolean,
|
||||
vegetarian: Boolean,
|
||||
});
|
||||
|
||||
@@ -32,6 +32,12 @@ const restaurantSchema = mongoose.Schema({
|
||||
alcohol: Boolean,
|
||||
delivery: Boolean,
|
||||
},
|
||||
links: {
|
||||
facebook: String,
|
||||
twitter: String,
|
||||
instagram: String,
|
||||
www: String,
|
||||
},
|
||||
phone: Number,
|
||||
hidden: Boolean,
|
||||
dishes: [mongoose.Types.ObjectId],
|
||||
|
||||
@@ -10,12 +10,23 @@ const userSchema = mongoose.Schema({
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
firstname: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
lastname: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
restaurantId: mongoose.Types.ObjectId,
|
||||
subscriptionActive: {
|
||||
type: Boolean,
|
||||
required: true,
|
||||
},
|
||||
subscriptionDue: Date,
|
||||
subscriptionDue: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
|
||||
export default mongoose.model("User", userSchema);
|
||||
|
||||
5
package-lock.json
generated
5
package-lock.json
generated
@@ -139,6 +139,11 @@
|
||||
"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": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ansi-align/-/ansi-align-3.0.0.tgz",
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@material-ui/core": "^4.11.0",
|
||||
"agile_crm": "^1.2.5",
|
||||
"bcrypt": "^5.0.0",
|
||||
"body-parser": "^1.19.0",
|
||||
"cookie-parser": "^1.4.5",
|
||||
|
||||
@@ -5,7 +5,7 @@ import mongoose from "mongoose";
|
||||
var router = express.Router();
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
Restaurant.find({ city: req.body.city }, (err, data) => {
|
||||
Restaurant.find({ city: req.query.city }, (err, data) => {
|
||||
if (err) {
|
||||
res.sendStatus(404);
|
||||
} else res.send(data);
|
||||
|
||||
@@ -9,7 +9,7 @@ var router = express.Router();
|
||||
// GET DISH BY ID
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
Dish.findById(req.body.dishId, (err, data) => {
|
||||
Dish.findById(req.query.dishId, (err, data) => {
|
||||
if (err) {
|
||||
res.sendStatus(404);
|
||||
} else
|
||||
|
||||
@@ -48,6 +48,10 @@ router.post("/", (req, res) => {
|
||||
city: sanitizer.sanitize.keepUnicode(req.body.city),
|
||||
imgUrl: services.saveImage(req.cookies["img"]),
|
||||
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,
|
||||
});
|
||||
restaurant.save((err) => {
|
||||
|
||||
@@ -50,7 +50,7 @@ router.get("/autocomplete/", (req, res) => {
|
||||
"name city",
|
||||
(err, doc) => {
|
||||
if (err) {
|
||||
res.send(404);
|
||||
res.sendStatus(404);
|
||||
} else {
|
||||
doc.forEach((value) => {
|
||||
cities.add(value.city);
|
||||
|
||||
@@ -4,9 +4,17 @@ import User from "../models/users.js";
|
||||
import bcrypt from "bcrypt";
|
||||
import * as services from "../services/services.js";
|
||||
import * as config from "../config/index.js";
|
||||
import AgileCRMManager from "agile_crm";
|
||||
const { API_KEY, jwtSecret } = config;
|
||||
|
||||
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) => {
|
||||
if (req.body.password && req.body.email) {
|
||||
@@ -19,12 +27,16 @@ router.post("/login", (req, res) => {
|
||||
err,
|
||||
result
|
||||
) {
|
||||
if (err) {
|
||||
res.sendStatus(500);
|
||||
} else {
|
||||
if (result) {
|
||||
var token = services.generateAuthToken(user);
|
||||
res.header("x-auth-token", token).status(202).send();
|
||||
} else {
|
||||
res.sendStatus(401);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -49,7 +61,6 @@ router.post("/check", (req, res) => {
|
||||
});
|
||||
|
||||
router.post("/register", (req, res) => {
|
||||
if (req.body.key === API_KEY) {
|
||||
services.checkEmailTaken(req.body.email, (result) => {
|
||||
if (result) {
|
||||
res.sendStatus(409);
|
||||
@@ -59,24 +70,23 @@ router.post("/register", (req, res) => {
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
email: req.body.email,
|
||||
password: hashedPass,
|
||||
subscriptionActive: req.body.subscriptionActive,
|
||||
subscriptionDue: services.dueDateBasedOnSubscription(
|
||||
req.body.subscriptionActive
|
||||
),
|
||||
firstname: req.body.firstname,
|
||||
lastname: req.body.lastname,
|
||||
subscriptionActive: true,
|
||||
subscriptionDue: services.halfYearFromNowDate(),
|
||||
});
|
||||
user.save((err) => {
|
||||
if (err) {
|
||||
res.sendStatus(500);
|
||||
} else {
|
||||
const contact = services.composeNewContact(user);
|
||||
agileAPI.contactAPI.add(contact, success, error);
|
||||
res.sendStatus(201);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -96,6 +96,7 @@ export function createDish(dish, cookie, generateId) {
|
||||
peanuts: dish.allergens.peanuts,
|
||||
sesame: dish.allergens.sesame,
|
||||
},
|
||||
ingredients: dish.ingredients,
|
||||
vegan: dish.vegan,
|
||||
vegetarian: dish.vegetarian,
|
||||
});
|
||||
@@ -117,6 +118,7 @@ export function createDish(dish, cookie, generateId) {
|
||||
peanuts: dish.allergens.peanuts,
|
||||
sesame: dish.allergens.sesame,
|
||||
},
|
||||
ingredients: dish.ingredients,
|
||||
vegan: dish.vegan,
|
||||
vegetarian: dish.vegetarian,
|
||||
});
|
||||
@@ -162,6 +164,18 @@ export function yearFromNowDate() {
|
||||
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) {
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
if (err) callback(false);
|
||||
@@ -177,8 +191,58 @@ export function hashPass(pass, callback) {
|
||||
|
||||
export function dueDateBasedOnSubscription(subscriptionActive) {
|
||||
if (subscriptionActive === true) {
|
||||
return yearFromNowDate();
|
||||
return halfYearFromNowDate();
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user