import { NextFunction, Request, Response } from "express"; import StormModel from "../models/stormModel"; import Logger from "../utils/logger"; import { ErrorResponse, SuccessResponse } from "../utils/response"; import { HttpCodes } from "../config/errors"; import sendMail from "../utils/sendMail"; export const generateCode = async ( req: Request, res: Response, next: NextFunction ) => { try { const { email } = req.body; const code = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); const newStorm = await StormModel.create({ email: req.body.email, code, }); await sendMail({ email, subject: `Snai3i storm registration`, data: { email, code, }, }); Logger.info(`Storm code ${code} generated successfully.`); SuccessResponse( res, HttpCodes.Created.code, newStorm, "Storm code generated successfully." ); } catch (error) { Logger.error(`Failed to generate code - ${error}`); return ErrorResponse( res, HttpCodes.InternalServerError.code, "Failed to generate code. Please try again!" ); } }; export const verifyCode = async ( req: Request, res: Response, next: NextFunction ) => { try { const { email, code } = req.body; const storm = await StormModel.findOne({ email, code }); if (!storm) { return ErrorResponse( res, HttpCodes.NotFound.code, "Code is invalid. Please try again!" ); } SuccessResponse( res, HttpCodes.OK.code, { storm, access: true }, "Code verified successfully." ); } catch (error) { Logger.error(`Failed to verify code - ${error}`); return ErrorResponse( res, HttpCodes.InternalServerError.code, "Failed to verify code. Please try again!" ); } };