diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index 5581d19ac..ae2d36e3e 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -16,36 +16,51 @@ module.exports = Self => { accessScopes: ['DEFAULT', 'read:multimedia']}); Self.renewToken = async function(ctx) { - const {accessToken: token} = ctx.req; - - const {courtesyTime} = await models.AccessTokenConfig.findOne({ - fields: ['courtesyTime'] - }); - const isNotExceeded = await Self.validateToken(ctx); - if (isNotExceeded) - return token; - - // Schedule to remove current token - setTimeout(async() => { - try { - const exists = await models.AccessToken.findById(token.id); - exists && await Self.logout(token.id); - } catch (err) { - // eslint-disable-next-line no-console - console.error(err); - } - }, courtesyTime * 1000); - - // Get scopes - let createTokenOptions = {}; - const {scopes} = token; - if (scopes) - createTokenOptions = {scopes: [scopes[0]]}; - // Create new accessToken - const user = await Self.findById(token.userId); - const accessToken = await user.accessTokens.create(createTokenOptions); + let token; let isNotExceeded; + try { + token = ctx.req.accessToken; - return {id: accessToken.id, ttl: accessToken.ttl}; + const {courtesyTime} = await models.AccessTokenConfig.findOne({ + fields: ['courtesyTime'] + }); + isNotExceeded = await Self.validateToken(ctx); + if (isNotExceeded) + return token; + + // Schedule to remove current token + setTimeout(async() => { + let exists; + try { + exists = await models.AccessToken.findById(token.id); + exists && await Self.logout(token.id); + } catch (error) { + // eslint-disable-next-line no-console + console.error(error); + const body = {error: error.message, now: Date.now(), userId: token?.userId ?? null, exists}; + await handleError(body); + throw new Error(error); + } + }, courtesyTime * 1000); + + // Get scopes + const {scopes} = token; + if (scopes) + createTokenOptions = {scopes: [scopes[0]]}; + // Create new accessToken + const user = await Self.findById(token.userId); + const accessToken = await user.accessTokens.create(createTokenOptions); + + return {id: accessToken.id, ttl: accessToken.ttl}; + } catch (error) { + const body = {error: error.message, now: Date.now(), userId: token?.userId ?? null, createTokenOptions, isNotExceeded}; + await handleError(body); + throw new Error(error); + } }; }; + +async function handleError(body, tag = 'renewToken') { + body = JSON.stringify(body); + await models.Application.rawSql('CALL util.debugAdd(?,?);', [tag, body]); +} diff --git a/back/methods/vn-user/specs/renew-token.spec.js b/back/methods/vn-user/specs/renew-token.spec.js index 70e7473d1..8f1bb54c1 100644 --- a/back/methods/vn-user/specs/renew-token.spec.js +++ b/back/methods/vn-user/specs/renew-token.spec.js @@ -61,4 +61,21 @@ describe('Renew Token', () => { expect(error).toBeUndefined(); expect(response.id).toEqual(ctx.req.accessToken.id); }); + + it('throw error', async() => { + let error; + + try { + await models.VnUser.renewToken({req: {token: null}}); + } catch (e) { + error = e; + } + + expect(error).toBeDefined(); + const query = 'SELECT * FROM util.debug'; + + const debugLog = await models.Application.rawSql(query, null); + + expect(debugLog.length).toEqual(1); + }); }); diff --git a/back/model-config.json b/back/model-config.json index b643ab54f..58fa86797 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -64,6 +64,9 @@ "EmailUser": { "dataSource": "vn" }, + "Expedition_PrintOut": { + "dataSource": "vn" + }, "Image": { "dataSource": "vn" }, @@ -190,4 +193,4 @@ "RouteConfig": { "dataSource": "vn" } -} +} \ No newline at end of file diff --git a/back/models/expedition_PrintOut.json b/back/models/expedition_PrintOut.json new file mode 100644 index 000000000..23a2fdbc4 --- /dev/null +++ b/back/models/expedition_PrintOut.json @@ -0,0 +1,19 @@ +{ + "name": "Expedition_PrintOut", + "base": "VnModel", + "options": { + "mysql": { + "table": "dipole.expedition_PrintOut" + } + }, + "properties": { + "expeditionFk": { + "type": "number", + "id": true, + "description": "id expeditionFk" + }, + "itemFk": { + "type": "number" + } + } +} \ No newline at end of file diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index f800a1da1..1bc2685ff 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -3882,3 +3882,11 @@ INSERT INTO `vn`.`calendarHolidays` (calendarHolidaysTypeFk, dated, calendarHoli (1, '2001-05-17', 1, 5), (1, '2001-05-18', 1, 5); + + INSERT INTO dipole.printer (id, description) + VALUES(1, ''); + + INSERT INTO dipole.expedition_PrintOut (expeditionFk, ticketFk, addressFk, street, postalCode, city, shopName, isPrinted, created, printerFk, routeFk, parkingCode, + truckName, clientFk, phone, province, agency, m3, workerCode, itemFk, quantity, longName, shelvingFk, comments) + VALUES(1, 1, 0, ' ', ' ', ' ', ' ', 0, '2001-01-01 00:00:00', 1, 0, ' ', ' ', 0, NULL, '', NULL, 0.000, NULL, 10, NULL, NULL, 'NCC', NULL); + diff --git a/db/routines/sage/procedures/accountingMovements_add.sql b/db/routines/sage/procedures/accountingMovements_add.sql index ada954334..8c129beb2 100644 --- a/db/routines/sage/procedures/accountingMovements_add.sql +++ b/db/routines/sage/procedures/accountingMovements_add.sql @@ -410,15 +410,26 @@ BEGIN FROM sage.movConta mc JOIN vn.supplier s ON s.account = mc.CodigoCuenta WHERE NOT enlazadoSage - )SELECT idClientSupplier, `type` + ),clientSupplierSync AS( + SELECT idClientSupplier, `type` + FROM sage.clientSupplier cs + WHERE isSync + ) + SELECT idClientSupplier, `type` FROM sage.clientSupplier cs WHERE NOT isSync UNION SELECT id, 'C' - FROM client + FROM client c + LEFT JOIN clientSupplierSync cs ON cs.idClientSupplier = c.id + AND cs.Type ='C' + WHERE cs.idClientSupplier IS NULL UNION SELECT id, 'P' - FROM supplier; + FROM supplier s + LEFT JOIN clientSupplierSync cs ON cs.idClientSupplier = s.id + AND cs.Type ='P' + WHERE cs.idClientSupplier IS NULL; CALL clientSupplier_add(vCompanyFk); diff --git a/db/routines/vn/triggers/business_beforeUpdate.sql b/db/routines/vn/triggers/business_beforeUpdate.sql index 4b7a10041..7dc69bc75 100644 --- a/db/routines/vn/triggers/business_beforeUpdate.sql +++ b/db/routines/vn/triggers/business_beforeUpdate.sql @@ -12,23 +12,21 @@ BEGIN END IF; IF !(OLD.started <=> NEW.started AND OLD.ended <=> NEW.ended) THEN - - SELECT COUNT(*) > 0 INTO isOverlapping - FROM business b - WHERE (util.hasDateOverlapped( - NEW.started, - IFNULL(NEW.ended, b.started), - b.started, - IFNULL(b.ended, NEW.started)) - OR (NEW.ended <=> NULL AND b.ended <=> NULL)) - AND b.id <> OLD.id - AND workerFk = OLD.workerFk; + SELECT util.hasDateOverlapped( + started, + ended, + NEW.started, + IFNULL(NEW.ended, b.started) + ) isOverlapped INTO isOverlapping + FROM vn.business b + WHERE workerFk = NEW.workerFK + AND b.id <> NEW.id + ORDER BY isOverlapped DESC + LIMIT 1; IF isOverlapping THEN CALL util.throw ('IS_OVERLAPPING'); END IF; - END IF; - END$$ DELIMITER ;