feat: add s3 env variables to .env.example

This commit is contained in:
Ahmed Bouhuolia
2024-05-30 17:58:58 +02:00
parent 8ed24748ec
commit 7ff36e8c4f
5 changed files with 35 additions and 21 deletions

View File

@@ -1,13 +1,19 @@
import { DeleteObjectCommand } from '@aws-sdk/client-s3';
import { s3 } from '@/lib/S3/S3';
import { Inject, Service } from 'typedi';
import { s3 } from '@/lib/S3/S3';
import HasTenancyService from '../Tenancy/TenancyService';
import config from '@/config';
import UnitOfWork from '../UnitOfWork';
import { Knex } from 'knex';
@Service()
export class DeleteAttachment {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private uow: UnitOfWork;
/**
* Deletes the give file attachment file key.
* @param {number} tenantId
@@ -17,7 +23,7 @@ export class DeleteAttachment {
const { Document, DocumentLink } = this.tenancy.models(tenantId);
const params = {
Bucket: process.env.AWS_BUCKET,
Bucket: config.s3.bucket,
Key: filekey,
};
await s3.send(new DeleteObjectCommand(params));
@@ -26,10 +32,14 @@ export class DeleteAttachment {
.findOne('key', filekey)
.throwIfNotFound();
// Delete all document links
await DocumentLink.query().where('documentId', foundDocument.id).delete();
await this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Delete all document links
await DocumentLink.query(trx)
.where('documentId', foundDocument.id)
.delete();
// Delete thedocument.
await Document.query().findById(foundDocument.id).delete();
// Delete thedocument.
await Document.query(trx).findById(foundDocument.id).delete();
});
}
}