server v1.0.0
This commit is contained in:
@@ -5,52 +5,6 @@ import Dish from "../models/dish.js";
|
||||
import User from "../models/users.js";
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
|
||||
export function composeNewContact(request) {
|
||||
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: "NIP",
|
||||
value: request.NIP,
|
||||
},
|
||||
{
|
||||
type: "CUSTOM",
|
||||
name: "CompanyName",
|
||||
value: request.companyName,
|
||||
},
|
||||
{
|
||||
type: "CUSTOM",
|
||||
name: "CompanyAdress",
|
||||
value: request.adress,
|
||||
},
|
||||
],
|
||||
};
|
||||
return contact;
|
||||
}
|
||||
|
||||
export async function createUser(request) {
|
||||
const password = await hashPass(request.body.password);
|
||||
const user = new User({
|
||||
@@ -68,25 +22,68 @@ export async function createUser(request) {
|
||||
return user;
|
||||
}
|
||||
|
||||
export function createRestaurant(request) {
|
||||
async function handleImageUpdate(request, previous) {
|
||||
if (!previous) {
|
||||
if (!request.imgURL) {
|
||||
return "empty";
|
||||
} else {
|
||||
const img = await saveImage(request.imgURL);
|
||||
return img;
|
||||
}
|
||||
} else {
|
||||
if (request.imgURL == previous.imgUrl) {
|
||||
return previous.imgUrl;
|
||||
} else {
|
||||
if (!request.imgURL) {
|
||||
return previous.imgUrl;
|
||||
} else {
|
||||
const img = await saveImage(request.imgURL);
|
||||
return img;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function createRestaurant(request, oldRestaurant) {
|
||||
try {
|
||||
const restaurant = new Restaurant({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
name: sanitizer.sanitize.keepUnicode(request.name),
|
||||
city: sanitizer.sanitize.keepUnicode(request.city),
|
||||
adress: sanitizer.sanitize.keepUnicode(request.adress),
|
||||
location: request.location,
|
||||
imgUrl: saveImage(request.imgURL),
|
||||
workingHours: request.workingHours,
|
||||
description: sanitizer.sanitize.keepUnicode(request.description),
|
||||
tags: request.tags,
|
||||
links: request.links,
|
||||
phone: request.phone,
|
||||
hidden: request.hidden,
|
||||
});
|
||||
return restaurant;
|
||||
if (!oldRestaurant) {
|
||||
const img = await handleImageUpdate(request);
|
||||
const restaurant = new Restaurant({
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
name: sanitizer.sanitize.keepUnicode(request.name),
|
||||
city: sanitizer.sanitize.keepUnicode(request.city),
|
||||
adress: sanitizer.sanitize.keepUnicode(request.adress),
|
||||
location: request.location,
|
||||
imgUrl: img,
|
||||
workingHours: request.workingHours,
|
||||
description: sanitizer.sanitize.keepUnicode(request.description),
|
||||
tags: request.tags,
|
||||
links: request.links,
|
||||
phone: request.phone,
|
||||
hidden: request.hidden,
|
||||
});
|
||||
return restaurant;
|
||||
} else {
|
||||
const img = await handleImageUpdate(request, oldRestaurant);
|
||||
const restaurant = new Restaurant({
|
||||
name: sanitizer.sanitize.keepUnicode(request.name),
|
||||
city: sanitizer.sanitize.keepUnicode(request.city),
|
||||
dishes: oldRestaurant.dishes,
|
||||
adress: sanitizer.sanitize.keepUnicode(request.adress),
|
||||
location: request.location,
|
||||
imgUrl: img,
|
||||
workingHours: request.workingHours,
|
||||
description: sanitizer.sanitize.keepUnicode(request.description),
|
||||
tags: request.tags,
|
||||
links: request.links,
|
||||
phone: request.phone,
|
||||
hidden: request.hidden,
|
||||
});
|
||||
return restaurant;
|
||||
}
|
||||
} catch (error) {
|
||||
throw newError("Invalid input data", 206);
|
||||
console.log(error);
|
||||
throw newError("Niewłaściwe dane", 206);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ export async function addDishToRestaurant(restaurantId, dishId) {
|
||||
{ _id: restaurantId },
|
||||
{ $push: { dishes: dishId } }
|
||||
).catch((error) => {
|
||||
throw newError("Couldn't add dish to restaurant", 500);
|
||||
throw newError("Nie udało się dodać dania do restauracji", 500);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -49,16 +49,67 @@ export async function addRestaurantToUser(user, restaurant) {
|
||||
await User.findByIdAndUpdate(user.id, {
|
||||
$push: { restaurants: restaurant._id },
|
||||
}).catch((e) => {
|
||||
throw newError("Couldn't add restaurant to user", 500);
|
||||
throw newError("Nie udało się dodać restauracji do użytkownika", 500);
|
||||
});
|
||||
}
|
||||
|
||||
function dueDateBasedOnSubscription(restaurant, monthsToAdd) {
|
||||
let date;
|
||||
if (
|
||||
restaurant.subscriptionActive === false ||
|
||||
!restaurant.subscriptionActive
|
||||
) {
|
||||
date = new Date();
|
||||
date.setMonth(date.getMonth() + monthsToAdd);
|
||||
return date;
|
||||
} else {
|
||||
date = restaurant.subscriptionDue;
|
||||
date.setMonth(date.getMonth() + monthsToAdd);
|
||||
return date;
|
||||
}
|
||||
}
|
||||
|
||||
function startDate(restaurant) {
|
||||
let date;
|
||||
if (
|
||||
restaurant.subscriptionActive === true &&
|
||||
restaurant.subscriptionStarted
|
||||
) {
|
||||
date = restaurant.subscriptionStarted;
|
||||
return date;
|
||||
} else {
|
||||
date = new Date();
|
||||
return date;
|
||||
}
|
||||
}
|
||||
|
||||
export async function renewSubscription(restaurantId, monthsToAdd) {
|
||||
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
|
||||
throw newError("Nie udało się pobrać restauracji.", 404);
|
||||
});
|
||||
const dueDate = dueDateBasedOnSubscription(restaurant, monthsToAdd);
|
||||
const start = startDate(restaurant);
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$set: {
|
||||
subscriptionActive: true,
|
||||
subscriptionDue: dueDate,
|
||||
subscriptionStarted: start,
|
||||
},
|
||||
}).catch((e) => {
|
||||
throw newError(
|
||||
"Nie udało się przedłużyć subskrypcji, spróbuj ponownie.",
|
||||
500
|
||||
);
|
||||
});
|
||||
return dueDateBasedOnSubscription(restaurant, monthsToAdd);
|
||||
}
|
||||
|
||||
export async function fetchRestaurant(id) {
|
||||
let data;
|
||||
await Restaurant.findById(id, (err, result) => {
|
||||
data = result;
|
||||
}).catch((e) => {
|
||||
throw newError("Couldn't fetch restaurant", 500);
|
||||
throw newError("Nie udało się pobrać restauracji.", 500);
|
||||
});
|
||||
return data;
|
||||
}
|
||||
@@ -74,14 +125,14 @@ export async function fetchAllDishesForRestaurant(restaurant) {
|
||||
|
||||
export async function fetchDish(id) {
|
||||
let data = await Dish.findById(id).catch((e) => {
|
||||
throw newError(`Couldn't fetch ${id}`, 404);
|
||||
throw newError(`Nie udało się pobrać ${id}`, 404);
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function fetchUser(email) {
|
||||
if (!email) throw newError("No input", 204);
|
||||
if (!email) throw newError("Brak danych", 204);
|
||||
const user = await User.findOne({ email: email });
|
||||
if (!user) throw newError("No such user...", 404);
|
||||
if (!user) throw newError("Użytkownik nie istnieje", 404);
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -88,13 +88,13 @@ export async function checkEmailTaken(email) {
|
||||
|
||||
export function validateUserToken(token) {
|
||||
if (!token) throw newError("Brak dostępu", 401);
|
||||
const verified = jwt
|
||||
.verify(token, jwtSecret, { ignoreExpiration: false })
|
||||
.catch((e) => {
|
||||
throw newError("Brak dostępu", 401);
|
||||
});
|
||||
if (!verified) throw newError("Brak dostępu", 401);
|
||||
return verified;
|
||||
try {
|
||||
const verified = jwt.verify(token, jwtSecret, { ignoreExpiration: false });
|
||||
if (!verified) throw newError("Brak dostępu", 401);
|
||||
return verified;
|
||||
} catch (error) {
|
||||
throw newError("Brak dostępu", 401);
|
||||
}
|
||||
}
|
||||
|
||||
export async function validateDishId(id) {
|
||||
@@ -142,17 +142,6 @@ 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 async function hashPass(pass) {
|
||||
if (pass.length < 6) {
|
||||
throw newError("Hasło za krótkie.", 500);
|
||||
@@ -166,21 +155,6 @@ export async function hashPass(pass) {
|
||||
}
|
||||
}
|
||||
|
||||
export function dueDateBasedOnSubscription(subscriptionActive) {
|
||||
if (subscriptionActive === true) {
|
||||
return halfYearFromNowDate();
|
||||
} else {
|
||||
return new Date();
|
||||
}
|
||||
}
|
||||
|
||||
export function toShortDate(date) {
|
||||
if (!date) return false;
|
||||
const shortDate =
|
||||
date.getMonth() + "/" + date.getDay() + "/" + date.getFullYear();
|
||||
return shortDate;
|
||||
}
|
||||
|
||||
export async function saveImage(url) {
|
||||
const newURL = await renameBlob(url);
|
||||
return newURL;
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import { toShortDate, generateNewPassword } from "./services";
|
||||
|
||||
jest.mock("@azure/storage-blob", () => {
|
||||
return {
|
||||
StorageSharedKeyCredential: jest.fn(),
|
||||
newPipeline: jest.fn(),
|
||||
BlobServiceClient: jest.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
jest.mock("bcrypt", () => {
|
||||
return {
|
||||
foo: jest.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
test("should return false for no date on input", () => {
|
||||
expect(toShortDate()).toBe(false);
|
||||
});
|
||||
Reference in New Issue
Block a user