fix: content vs element

This commit is contained in:
Mohamed Marrouchi
2024-11-19 12:29:56 +01:00
parent d0f323008d
commit 7e5884a82f
7 changed files with 79 additions and 81 deletions

View File

@@ -14,7 +14,10 @@ import { AttachmentRepository } from '@/attachment/repositories/attachment.repos
import { AttachmentModel } from '@/attachment/schemas/attachment.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
import { FileType } from '@/chat/schemas/types/attachment';
import { OutgoingMessageFormat } from '@/chat/schemas/types/message';
import {
ContentElement,
OutgoingMessageFormat,
} from '@/chat/schemas/types/message';
import { ContentOptions } from '@/chat/schemas/types/options';
import { LoggerService } from '@/logger/logger.service';
import { IGNORED_TEST_FIELDS } from '@/utils/test/constants';
@@ -159,7 +162,10 @@ describe('ContentService', () => {
},
];
it('should return all content attachment ids', () => {
const result = contentService.getAttachmentIds(contents, 'image');
const result = contentService.getAttachmentIds(
contents.map(Content.toElement),
'image',
);
expect(result).toEqual(['123', '456']);
});
@@ -172,22 +178,19 @@ describe('ContentService', () => {
describe('populateAttachments', () => {
it('should return populated content', async () => {
const storeContents = await contentService.find({ title: /^store/ });
const populatedStoreContents: Content[] = await Promise.all(
storeContents.map(async (store) => {
const attachmentId = store.dynamicFields.image.payload.attachment_id;
const elements: ContentElement[] = await Promise.all(
storeContents.map(Content.toElement).map(async (store) => {
const attachmentId = store.image.payload.attachment_id;
if (attachmentId) {
const attachment = await attachmentService.findOne(attachmentId);
if (attachment) {
return {
...store,
dynamicFields: {
...store.dynamicFields,
image: {
type: 'image',
payload: {
...attachment,
url: `http://localhost:4000/attachment/download/${attachment.id}/${attachment.name}`,
},
image: {
type: 'image',
payload: {
...attachment,
url: `http://localhost:4000/attachment/download/${attachment.id}/${attachment.name}`,
},
},
};
@@ -197,10 +200,10 @@ describe('ContentService', () => {
}),
);
const result = await contentService.populateAttachments(
storeContents,
storeContents.map(Content.toElement),
'image',
);
expect(result).toEqualPayload(populatedStoreContents);
expect(result).toEqualPayload(elements);
});
});
@@ -221,9 +224,7 @@ describe('ContentService', () => {
{ status: true },
{ skip: 0, limit: 10, sort: ['createdAt', 'desc'] },
);
const flattenedElements = actualData.map((content) =>
Content.flatDynamicFields(content),
);
const flattenedElements = actualData.map(Content.toElement);
const content = await contentService.getContent(contentOptions, 0);
expect(content?.elements).toEqualPayload(flattenedElements, [
...IGNORED_TEST_FIELDS,
@@ -237,9 +238,7 @@ describe('ContentService', () => {
{ status: true, entity: contentType.id },
{ skip: 0, limit: 10, sort: ['createdAt', 'desc'] },
);
const flattenedElements = actualData.map((content) =>
Content.flatDynamicFields(content),
);
const flattenedElements = actualData.map(Content.toElement);
const content = await contentService.getContent(
{
...contentOptions,
@@ -257,9 +256,7 @@ describe('ContentService', () => {
{ status: true, entity: contentType.id, title: /^Jean/ },
{ skip: 0, limit: 10, sort: ['createdAt', 'desc'] },
);
const flattenedElements = actualData.map((content) =>
Content.flatDynamicFields(content),
);
const flattenedElements = actualData.map(Content.toElement);
const content = await contentService.getContent(
{
...contentOptions,
@@ -276,9 +273,7 @@ describe('ContentService', () => {
{ status: true },
{ skip: 2, limit: 2, sort: ['createdAt', 'desc'] },
);
const flattenedElements = actualData.map((content) =>
Content.flatDynamicFields(content),
);
const flattenedElements = actualData.map(Content.toElement);
const content = await contentService.getContent(
{
...contentOptions,

View File

@@ -11,7 +11,10 @@ import { Injectable } from '@nestjs/common';
import { Attachment } from '@/attachment/schemas/attachment.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
import { WithUrl } from '@/chat/schemas/types/attachment';
import { StdOutgoingListMessage } from '@/chat/schemas/types/message';
import {
ContentElement,
StdOutgoingListMessage,
} from '@/chat/schemas/types/message';
import { ContentOptions } from '@/chat/schemas/types/options';
import { LoggerService } from '@/logger/logger.service';
import { BaseService } from '@/utils/generics/base-service';
@@ -57,10 +60,10 @@ export class ContentService extends BaseService<
*
* @return A list of attachment IDs.
*/
getAttachmentIds(contents: Content[], attachmentFieldName: string) {
getAttachmentIds(contents: ContentElement[], attachmentFieldName: string) {
return contents.reduce((acc, content) => {
if (attachmentFieldName in content.dynamicFields) {
const attachment = content.dynamicFields[attachmentFieldName];
if (attachmentFieldName in content) {
const attachment = content[attachmentFieldName];
if (
typeof attachment === 'object' &&
@@ -84,16 +87,16 @@ export class ContentService extends BaseService<
/**
* Populates attachment fields within content entities with detailed attachment information.
*
* @param contents - An array of content entities.
* @param elements - An array of content entities.
* @param attachmentFieldName - The name of the attachment field to populate.
*
* @return A list of content with populated attachment data.
*/
async populateAttachments(
contents: Content[],
elements: ContentElement[],
attachmentFieldName: string,
): Promise<Content[]> {
const attachmentIds = this.getAttachmentIds(contents, attachmentFieldName);
): Promise<ContentElement[]> {
const attachmentIds = this.getAttachmentIds(elements, attachmentFieldName);
if (attachmentIds.length > 0) {
const attachments = await this.attachmentService.find({
@@ -107,8 +110,8 @@ export class ContentService extends BaseService<
},
{} as { [key: string]: WithUrl<Attachment> },
);
const populatedContents = contents.map((content) => {
const attachmentField = content.dynamicFields[attachmentFieldName];
const populatedContents = elements.map((content) => {
const attachmentField = content[attachmentFieldName];
if (
typeof attachmentField === 'object' &&
'attachment_id' in attachmentField.payload
@@ -116,17 +119,14 @@ export class ContentService extends BaseService<
const attachmentId = attachmentField?.payload?.attachment_id;
return {
...content,
dynamicFields: {
...content.dynamicFields,
[attachmentFieldName]: {
type: attachmentField.type,
payload: {
...(attachmentsById[attachmentId] || attachmentField.payload),
url: Attachment.getAttachmentUrl(
attachmentId,
attachmentsById[attachmentId].name,
),
},
[attachmentFieldName]: {
type: attachmentField.type,
payload: {
...(attachmentsById[attachmentId] || attachmentField.payload),
url: Attachment.getAttachmentUrl(
attachmentId,
attachmentsById[attachmentId].name,
),
},
},
};
@@ -136,7 +136,7 @@ export class ContentService extends BaseService<
});
return populatedContents;
}
return contents;
return elements;
}
/**
@@ -175,21 +175,15 @@ export class ContentService extends BaseService<
limit,
sort: ['createdAt', 'desc'],
});
const elements = contents.map(Content.toElement);
const attachmentFieldName = options.fields.image_url;
if (attachmentFieldName) {
// Populate attachment when there's an image field
const populatedContents = await this.populateAttachments(
contents,
attachmentFieldName,
);
const flatContent = populatedContents.map((content) => ({
...content,
...content.dynamicFields,
dynamicFields: undefined,
}));
return {
elements: flatContent,
elements: await this.populateAttachments(
elements,
attachmentFieldName,
),
pagination: {
total,
skip,
@@ -198,7 +192,7 @@ export class ContentService extends BaseService<
};
}
return {
elements: contents,
elements,
pagination: {
total,
skip,