|
| 1 | +import { IUser } from '../models/user/fields'; |
| 2 | +import OTP from '../models/otp/OTP'; |
| 3 | +import ExternalUser from '../models/externaluser/ExternalUser'; |
| 4 | +import { BadRequestError, NotFoundError } from '../types/errors'; |
| 5 | +import { createJwt } from '../services/permissions'; |
| 6 | + |
| 7 | +export async function generateOTP(requestUser: IUser, email: string) { |
| 8 | + const externalUser = await ExternalUser.findOne({ email }); |
| 9 | + if (!externalUser) { |
| 10 | + throw new BadRequestError('Email not found in external users'); |
| 11 | + } |
| 12 | + |
| 13 | + const code = Math.floor(100000 + Math.random() * 900000).toString(); |
| 14 | + const expiration = Date.now() + 10 * 60 * 1000; |
| 15 | + |
| 16 | + const otp = new OTP({ |
| 17 | + code, |
| 18 | + email, |
| 19 | + expiration, |
| 20 | + used: false, |
| 21 | + issuedBy: requestUser.email, |
| 22 | + }); |
| 23 | + |
| 24 | + await otp.save(); |
| 25 | + |
| 26 | + return { |
| 27 | + success: true, |
| 28 | + message: 'OTP generated successfully', |
| 29 | + code, |
| 30 | + expiration: new Date(expiration), |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +export async function verifyOTP( |
| 35 | + requestUser: IUser | null, |
| 36 | + code: string, |
| 37 | + email: string, |
| 38 | +) { |
| 39 | + const externalUser = await ExternalUser.findOne({ email }); |
| 40 | + if (!externalUser) { |
| 41 | + throw new BadRequestError('Email not found in external users'); |
| 42 | + } |
| 43 | + |
| 44 | + console.log('OTP.findOne', code, email); |
| 45 | + const otp = await OTP.findOne({ code, email }); |
| 46 | + if (!otp) { |
| 47 | + throw new NotFoundError('Invalid OTP code'); |
| 48 | + } |
| 49 | + |
| 50 | + if (otp.used) { |
| 51 | + throw new BadRequestError('OTP code already used'); |
| 52 | + } |
| 53 | + |
| 54 | + if (Date.now() > otp.expiration) { |
| 55 | + throw new BadRequestError('OTP code expired'); |
| 56 | + } |
| 57 | + |
| 58 | + otp.used = true; |
| 59 | + otp.usedBy = externalUser._id.toString(); |
| 60 | + otp.usedAt = new Date(); |
| 61 | + otp.usedName = externalUser.firstName + ' ' + externalUser.lastName; |
| 62 | + await otp.save(); |
| 63 | + |
| 64 | + const token = createJwt({ |
| 65 | + id: externalUser._id, |
| 66 | + idpLinkID: `OTP-${externalUser._id}`, |
| 67 | + roles: { |
| 68 | + volunteer: true, |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + return { |
| 73 | + success: true, |
| 74 | + message: 'OTP verified successfully', |
| 75 | + user: externalUser, |
| 76 | + token: token, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +export async function getAllOTPs(requestUser: IUser) { |
| 81 | + const otps = await OTP.find({}).sort({ createdAt: -1 }); |
| 82 | + |
| 83 | + return { |
| 84 | + success: true, |
| 85 | + otps: otps.map((otp) => ({ |
| 86 | + id: otp._id, |
| 87 | + code: otp.code, |
| 88 | + email: otp.email, |
| 89 | + used: otp.used, |
| 90 | + expiration: new Date(otp.expiration), |
| 91 | + createdAt: otp.createdAt, |
| 92 | + usedBy: otp.usedBy, |
| 93 | + usedAt: otp.usedAt, |
| 94 | + issuedBy: otp.issuedBy, |
| 95 | + usedName: otp.usedName, |
| 96 | + })), |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +export async function expireOTP(requestUser: IUser, otpId: string) { |
| 101 | + const otp = await OTP.findById(otpId); |
| 102 | + if (!otp) { |
| 103 | + throw new NotFoundError('OTP not found'); |
| 104 | + } |
| 105 | + |
| 106 | + otp.expiration = Date.now(); |
| 107 | + await otp.save(); |
| 108 | + |
| 109 | + return { |
| 110 | + success: true, |
| 111 | + message: 'OTP expired successfully', |
| 112 | + }; |
| 113 | +} |
0 commit comments