| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import {
- User
- } from "../../../models/User";
- import redis from "../../../config/redis";
- import {
- StartMailVerifyResult,
- StartMailVerifyInput
- } from "./types";
- import {
- verificationCode,
- TTL_SECONDS
- } from "../../../utils";
- // import { sendMail } from "../../../utils/mailer"; // mail gönderme fonskiyonu bu şekilde ilerde import edilecek.
- const startMailVerify = async (input: StartMailVerifyInput): Promise<StartMailVerifyResult> => {
- try {
- const {
- userID // TODO MAİL ALINACAK DTO İLE YAPIALCAK MAİL İLE USER ARANCAK MONGO DA
- } = 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
- }
- };
- }
- 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;
|