This commit is contained in:
hailin 2025-11-24 02:38:15 -08:00
parent 77725bd769
commit c6c2545095
3 changed files with 67 additions and 48 deletions

View File

@ -96,8 +96,9 @@ export class UserAccount {
params.initialDeviceId, params.deviceName || '未命名设备', new Date(), new Date(), params.initialDeviceId, params.deviceName || '未命名设备', new Date(), new Date(),
)); ));
// UserID将由数据库自动生成(autoincrement)这里使用临时值0
const account = new UserAccount( const account = new UserAccount(
UserId.generate(), params.accountSequence, devices, null, UserId.create(0), params.accountSequence, devices, null,
`用户${params.accountSequence.value}`, null, params.inviterSequence, `用户${params.accountSequence.value}`, null, params.inviterSequence,
ReferralCode.generate(), params.province, params.city, null, ReferralCode.generate(), params.province, params.city, null,
new Map(), null, KYCStatus.NOT_VERIFIED, AccountStatus.ACTIVE, new Map(), null, KYCStatus.NOT_VERIFIED, AccountStatus.ACTIVE,

View File

@ -5,21 +5,27 @@ import { wordlist } from '@scure/bip39/wordlists/english';
// ============ UserId ============ // ============ UserId ============
export class UserId { export class UserId {
constructor(public readonly value: string) { constructor(public readonly value: bigint) {
if (!value) throw new DomainError('UserId不能为空'); if (!value) throw new DomainError('UserId不能为空');
} }
static generate(): UserId { static create(value: bigint | string | number): UserId {
return new UserId(crypto.randomUUID()); if (typeof value === 'string') {
return new UserId(BigInt(value));
}
if (typeof value === 'number') {
return new UserId(BigInt(value));
} }
static create(value: string): UserId {
return new UserId(value); return new UserId(value);
} }
equals(other: UserId): boolean { equals(other: UserId): boolean {
return this.value === other.value; return this.value === other.value;
} }
toString(): string {
return this.value.toString();
}
} }
// ============ AccountSequence ============ // ============ AccountSequence ============

View File

@ -14,13 +14,15 @@ export class UserAccountRepositoryImpl implements UserAccountRepository {
async save(account: UserAccount): Promise<void> { async save(account: UserAccount): Promise<void> {
const devices = account.getAllDevices(); const devices = account.getAllDevices();
const wallets = account.getAllWalletAddresses(); const isNewAccount = account.userId.value === BigInt(0);
await this.prisma.$transaction(async (tx) => { await this.prisma.$transaction(async (tx) => {
await tx.userAccount.upsert({ let savedUserId: bigint;
where: { userId: BigInt(account.userId.value) },
create: { if (isNewAccount) {
userId: BigInt(account.userId.value), // 新账户让数据库自动生成userId
const created = await tx.userAccount.create({
data: {
accountSequence: BigInt(account.accountSequence.value), accountSequence: BigInt(account.accountSequence.value),
phoneNumber: account.phoneNumber?.value || null, phoneNumber: account.phoneNumber?.value || null,
nickname: account.nickname, nickname: account.nickname,
@ -39,7 +41,15 @@ export class UserAccountRepositoryImpl implements UserAccountRepository {
registeredAt: account.registeredAt, registeredAt: account.registeredAt,
lastLoginAt: account.lastLoginAt, lastLoginAt: account.lastLoginAt,
}, },
update: { });
savedUserId = created.userId;
// 更新聚合根的userId
(account as any)._userId = UserId.create(savedUserId);
} else {
// 已存在的账户,更新
await tx.userAccount.update({
where: { userId: BigInt(account.userId.value) },
data: {
phoneNumber: account.phoneNumber?.value || null, phoneNumber: account.phoneNumber?.value || null,
nickname: account.nickname, nickname: account.nickname,
avatarUrl: account.avatarUrl, avatarUrl: account.avatarUrl,
@ -55,13 +65,15 @@ export class UserAccountRepositoryImpl implements UserAccountRepository {
lastLoginAt: account.lastLoginAt, lastLoginAt: account.lastLoginAt,
}, },
}); });
savedUserId = account.userId.value;
}
// Sync devices // Sync devices
await tx.userDevice.deleteMany({ where: { userId: BigInt(account.userId.value) } }); await tx.userDevice.deleteMany({ where: { userId: savedUserId } });
if (devices.length > 0) { if (devices.length > 0) {
await tx.userDevice.createMany({ await tx.userDevice.createMany({
data: devices.map((d) => ({ data: devices.map((d) => ({
userId: BigInt(account.userId.value), userId: savedUserId,
deviceId: d.deviceId, deviceId: d.deviceId,
deviceName: d.deviceName, deviceName: d.deviceName,
addedAt: d.addedAt, addedAt: d.addedAt,