rename function, add parameter for optinType, sort by updatedAt DESC

This commit is contained in:
Moriz Wahl 2022-03-29 00:50:50 +02:00
parent 3128ede829
commit cb90249301
2 changed files with 12 additions and 11 deletions

View File

@ -34,7 +34,7 @@ import { Decay } from '@model/Decay'
import Paginated from '@arg/Paginated'
import { Order } from '@enum/Order'
import { communityUser } from '@/util/communityUser'
import { checkExistingOptInCode, activationLink } from './UserResolver'
import { checkOptInCode, activationLink } from './UserResolver'
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
// const EMAIL_OPT_IN_REGISTER = 1
@ -380,12 +380,11 @@ export class AdminResolver {
// can be both types: REGISTER and RESET_PASSWORD
let optInCode = await LoginEmailOptIn.findOne({
userId: user.id,
where: { userId: user.id },
order: { updatedAt: 'DESC' },
})
optInCode = checkExistingOptInCode(optInCode, user.id)
// keep the optin type (when newly created is is REGISTER)
await LoginEmailOptIn.save(optInCode)
optInCode = await checkOptInCode(optInCode, user.id)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const emailSent = await sendAccountActivationEmail({

View File

@ -158,10 +158,11 @@ const newEmailOptIn = (userId: number): LoginEmailOptIn => {
// needed by AdminResolver
// checks if given code exists and can be resent
// if optIn does not exits, it is created
export const checkExistingOptInCode = (
export const checkOptInCode = async (
optInCode: LoginEmailOptIn | undefined,
userId: number,
): LoginEmailOptIn => {
optInType: OptInType = OptInType.EMAIL_OPT_IN_REGISTER,
): Promise<LoginEmailOptIn> => {
if (optInCode) {
if (!canResendOptIn(optInCode)) {
throw new Error(
@ -175,6 +176,10 @@ export const checkExistingOptInCode = (
} else {
optInCode = newEmailOptIn(userId)
}
optInCode.emailOptInTypeId = optInType
await LoginEmailOptIn.save(optInCode).catch(() => {
throw new Error('Unable to save optin code.')
})
return optInCode
}
@ -395,10 +400,7 @@ export class UserResolver {
userId: user.id,
})
optInCode = checkExistingOptInCode(optInCode, user.id)
// now it is RESET_PASSWORD
optInCode.emailOptInTypeId = OptInType.EMAIL_OPT_IN_RESET_PASSWORD
await LoginEmailOptIn.save(optInCode)
optInCode = await checkOptInCode(optInCode, user.id, OptInType.EMAIL_OPT_IN_RESET_PASSWORD)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const emailSent = await sendResetPasswordEmail({