diff --git a/back/methods/collection/spec/assign.spec.js b/back/methods/collection/spec/assign.spec.js index 745343819..b00631d22 100644 --- a/back/methods/collection/spec/assign.spec.js +++ b/back/methods/collection/spec/assign.spec.js @@ -28,9 +28,10 @@ describe('ticket assign()', () => { await tx.rollback(); }); - it('should throw an error when there is not picking tickets', async() => { + it('should throw an error when there are no picking tickets', async() => { try { await models.Collection.assign(ctx, options); + fail('Expected an error to be thrown, but none was thrown.'); } catch (e) { expect(e.message).toEqual('There are not picking tickets'); } diff --git a/db/routines/vn/procedures/entry_isEditable.sql b/db/routines/vn/procedures/entry_isEditable.sql index 22cb25ba5..c417f6789 100644 --- a/db/routines/vn/procedures/entry_isEditable.sql +++ b/db/routines/vn/procedures/entry_isEditable.sql @@ -9,16 +9,18 @@ BEGIN * * @param vSelf Id de entrada */ - DECLARE vIsNotEditable BOOL; + DECLARE vIsNotEditable BOOL DEFAULT FALSE; - SELECT (e.typeFk IS NULL OR NOT et.isInformal) INTO vIsNotEditable + SELECT TRUE INTO vIsNotEditable FROM `entry` e LEFT JOIN entryType et ON et.code = e.typeFk WHERE e.id = vSelf - AND e.isBooked; + AND e.isBooked + AND (e.typeFk IS NULL OR NOT et.isInformal); + + IF vIsNotEditable AND NOT IFNULL(@isModeInventory, FALSE) THEN + CALL util.throw(CONCAT('Entry ', vSelf, ' is not editable')); + END IF; - IF vIsNotEditable AND NOT IFNULL(@isModeInventory, FALSE) THEN - CALL util.throw(CONCAT('Entry ', vSelf, ' is not editable')); - END IF; END$$ DELIMITER ; diff --git a/modules/entry/back/models/specs/entry.spec.js b/modules/entry/back/models/specs/entry.spec.js new file mode 100644 index 000000000..15a8202c4 --- /dev/null +++ b/modules/entry/back/models/specs/entry.spec.js @@ -0,0 +1,97 @@ +const {models} = require('vn-loopback/server/server'); +const LoopBackContext = require('loopback-context'); + +describe('entry_isEditable trigger', () => { + const activeCtx = { + accessToken: {userId: 5}, + http: { + req: { + headers: {origin: 'http://localhost'} + } + } + }; + const ctx = {req: activeCtx}; + const entryId = 1; + let tx; + let options; + let entry; + + beforeEach(async() => { + spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({active: ctx.req}); + tx = await models.Entry.beginTransaction({}); + options = {transaction: tx}; + + entry = await models.Entry.findById(entryId, null, options); + }); + + afterEach(async() => { + await tx.rollback(); + }); + + async function prepareEntry(isBooked, typeFk) { + let newCreated = Date.vnNew(); + await entry.updateAttributes({isBooked, typeFk}, options); + await entry.updateAttributes({dated: newCreated}, options); + } + + it('should throw an error when entry is booked and typeFk is null', async() => { + let error; + try { + await prepareEntry(true, null); + } catch (e) { + error = e; + } + + expect(error.message).toContain(`Entry ${entryId} is not editable`); + }); + + it('should throw an error when entry is booked and typeFk is not informal', async() => { + let error; + try { + const type = await models.EntryType.findOne({where: {isInformal: false}}, options); + await prepareEntry(true, type.code); + } catch (e) { + error = e; + } + + expect(error.message).toContain(`Entry ${entryId} is not editable`); + }); + + it('should not throw an error when entry is booked and typeFk is informal', async() => { + let error; + try { + const type = await models.EntryType.findOne({where: {isInformal: true}}, options); + await prepareEntry(true, type.code); + } catch (e) { + error = e; + } + + expect(error).toBeUndefined(); + }); + + it('should not throw an error when entry is not booked', async() => { + let error; + try { + const type = await models.EntryType.findOne({}, options); + await prepareEntry(false, type.code); + } catch (e) { + error = e; + } + + expect(error).toBeUndefined(); + }); + + it('should not throw an error when @isModeInventory is true', async() => { + let error; + try { + await models.Application.rawSql('SET @isModeInventory = TRUE;', null, options); + await prepareEntry(true, null); + } catch (e) { + error = e; + } finally { + await models.Application.rawSql('SET @isModeInventory = FALSE;', null, options); + } + + expect(error).toBeUndefined(); + }); +});