import { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { IAttachment } from '../../core/interfaces/attachment.interface'; import { AbstractAttachmentDto } from './../dtos/abstract-attachment.dto'; @Injectable() export class AbstractAttachmentService { constructor( @InjectModel('Attachment') private readonly attachmentModel: Model, ) {} /** * Add attachment * * @param {any[]} files * @param {string} userId * @returns {Promise} * @memberof AbstractAttachmentService */ async addAttachments(files: any[], userId: string): Promise { const attachments: IAttachment[] = []; for (const file of files) { // Get the file properties const { filename, originalname, mimetype, size } = file; // Form the attachment object let attachment = new AbstractAttachmentDto(filename, originalname, mimetype, size, true); // Collect all attachments attachments.push(new this.attachmentModel(attachment)); } // Persist the data return await this.attachmentModel.insertMany(attachments); } }