index.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {
  2. User
  3. } from "../../../models/User";
  4. import redis from "../../../config/redis";
  5. import {
  6. StartMailVerifyResult,
  7. StartMailVerifyInput
  8. } from "./types";
  9. import {
  10. verificationCode,
  11. TTL_SECONDS
  12. } from "../../../utils";
  13. // import { sendMail } from "../../../utils/mailer"; // mail gönderme fonskiyonu bu şekilde ilerde import edilecek.
  14. const startMailVerify = async (input: StartMailVerifyInput): Promise<StartMailVerifyResult> => {
  15. try {
  16. const {
  17. userID // TODO MAİL ALINACAK DTO İLE YAPIALCAK MAİL İLE USER ARANCAK MONGO DA
  18. } = input;
  19. if (!userID) {
  20. return {
  21. message: "userID-required",
  22. code: 400,
  23. };
  24. }
  25. const user = await User.findById(userID);
  26. if (!user) {
  27. return {
  28. message: "user-not-found",
  29. code: 404
  30. };
  31. }
  32. if (user.isMailVerified) {
  33. return {
  34. message: "mail-already-verified",
  35. code: 400
  36. };
  37. }
  38. const existingTTL = await redis.ttl(`mail-verify-${userID}`);
  39. if (existingTTL > 0) {
  40. return {
  41. message: "please-wait-before-requesting-again",
  42. code: 429,
  43. payload: {
  44. remainingTime: existingTTL
  45. }
  46. };
  47. }
  48. await redis.setex(`mail-verify-${userID}`, TTL_SECONDS, verificationCode);
  49. // MAİL GÖNDERME İŞLEMİ (mail servisini bağla) bu kısımda olacak şimdilik elle müdahale ediliyor.
  50. // await sendMail(user.mail, "Doğrulama Kodunuz", `Kodunuz: ${verificationCode}`); şeklinde olacak.
  51. return {
  52. message: "verification-code-sent",
  53. code: 200,
  54. payload: {
  55. remainingTime: TTL_SECONDS
  56. }
  57. };
  58. } catch (error) {
  59. console.error("StartMailVerify error:", error);
  60. return {
  61. message: "internal-server-error",
  62. code: 500
  63. };
  64. }
  65. };
  66. export default startMailVerify;