This commit is contained in:
parent
77725bd769
commit
c6c2545095
|
|
@ -96,8 +96,9 @@ export class UserAccount {
|
|||
params.initialDeviceId, params.deviceName || '未命名设备', new Date(), new Date(),
|
||||
));
|
||||
|
||||
// UserID将由数据库自动生成(autoincrement),这里使用临时值0
|
||||
const account = new UserAccount(
|
||||
UserId.generate(), params.accountSequence, devices, null,
|
||||
UserId.create(0), params.accountSequence, devices, null,
|
||||
`用户${params.accountSequence.value}`, null, params.inviterSequence,
|
||||
ReferralCode.generate(), params.province, params.city, null,
|
||||
new Map(), null, KYCStatus.NOT_VERIFIED, AccountStatus.ACTIVE,
|
||||
|
|
|
|||
|
|
@ -5,21 +5,27 @@ import { wordlist } from '@scure/bip39/wordlists/english';
|
|||
|
||||
// ============ UserId ============
|
||||
export class UserId {
|
||||
constructor(public readonly value: string) {
|
||||
constructor(public readonly value: bigint) {
|
||||
if (!value) throw new DomainError('UserId不能为空');
|
||||
}
|
||||
|
||||
static generate(): UserId {
|
||||
return new UserId(crypto.randomUUID());
|
||||
}
|
||||
|
||||
static create(value: string): UserId {
|
||||
static create(value: bigint | string | number): UserId {
|
||||
if (typeof value === 'string') {
|
||||
return new UserId(BigInt(value));
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
return new UserId(BigInt(value));
|
||||
}
|
||||
return new UserId(value);
|
||||
}
|
||||
|
||||
equals(other: UserId): boolean {
|
||||
return this.value === other.value;
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.value.toString();
|
||||
}
|
||||
}
|
||||
|
||||
// ============ AccountSequence ============
|
||||
|
|
|
|||
|
|
@ -14,54 +14,66 @@ export class UserAccountRepositoryImpl implements UserAccountRepository {
|
|||
|
||||
async save(account: UserAccount): Promise<void> {
|
||||
const devices = account.getAllDevices();
|
||||
const wallets = account.getAllWalletAddresses();
|
||||
const isNewAccount = account.userId.value === BigInt(0);
|
||||
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
await tx.userAccount.upsert({
|
||||
where: { userId: BigInt(account.userId.value) },
|
||||
create: {
|
||||
userId: BigInt(account.userId.value),
|
||||
accountSequence: BigInt(account.accountSequence.value),
|
||||
phoneNumber: account.phoneNumber?.value || null,
|
||||
nickname: account.nickname,
|
||||
avatarUrl: account.avatarUrl,
|
||||
inviterSequence: account.inviterSequence ? BigInt(account.inviterSequence.value) : null,
|
||||
referralCode: account.referralCode.value,
|
||||
provinceCode: account.province.value,
|
||||
cityCode: account.city.value,
|
||||
address: account.addressDetail,
|
||||
kycStatus: account.kycStatus,
|
||||
realName: account.kycInfo?.realName || null,
|
||||
idCardNumber: account.kycInfo?.idCardNumber || null,
|
||||
idCardFrontUrl: account.kycInfo?.idCardFrontUrl || null,
|
||||
idCardBackUrl: account.kycInfo?.idCardBackUrl || null,
|
||||
status: account.status,
|
||||
registeredAt: account.registeredAt,
|
||||
lastLoginAt: account.lastLoginAt,
|
||||
},
|
||||
update: {
|
||||
phoneNumber: account.phoneNumber?.value || null,
|
||||
nickname: account.nickname,
|
||||
avatarUrl: account.avatarUrl,
|
||||
provinceCode: account.province.value,
|
||||
cityCode: account.city.value,
|
||||
address: account.addressDetail,
|
||||
kycStatus: account.kycStatus,
|
||||
realName: account.kycInfo?.realName || null,
|
||||
idCardNumber: account.kycInfo?.idCardNumber || null,
|
||||
idCardFrontUrl: account.kycInfo?.idCardFrontUrl || null,
|
||||
idCardBackUrl: account.kycInfo?.idCardBackUrl || null,
|
||||
status: account.status,
|
||||
lastLoginAt: account.lastLoginAt,
|
||||
},
|
||||
});
|
||||
let savedUserId: bigint;
|
||||
|
||||
if (isNewAccount) {
|
||||
// 新账户,让数据库自动生成userId
|
||||
const created = await tx.userAccount.create({
|
||||
data: {
|
||||
accountSequence: BigInt(account.accountSequence.value),
|
||||
phoneNumber: account.phoneNumber?.value || null,
|
||||
nickname: account.nickname,
|
||||
avatarUrl: account.avatarUrl,
|
||||
inviterSequence: account.inviterSequence ? BigInt(account.inviterSequence.value) : null,
|
||||
referralCode: account.referralCode.value,
|
||||
provinceCode: account.province.value,
|
||||
cityCode: account.city.value,
|
||||
address: account.addressDetail,
|
||||
kycStatus: account.kycStatus,
|
||||
realName: account.kycInfo?.realName || null,
|
||||
idCardNumber: account.kycInfo?.idCardNumber || null,
|
||||
idCardFrontUrl: account.kycInfo?.idCardFrontUrl || null,
|
||||
idCardBackUrl: account.kycInfo?.idCardBackUrl || null,
|
||||
status: account.status,
|
||||
registeredAt: account.registeredAt,
|
||||
lastLoginAt: account.lastLoginAt,
|
||||
},
|
||||
});
|
||||
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,
|
||||
nickname: account.nickname,
|
||||
avatarUrl: account.avatarUrl,
|
||||
provinceCode: account.province.value,
|
||||
cityCode: account.city.value,
|
||||
address: account.addressDetail,
|
||||
kycStatus: account.kycStatus,
|
||||
realName: account.kycInfo?.realName || null,
|
||||
idCardNumber: account.kycInfo?.idCardNumber || null,
|
||||
idCardFrontUrl: account.kycInfo?.idCardFrontUrl || null,
|
||||
idCardBackUrl: account.kycInfo?.idCardBackUrl || null,
|
||||
status: account.status,
|
||||
lastLoginAt: account.lastLoginAt,
|
||||
},
|
||||
});
|
||||
savedUserId = account.userId.value;
|
||||
}
|
||||
|
||||
// Sync devices
|
||||
await tx.userDevice.deleteMany({ where: { userId: BigInt(account.userId.value) } });
|
||||
await tx.userDevice.deleteMany({ where: { userId: savedUserId } });
|
||||
if (devices.length > 0) {
|
||||
await tx.userDevice.createMany({
|
||||
data: devices.map((d) => ({
|
||||
userId: BigInt(account.userId.value),
|
||||
userId: savedUserId,
|
||||
deviceId: d.deviceId,
|
||||
deviceName: d.deviceName,
|
||||
addedAt: d.addedAt,
|
||||
|
|
|
|||
Loading…
Reference in New Issue