fix: minor

This commit is contained in:
Mohamed Marrouchi 2025-01-15 19:05:28 +01:00
parent bdf1763503
commit 0b4a1085ec
10 changed files with 44 additions and 57 deletions

View File

@ -143,7 +143,7 @@ export class AttachmentController extends BaseController<Attachment> {
);
}
const attachments = [];
const attachments: Attachment[] = [];
for (const file of files.file) {
const attachment = await this.attachmentService.store(file, {
name: file.originalname,
@ -153,7 +153,10 @@ export class AttachmentController extends BaseController<Attachment> {
createdBy: userId,
createdByRef: 'User',
});
attachments.push(attachment);
if (attachment) {
attachments.push(attachment);
}
}
return attachments;

View File

@ -132,5 +132,6 @@ export class AttachmentContextParamDto {
})
@IsString()
@IsIn(Object.values(AttachmentContext))
context?: TAttachmentContext;
@IsNotEmpty()
context: TAttachmentContext;
}

View File

@ -181,10 +181,10 @@ export class AttachmentService extends BaseService<Attachment> {
async store(
file: Buffer | Stream | Readable | Express.Multer.File,
metadata: AttachmentMetadataDto,
): Promise<Attachment | undefined> {
): Promise<Attachment | null> {
if (this.getStoragePlugin()) {
const storedDto = await this.getStoragePlugin()?.store?.(file, metadata);
return storedDto ? await this.create(storedDto) : undefined;
return storedDto ? await this.create(storedDto) : null;
} else {
const rootDir = this.getRootDirByContext(metadata.context);
const uniqueFilename = generateUniqueFilename(metadata.name);

View File

@ -8,7 +8,6 @@
import { Attachment } from '@/attachment/schemas/attachment.schema';
import { Subscriber } from '@/chat/schemas/subscriber.schema';
import { AttachmentPayload } from '@/chat/schemas/types/attachment';
import { SubscriberChannelData } from '@/chat/schemas/types/channel';
import {
IncomingMessageType,
@ -78,7 +77,6 @@ export default abstract class EventWrapper<
messageType: this.getMessageType(),
payload: this.getPayload(),
message: this.getMessage(),
attachments: this.getAttachments(),
deliveredMessages: this.getDeliveredMessages(),
watermark: this.getWatermark(),
},
@ -205,7 +203,15 @@ export default abstract class EventWrapper<
this._adapter.eventType === StdEventType.message &&
this._adapter.messageType === IncomingMessageType.attachments
) {
await this._handler.persistMessageAttachments(this);
await this._handler.persistMessageAttachments(
this as EventWrapper<
any,
any,
ChannelName,
ChannelHandler<ChannelName>,
Record<string, any>
>,
);
}
}
@ -263,13 +269,6 @@ export default abstract class EventWrapper<
return '';
}
/**
* Returns the list of received attachments
*
* @returns Received attachments message
*/
abstract getAttachments(): AttachmentPayload[];
/**
* Returns the list of delivered messages
*
@ -393,14 +392,6 @@ export class GenericEventWrapper extends EventWrapper<
throw new Error('Unknown incoming message type');
}
/**
* @returns A list of received attachments
* @deprecated - This method is deprecated
*/
getAttachments(): AttachmentPayload[] {
return [];
}
/**
* Returns the delivered messages ids
*

View File

@ -256,6 +256,10 @@ export class ChatService {
this.eventEmitter.emit('hook:stats:entry', 'new_users', 'New users');
subscriberData.channel = event.getChannelData();
subscriber = await this.subscriberService.create(subscriberData);
if (!subscriber) {
throw new Error('Unable to create a new subscriber');
}
} else {
// Already existing user profile
// Exec lastvisit hook
@ -288,18 +292,22 @@ export class ChatService {
avatar: avatar.id,
},
);
if (!subscriber) {
throw new Error('Unable to update the subscriber avatar');
}
}
}
} catch (err) {
this.logger.error(
`Unable to retrieve avatar for subscriber ${subscriber.id}`,
`Unable to retrieve avatar for subscriber ${event.getSenderForeignId()}`,
err,
);
}
}
// Set the subscriber object
event.setSender(subscriber);
event.setSender(subscriber!);
// Preprocess the event (persist attachments, ...)
if (event.preprocess) {
@ -309,7 +317,7 @@ export class ChatService {
// Trigger message received event
this.eventEmitter.emit('hook:chatbot:received', event);
if (subscriber.assignedTo) {
if (subscriber?.assignedTo) {
this.logger.debug('Conversation taken over', subscriber.assignedTo);
return;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@ -149,17 +149,6 @@ export class ConversationService extends BaseService<
lat: parseFloat(coordinates.lat.toString()),
lon: parseFloat(coordinates.lon.toString()),
};
} else if (msgType === 'attachments') {
// @TODO : deprecated in favor of geolocation msgType
const attachments = event.getAttachments();
// @ts-expect-error deprecated
if (attachments.length === 1 && attachments[0].type === 'location') {
// @ts-expect-error deprecated
const coord = attachments[0].payload.coordinates;
convo.context.user_location = { lat: 0, lon: 0 };
convo.context.user_location.lat = parseFloat(coord.lat);
convo.context.user_location.lon = parseFloat(coord.long);
}
}
// Deal with load more in the case of a list display

View File

@ -604,6 +604,11 @@ export default abstract class BaseWebChannelHandler<
try {
const { type, data } = req.body as Web.IncomingMessage;
if (!req.session?.web?.profile?.id) {
this.logger.debug('Web Channel Handler : No session');
return null;
}
// Check if any file is provided
if (type !== 'file' || !('file' in data) || !data.file) {
this.logger.debug('Web Channel Handler : No files provided');
@ -622,7 +627,7 @@ export default abstract class BaseWebChannelHandler<
type: data.type,
context: 'message_attachment',
createdByRef: 'Subscriber',
createdBy: req.session.web.profile?.id,
createdBy: req.session?.web?.profile?.id,
});
} catch (err) {
this.logger.error(
@ -677,7 +682,7 @@ export default abstract class BaseWebChannelHandler<
const file = await multerUpload;
// Check if any file is provided
if (!req.file) {
if (!file) {
this.logger.debug('Web Channel Handler : No files provided');
return null;
}

View File

@ -9,7 +9,6 @@
import { Attachment } from '@/attachment/schemas/attachment.schema';
import EventWrapper from '@/channel/lib/EventWrapper';
import { ChannelName } from '@/channel/types';
import { AttachmentPayload } from '@/chat/schemas/types/attachment';
import {
IncomingMessageType,
PayloadType,
@ -296,17 +295,6 @@ export default class WebEventWrapper<
}
}
/**
* Return the list of recieved attachments
*
* @deprecated
* @returns Received attachments message
*/
getAttachments(): AttachmentPayload[] {
const message = this.getMessage() as any;
return 'attachment' in message ? [].concat(message.attachment) : [];
}
/**
* Return the delivered messages ids
*

View File

@ -38,11 +38,11 @@ const getAdminUser = async () => {
const UserModel = mongoose.model<User>(User.name, userSchema);
const adminRole = await RoleModel.findOne({ name: 'admin' });
const user = await UserModel.findOne({ roles: [adminRole._id] }).sort({
const user = await UserModel.findOne({ roles: [adminRole!._id] }).sort({
createdAt: 'asc',
});
return user;
return user!;
};
/**
@ -638,7 +638,7 @@ const migrateAndPopulateAttachmentMessages = async ({
if ('attachment_id' in msg.message.attachment.payload) {
// Add extra attrs
await attachmentService.updateOne(
msg.message.attachment.payload.attachment_id,
msg.message.attachment.payload.attachment_id as string,
{
createdByRef: msg.sender ? 'Subscriber' : 'User',
createdBy: msg.sender ? msg.sender : adminUser.id,
@ -679,6 +679,8 @@ const migrateAndPopulateAttachmentMessages = async ({
if (attachment) {
await updateAttachmentId(msg._id, attachment.id);
} else {
logger.warn(`Unable to store attachment for message ${msg._id}`);
}
}
} else {

View File

@ -24,7 +24,7 @@ export const attachmentFixtures: AttachmentCreateDto[] = [
},
context: 'content_attachment',
createdByRef: 'User',
createdBy: null,
createdBy: '9'.repeat(24),
},
{
name: 'store2.jpg',
@ -38,7 +38,7 @@ export const attachmentFixtures: AttachmentCreateDto[] = [
},
context: 'content_attachment',
createdByRef: 'User',
createdBy: null,
createdBy: '9'.repeat(24),
},
];