feat: catch exceptions link attachment service

This commit is contained in:
Ahmed Bouhuolia
2024-05-29 22:15:31 +02:00
parent 6e50de1d28
commit 308a4f62ae
13 changed files with 53 additions and 34 deletions

View File

@@ -7,6 +7,7 @@ import {
} from './_utils';
import HasTenancyService from '../Tenancy/TenancyService';
import { ServiceError } from '@/exceptions';
import { ERRORS } from './constants';
@Service()
export class LinkAttachment {
@@ -32,18 +33,21 @@ export class LinkAttachment {
const LinkModel = models[modelRef];
validateLinkModelExists(LinkModel);
const foundFile = await Document.query(trx)
.findOne('key', filekey)
.throwIfNotFound();
const foundLinkModel = await LinkModel.query(trx).findById(modelId);
validateLinkModelEntryExists(foundLinkModel);
const foundLinks = await DocumentLink.query(trx)
.where('modelRef', modelRef)
.where('modelId', modelId);
.where('modelId', modelId)
.where('documentId', foundFile.id);
if (foundLinks.length > 0) {
throw new ServiceError('DOCUMENT_LINK_ALREADY_LINKED');
throw new ServiceError(ERRORS.DOCUMENT_LINK_ALREADY_LINKED);
}
const foundFile = await Document.query(trx).findOne('key', filekey);
await DocumentLink.query(trx).insert({
modelRef,
modelId,
@@ -67,15 +71,12 @@ export class LinkAttachment {
modelId: number,
trx?: Knex.Transaction
) {
await bluebird.map(
filekeys,
(fieldKey: string) =>
this.link(tenantId, fieldKey, modelRef, modelId, trx),
{
concurrency: CONCURRENCY_ASYNC,
return bluebird.each(filekeys, async (fieldKey: string) => {
try {
await this.link(tenantId, fieldKey, modelRef, modelId, trx);
} catch {
// Ignore catching exceptions in bulk action.
}
);
});
}
}
const CONCURRENCY_ASYNC = 10;

View File

@@ -34,7 +34,7 @@ export class UnlinkAttachment {
const foundLinkModel = await LinkModel.query(trx).findById(modelId);
validateLinkModelEntryExists(foundLinkModel);
const document = await Document.query().findOne('key', filekey);
const document = await Document.query(trx).findOne('key', filekey);
// Delete the document link.
await DocumentLink.query(trx)
@@ -59,14 +59,13 @@ export class UnlinkAttachment {
modelId: number,
trx?: Knex.Transaction
): Promise<void> {
await bluebird.map(
filekeys,
(fieldKey: string) =>
this.unlink(tenantId, fieldKey, modelRef, modelId, trx),
{
concurrency: CONCURRENCY_ASYNC,
await bluebird.each(filekeys, (fieldKey: string) => {
try {
this.unlink(tenantId, fieldKey, modelRef, modelId, trx);
} catch {
// Ignore catching exceptions on bulk action.
}
);
});
}
/**
@@ -124,5 +123,3 @@ export class UnlinkAttachment {
await this.bulkUnlink(tenantId, modelLinkKeys, modelRef, modelId, trx);
}
}
const CONCURRENCY_ASYNC = 10;

View File

@@ -1,4 +1,5 @@
export enum ERRORS {
DOCUMENT_LINK_REF_INVALID = 'DOCUMENT_LINK_REF_INVALID',
DOCUMENT_LINK_ID_INVALID = 'DOCUMENT_LINK_ID_INVALID',
DOCUMENT_LINK_ALREADY_LINKED = 'DOCUMENT_LINK_ALREADY_LINKED'
}

View File

@@ -96,13 +96,15 @@ export class AttachmentsOnBills {
tenantId,
billDTO,
bill,
trx
}: IBillEditedPayload) {
const keys = billDTO.attachments?.map((attachment) => attachment.key);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
tenantId,
keys,
'Bill',
bill.id
bill.id,
trx
);
}

View File

@@ -96,6 +96,7 @@ export class AttachmentsOnCreditNote {
tenantId,
creditNoteEditDTO,
oldCreditNote,
trx,
}: ICreditNoteEditedPayload) {
const keys = creditNoteEditDTO.attachments?.map(
(attachment) => attachment.key
@@ -104,7 +105,8 @@ export class AttachmentsOnCreditNote {
tenantId,
keys,
'CreditNote',
oldCreditNote.id
oldCreditNote.id,
trx
);
}

View File

@@ -96,13 +96,15 @@ export class AttachmentsOnExpenses {
tenantId,
expenseDTO,
expense,
trx,
}: IExpenseEventEditPayload) {
const keys = expenseDTO.attachments?.map((attachment) => attachment.key);
await this.unlinkAttachmentService.unlinkUnpresentedKeys(
tenantId,
keys,
'Expense',
expense.id
expense.id,
trx
);
}

View File

@@ -98,6 +98,7 @@ export class AttachmentsOnManualJournals {
tenantId,
manualJournalDTO,
manualJournal,
trx
}: IManualJournalEventEditedPayload) {
const keys = manualJournalDTO.attachments?.map(
(attachment) => attachment.key
@@ -106,7 +107,8 @@ export class AttachmentsOnManualJournals {
tenantId,
keys,
'SaleInvoice',
manualJournal.id
manualJournal.id,
trx
);
}

View File

@@ -12,7 +12,7 @@ import { ValidateAttachments } from '../ValidateAttachments';
import { UnlinkAttachment } from '../UnlinkAttachment';
@Service()
export class AttachmentsOnBillPayments{
export class AttachmentsOnBillPayments {
@Inject()
private linkAttachmentService: LinkAttachment;
@@ -98,6 +98,7 @@ export class AttachmentsOnBillPayments{
tenantId,
billPaymentDTO,
oldBillPayment,
trx,
}: IBillPaymentEventEditedPayload) {
const keys = billPaymentDTO.attachments?.map(
(attachment) => attachment.key
@@ -106,7 +107,8 @@ export class AttachmentsOnBillPayments{
tenantId,
keys,
'BillPayment',
oldBillPayment.id
oldBillPayment.id,
trx
);
}

View File

@@ -98,6 +98,7 @@ export class AttachmentsOnPaymentsReceived {
tenantId,
paymentReceiveDTO,
oldPaymentReceive,
trx,
}: IPaymentReceiveEditedPayload) {
const keys = paymentReceiveDTO.attachments?.map(
(attachment) => attachment.key
@@ -106,7 +107,8 @@ export class AttachmentsOnPaymentsReceived {
tenantId,
keys,
'PaymentReceive',
oldPaymentReceive.id
oldPaymentReceive.id,
trx
);
}

View File

@@ -98,6 +98,7 @@ export class AttachmentsOnSaleEstimates {
tenantId,
estimateDTO,
oldSaleEstimate,
trx
}: ISaleEstimateEditedPayload) {
const keys = estimateDTO.attachments?.map((attachment) => attachment.key);
@@ -105,7 +106,8 @@ export class AttachmentsOnSaleEstimates {
tenantId,
keys,
'SaleEstimate',
oldSaleEstimate.id
oldSaleEstimate.id,
trx
);
}

View File

@@ -98,6 +98,7 @@ export class AttachmentsOnSaleInvoiceCreated {
tenantId,
saleInvoiceDTO,
saleInvoice,
trx,
}: ISaleInvoiceEditedPayload) {
// if (isEmpty(saleInvoiceDTO.attachments)) return;
@@ -108,7 +109,8 @@ export class AttachmentsOnSaleInvoiceCreated {
tenantId,
keys,
'SaleInvoice',
saleInvoice.id
saleInvoice.id,
trx
);
}

View File

@@ -98,6 +98,7 @@ export class AttachmentsOnSaleReceipt {
tenantId,
saleReceiptDTO,
saleReceipt,
trx,
}: ISaleReceiptEditedPayload) {
const keys = saleReceiptDTO.attachments?.map(
(attachment) => attachment.key
@@ -106,7 +107,8 @@ export class AttachmentsOnSaleReceipt {
tenantId,
keys,
'SaleReceipt',
saleReceipt.id
saleReceipt.id,
trx
);
}

View File

@@ -98,6 +98,7 @@ export class AttachmentsOnVendorCredits {
tenantId,
vendorCreditDTO,
oldVendorCredit,
trx,
}: IVendorCreditEditedPayload) {
const keys = vendorCreditDTO.attachments?.map(
(attachment) => attachment.key
@@ -106,7 +107,8 @@ export class AttachmentsOnVendorCredits {
tenantId,
keys,
'VendorCredit',
oldVendorCredit.id
oldVendorCredit.id,
trx
);
}