|
@@ -0,0 +1,77 @@
|
|
|
|
|
+import {
|
|
|
|
|
+ User
|
|
|
|
|
+} from "../../../models/User";
|
|
|
|
|
+import redis from "../../../config/redis";
|
|
|
|
|
+import {
|
|
|
|
|
+ SmsSendResult,
|
|
|
|
|
+ SmsSendInput
|
|
|
|
|
+} from "./types";
|
|
|
|
|
+import {
|
|
|
|
|
+ verificationCode,
|
|
|
|
|
+ TTL_SECONDS
|
|
|
|
|
+} from "../../../utils";
|
|
|
|
|
+
|
|
|
|
|
+const smsSend = async (input: SmsSendInput): Promise<SmsSendResult> => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const {
|
|
|
|
|
+ userID
|
|
|
|
|
+ } = input;
|
|
|
|
|
+
|
|
|
|
|
+ const user = await User.findById(userID);
|
|
|
|
|
+ if (!user) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ message: "user-not-found",
|
|
|
|
|
+ code: 404
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (user.isPhoneVerified) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ message: "phone-already-verified",
|
|
|
|
|
+ code: 400
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!user.phoneNumber) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ message: "phone-number-missing",
|
|
|
|
|
+ code: 400
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const redisKey = `sms-verify-${userID}`;
|
|
|
|
|
+
|
|
|
|
|
+ const existingTTL = await redis.ttl(redisKey);
|
|
|
|
|
+ if (existingTTL > 0) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ message: "please-wait-before-requesting-again",
|
|
|
|
|
+ code: 429,
|
|
|
|
|
+ payload: {
|
|
|
|
|
+ remainingTime: existingTTL
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ await redis.setex(redisKey, TTL_SECONDS, verificationCode);
|
|
|
|
|
+
|
|
|
|
|
+ // 6. SMS GÖNDERME İŞLEMİ
|
|
|
|
|
+ // await sendSMS(user.phoneNumber, `Giriş için doğrulama kodunuz: ${verificationCode}`);
|
|
|
|
|
+
|
|
|
|
|
+ return {
|
|
|
|
|
+ message: "sms-code-sent",
|
|
|
|
|
+ code: 200,
|
|
|
|
|
+ payload: {
|
|
|
|
|
+ remainingTime: TTL_SECONDS
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error("SmsSend action error:", error);
|
|
|
|
|
+ return {
|
|
|
|
|
+ message: "internal-server-error",
|
|
|
|
|
+ code: 500
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+export default smsSend;
|