87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
/**
|
|
* Party Share Mapper
|
|
*
|
|
* Maps between domain PartyShare entity and persistence entity.
|
|
*/
|
|
|
|
import { Injectable } from '@nestjs/common';
|
|
import { PartyShare } from '../../../domain/entities/party-share.entity';
|
|
import {
|
|
ShareId,
|
|
PartyId,
|
|
SessionId,
|
|
ShareData,
|
|
PublicKey,
|
|
Threshold,
|
|
} from '../../../domain/value-objects';
|
|
import { PartyShareType, PartyShareStatus } from '../../../domain/enums';
|
|
|
|
/**
|
|
* Persistence entity structure (matches Prisma model)
|
|
*/
|
|
export interface PartySharePersistence {
|
|
id: string;
|
|
partyId: string;
|
|
sessionId: string;
|
|
shareType: string;
|
|
shareData: string; // JSON string
|
|
publicKey: string; // Hex string
|
|
thresholdN: number;
|
|
thresholdT: number;
|
|
status: string;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
lastUsedAt: Date | null;
|
|
}
|
|
|
|
@Injectable()
|
|
export class PartyShareMapper {
|
|
/**
|
|
* Convert persistence entity to domain entity
|
|
*/
|
|
toDomain(entity: PartySharePersistence): PartyShare {
|
|
const shareDataJson = JSON.parse(entity.shareData);
|
|
|
|
return PartyShare.reconstruct({
|
|
id: ShareId.create(entity.id),
|
|
partyId: PartyId.create(entity.partyId),
|
|
sessionId: SessionId.create(entity.sessionId),
|
|
shareType: entity.shareType as PartyShareType,
|
|
shareData: ShareData.fromJSON(shareDataJson),
|
|
publicKey: PublicKey.fromHex(entity.publicKey),
|
|
threshold: Threshold.create(entity.thresholdN, entity.thresholdT),
|
|
status: entity.status as PartyShareStatus,
|
|
createdAt: entity.createdAt,
|
|
updatedAt: entity.updatedAt,
|
|
lastUsedAt: entity.lastUsedAt || undefined,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Convert domain entity to persistence entity
|
|
*/
|
|
toPersistence(domain: PartyShare): PartySharePersistence {
|
|
return {
|
|
id: domain.id.value,
|
|
partyId: domain.partyId.value,
|
|
sessionId: domain.sessionId.value,
|
|
shareType: domain.shareType,
|
|
shareData: JSON.stringify(domain.shareData.toJSON()),
|
|
publicKey: domain.publicKey.toHex(),
|
|
thresholdN: domain.threshold.n,
|
|
thresholdT: domain.threshold.t,
|
|
status: domain.status,
|
|
createdAt: domain.createdAt,
|
|
updatedAt: domain.updatedAt,
|
|
lastUsedAt: domain.lastUsedAt || null,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Convert multiple persistence entities to domain entities
|
|
*/
|
|
toDomainList(entities: PartySharePersistence[]): PartyShare[] {
|
|
return entities.map(entity => this.toDomain(entity));
|
|
}
|
|
}
|