Bladeren bron

Merge branch 'feature/startMailVerified-feature' into develop

BedirhanOZCAN 1 maand geleden
bovenliggende
commit
75f90a095b
2 gewijzigde bestanden met toevoegingen van 87 en 0 verwijderingen
  1. 76 0
      src/actions/auth/startMailVerified/index.ts
  2. 11 0
      src/actions/auth/startMailVerified/types.ts

+ 76 - 0
src/actions/auth/startMailVerified/index.ts

@@ -0,0 +1,76 @@
+import {
+    User
+} from "../../../models/User";
+import redis from "../../../config/redis";
+import {
+    StartMailVerifyResult,
+    StartMailVerifyInput
+} from "./types";
+// import { sendMail } from "../../../utils/mailer"; // mail gönderme fonskiyonu bu şekilde ilerde import edilecek.
+
+const startMailVerify = async (input: StartMailVerifyInput): Promise<StartMailVerifyResult> => {
+    try {
+        const {
+            userID
+        } = input;
+
+        if (!userID) {
+            return {
+                message: "userID-required",
+                code: 400,
+            };
+        }
+
+        const user = await User.findById(userID);
+        if (!user) {
+            return {
+                message: "user-not-found",
+                code: 404
+            };
+        }
+
+        if (user.isMailVerified) {
+            return {
+                message: "mail-already-verified",
+                code: 400
+            };
+        }
+
+        const existingTTL = await redis.ttl(`mail-verify-${userID}`);
+
+        if (existingTTL > 0) {
+            return {
+                message: "please-wait-before-requesting-again",
+                code: 429,
+                payload: {
+                    remainingTime: existingTTL
+                }
+            };
+        }
+        //TODO BURADAKİ KOD ÜRETİMİ FARKLI BİR DOSYADAN MI ÜRETİLCEK UTİLS GİBİ
+        const verificationCode = Math.floor(100000 + Math.random() * 900000).toString();
+        const TTL_SECONDS = 180; // Kodun geçerlilik süresi bu.
+
+        await redis.setex(`mail-verify-${userID}`, TTL_SECONDS, verificationCode);
+
+        // MAİL GÖNDERME İŞLEMİ (mail servisini bağla) bu kısımda olacak şimdilik elle müdahale ediliyor.
+        // await sendMail(user.mail, "Doğrulama Kodunuz", `Kodunuz: ${verificationCode}`); şeklinde olacak.
+
+        return {
+            message: "verification-code-sent",
+            code: 200,
+            payload: {
+                remainingTime: TTL_SECONDS
+            }
+        };
+
+    } catch (error) {
+        console.error("StartMailVerify error:", error);
+        return {
+            message: "internal-server-error",
+            code: 500
+        };
+    }
+};
+
+export default startMailVerify;

+ 11 - 0
src/actions/auth/startMailVerified/types.ts

@@ -0,0 +1,11 @@
+export interface StartMailVerifyInput {
+    userID: string;
+}
+
+export interface StartMailVerifyResult {
+    message: string;
+    code: number;
+    payload?: {
+        remainingTime: number;
+    };
+}