rwadurian/backend/services/identity-service/src/infrastructure/persistence/mappers/user-account.mapper.ts

58 lines
2.1 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { UserAccount } from '@/domain/aggregates/user-account/user-account.aggregate';
import { WalletAddress } from '@/domain/entities/wallet-address.entity';
import { DeviceInfo, KYCInfo, KYCStatus, AccountStatus, ChainType, AddressStatus } from '@/domain/value-objects';
import { UserAccountEntity } from '../entities/user-account.entity';
@Injectable()
export class UserAccountMapper {
toDomain(entity: UserAccountEntity): UserAccount {
const devices = (entity.devices || []).map(
(d) => new DeviceInfo(d.deviceId, d.deviceName || '未命名设备', d.addedAt, d.lastActiveAt),
);
const wallets = (entity.walletAddresses || []).map((w) =>
WalletAddress.reconstruct({
addressId: w.addressId.toString(),
userId: w.userId.toString(),
chainType: w.chainType as ChainType,
address: w.address,
encryptedMnemonic: w.encryptedMnemonic || '',
status: w.status as AddressStatus,
boundAt: w.boundAt,
}),
);
const kycInfo =
entity.realName && entity.idCardNumber
? KYCInfo.create({
realName: entity.realName,
idCardNumber: entity.idCardNumber,
idCardFrontUrl: entity.idCardFrontUrl || '',
idCardBackUrl: entity.idCardBackUrl || '',
})
: null;
return UserAccount.reconstruct({
userId: entity.userId.toString(),
accountSequence: Number(entity.accountSequence),
devices,
phoneNumber: entity.phoneNumber,
nickname: entity.nickname,
avatarUrl: entity.avatarUrl,
inviterSequence: entity.inviterSequence ? Number(entity.inviterSequence) : null,
referralCode: entity.referralCode,
province: entity.provinceCode,
city: entity.cityCode,
address: entity.address,
walletAddresses: wallets,
kycInfo,
kycStatus: entity.kycStatus as KYCStatus,
status: entity.status as AccountStatus,
registeredAt: entity.registeredAt,
lastLoginAt: entity.lastLoginAt,
updatedAt: entity.updatedAt,
});
}
}