diff --git a/libs/model/src/aggregates/user/signIn/utils.ts b/libs/model/src/aggregates/user/signIn/utils.ts index dbac9eafdd5..ca59320a2f4 100644 --- a/libs/model/src/aggregates/user/signIn/utils.ts +++ b/libs/model/src/aggregates/user/signIn/utils.ts @@ -2,7 +2,7 @@ import { InvalidActor, logger } from '@hicommonwealth/core'; import { SignIn } from '@hicommonwealth/schemas'; import { BalanceSourceType, UserTierMap } from '@hicommonwealth/shared'; import { User as PrivyUser } from '@privy-io/server-auth'; -import { Transaction } from 'sequelize'; +import { QueryTypes, Transaction } from 'sequelize'; import { z } from 'zod'; import { config } from '../../../config'; import { models } from '../../../database'; @@ -316,12 +316,10 @@ export async function signInUser({ }) { let addressCount = 1; let transferredUser = false; - let address: AddressAttributes | undefined, - newAddress = false; let foundOrCreatedUser: UserAttributes | undefined; let newUser = false; - await models.sequelize.transaction(async (transaction) => { + const address = await models.sequelize.transaction(async (transaction) => { const userRes = await findOrCreateUser({ address: payload.address, ssoInfo: verifiedSsoInfo, @@ -363,28 +361,78 @@ export async function signInUser({ transaction, }); - [address, newAddress] = await models.Address.findOrCreate({ - where: { community_id: payload.community_id, address: payload.address }, - defaults: { - community_id: payload.community_id, - address: payload.address, - user_id: signedInUser?.id ?? foundOrCreatedUser.id!, - hex: payload.hex, - wallet_id: payload.wallet_id, - verification_token: verificationData.verification_token, - verification_token_expires: verificationData.verification_token_expires, - block_info: payload.block_info ?? null, - last_active: new Date(), - verified: new Date(), - role: 'member', - ghost_address: false, - is_banned: false, + const replacements = { + community_id: payload.community_id, + address: payload.address, + user_id: signedInUser?.id || foundOrCreatedUser.id!, + hex: payload.hex || null, + wallet_id: payload.wallet_id, + verification_token: verificationData.verification_token, + verification_token_expires: verificationData.verification_token_expires, + block_info: payload.block_info || null, + }; + const [inserted] = await models.sequelize.query( + ` + INSERT INTO "Addresses" ( + community_id, + address, + user_id, + hex, + wallet_id, + verification_token, + verification_token_expires, + block_info, + last_active, + verified, + role, + ghost_address, + is_banned, + created_at, + updated_at + ) + VALUES ( + :community_id, + :address, + :user_id, + :hex, + :wallet_id, + :verification_token, + :verification_token_expires, + :block_info, + NOW(), + NOW(), + 'member', + FALSE, + FALSE, + NOW(), + NOW() + ) + ON CONFLICT (community_id, address) + DO NOTHING + RETURNING *; + `, + { + raw: true, + replacements, + transaction, }, - transaction, - }); + ); + + const [found] = await models.sequelize.query( + `SELECT * FROM "Addresses" WHERE community_id = :community_id AND address = :address; `, + { + type: QueryTypes.SELECT, + raw: true, + replacements: { + community_id: payload.community_id, + address: payload.address, + }, + transaction, + }, + ); // recover deleted users - if (address && !newAddress && address.user_id === null) { + if (found && !inserted && found.user_id === null) { await models.Address.update( { user_id: signedInUser?.id ?? foundOrCreatedUser.id!, @@ -397,7 +445,7 @@ export async function signInUser({ verified: new Date(), }, { - where: { id: address.id, user_id: null }, + where: { id: found.id, user_id: null }, transaction, }, ); @@ -407,8 +455,8 @@ export async function signInUser({ newUser, user: signedInUser || foundOrCreatedUser, transferredUser, - address, - newAddress, + address: found, + newAddress: !!inserted, transaction, originalUserId: transferredUser ? foundOrCreatedUser.id : undefined, ethChainId, @@ -420,6 +468,7 @@ export async function signInUser({ }, transaction, }); + return { found, inserted }; }); if (!address || !foundOrCreatedUser) @@ -428,8 +477,8 @@ export async function signInUser({ return { user: signedInUser || foundOrCreatedUser, newUser, - address, - newAddress, + address: address.found, + newAddress: !!address.inserted, addressCount, transferredUser, };