Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 77 additions & 28 deletions libs/model/src/aggregates/user/signIn/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<AddressAttributes>(
`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!,
Expand All @@ -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,
},
);
Expand All @@ -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,
Expand All @@ -420,6 +468,7 @@ export async function signInUser({
},
transaction,
});
return { found, inserted };
});

if (!address || !foundOrCreatedUser)
Expand All @@ -428,8 +477,8 @@ export async function signInUser({
return {
user: signedInUser || foundOrCreatedUser,
newUser,
address,
newAddress,
address: address.found,
newAddress: !!address.inserted,
addressCount,
transferredUser,
};
Expand Down
Loading