Improve collaboration request details response
This commit is contained in:
@@ -294,7 +294,10 @@ export class CollaborationRequestsService {
|
||||
this.collaborationRequestModel.countDocuments(filter).exec(),
|
||||
]);
|
||||
|
||||
return buildPaginatedResponse(items, { page, limit, total, offset: skip });
|
||||
return buildPaginatedResponse(
|
||||
items.map((item: CollaborationRequestDocument) => this.serializeRequest(item)),
|
||||
{ page, limit, total, offset: skip },
|
||||
);
|
||||
}
|
||||
|
||||
async approve(targetUserId: string, requestId: string) {
|
||||
@@ -510,7 +513,7 @@ export class CollaborationRequestsService {
|
||||
throw new NotFoundException('Collaboration request not found');
|
||||
}
|
||||
|
||||
return request;
|
||||
return this.serializeRequest(request);
|
||||
}
|
||||
|
||||
private async createCollaborationNotification(options: {
|
||||
@@ -538,6 +541,9 @@ export class CollaborationRequestsService {
|
||||
metadata: {
|
||||
...(options.metadata ?? {}),
|
||||
collaborationRequestId: options.requestId,
|
||||
requestId: options.requestId,
|
||||
status: options.metadata?.status ?? 'pending',
|
||||
postId: options.resourceType === 'post' ? options.referenceId : options.metadata?.postId,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -594,4 +600,90 @@ export class CollaborationRequestsService {
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
private serializeRequest(request: unknown) {
|
||||
const plain = this.toPlainRecord(request);
|
||||
const requester = this.serializeUser(plain.requesterId);
|
||||
const targetUser = this.serializeUser(plain.targetUserId);
|
||||
const post = this.serializePost(plain.postId);
|
||||
const postId = this.extractEntityId(plain.postId) || plain.postId;
|
||||
|
||||
return {
|
||||
...plain,
|
||||
id: this.extractEntityId(plain) || plain.id,
|
||||
requesterId: plain.requesterId,
|
||||
targetUserId: plain.targetUserId,
|
||||
postId,
|
||||
requester,
|
||||
targetUser,
|
||||
post,
|
||||
attachmentUrl: typeof plain.attachmentUrl === 'string' ? plain.attachmentUrl : '',
|
||||
attachmentType: plain.attachmentType ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
private serializeUser(value: unknown) {
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const plain = this.toPlainRecord(value);
|
||||
const id = this.extractEntityId(plain) || this.extractEntityId(value);
|
||||
|
||||
return {
|
||||
...plain,
|
||||
_id: plain._id ?? id,
|
||||
id,
|
||||
username: plain.username ?? '',
|
||||
name: plain.name ?? '',
|
||||
stageName: plain.stageName ?? '',
|
||||
avatar: plain.avatar ?? '',
|
||||
isVerified: Boolean(plain.isVerified),
|
||||
};
|
||||
}
|
||||
|
||||
private serializePost(value: unknown) {
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const plain = this.toPlainRecord(value);
|
||||
const id = this.extractEntityId(plain) || this.extractEntityId(value);
|
||||
|
||||
return {
|
||||
...plain,
|
||||
_id: plain._id ?? id,
|
||||
id,
|
||||
};
|
||||
}
|
||||
|
||||
private toPlainRecord(value: unknown): Record<string, any> {
|
||||
if (!value) {
|
||||
return {};
|
||||
}
|
||||
|
||||
if (typeof value === 'string' || value instanceof Types.ObjectId) {
|
||||
const id = this.extractEntityId(value);
|
||||
return { _id: id, id };
|
||||
}
|
||||
|
||||
if (typeof value === 'object') {
|
||||
const maybeDocument = value as { toJSON?: () => unknown; toObject?: () => unknown };
|
||||
if (typeof maybeDocument.toJSON === 'function') {
|
||||
const json = maybeDocument.toJSON();
|
||||
if (json && typeof json === 'object') {
|
||||
return json as Record<string, any>;
|
||||
}
|
||||
}
|
||||
if (typeof maybeDocument.toObject === 'function') {
|
||||
const object = maybeDocument.toObject();
|
||||
if (object && typeof object === 'object') {
|
||||
return object as Record<string, any>;
|
||||
}
|
||||
}
|
||||
return { ...(value as Record<string, any>) };
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user