diff --git a/server/src/api/controllers/Sales/SalesEstimates.ts b/server/src/api/controllers/Sales/SalesEstimates.ts index 14beda35c..c8573c56c 100644 --- a/server/src/api/controllers/Sales/SalesEstimates.ts +++ b/server/src/api/controllers/Sales/SalesEstimates.ts @@ -413,11 +413,6 @@ export default class SalesEstimatesController extends BaseController { errors: [{ type: 'NOT_SELL_ABLE_ITEMS', code: 800 }], }); } - if (error.errorType === 'SALE_ESTIMATE_ALREADY_APPROVED') { - return res.boom.badRequest(null, { - errors: [{ type: 'CUSTOMER_NOT_FOUND', code: 900 }], - }); - } if (error.errorType === 'SALE_ESTIMATE_ALREADY_APPROVED') { return res.boom.badRequest(null, { errors: [{ type: 'CUSTOMER_NOT_FOUND', code: 1000 }], @@ -443,6 +438,16 @@ export default class SalesEstimatesController extends BaseController { errors: [{ type: 'SALE_ESTIMATE_NO_IS_REQUIRED', code: 1400 }], }); } + if (error.errorType === 'SALE_ESTIMATE_CONVERTED_TO_INVOICE') { + return res.boom.badRequest(null, { + errors: [{ type: 'SALE_ESTIMATE_CONVERTED_TO_INVOICE', code: 1500 }], + }); + } + if (error.errorType === 'SALE_ESTIMATE_ALREADY_DELIVERED') { + return res.boom.badRequest(null, { + errors: [{ type: 'SALE_ESTIMATE_ALREADY_DELIVERED', code: 1600 }], + }); + } } next(error); } diff --git a/server/src/services/Sales/Estimates/constants.ts b/server/src/services/Sales/Estimates/constants.ts index 3e4b4287d..2b58c74a8 100644 --- a/server/src/services/Sales/Estimates/constants.ts +++ b/server/src/services/Sales/Estimates/constants.ts @@ -1,3 +1,16 @@ + +export const ERRORS = { + SALE_ESTIMATE_NOT_FOUND: 'SALE_ESTIMATE_NOT_FOUND', + SALE_ESTIMATE_NUMBER_EXISTANCE: 'SALE_ESTIMATE_NUMBER_EXISTANCE', + SALE_ESTIMATE_CONVERTED_TO_INVOICE: 'SALE_ESTIMATE_CONVERTED_TO_INVOICE', + SALE_ESTIMATE_NOT_DELIVERED: 'SALE_ESTIMATE_NOT_DELIVERED', + SALE_ESTIMATE_ALREADY_REJECTED: 'SALE_ESTIMATE_ALREADY_REJECTED', + CUSTOMER_HAS_SALES_ESTIMATES: 'CUSTOMER_HAS_SALES_ESTIMATES', + SALE_ESTIMATE_NO_IS_REQUIRED: 'SALE_ESTIMATE_NO_IS_REQUIRED', + SALE_ESTIMATE_ALREADY_DELIVERED: 'SALE_ESTIMATE_ALREADY_DELIVERED', + SALE_ESTIMATE_ALREADY_APPROVED: 'SALE_ESTIMATE_ALREADY_APPROVED' +}; + export const DEFAULT_VIEW_COLUMNS = []; export const DEFAULT_VIEWS = [ { diff --git a/server/src/services/Sales/SalesEstimate.ts b/server/src/services/Sales/SalesEstimate.ts index b43036330..0091049dd 100644 --- a/server/src/services/Sales/SalesEstimate.ts +++ b/server/src/services/Sales/SalesEstimate.ts @@ -22,8 +22,8 @@ import { ServiceError } from 'exceptions'; import CustomersService from 'services/Contacts/CustomersService'; import moment from 'moment'; import AutoIncrementOrdersService from './AutoIncrementOrdersService'; -import { ERRORS } from './constants'; import SaleEstimateTransformer from './Estimates/SaleEstimateTransformer'; +import { ERRORS } from './Estimates/constants'; /** * Sale estimate service. @@ -355,26 +355,20 @@ export default class SaleEstimateService implements ISalesEstimatesService{ tenantId, estimateId ); - // Throw error if the sale estimate converted to sale invoice. if (oldSaleEstimate.convertedToInvoiceId) { throw new ServiceError(ERRORS.SALE_ESTIMATE_CONVERTED_TO_INVOICE); } - - this.logger.info( - '[sale_estimate] delete sale estimate and associated entries from the storage.' - ); + // Delete sale estimate entries. await ItemEntry.query() .where('reference_id', estimateId) .where('reference_type', 'SaleEstimate') .delete(); + // Delete sale estimate transaction. await SaleEstimate.query().where('id', estimateId).delete(); - this.logger.info('[sale_estimate] deleted successfully.', { - tenantId, - estimateId, - }); + // Triggers `onSaleEstimatedDeleted` event. await this.eventDispatcher.dispatch(events.saleEstimate.onDeleted, { tenantId, saleEstimateId: estimateId,