diff --git a/e2e/paths/10-travel/03_descriptor.spec.js b/e2e/paths/10-travel/03_descriptor.spec.js index d7d38c7af..619228f35 100644 --- a/e2e/paths/10-travel/03_descriptor.spec.js +++ b/e2e/paths/10-travel/03_descriptor.spec.js @@ -119,7 +119,7 @@ describe('Travel descriptor path', () => { await page.waitToClick(selectors.travelDescriptor.acceptClonation); const message = await page.waitForSnackbar(); - expect(message.text).toContain('A travel with this data already exists'); + expect(message.text).toContain('Unable to clone this travel'); }); it('should update the landed date to a future date to enable cloneWithEntries', async() => { diff --git a/loopback/locale/en.json b/loopback/locale/en.json index 1f066a965..70f3984a6 100644 --- a/loopback/locale/en.json +++ b/loopback/locale/en.json @@ -95,7 +95,7 @@ "The nif cannot be empty": "The nif cannot be empty", "Amount cannot be zero": "Amount cannot be zero", "Company has to be official": "Company has to be official", - "A travel with this data already exists": "A travel with this data already exists", + "Unable to clone this travel": "Unable to clone this travel", "The observation type can't be repeated": "The observation type can't be repeated", "New ticket request has been created with price": "New ticket request has been created *'{{description}}'* for day *{{shipped}}*, with a quantity of *{{quantity}}* and a price of *{{price}} €*", "New ticket request has been created": "New ticket request has been created *'{{description}}'* for day *{{shipped}}*, with a quantity of *{{quantity}}*", diff --git a/loopback/locale/es.json b/loopback/locale/es.json index f05318777..884db41f0 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -143,7 +143,7 @@ "ORDER_ROW_UNAVAILABLE": "No hay disponibilidad de este producto", "Distance must be lesser than 1000": "La distancia debe ser inferior a 1000", "This ticket is deleted": "Este ticket está eliminado", - "A travel with this data already exists": "Ya existe un travel con estos datos", + "Unable to clone this travel": "No ha sido posible clonar este travel", "This thermograph id already exists": "La id del termógrafo ya existe", "Choose a date range or days forward": "Selecciona un rango de fechas o días en adelante", "ORDER_ALREADY_CONFIRMED": "ORDER_ALREADY_CONFIRMED", diff --git a/modules/client/front/postcode/province/index.spec.js b/modules/client/front/postcode/province/index.spec.js index de517570e..f73a3c284 100644 --- a/modules/client/front/postcode/province/index.spec.js +++ b/modules/client/front/postcode/province/index.spec.js @@ -18,8 +18,8 @@ describe('Client', () => { describe('onAccept()', () => { it('should perform a POST query and show a success snackbar', () => { - let params = {name: 'New Jersey', countryFk: 1}; - controller.province = {name: 'New Jersey', countryFk: 1}; + const params = {name: 'New Jersey', autonomyFk: 1}; + controller.province = {name: 'New Jersey', autonomyFk: 1}; jest.spyOn(controller.vnApp, 'showMessage'); $httpBackend.expect('PATCH', `provinces`, params).respond(200, params); diff --git a/modules/travel/back/methods/travel/cloneWithEntries.js b/modules/travel/back/methods/travel/cloneWithEntries.js index ef385f1e0..6190c4487 100644 --- a/modules/travel/back/methods/travel/cloneWithEntries.js +++ b/modules/travel/back/methods/travel/cloneWithEntries.js @@ -14,7 +14,7 @@ module.exports = Self => { http: {source: 'path'} }], returns: { - type: 'Object', + type: 'object', description: 'The new cloned travel id', root: true, }, @@ -48,46 +48,52 @@ module.exports = Self => { let stmts = []; let stmt; - try { - stmt = new ParameterizedSQL( - `CALL travel_cloneWithEntries(?, ?, ?, ?, @vTravelFk)`, [ - id, started, ended, travel.ref]); + stmt = new ParameterizedSQL( + `CALL travel_cloneWithEntries(?, ?, ?, ?, ?, ?, ?, @vTravelFk)`, [ + id, + started, + ended, + travel.warehouseOutFk, + travel.warehouseInFk, + travel.ref, + travel.agencyFk + ] + ); - stmts.push(stmt); - const index = stmts.push('SELECT @vTravelFk AS id') - 1; + stmts.push(stmt); + const newTravelIndex = stmts.push('SELECT @vTravelFk AS id') - 1; - const sql = ParameterizedSQL.join(stmts, ';'); - const result = await conn.executeStmt(sql); - const [lastInsert] = result[index]; - const newTravel = await Self.findById(lastInsert.id, { - fields: [ - 'id', - 'shipped', - 'landed', - 'warehouseInFk', - 'warehouseOutFk', - 'agencyFk', - 'ref' - ] - }); + const sql = ParameterizedSQL.join(stmts, ';'); + const result = await conn.executeStmt(sql); + const [lastInsert] = result[newTravelIndex]; - const oldProperties = await loggable.translateValues(Self, travel); - const newProperties = await loggable.translateValues(Self, newTravel); - await models.TravelLog.create({ - originFk: newTravel.id, - userFk: userId, - action: 'insert', - changedModel: 'Travel', - changedModelId: newTravel.id, - oldInstance: oldProperties, - newInstance: newProperties - }); + if (!lastInsert.id) + throw new UserError('Unable to clone this travel'); - return newTravel.id; - } catch (error) { - if (error.code === 'ER_DUP_ENTRY') - throw new UserError('A travel with this data already exists'); - throw error; - } + const newTravel = await Self.findById(lastInsert.id, { + fields: [ + 'id', + 'shipped', + 'landed', + 'warehouseInFk', + 'warehouseOutFk', + 'agencyFk', + 'ref' + ] + }); + + const oldProperties = await loggable.translateValues(Self, travel); + const newProperties = await loggable.translateValues(Self, newTravel); + await models.TravelLog.create({ + originFk: newTravel.id, + userFk: userId, + action: 'insert', + changedModel: 'Travel', + changedModelId: newTravel.id, + oldInstance: oldProperties, + newInstance: newProperties + }); + + return newTravel.id; }; };