mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
feat: catch exceptions link attachment service
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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'
|
||||
}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user