From 7cd920686db91f4a53ade117b0d23c21cc034a41 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 24 Mar 2022 17:50:34 +0100 Subject: [PATCH] helper function to create a new email optin object. Use this in create user --- backend/src/graphql/resolver/UserResolver.ts | 23 +++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index a1d6d6e1d..579940c2b 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -147,12 +147,20 @@ const SecretKeyCryptographyDecrypt = (encryptedMessage: Buffer, encryptionKey: B return message } +const newEmailOptin = (userId: number): LoginEmailOptIn => { + const emailOptIn = new LoginEmailOptIn() + emailOptIn.verificationCode = random(64) + emailOptIn.userId = userId + emailOptIn.emailOptInTypeId = OptinType.EMAIL_OPT_IN_REGISTER + return emailOptIn +} + const createEmailOptIn = async ( - loginUserId: number, + userId: number, queryRunner: QueryRunner, ): Promise => { let emailOptIn = await LoginEmailOptIn.findOne({ - userId: loginUserId, + userId, emailOptInTypeId: OptinType.EMAIL_OPT_IN_REGISTER, }) @@ -165,7 +173,7 @@ const createEmailOptIn = async ( } else { emailOptIn = new LoginEmailOptIn() emailOptIn.verificationCode = random(64) - emailOptIn.userId = loginUserId + emailOptIn.userId = userId emailOptIn.emailOptInTypeId = OptinType.EMAIL_OPT_IN_REGISTER } await queryRunner.manager.save(emailOptIn).catch((error) => { @@ -363,9 +371,12 @@ export class UserResolver { throw new Error('error saving user') }) - // Store EmailOptIn in DB - // TODO: this has duplicate code with sendResetPasswordEmail - const emailOptIn = await createEmailOptIn(dbUser.id, queryRunner) + const emailOptIn = newEmailOptin(dbUser.id) + await queryRunner.manager.save(emailOptIn).catch((error) => { + // eslint-disable-next-line no-console + console.log('Error while saving emailOptIn', error) + throw new Error('error saving email opt in') + }) const activationLink = CONFIG.EMAIL_LINK_VERIFICATION.replace( /{optin}/g,