fix document 500 error
This commit is contained in:
@@ -124,17 +124,47 @@ export class DocumentsService {
|
||||
}
|
||||
|
||||
private toResponse(document: Awaited<ReturnType<PrismaService['userDocument']['create']>>) {
|
||||
const metadata = document.metadata as { encryptedPayload?: string } | null;
|
||||
return {
|
||||
id: document.id,
|
||||
userId: document.userId,
|
||||
type: document.type,
|
||||
number: this.encryption.decrypt(document.number),
|
||||
issuedAt: document.issuedAt?.toISOString(),
|
||||
expiresAt: document.expiresAt?.toISOString(),
|
||||
metadataJson: metadata?.encryptedPayload ? this.encryption.decrypt(metadata.encryptedPayload) : undefined,
|
||||
createdAt: document.createdAt.toISOString(),
|
||||
updatedAt: document.updatedAt.toISOString()
|
||||
number: this.encryption.decryptIfNeeded(document.number),
|
||||
issuedAt: this.formatOptionalDate(document.issuedAt),
|
||||
expiresAt: this.formatOptionalDate(document.expiresAt),
|
||||
metadataJson: this.resolveMetadataJson(document.metadata),
|
||||
createdAt: this.formatRequiredDate(document.createdAt),
|
||||
updatedAt: this.formatRequiredDate(document.updatedAt)
|
||||
};
|
||||
}
|
||||
|
||||
private resolveMetadataJson(metadata: Prisma.JsonValue | null): string | undefined {
|
||||
if (!metadata || typeof metadata !== 'object' || Array.isArray(metadata)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const record = metadata as Record<string, unknown>;
|
||||
if (typeof record.encryptedPayload === 'string') {
|
||||
return this.encryption.decryptIfNeeded(record.encryptedPayload);
|
||||
}
|
||||
|
||||
if (Object.keys(record).length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return JSON.stringify(record);
|
||||
}
|
||||
|
||||
private formatOptionalDate(value: Date | null | undefined): string | undefined {
|
||||
if (!value || Number.isNaN(value.getTime())) {
|
||||
return undefined;
|
||||
}
|
||||
return value.toISOString();
|
||||
}
|
||||
|
||||
private formatRequiredDate(value: Date): string {
|
||||
if (Number.isNaN(value.getTime())) {
|
||||
return new Date(0).toISOString();
|
||||
}
|
||||
return value.toISOString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user