|
@@ -1,7 +1,9 @@
|
|
|
import jwt from "jsonwebtoken";
|
|
import jwt from "jsonwebtoken";
|
|
|
import {
|
|
import {
|
|
|
- User
|
|
|
|
|
-} from "../../../models/User";
|
|
|
|
|
|
|
+ Subscription,
|
|
|
|
|
+ User,
|
|
|
|
|
+ Plan
|
|
|
|
|
+} from "../../../models/index";
|
|
|
import {
|
|
import {
|
|
|
LoginResult,
|
|
LoginResult,
|
|
|
LoginInput
|
|
LoginInput
|
|
@@ -21,14 +23,14 @@ const login = async (input: LoginInput): Promise<LoginResult> => {
|
|
|
if (!user) {
|
|
if (!user) {
|
|
|
return {
|
|
return {
|
|
|
message: "user-not-found",
|
|
message: "user-not-found",
|
|
|
- code: 404,
|
|
|
|
|
|
|
+ code: 404
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (user.password !== password) {
|
|
if (user.password !== password) {
|
|
|
return {
|
|
return {
|
|
|
message: "wrong-password",
|
|
message: "wrong-password",
|
|
|
- code: 401,
|
|
|
|
|
|
|
+ code: 401
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -45,17 +47,41 @@ const login = async (input: LoginInput): Promise<LoginResult> => {
|
|
|
if (!user.isApproved) {
|
|
if (!user.isApproved) {
|
|
|
return {
|
|
return {
|
|
|
message: "your-account-is-currently-under-review-we-will-get-back-to-you",
|
|
message: "your-account-is-currently-under-review-we-will-get-back-to-you",
|
|
|
- code: 200,
|
|
|
|
|
|
|
+ code: 200
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ const activeSubscription = await Subscription.findOne({
|
|
|
|
|
+ userID: user._id.toString(),
|
|
|
|
|
+ status: "active",
|
|
|
|
|
+ isActive: true
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ let planLimits = null;
|
|
|
|
|
+
|
|
|
|
|
+ if (activeSubscription) {
|
|
|
|
|
+ const plan = await Plan.findById(activeSubscription.planID);
|
|
|
|
|
+ if (plan) {
|
|
|
|
|
+ planLimits = {
|
|
|
|
|
+ recommendedProductLimit: plan.recommendedProductLimit,
|
|
|
|
|
+ categoryLimit: plan.categoryLimit,
|
|
|
|
|
+ productLimit: plan.productLimit,
|
|
|
|
|
+ planID: plan._id.toString(),
|
|
|
|
|
+ menuLimit: plan.menuLimit
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const tokenPayload = {
|
|
|
|
|
+ companyName: user.companyName,
|
|
|
|
|
+ fullName: user.fullName,
|
|
|
|
|
+ planDetails: planLimits,
|
|
|
|
|
+ userID: user._id,
|
|
|
|
|
+ mail: user.mail
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
const accessToken = jwt.sign(
|
|
const accessToken = jwt.sign(
|
|
|
- {
|
|
|
|
|
- companyName: user.companyName,
|
|
|
|
|
- fullName: user.fullName,
|
|
|
|
|
- userID: user._id,
|
|
|
|
|
- mail: user.mail
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ tokenPayload,
|
|
|
process.env.JWT_SECRET as string,
|
|
process.env.JWT_SECRET as string,
|
|
|
{
|
|
{
|
|
|
expiresIn: "4h"
|
|
expiresIn: "4h"
|
|
@@ -65,12 +91,7 @@ const login = async (input: LoginInput): Promise<LoginResult> => {
|
|
|
await redis.setex(user._id.toString(), 14400, accessToken);
|
|
await redis.setex(user._id.toString(), 14400, accessToken);
|
|
|
|
|
|
|
|
const refreshToken = jwt.sign(
|
|
const refreshToken = jwt.sign(
|
|
|
- {
|
|
|
|
|
- companyName: user.companyName,
|
|
|
|
|
- fullName: user.fullName,
|
|
|
|
|
- userID: user._id,
|
|
|
|
|
- mail: user.mail
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ tokenPayload,
|
|
|
process.env.JWT_SECRET as string,
|
|
process.env.JWT_SECRET as string,
|
|
|
{
|
|
{
|
|
|
expiresIn: "30d"
|
|
expiresIn: "30d"
|