Skip to content

Add wallet to user #2296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
9 changes: 1 addition & 8 deletions src/datasources/users/entities/wallets.entity.db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RowSchema } from '@/datasources/db/v1/entities/row.entity';
import {
Column,
Entity,
Expand All @@ -9,14 +8,8 @@ import {
} from 'typeorm';
import { z } from 'zod';
import { getAddress } from 'viem';
import { AddressSchema } from '@/validation/entities/schemas/address.schema';
import { User } from '@/datasources/users/entities/users.entity.db';
import { UserSchema } from '@/domain/users/entities/user.entity';

export const WalletSchema = RowSchema.extend({
address: AddressSchema,
user: UserSchema,
});
import { WalletSchema } from '@/domain/users/entities/wallet.entity';

@Entity('wallets')
@Unique('UQ_wallet_address', ['address'])
Expand Down
11 changes: 11 additions & 0 deletions src/domain/users/entities/wallet.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { z } from 'zod';
import { RowSchema } from '@/datasources/db/v1/entities/row.entity';
import { UserSchema } from '@/domain/users/entities/user.entity';
import { AddressSchema } from '@/validation/entities/schemas/address.schema';

export type Wallet = z.infer<typeof WalletSchema>;

export const WalletSchema = RowSchema.extend({
address: AddressSchema,
user: UserSchema,
});
6 changes: 6 additions & 0 deletions src/domain/users/users.repository.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AuthPayload } from '@/domain/auth/entities/auth-payload.entity';
import type { User, UserStatus } from '@/domain/users/entities/user.entity';
import type { Wallet } from '@/domain/users/entities/wallet.entity';

export const IUsersRepository = Symbol('IUsersRepository');

Expand All @@ -8,4 +9,9 @@ export interface IUsersRepository {
status: UserStatus;
authPayload: AuthPayload;
}): Promise<Pick<User, 'id'>>;

addWalletToUser(args: {
newSignerAddress: `0x${string}`;
authPayload: AuthPayload;
}): Promise<Pick<Wallet, 'id'>>;
}
39 changes: 39 additions & 0 deletions src/domain/users/users.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,43 @@ export class UsersRepository implements IUsersRepository {
},
);
}

async addWalletToUser(args: {
newSignerAddress: `0x${string}`;
authPayload: AuthPayload;
}): Promise<Pick<Wallet, 'id'>> {
return await this.postgresDatabaseService.transaction(
async (entityManager: EntityManager) => {
const walletRepository = entityManager.getRepository(Wallet);

const authenticatedWallet = await walletRepository.findOne({
where: { address: args.authPayload.signer_address },
relations: { user: true },
});

if (!authenticatedWallet?.user) {
throw new Error('User not found');
}

const walletAlreadyExists = Boolean(
await walletRepository.findOne({
where: { address: args.newSignerAddress },
}),
);

if (walletAlreadyExists) {
throw new ConflictException(
'A wallet with the same address already exists',
);
}

const walletInsertResult = await walletRepository.insert({
user: authenticatedWallet.user,
address: args.newSignerAddress,
});

return { id: walletInsertResult.identifiers[0].id };
},
);
}
}
Loading