|
|
@@ -11,113 +11,122 @@ import {
|
|
|
import redis from "../../../config/redis";
|
|
|
|
|
|
const login = async (input: LoginInput): Promise<LoginResult> => {
|
|
|
- const {
|
|
|
- password,
|
|
|
- mail
|
|
|
- } = input;
|
|
|
+ try{
|
|
|
+ const {
|
|
|
+ password,
|
|
|
+ mail
|
|
|
+ } = input;
|
|
|
+
|
|
|
+ const user = await User.findOne({
|
|
|
+ mail
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!user) {
|
|
|
+ return {
|
|
|
+ message: "user-not-found",
|
|
|
+ code: 404
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
- const user = await User.findOne({
|
|
|
- mail
|
|
|
- });
|
|
|
+ if (user.password !== password) {
|
|
|
+ return {
|
|
|
+ message: "wrong-password",
|
|
|
+ code: 401
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
- if (!user) {
|
|
|
- return {
|
|
|
- message: "user-not-found",
|
|
|
- code: 404
|
|
|
- };
|
|
|
- }
|
|
|
+ if (!user.isPhoneVerified) {
|
|
|
+ return {
|
|
|
+ message: "please-verify-your-phone-first",
|
|
|
+ code: 403,
|
|
|
+ payload: {
|
|
|
+ userID: user._id.toString()
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
- if (user.password !== password) {
|
|
|
- return {
|
|
|
- message: "wrong-password",
|
|
|
- code: 401
|
|
|
- };
|
|
|
- }
|
|
|
+ if (!user.isApproved) {
|
|
|
+ return {
|
|
|
+ message: "your-account-is-currently-under-review-we-will-get-back-to-you",
|
|
|
+ code: 200
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
- if (!user.isPhoneVerified) {
|
|
|
- return {
|
|
|
- message: "please-verify-your-phone-first",
|
|
|
- code: 403,
|
|
|
- payload: {
|
|
|
- userID: user._id.toString()
|
|
|
+ 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
|
|
|
+ };
|
|
|
}
|
|
|
- };
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- if (!user.isApproved) {
|
|
|
- return {
|
|
|
- message: "your-account-is-currently-under-review-we-will-get-back-to-you",
|
|
|
- code: 200
|
|
|
+ const tokenPayload = {
|
|
|
+ companyName: user.companyName,
|
|
|
+ fullName: user.fullName,
|
|
|
+ planDetails: planLimits,
|
|
|
+ userID: user._id,
|
|
|
+ mail: user.mail
|
|
|
};
|
|
|
- }
|
|
|
|
|
|
- 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 accessToken = jwt.sign(
|
|
|
+ tokenPayload,
|
|
|
+ process.env.JWT_SECRET as string,
|
|
|
+ {
|
|
|
+ expiresIn: "4h"
|
|
|
+ }
|
|
|
+ );
|
|
|
|
|
|
- const tokenPayload = {
|
|
|
- companyName: user.companyName,
|
|
|
- fullName: user.fullName,
|
|
|
- planDetails: planLimits,
|
|
|
- userID: user._id,
|
|
|
- mail: user.mail
|
|
|
- };
|
|
|
-
|
|
|
- const accessToken = jwt.sign(
|
|
|
- tokenPayload,
|
|
|
- process.env.JWT_SECRET as string,
|
|
|
- {
|
|
|
- expiresIn: "4h"
|
|
|
- }
|
|
|
- );
|
|
|
+ await redis.setex(user._id.toString(), 14400, accessToken);
|
|
|
+
|
|
|
+ const refreshToken = jwt.sign(
|
|
|
+ tokenPayload,
|
|
|
+ process.env.JWT_SECRET as string,
|
|
|
+ {
|
|
|
+ expiresIn: "30d"
|
|
|
+ }
|
|
|
+ );
|
|
|
|
|
|
- await redis.setex(user._id.toString(), 14400, accessToken);
|
|
|
+ user.refreshToken = refreshToken;
|
|
|
+ await user.save();
|
|
|
|
|
|
- const refreshToken = jwt.sign(
|
|
|
- tokenPayload,
|
|
|
- process.env.JWT_SECRET as string,
|
|
|
- {
|
|
|
- expiresIn: "30d"
|
|
|
- }
|
|
|
- );
|
|
|
-
|
|
|
- user.refreshToken = refreshToken;
|
|
|
- await user.save();
|
|
|
-
|
|
|
- return {
|
|
|
- message: "login-successful",
|
|
|
- code: 200,
|
|
|
- payload: {
|
|
|
- refreshToken,
|
|
|
- accessToken,
|
|
|
- user: {
|
|
|
- phoneNumber: user.phoneNumber,
|
|
|
- companyName: user.companyName,
|
|
|
- userID: user._id.toString(),
|
|
|
- firstName: user.firstName,
|
|
|
- lastName: user.lastName,
|
|
|
- fullName: user.fullName,
|
|
|
- mail: user.mail,
|
|
|
+ return {
|
|
|
+ message: "login-successful",
|
|
|
+ code: 200,
|
|
|
+ payload: {
|
|
|
+ refreshToken,
|
|
|
+ accessToken,
|
|
|
+ user: {
|
|
|
+ phoneNumber: user.phoneNumber,
|
|
|
+ companyName: user.companyName,
|
|
|
+ userID: user._id.toString(),
|
|
|
+ firstName: user.firstName,
|
|
|
+ lastName: user.lastName,
|
|
|
+ fullName: user.fullName,
|
|
|
+ mail: user.mail,
|
|
|
+ },
|
|
|
},
|
|
|
- },
|
|
|
- };
|
|
|
+ };
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ console.error("Login error:", error);
|
|
|
+ return {
|
|
|
+ message: "internal-server-error",
|
|
|
+ code: 500
|
|
|
+ };
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
export default login;
|